uniform.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var test = require('tap').test;
  2. var distributions = require('../../distributions.js');
  3. var equals = require('../equal.js');
  4. test('testing standard uniform density function', function (t) {
  5. var uniform = distributions.Uniform();
  6. var c = [
  7. [-1.0, 0.0],
  8. [+0.0, 1.0],
  9. [+0.5, 1.0],
  10. [+1.0, 1.0],
  11. [+2.0, 0.0]
  12. ];
  13. equals.absoluteEqual({
  14. test: t,
  15. map: c,
  16. fn: uniform.pdf.bind(uniform),
  17. limit: 0.0000005
  18. });
  19. t.end();
  20. });
  21. test('testing standard uniform cumulative function', function (t) {
  22. var uniform = distributions.Uniform();
  23. var c = [
  24. [-1.0, 0.0],
  25. [+0.0, 0.0],
  26. [+0.5, 0.5],
  27. [+1.0, 1.0],
  28. [+2.0, 1.0]
  29. ];
  30. equals.absoluteEqual({
  31. test: t,
  32. map: c,
  33. fn: uniform.cdf.bind(uniform),
  34. limit: 0.0000005
  35. });
  36. t.end();
  37. });
  38. test('testing standard uniform inverse function', function (t) {
  39. var uniform = distributions.Uniform();
  40. var c = [
  41. [-1.0, NaN],
  42. [+0.0, 0.0],
  43. [+0.5, 0.5],
  44. [+1.0, 1.0],
  45. [+2.0, NaN]
  46. ];
  47. equals.absoluteEqual({
  48. test: t,
  49. map: c,
  50. fn: uniform.inv.bind(uniform),
  51. limit: 0.0000005
  52. });
  53. t.end();
  54. });
  55. test('testing standard uniform key values', function (t) {
  56. var uniform = distributions.Uniform();
  57. t.equal(uniform.median(), 0.5);
  58. t.equal(uniform.mean(), 0.5);
  59. t.equal(uniform.variance(), 1/12);
  60. t.end();
  61. });
  62. test('testing none standard uniform density function', function (t) {
  63. var uniform = distributions.Uniform(-2, 2);
  64. var c = [
  65. [-3.0, 0.0],
  66. [-2.0, 0.25],
  67. [+0.0, 0.25],
  68. [+0.5, 0.25],
  69. [+1.0, 0.25],
  70. [+2.0, 0.25],
  71. [+3.0, 0.0]
  72. ];
  73. equals.absoluteEqual({
  74. test: t,
  75. map: c,
  76. fn: uniform.pdf.bind(uniform),
  77. limit: 0.0000005
  78. });
  79. t.end();
  80. });
  81. test('testing none standard uniform cumulative function', function (t) {
  82. var uniform = distributions.Uniform(-2, 2);
  83. var c = [
  84. [-3.0, 0.0],
  85. [-2.0, 0.0],
  86. [+0.0, 0.5],
  87. [+0.5, 0.625],
  88. [+1.0, 0.75],
  89. [+2.0, 1.0],
  90. [+3.0, 1.0]
  91. ];
  92. equals.absoluteEqual({
  93. test: t,
  94. map: c,
  95. fn: uniform.cdf.bind(uniform),
  96. limit: 0.0000005
  97. });
  98. t.end();
  99. });
  100. test('testing none standard uniform inverse function', function (t) {
  101. var uniform = distributions.Uniform(-2, 2);
  102. var c = [
  103. [-1.0, NaN],
  104. [+0.0, -2.0],
  105. [+0.5, 0.0],
  106. [+1.0, +2.0],
  107. [+2.0, NaN]
  108. ];
  109. equals.absoluteEqual({
  110. test: t,
  111. map: c,
  112. fn: uniform.inv.bind(uniform),
  113. limit: 0.0000005
  114. });
  115. t.end();
  116. });
  117. test('testing node standard uniform key values', function (t) {
  118. var uniform = distributions.Uniform(-2, 2);
  119. t.equal(uniform.median(), 0);
  120. t.equal(uniform.mean(), 0);
  121. t.equal(uniform.variance(), 16/12);
  122. t.end();
  123. });