abstact.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. function AbstactStudentT(options) {
  3. this._options = options;
  4. }
  5. module.exports = AbstactStudentT;
  6. AbstactStudentT.prototype.testValue = function () {
  7. const diff = (this._mean - this._options.mu);
  8. return diff / this._se;
  9. };
  10. // Use cdf(-t) instead of 1 - cdf(t), and cdf(-|t|) instead of 1 - cdf(|t|)
  11. // to avoid a numerical error when computing 1 - epsilon.
  12. AbstactStudentT.prototype.pValue = function () {
  13. const t = this.testValue();
  14. switch (this._options.alternative) {
  15. case 1: // mu > mu[0]
  16. return this._dist.cdf(-t);
  17. case -1: // mu < mu[0]
  18. return this._dist.cdf(t);
  19. case 0: // mu != mu[0]
  20. return 2 * (this._dist.cdf(-Math.abs(t)));
  21. }
  22. };
  23. AbstactStudentT.prototype.confidence = function () {
  24. let pm;
  25. switch (this._options.alternative) {
  26. case 1: // mu > mu[0]
  27. pm = Math.abs(this._dist.inv(this._options.alpha)) * this._se;
  28. return [this._mean - pm, Infinity];
  29. case -1: // mu < mu[0]
  30. pm = Math.abs(this._dist.inv(this._options.alpha)) * this._se;
  31. return [-Infinity, this._mean + pm];
  32. case 0: // mu != mu[0]
  33. pm = Math.abs(this._dist.inv(this._options.alpha / 2)) * this._se;
  34. return [this._mean - pm, this._mean + pm];
  35. }
  36. };
  37. AbstactStudentT.prototype.valid = function () {
  38. return this.pValue() >= this._options.alpha;
  39. };
  40. AbstactStudentT.prototype.freedom = function () {
  41. return this._df;
  42. }