-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_pdf.js
38 lines (30 loc) · 895 Bytes
/
kernel_pdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//kernel_pdf.js is part of mathbiol.org; Please see documentation there
//Helena F. Deus ([email protected])
//200809
function kernel_pdf(data, binF) {
if(!binF) // number of bins
{binF = 50;
}
var n = data.length;
//get the data min
var min = Math.min.apply(Math, data);
var max = Math.max.apply(Math, data);
//get the positions of the bins
var x = linspace(min,max,binF);
var h =1.06*Math.pow(n,(-1/5));
h=Math.floor(h*10000)/10000;
var fhat = new Array();
for (var i=0; i<n; i++) {
var f = new Array();
for (var j=0; j<x.length; j++) {
var tmp1 = (-1/(2*Math.pow(h,2)));
var tmp2 = Math.pow((x[j]-data[i]),2);
var tmp3 = tmp1*tmp2;
var tmp4 = Math.pow(Math.E, tmp3)/Math.sqrt(2*Math.PI)/h
var tmp5 = tmp4/n;
if(!fhat[j]) { fhat[j] = 0};
fhat[j] = (fhat[j] + tmp5);
}
}
return fhat;
}