Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add logit and cumulativeStdLogisticProbability methods
* Add logistic distribution utilities * Split out logit test and test boundary condition Co-authored-by: Daniel Saxton <dsaxton@pm.me>
- Loading branch information
Showing
7 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* https://simplestatistics.org/docs/#cumulativestdlogisticprobability | ||
*/ | ||
declare function cumulativeStdLogisticProbability(x: number): number; | ||
|
||
export default cumulativeStdLogisticProbability; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* **[Logistic Cumulative Distribution Function](https://en.wikipedia.org/wiki/Logistic_distribution)** | ||
* | ||
* @param {number} x | ||
* @returns {number} cumulative standard logistic probability | ||
*/ | ||
function cumulativeStdLogisticProbability(x) { | ||
return 1 / (Math.exp(-x) + 1); | ||
} | ||
|
||
export default cumulativeStdLogisticProbability; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* https://simplestatistics.org/docs/#logit | ||
*/ | ||
declare function logit(p: number): number; | ||
|
||
export default logit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* The [Logit](https://en.wikipedia.org/wiki/Logit) | ||
* is the inverse of cumulativeStdLogisticProbability, | ||
* and is also known as the logistic quantile function. | ||
* | ||
* @param {number} p | ||
* @returns {number} logit | ||
*/ | ||
function logit(p) { | ||
if (p <= 0 || p >= 1) { | ||
throw new Error("p must be strictly between zero and one"); | ||
} | ||
return Math.log(p / (1 - p)); | ||
} | ||
|
||
export default logit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint no-shadow: 0 */ | ||
|
||
const test = require("tap").test; | ||
const ss = require("../"); | ||
|
||
test("logit", function (t) { | ||
t.test("inverse", function (t) { | ||
for (let i = -3; i <= 3; i += 0.01) { | ||
if ( | ||
Math.abs(ss.logit(ss.cumulativeStdLogisticProbability(i)) - i) > | ||
ss.epsilon | ||
) { | ||
t.fail("not an inverse of logit at " + i); | ||
} | ||
} | ||
t.end(); | ||
}); | ||
|
||
t.test("out-of-bounds input", function (t) { | ||
t.throws(function () { | ||
ss.logit(2); | ||
}); | ||
t.end(); | ||
}); | ||
|
||
t.end(); | ||
}); |