Reputation: 1657
Is it possible to calculate a percentile value by a given z-score in JavaScript?
E.g. z-score of 1.881 should give me 0,97 or 97%. This example is easy, but I want to calculate each percentile given by a z-score.
Upvotes: 2
Views: 4662
Reputation: 333
Seeking a statistical javascript function to return p-value from a z-score
That what you are looking for?
function GetZPercent(z) {
// z == number of standard deviations from the mean
// if z is greater than 6.5 standard deviations from the mean the
// number of significant digits will be outside of a reasonable range
if (z < -6.5) {
return 0.0;
}
if (z > 6.5) {
return 1.0;
}
var factK = 1;
var sum = 0;
var term = 1;
var k = 0;
var loopStop = Math.exp(-23);
while(Math.abs(term) > loopStop) {
term = .3989422804 * Math.pow(-1,k) * Math.pow(z,k) / (2 * k + 1) / Math.pow(2,k) * Math.pow(z,k+1) / factK;
sum += term;
k++;
factK *= k;
}
sum += 0.5;
return sum;
}
Upvotes: 4
Reputation: 1045
Consult this academic web-site: http://sphweb.bumc.bu.edu/otlt/MPH-Modules/BS/BS704_Probability/BS704_Probability10.html
In order to calculate the percentile value, you need to give it the z-score (which you've got), and multiply by both the mean and the standard deviation. Both the mean and standard deviation needs to come from the sample (e.g. the dataset). You'd have to calculate these through some kind of a function that loops through an array.
Upvotes: 0