Skip to content

Commit

Permalink
inv cumulative probability for t-distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
chen0040 committed May 16, 2017
1 parent a67579d commit b73079f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "js-stats",
"version": "1.0.0",
"description": "Package implements various statistic methods and distribution functions such as normal, fisher and student-t, and so on",
"description": "Package implements various statistics and distribution function",
"author": "Xianshun Chen",
"contributors": [
"Xianshun Chen <xs0040@gmail.com>"
],
"license": "MIT",
"main": "index.js",
"directories": {
Expand Down
14 changes: 14 additions & 0 deletions src/jsstats.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ var jsstats = jsstats || {};

this.ntable = [ 1.880794, 1.959964, 2.053749, 2.170090, 2.326348, 2.575829 ];

this.percentiles = [0.970, 0.975, 0.980, 0.985, 0.990, 0.995];

this.ttable =
[
[10.57889, 12.7062, 15.89454, 21.20495, 31.82052, 63.65674],
Expand Down Expand Up @@ -256,6 +258,18 @@ var jsstats = jsstats || {};
return tcdf;
};

TDistribution.prototype.invCumulativeProbability = function(p, df) {
if(!df){
df = this.df;
}
var delta = 0.005;
var row = df - 1;
var column = Math.round((p - this.percentiles[0]) / delta);
column = (column >= 0) ? column : 0;
column = (column < this.percentiles.Length) ? column : this.percentiles.length - 1;
return (row < this.ttable.length) ? this.ttable[row][column] : this.ntable[column];
};

jsstats.TDistribution = TDistribution;


Expand Down
9 changes: 0 additions & 9 deletions src/t-distribution.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/t-distribution-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe("Create t distribution", function() {
var distribution = new jsstats.TDistribution(10.0);
expect(distribution.cumulativeProbability(0.0)).to.equal(0.5);
});
it('has t_df = 0 with probability of 0.5', function(){
var distribution = new jsstats.TDistribution(10);
expect(distribution.invCumulativeProbability(0.5)).to.equal(0.0);
});
});


Expand Down

0 comments on commit b73079f

Please sign in to comment.