normal.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. var test = require('tap').test;
  2. var distributions = require('../../distributions.js');
  3. var equals = require('../equal.js');
  4. test('testing standard normal density function', function (t) {
  5. var normal = distributions.Normal();
  6. var c = [
  7. [-3.0, 0.004431848],
  8. [-2.5, 0.017528300],
  9. [-2.0, 0.053990967],
  10. [-1.5, 0.129517596],
  11. [-1.0, 0.241970725],
  12. [-0.5, 0.352065327],
  13. [ 0.0, 0.398942280],
  14. [+0.5, 0.352065327],
  15. [+1.0, 0.241970725],
  16. [+1.5, 0.129517596],
  17. [+2.0, 0.053990967],
  18. [+2.5, 0.017528300],
  19. [+3.0, 0.004431848]
  20. ];
  21. equals.absoluteEqual({
  22. test: t,
  23. map: c,
  24. fn: normal.pdf.bind(normal),
  25. limit: 0.0000005
  26. });
  27. t.end();
  28. });
  29. test('testing standard normal cumulative function', function (t) {
  30. var normal = distributions.Normal();
  31. var c = [
  32. [-3.0, 0.001349898],
  33. [-2.5, 0.006209665],
  34. [-2.0, 0.022750132],
  35. [-1.5, 0.066807201],
  36. [-1.0, 0.158655254],
  37. [-0.5, 0.308537539],
  38. [ 0.0, 0.500000000],
  39. [+0.5, 0.691462461],
  40. [+1.0, 0.841344746],
  41. [+1.5, 0.933192799],
  42. [+2.0, 0.977249868],
  43. [+2.5, 0.993790335],
  44. [+3.0, 0.998650102]
  45. ];
  46. equals.absoluteEqual({
  47. test: t,
  48. map: c,
  49. fn: normal.cdf.bind(normal),
  50. limit: 0.0000005
  51. });
  52. t.end();
  53. });
  54. test('testing standard normal inverse function', function (t) {
  55. var normal = distributions.Normal();
  56. var c = [
  57. [+0.0, -Infinity],
  58. [+0.1, -1.2815516],
  59. [+0.2, -0.8416212],
  60. [+0.3, -0.5244005],
  61. [+0.4, -0.2533471],
  62. [+0.5, 0.0],
  63. [+0.6, +0.2533471],
  64. [+0.7, +0.5244005],
  65. [+0.8, +0.8416212],
  66. [+0.9, +1.2815516],
  67. [+1.0, +Infinity]
  68. ];
  69. equals.absoluteEqual({
  70. test: t,
  71. map: c,
  72. fn: normal.inv.bind(normal),
  73. limit: 0.0000005
  74. });
  75. t.end();
  76. });
  77. test('testing standard normal key values', function (t) {
  78. var normal = distributions.Normal();
  79. t.equal(normal.median(), 0);
  80. t.equal(normal.mean(), 0);
  81. t.equal(normal.variance(), 1);
  82. t.end();
  83. });
  84. test('testing none standard normal density function', function (t) {
  85. var normal = distributions.Normal(1, 2);
  86. var c = [
  87. [-2.0, 0.0647588],
  88. [-1.0, 0.1209854],
  89. [ 0.0, 0.1760327],
  90. [+1.0, 0.1994711],
  91. [+2.0, 0.1760327],
  92. [+3.0, 0.1209854],
  93. [+4.0, 0.0647588]
  94. ];
  95. equals.absoluteEqual({
  96. test: t,
  97. map: c,
  98. fn: normal.pdf.bind(normal),
  99. limit: 0.0000005
  100. });
  101. t.end();
  102. });
  103. test('testing none standard normal cumulative function', function (t) {
  104. var normal = distributions.Normal(1, 2);
  105. var c = [
  106. [-2.0, 0.0668072],
  107. [-1.0, 0.1586553],
  108. [ 0.0, 0.3085375],
  109. [+1.0, 0.5000000],
  110. [+2.0, 0.6914625],
  111. [+3.0, 0.8413447],
  112. [+4.0, 0.9331928]
  113. ];
  114. equals.absoluteEqual({
  115. test: t,
  116. map: c,
  117. fn: normal.cdf.bind(normal),
  118. limit: 0.0000005
  119. });
  120. t.end();
  121. });
  122. test('testing none standard normal inverse function', function (t) {
  123. var normal = distributions.Normal(1, 2);
  124. var c = [
  125. [+0.0, -Infinity],
  126. [+0.2, -0.6832425],
  127. [+0.4, +0.4933058],
  128. [+0.5, 1.0],
  129. [+0.6, +1.5066942],
  130. [+0.8, +2.6832425],
  131. [+1.0, +Infinity]
  132. ];
  133. equals.absoluteEqual({
  134. test: t,
  135. map: c,
  136. fn: normal.inv.bind(normal),
  137. limit: 0.0000005
  138. });
  139. t.end();
  140. });
  141. test('testing none standard normal key values', function (t) {
  142. var normal = distributions.Normal(1, 2);
  143. t.equal(normal.median(), 1);
  144. t.equal(normal.mean(), 1);
  145. t.equal(normal.variance(), 4);
  146. t.end();
  147. });