Changeset 88
- Timestamp:
- 10/18/09 06:57:55 (3 years ago)
- Files:
-
- docs/alloc.html (modified) (1 diff)
- docs/base.html (modified) (6 diffs)
- docs/cor.html (modified) (6 diffs)
- docs/distrib.html (modified) (4 diffs)
- docs/gamma.html (modified) (1 diff)
- docs/infotheory.html (modified) (1 diff)
- docs/random.html (modified) (1 diff)
- docs/regress.html (modified) (1 diff)
- docs/sort.html (modified) (1 diff)
- docs/stats.html (modified) (1 diff)
- docs/summary.html (modified) (13 diffs)
- docs/tests.html (modified) (10 diffs)
- trunk/alloc.d (modified) (1 diff)
- trunk/base.d (modified) (14 diffs)
- trunk/cor.d (modified) (6 diffs)
- trunk/distrib.d (modified) (7 diffs)
- trunk/infotheory.d (modified) (1 diff)
- trunk/random.d (modified) (1 diff)
- trunk/regress.d (modified) (4 diffs)
- trunk/sort.d (modified) (1 diff)
- trunk/stats.d (modified) (1 diff)
- trunk/summary.d (modified) (20 diffs)
- trunk/tests.d (modified) (39 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
docs/alloc.html
r87 r88 798 798 Page was generated with 799 799 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 800 on Tue Oct 13 20:16:252009800 on Sun Oct 18 13:42:20 2009 801 801 802 802 </td></tr> docs/base.html
r87 r88 266 266 <font color=blue>assert</font>(s == [0, 1, 2, 3, 4]); 267 267 </pre> 268 268 269 <br><br> 269 270 … … 404 405 405 406 406 <dd>No, nothing this horribly inefficient is used internally. 407 A struct that generates all possible permutations of a sequence, 407 <dd>A struct that generates all possible permutations of a sequence, 408 408 and can be iterated over with foreach. Note that permutations are 409 409 output in undefined order. Also note that the returned permutations … … 427 427 <font color=blue>assert</font>(res.length == 6); 428 428 </pre> 429 429 430 <br><br> 430 431 … … 508 509 <font color=blue>auto</font> p = <u>perm</u>(5); <font color=green>// Permutations of integers on range [0, 5]. 509 510 </font> <font color=blue>auto</font> p = <u>perm</u>(-1, 2); <font color=green>// Permutations of integers on range [-1, 2]. 510 </font></pre>511 512 <br><br>513 514 </dd>515 516 <script>explorer.outline.writeEnabled = true;</script>517 <dt><span class="decl">CombRet!(T)518 <span class="currsymbol">comb</span>519 <script>explorer.outline.addDecl('comb');</script>520 521 (T)(T <span class="funcparam">stuff</span>, uint <span class="funcparam">r</span>);522 </span></dt>523 <script>explorer.outline.writeEnabled = false;</script>524 525 526 <dd>Create a Comb struct from a range or of a set of bounds.527 <br><br>528 <b>Note:</b><br>529 CombRet is just a template to figure out what this should return.530 I would use auto if not for bug 2251.531 532 <br><br>533 <b>Examples:</b><br>534 <pre class="d_code"> <font color=blue>auto</font> p = <u>comb</u>([1,2,3]);535 <font color=blue>auto</font> p = <u>comb</u>(5); <font color=green>// Permutations of integers on range [0, 5].536 511 </font></pre> 537 512 … … 653 628 654 629 <script>explorer.outline.writeEnabled = true;</script> 630 <dt><span class="decl">CombRet!(T) 631 <span class="currsymbol">comb</span> 632 <script>explorer.outline.addDecl('comb');</script> 633 634 (T)(T <span class="funcparam">stuff</span>, uint <span class="funcparam">r</span>); 635 </span></dt> 636 <script>explorer.outline.writeEnabled = false;</script> 637 638 639 <dd>Create a Comb struct from a range or of a set of bounds. 640 <br><br> 641 <b>Note:</b><br> 642 CombRet is just a template to figure out what this should return. 643 I would use auto if not for bug 2251. 644 645 <br><br> 646 <b>Examples:</b><br> 647 <pre class="d_code"> <font color=blue>auto</font> p = <u>comb</u>([1,2,3]); 648 <font color=blue>auto</font> p = <u>comb</u>(5); <font color=green>// Permutations of integers on range [0, 5]. 649 </font></pre> 650 651 <br><br> 652 653 </dd> 654 655 <script>explorer.outline.writeEnabled = true;</script> 655 656 <dt><span class="decl">T[] 656 657 <span class="currsymbol">intersect</span> … … 807 808 Page was generated with 808 809 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 809 on Tue Oct 13 20:16:252009810 on Sun Oct 18 13:42:20 2009 810 811 811 812 </td></tr> docs/cor.html
r87 r88 24 24 <dl> 25 25 <script>explorer.outline.writeEnabled = true;</script> 26 <dt><span class="decl"> real26 <dt><span class="decl">Pcor 27 27 <span class="currsymbol">pcor</span> 28 28 <script>explorer.outline.addDecl('pcor');</script> … … 33 33 34 34 35 <dd>Pearson correlation. When the term correlation is used unqualified, it is 35 <dd>Convenience function for calculating Pearson correlation. 36 When the term correlation is used unqualified, it is 36 37 usually referring to this quantity. This is a parametric correlation 37 38 metric and should not be used with extremely ill-behaved data. … … 39 40 lengths, it uses the first min(input1.length, input2.length) elements. 40 41 <br><br> 42 <b>Note:</b><br> 43 The Pcor struct returned by this function is alias this'd to the 44 correlation coefficient. Therefore, the result from this function can 45 be treated simply as a floating point number. 46 47 <br><br> 41 48 42 49 </dd> … … 44 51 <script>explorer.outline.writeEnabled = true;</script> 45 52 <dt><span class="decl">struct 46 <span class="currsymbol"> OnlinePcor</span>47 <script>explorer.outline.addDecl(' OnlinePcor');</script>53 <span class="currsymbol">Pcor</span> 54 <script>explorer.outline.addDecl('Pcor');</script> 48 55 49 56 ; … … 58 65 <br><br> 59 66 67 <span class="currsymbol">Pcor</span> 68 <script>explorer.outline.addDecl('Pcor');</script> 69 70 .cor is alias this'd, so if this struct is used as a real, it will 71 be converted to a simple correlation coefficient automatically. 72 73 <br><br> 74 60 75 61 76 <script>explorer.outline.incSymbolLevel();</script> … … 274 289 Page was generated with 275 290 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 276 on Tue Oct 13 20:16:252009291 on Sun Oct 18 13:42:20 2009 277 292 278 293 </td></tr> docs/distrib.html
r87 r88 572 572 573 573 (real <span class="funcparam">x</span>, real <span class="funcparam">df1</span>, real <span class="funcparam">df2</span>); 574 <br>real575 <span class="currsymbol">invFisherCDFR</span>576 <script>explorer.outline.addDecl('invFisherCDFR');</script>577 578 (real <span class="funcparam">df1</span>, real <span class="funcparam">df2</span>, real <span class="funcparam">p</span>);579 574 </span></dt> 580 575 <script>explorer.outline.writeEnabled = false;</script> … … 626 621 <script>explorer.outline.writeEnabled = true;</script> 627 622 <dt><span class="decl">real 623 <span class="currsymbol">invFisherCDFR</span> 624 <script>explorer.outline.addDecl('invFisherCDFR');</script> 625 626 (real <span class="funcparam">df1</span>, real <span class="funcparam">df2</span>, real <span class="funcparam">p</span>); 627 </span></dt> 628 <script>explorer.outline.writeEnabled = false;</script> 629 630 631 <dd>Inverse of complemented Fisher distribution 632 <br><br> 633 Finds the F density argument x such that the integral 634 from x to infinity of the F density is equal to the 635 given probability <span class="funcparam">p</span>. 636 <br><br> 637 638 This is accomplished using the inverse beta integral 639 function and the relations 640 <br><br> 641 642 z = betaIncompleteInv( <span class="funcparam">df2</span>/2, <span class="funcparam">df1</span>/2, <span class="funcparam">p</span> ), 643 x = <span class="funcparam">df2</span> (1-z) / (<span class="funcparam">df1</span> z). 644 <br><br> 645 646 Note that the following relations hold for the inverse of 647 the uncomplemented F distribution: 648 <br><br> 649 650 z = betaIncompleteInv( <span class="funcparam">df1</span>/2, <span class="funcparam">df2</span>/2, <span class="funcparam">p</span> ), 651 x = <span class="funcparam">df2</span> z / (<span class="funcparam">df1</span> (1-z)). 652 ditto 653 <br><br> 654 655 </dd> 656 657 <script>explorer.outline.writeEnabled = true;</script> 658 <dt><span class="decl">real 628 659 <span class="currsymbol">negBinomPMF</span> 629 660 <script>explorer.outline.addDecl('negBinomPMF');</script> … … 810 841 <script>explorer.outline.writeEnabled = true;</script> 811 842 <dt><span class="decl">pure nothrow real 843 <span class="currsymbol">laplacePDF</span> 844 <script>explorer.outline.addDecl('laplacePDF');</script> 845 846 (real <span class="funcparam">x</span>, real <span class="funcparam">mu</span> = 0, real <span class="funcparam">b</span> = 1); 847 </span></dt> 848 <script>explorer.outline.writeEnabled = false;</script> 849 850 851 <dd><br><br> 852 </dd> 853 854 <script>explorer.outline.writeEnabled = true;</script> 855 <dt><span class="decl">pure nothrow real 812 856 <span class="currsymbol">laplaceCDF</span> 813 857 <script>explorer.outline.addDecl('laplaceCDF');</script> … … 869 913 Page was generated with 870 914 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 871 on Tue Oct 13 20:16:262009915 on Sun Oct 18 13:42:20 2009 872 916 873 917 </td></tr> docs/gamma.html
r87 r88 242 242 Page was generated with 243 243 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 244 on Tue Oct 13 20:16:262009244 on Sun Oct 18 13:42:20 2009 245 245 246 246 </td></tr> docs/infotheory.html
r87 r88 208 208 Page was generated with 209 209 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 210 on Tue Oct 13 20:16:262009210 on Sun Oct 18 13:42:20 2009 211 211 212 212 </td></tr> docs/random.html
r87 r88 312 312 Page was generated with 313 313 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 314 on Tue Oct 13 20:16:262009314 on Sun Oct 18 13:42:20 2009 315 315 316 316 </td></tr> docs/regress.html
r87 r88 408 408 Page was generated with 409 409 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 410 on Tue Oct 13 20:16:262009410 on Sun Oct 18 13:42:20 2009 411 411 412 412 </td></tr> docs/sort.html
r87 r88 406 406 Page was generated with 407 407 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 408 on Tue Oct 13 20:16:262009408 on Sun Oct 18 13:42:21 2009 409 409 410 410 </td></tr> docs/stats.html
r87 r88 21 21 Page was generated with 22 22 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 23 on Tue Oct 13 20:16:26200923 on Sun Oct 18 13:42:21 2009 24 24 25 25 </td></tr> docs/summary.html
r87 r88 127 127 <script>explorer.outline.writeEnabled = true;</script> 128 128 <dt><span class="decl">struct 129 <span class="currsymbol"> OnlineMean</span>130 <script>explorer.outline.addDecl(' OnlineMean');</script>129 <span class="currsymbol">Mean</span> 130 <script>explorer.outline.addDecl('Mean');</script> 131 131 132 132 ; … … 140 140 <br><br> 141 141 <b>Examples:</b><br> 142 <pre class="d_code"> <u> OnlineMean</u> summ;142 <pre class="d_code"> <u>Mean</u> summ; 143 143 summ.put(1); 144 144 summ.put(2); … … 214 214 <script>explorer.outline.writeEnabled = true;</script> 215 215 <dt><span class="decl">struct 216 <span class="currsymbol"> OnlineGeometricMean</span>217 <script>explorer.outline.addDecl(' OnlineGeometricMean');</script>216 <span class="currsymbol">GeometricMean</span> 217 <script>explorer.outline.addDecl('GeometricMean');</script> 218 218 219 219 ; … … 320 320 <script>explorer.outline.writeEnabled = true;</script> 321 321 <dt><span class="decl">struct 322 <span class="currsymbol"> OnlineMeanSD</span>323 <script>explorer.outline.addDecl(' OnlineMeanSD');</script>322 <span class="currsymbol">MeanSD</span> 323 <script>explorer.outline.addDecl('MeanSD');</script> 324 324 325 325 ; … … 331 331 for stdev, var cost a few floating point ops. Getter for mean costs 332 332 a single branch to check for N == 0. Relatively expensive floating point 333 ops, if you only need mean, try OnlineMean. This struct uses O(1) space and333 ops, if you only need mean, try Mean. This struct uses O(1) space and 334 334 does *NOT* store the individual elements. 335 335 <br><br> 336 336 <b>Examples:</b><br> 337 <pre class="d_code"> <u> OnlineMeanSD</u> summ;337 <pre class="d_code"> <u>MeanSD</u> summ; 338 338 summ.put(1); 339 339 summ.put(2); … … 434 434 435 435 <script>explorer.outline.writeEnabled = true;</script> 436 <dt><span class="decl">struct 437 <span class="currsymbol">MeanSD</span> 438 <script>explorer.outline.addDecl('MeanSD');</script> 439 440 ; 441 </span></dt> 442 <script>explorer.outline.writeEnabled = false;</script> 443 444 445 <dd>Simple holder for mean, stdev/variance. Plain old data, accessing is 446 cheap. 447 <br><br> 448 449 450 <script>explorer.outline.incSymbolLevel();</script> 451 <dl> 452 <script>explorer.outline.writeEnabled = true;</script> 453 <dt><span class="decl">real 454 <span class="currsymbol">mean</span> 455 <script>explorer.outline.addDecl('mean');</script> 456 457 ; 458 </span></dt> 459 <script>explorer.outline.writeEnabled = false;</script> 460 461 462 <dd><br><br> 463 </dd> 464 465 <script>explorer.outline.writeEnabled = true;</script> 466 <dt><span class="decl">real 467 <span class="currsymbol">SD</span> 468 <script>explorer.outline.addDecl('SD');</script> 469 470 ; 471 </span></dt> 472 <script>explorer.outline.writeEnabled = false;</script> 473 474 475 <dd><br><br> 476 </dd> 477 </dl> 478 <script>explorer.outline.decSymbolLevel();</script> 479 436 <dt><span class="decl">MeanSD 437 <span class="currsymbol">meanStdev</span> 438 <script>explorer.outline.addDecl('meanStdev');</script> 439 440 (T)(T <span class="funcparam">data</span>); 441 </span></dt> 442 <script>explorer.outline.writeEnabled = false;</script> 443 444 445 <dd>Convenience function that puts all elements of data into a MeanSD struct, 446 then returns this struct. 447 <br><br> 480 448 481 449 </dd> … … 502 470 503 471 <script>explorer.outline.writeEnabled = true;</script> 504 <dt><span class="decl">MeanSD505 <span class="currsymbol">meanVariance</span>506 <script>explorer.outline.addDecl('meanVariance');</script>507 508 (T)(T <span class="funcparam">data</span>);509 </span></dt>510 <script>explorer.outline.writeEnabled = false;</script>511 512 513 <dd>Calculates both mean and variance of an input range, returns a MeanSD514 struct.515 <br><br>516 517 </dd>518 519 <script>explorer.outline.writeEnabled = true;</script>520 <dt><span class="decl">MeanSD521 <span class="currsymbol">meanStdev</span>522 <script>explorer.outline.addDecl('meanStdev');</script>523 524 (T)(T <span class="funcparam">data</span>);525 </span></dt>526 <script>explorer.outline.writeEnabled = false;</script>527 528 529 <dd>Calculates both mean and standard deviation of an input range, returns a530 MeanSD struct.531 <br><br>532 533 </dd>534 535 <script>explorer.outline.writeEnabled = true;</script>536 472 <dt><span class="decl">real 537 473 <span class="currsymbol">stdev</span> … … 551 487 <script>explorer.outline.writeEnabled = true;</script> 552 488 <dt><span class="decl">struct 553 <span class="currsymbol"> OnlineSummary</span>554 <script>explorer.outline.addDecl(' OnlineSummary');</script>489 <span class="currsymbol">Summary</span> 490 <script>explorer.outline.addDecl('Summary');</script> 555 491 556 492 ; … … 561 497 <dd>Output range to compute mean, stdev, variance, skewness, kurtosis, min, and 562 498 max online. Using this struct is relatively expensive, so if you just need 563 mean and/or stdev, try OnlineMeanSD or OnlineMean. Getter methods for stdev,499 mean and/or stdev, try MeanSD or Mean. Getter methods for stdev, 564 500 var cost a few floating point ops. Getter for mean costs a single branch to 565 501 check for N == 0. Getters for skewness and kurtosis cost a whole bunch of … … 568 504 <br><br> 569 505 <b>Examples:</b><br> 570 <pre class="d_code"> <u> OnlineSummary</u> summ;506 <pre class="d_code"> <u>Summary</u> summ; 571 507 summ.put(1); 572 508 summ.put(2); … … 789 725 790 726 <script>explorer.outline.writeEnabled = true;</script> 791 <dt><span class="decl">struct792 <span class="currsymbol">Summary</span>793 <script>explorer.outline.addDecl('Summary');</script>794 795 ;796 </span></dt>797 <script>explorer.outline.writeEnabled = false;</script>798 799 800 <dd>Plain old data struct for holding results of summary(). Accessing members801 is cheap.802 <br><br>803 804 805 <script>explorer.outline.incSymbolLevel();</script>806 <dl>807 <script>explorer.outline.writeEnabled = true;</script>808 <dt><span class="decl">ulong809 <span class="currsymbol">N</span>810 <script>explorer.outline.addDecl('N');</script>811 812 ;813 </span></dt>814 <script>explorer.outline.writeEnabled = false;</script>815 816 817 <dd><br><br>818 </dd>819 820 <script>explorer.outline.writeEnabled = true;</script>821 <dt><span class="decl">real822 <span class="currsymbol">mean</span>823 <script>explorer.outline.addDecl('mean');</script>824 825 ;826 </span></dt>827 <script>explorer.outline.writeEnabled = false;</script>828 829 830 <dd><br><br>831 </dd>832 833 <script>explorer.outline.writeEnabled = true;</script>834 <dt><span class="decl">real835 <span class="currsymbol">var</span>836 <script>explorer.outline.addDecl('var');</script>837 838 ;839 </span></dt>840 <script>explorer.outline.writeEnabled = false;</script>841 842 843 <dd><br><br>844 </dd>845 846 <script>explorer.outline.writeEnabled = true;</script>847 <dt><span class="decl">real848 <span class="currsymbol">SD</span>849 <script>explorer.outline.addDecl('SD');</script>850 851 ;852 </span></dt>853 <script>explorer.outline.writeEnabled = false;</script>854 855 856 <dd><br><br>857 </dd>858 859 <script>explorer.outline.writeEnabled = true;</script>860 <dt><span class="decl">real861 <span class="currsymbol">skew</span>862 <script>explorer.outline.addDecl('skew');</script>863 864 ;865 </span></dt>866 <script>explorer.outline.writeEnabled = false;</script>867 868 869 <dd><br><br>870 </dd>871 872 <script>explorer.outline.writeEnabled = true;</script>873 <dt><span class="decl">real874 <span class="currsymbol">kurtosis</span>875 <script>explorer.outline.addDecl('kurtosis');</script>876 877 ;878 </span></dt>879 <script>explorer.outline.writeEnabled = false;</script>880 881 882 <dd><br><br>883 </dd>884 885 <script>explorer.outline.writeEnabled = true;</script>886 <dt><span class="decl">real887 <span class="currsymbol">min</span>888 <script>explorer.outline.addDecl('min');</script>889 890 ;891 </span></dt>892 <script>explorer.outline.writeEnabled = false;</script>893 894 895 <dd><br><br>896 </dd>897 898 <script>explorer.outline.writeEnabled = true;</script>899 <dt><span class="decl">real900 <span class="currsymbol">max</span>901 <script>explorer.outline.addDecl('max');</script>902 903 ;904 </span></dt>905 <script>explorer.outline.writeEnabled = false;</script>906 907 908 <dd><br><br>909 </dd>910 911 <script>explorer.outline.writeEnabled = true;</script>912 <dt><span class="decl">string913 <span class="currsymbol">toString</span>914 <script>explorer.outline.addDecl('toString');</script>915 916 ();917 </span></dt>918 <script>explorer.outline.writeEnabled = false;</script>919 920 921 <dd><br><br>922 </dd>923 </dl>924 <script>explorer.outline.decSymbolLevel();</script>925 926 927 </dd>928 929 <script>explorer.outline.writeEnabled = true;</script>930 727 <dt><span class="decl">Summary 931 728 <span class="currsymbol">summary</span> … … 937 734 938 735 939 <dd>Calculates all 940 <span class="currsymbol">summary</span> 941 <script>explorer.outline.addDecl('summary');</script> 942 943 stats (mean, variance, standard dev., skewness 944 and kurtosis) on an input range with elements that can be implicitly 945 converted to real. Returns the results in a Summary struct. 736 <dd>Convenience function. Puts all elements of data into a Summary struct, 737 and returns this struct. 946 738 <br><br> 947 739 … … 1116 908 Page was generated with 1117 909 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 1118 on Tue Oct 13 20:16:262009910 on Sun Oct 18 13:42:21 2009 1119 911 1120 912 </td></tr> docs/tests.html
r87 r88 247 247 248 248 <script>explorer.outline.writeEnabled = true;</script> 249 <dt><span class="decl">template 250 <span class="currsymbol">isSummary</span> 251 <script>explorer.outline.addDecl('isSummary');</script> 252 253 (T)</span></dt> 254 <script>explorer.outline.writeEnabled = false;</script> 255 256 257 <dd>Tests whether a struct/class has the necessary information for calculating 258 a T-test. It must have a property .mean (mean), .stdev (stdandard deviation), 259 .var (variance), and .N (sample size). 260 261 <br><br> 262 263 </dd> 264 265 <script>explorer.outline.writeEnabled = true;</script> 249 266 <dt><span class="decl">ConfInt 250 267 <span class="currsymbol">studentsTTest</span> … … 264 281 A ConfInt containing T, the P-value and the boundaries of 265 282 the confidence interval for mean(T) at the level specified. 283 <br><br> 284 285 </dd> 286 287 <script>explorer.outline.writeEnabled = true;</script> 288 <dt><span class="decl">ConfInt 289 <span class="currsymbol">studentsTTest</span> 290 <script>explorer.outline.addDecl('studentsTTest');</script> 291 292 (T)(T <span class="funcparam">summary</span>, real <span class="funcparam">testMean</span> = 0, Alt <span class="funcparam">alt</span> = Alt.TWOSIDE, real <span class="funcparam">confLevel</span> = 0.95); 293 </span></dt> 294 <script>explorer.outline.writeEnabled = false;</script> 295 296 297 <dd>Calculate the test directly from summary statistics of the data. 266 298 <br><br> 267 299 … … 294 326 <script>explorer.outline.writeEnabled = true;</script> 295 327 <dt><span class="decl">ConfInt 328 <span class="currsymbol">studentsTTest</span> 329 <script>explorer.outline.addDecl('studentsTTest');</script> 330 331 (T, U)(T <span class="funcparam">sample1Summary</span>, U <span class="funcparam">sample2Summary</span>, real <span class="funcparam">testMean</span> = 0, Alt <span class="funcparam">alt</span> = Alt.TWOSIDE, real <span class="funcparam">confLevel</span> = 0.95); 332 </span></dt> 333 <script>explorer.outline.writeEnabled = false;</script> 334 335 336 <dd>Calculate the test directly from summary statistics for each sample. 337 <br><br> 338 339 </dd> 340 341 <script>explorer.outline.writeEnabled = true;</script> 342 <dt><span class="decl">ConfInt 296 343 <span class="currsymbol">welchTTest</span> 297 344 <script>explorer.outline.addDecl('welchTTest');</script> … … 311 358 boundaries of the confidence interval for the difference between means 312 359 of sample1 and sample2 at the specified level. 360 <br><br> 361 362 </dd> 363 364 <script>explorer.outline.writeEnabled = true;</script> 365 <dt><span class="decl">ConfInt 366 <span class="currsymbol">welchTTest</span> 367 <script>explorer.outline.addDecl('welchTTest');</script> 368 369 (T, U)(T <span class="funcparam">sample1Summary</span>, U <span class="funcparam">sample2Summary</span>, real <span class="funcparam">testMean</span> = 0, Alt <span class="funcparam">alt</span> = Alt.TWOSIDE, real <span class="funcparam">confLevel</span> = 0.95); 370 </span></dt> 371 <script>explorer.outline.writeEnabled = false;</script> 372 373 374 <dd>Calculate the test directly from summary statistics for each sample. 313 375 <br><br> 314 376 … … 336 398 boundaries of the confidence interval for the mean difference between 337 399 corresponding elements of sample1 and sample2 at the specified level. 400 <br><br> 401 402 </dd> 403 404 <script>explorer.outline.writeEnabled = true;</script> 405 <dt><span class="decl">ConfInt 406 <span class="currsymbol">pairedTTest</span> 407 <script>explorer.outline.addDecl('pairedTTest');</script> 408 409 (T)(T <span class="funcparam">diffSummary</span>, real <span class="funcparam">testMean</span> = 0, Alt <span class="funcparam">alt</span> = Alt.TWOSIDE, real <span class="funcparam">confLevel</span> = 0.95); 410 </span></dt> 411 <script>explorer.outline.writeEnabled = false;</script> 412 413 414 <dd>Compute the test directly from summary statistics of the differences between 415 corresponding samples. 338 416 <br><br> 339 417 … … 404 482 <b>References:</b><br> 405 483 "Concepts and Applications of Inferrential Statistics". 406 Richard Lowry. Vassar College. Onlineversion.484 Richard Lowry. Vassar College. version. 407 485 <br><br> 408 486 <b>http:</b><br> … … 810 888 811 889 Yates's continuity correction is never used in this implementation. If 812 you want something conservative, use fisherExact().890 you want something that's guaranteed to be conservative, use fisherExact(). 813 891 <br><br> 814 892 … … 1049 1127 <script>explorer.outline.writeEnabled = true;</script> 1050 1128 <dt><span class="decl">struct 1051 <span class="currsymbol"> OnlineRunsTest</span>1052 <script>explorer.outline.addDecl(' OnlineRunsTest');</script>1129 <span class="currsymbol">RunsTest</span> 1130 <script>explorer.outline.addDecl('RunsTest');</script> 1053 1131 1054 1132 (alias positive = "a > 0",T); … … 1092 1170 <script>explorer.outline.writeEnabled = true;</script> 1093 1171 <dt><span class="decl">real 1094 <span class="currsymbol">p Val</span>1095 <script>explorer.outline.addDecl('p Val');</script>1172 <span class="currsymbol">p</span> 1173 <script>explorer.outline.addDecl('p');</script> 1096 1174 1097 1175 (Alt <span class="funcparam">alt</span> = Alt.TWOSIDE); … … 1408 1486 Page was generated with 1409 1487 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 1410 on Tue Oct 13 20:16:2620091488 on Sun Oct 18 13:42:21 2009 1411 1489 1412 1490 </td></tr> trunk/alloc.d
r86 r88 3 3 * Author: David Simcha*/ 4 4 /* 5 * You may use this software under your choice of either of the following 6 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 7 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 8 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 9 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 10 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 11 * GET A LIFE. 5 * License: 6 * Boost Software License - Version 1.0 - August 17th, 2003 12 7 * 13 * ---------------------Phobos License: --------------------------------------- 8 * Permission is hereby granted, free of charge, to any person or organization 9 * obtaining a copy of the software and accompanying documentation covered by 10 * this license (the "Software") to use, reproduce, display, distribute, 11 * execute, and transmit the Software, and to prepare derivative works of the 12 * Software, and to permit third-parties to whom the Software is furnished to 13 * do so, all subject to the following: 14 14 * 15 * Copyright (C) 2008-2009 by David Simcha. 15 * The copyright notices in the Software and this entire statement, including 16 * the above license grant, this restriction and the following disclaimer, 17 * must be included in all copies of the Software, in whole or in part, and 18 * all derivative works of the Software, unless such copies or derivative 19 * works are solely in the form of machine-executable object code generated by 20 * a source language processor. 16 21 * 17 * This software is provided 'as-is', without any express or implied 18 * warranty. In no event will the authors be held liable for any damages 19 * arising from the use of this software. 20 * 21 * Permission is granted to anyone to use this software for any purpose, 22 * including commercial applications, and to alter it and redistribute it 23 * freely, in both source and binary form, subject to the following 24 * restrictions: 25 * 26 * o The origin of this software must not be misrepresented; you must not 27 * claim that you wrote the original software. If you use this software 28 * in a product, an acknowledgment in the product documentation would be 29 * appreciated but is not required. 30 * o Altered source versions must be plainly marked as such, and must not 31 * be misrepresented as being the original software. 32 * o This notice may not be removed or altered from any source 33 * distribution. 34 * 35 * --------------------BSD License: ----------------------------------------- 36 * 37 * Copyright (c) 2008-2009, David Simcha 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions are met: 42 * 43 * * Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 46 * * Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * * Neither the name of the authors nor the 51 * names of its contributors may be used to endorse or promote products 52 * derived from this software without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 55 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 57 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 58 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 60 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 61 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 25 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 26 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 * DEALINGS IN THE SOFTWARE. 64 29 */ 65 30 trunk/base.d
r86 r88 6 6 * Author: David Simcha*/ 7 7 /* 8 * You may use this software under your choice of either of the following 9 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 10 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 11 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 12 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 13 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 14 * GET A LIFE. 15 * 16 * ---------------------Phobos License: --------------------------------------- 17 * 18 * Copyright (C) 2008-2009 by David Simcha. 19 * 20 * This software is provided 'as-is', without any express or implied 21 * warranty. In no event will the authors be held liable for any damages 22 * arising from the use of this software. 23 * 24 * Permission is granted to anyone to use this software for any purpose, 25 * including commercial applications, and to alter it and redistribute it 26 * freely, in both source and binary form, subject to the following 27 * restrictions: 28 * 29 * o The origin of this software must not be misrepresented; you must not 30 * claim that you wrote the original software. If you use this software 31 * in a product, an acknowledgment in the product documentation would be 32 * appreciated but is not required. 33 * o Altered source versions must be plainly marked as such, and must not 34 * be misrepresented as being the original software. 35 * o This notice may not be removed or altered from any source 36 * distribution. 37 * 38 * --------------------BSD License: ----------------------------------------- 39 * 40 * Copyright (c) 2008-2009, David Simcha 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions are met: 45 * 46 * * Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 49 * * Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 53 * * Neither the name of the authors nor the 54 * names of its contributors may be used to endorse or promote products 55 * derived from this software without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 58 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 59 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 60 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 61 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 62 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 64 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 66 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 8 * License: 9 * Boost Software License - Version 1.0 - August 17th, 2003 10 * 11 * Permission is hereby granted, free of charge, to any person or organization 12 * obtaining a copy of the software and accompanying documentation covered by 13 * this license (the "Software") to use, reproduce, display, distribute, 14 * execute, and transmit the Software, and to prepare derivative works of the 15 * Software, and to permit third-parties to whom the Software is furnished to 16 * do so, all subject to the following: 17 * 18 * The copyright notices in the Software and this entire statement, including 19 * the above license grant, this restriction and the following disclaimer, 20 * must be included in all copies of the Software, in whole or in part, and 21 * all derivative works of the Software, unless such copies or derivative 22 * works are solely in the form of machine-executable object code generated by 23 * a source language processor. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 28 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 29 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 31 * DEALINGS IN THE SOFTWARE. 67 32 */ 68 69 33 module dstats.base; 70 34 … … 72 36 private import dstats.sort, std.c.stdlib, std.bigint, std.typecons, 73 37 std.functional, std.algorithm, std.range, std.bitmanip, 74 std.stdio ;38 std.stdio, std.contracts; 75 39 76 40 import std.string : strip; … … 79 43 immutable real[] staticLogFacTable; 80 44 81 enum : size_t { 82 staticFacTableLen = 10_000, 83 } 45 private enum size_t staticFacTableLen = 10_000; 84 46 85 47 static this() { … … 262 224 if(isForwardRange!(T) && realInput!(T) && isIntegral!(Ret)) 263 225 in { 264 assert(nbin <= Ret.max + 1);265 226 assert(nbin > 0); 266 227 } body { 228 // This condition is too important, too non-obvious and too cheap to check 229 // to be turned off in release mode: 230 enforce(nbin <= (cast(uint) Ret.max) + 1, "Cannot bin into " ~ 231 to!string(nbin) ~ " bins and store the results in a " ~ 232 Ret.stringof ~ "."); 233 267 234 alias ElementType!(T) E; 268 235 Unqual!(E) min = data.front, max = data.front; … … 306 273 assert(res == [cast(ubyte) 0, 0, 0, 0, 1, 3, 5, 7, 8, 9]); 307 274 TempAlloc.free; 275 276 // Make sure this throws: 277 try { 278 auto foo = bin( seq(0U, 1_000U), 512); 279 assert(0); 280 } catch(Exception e) { 281 // It's supposed to throw. 282 } 283 308 284 writeln("Passed bin unittest."); 309 285 } … … 326 302 assert(nbin > 0); 327 303 assert(nbin <= data.length); 328 assert(nbin <= Ret.max + 1);329 304 } body { 305 // This condition is too important, too non-obvious and too cheap to check 306 // to be turned off in release mode: 307 enforce(nbin <= (cast(uint) Ret.max) + 1, "Cannot bin into " ~ 308 to!string(nbin) ~ " bins and store the results in a " ~ 309 Ret.stringof ~ "."); 310 330 311 Ret[] result; 331 312 if(alloc == Alloc.HEAP) { … … 365 346 assert(res == [cast(ubyte) 1, 0, 1, 0, 2, 3, 0, 2, 2, 1, 1, 3, 3, 2, 3, 0]); 366 347 TempAlloc.free; 348 349 // Make sure this throws: 350 try { 351 auto foo = frqBin( seq(0U, 1_000U), 512); 352 assert(0); 353 } catch(Exception e) { 354 // It's supposed to throw. 355 } 356 367 357 writeln("Passed frqBin unittest."); 368 358 } … … 375 365 * auto s = seq(0, 5); 376 366 * assert(s == [0, 1, 2, 3, 4]); 377 * --- */378 367 * --- 368 */ 379 369 T[] seq(T)(T start, T end, T increment = 1, Alloc alloc = Alloc.HEAP) { 380 370 T[] output; … … 463 453 } 464 454 465 // Used internally by rank() .455 // Used internally by rank() and dstats.cor.scor(). 466 456 void averageTies(T, U)(T sortedInput, U[] ranks, uint[] perms) nothrow 467 457 in { … … 538 528 /// 539 529 /*Values up to 9,999 are pre-calculated and stored in 540 * an i nvariantglobal array, for performance. After this point, the gamma530 * an immutable global array, for performance. After this point, the gamma 541 531 * function is used, because caching would take up too much memory, and if 542 532 * done lazily, would cause threading issues.*/ … … 577 567 writefln("Passed logNcomb unit test."); 578 568 } 579 580 /////No, nothing this horribly inefficient is used internally.581 //BigInt factorial(uint N) {582 // BigInt result = 1;583 // for(uint i = 2; i <= N; i++) {584 // result *= i;585 // }586 // return result;587 //}588 //589 //unittest {590 // assert(factorial(4) == 24);591 // assert(factorial(5) == 120);592 // assert(factorial(6) == 720);593 // assert(factorial(7) == 5040);594 // assert(factorial(3) == 6);595 // writefln("Passed factorial test.");596 //}597 569 598 570 /**A struct that generates all possible permutations of a sequence, … … 618 590 * assert(res.canFind([3.0, 2.0, 1.0])); 619 591 * assert(res.length == 6); 620 * ---*/ 592 * --- 593 */ 621 594 struct Perm(T = uint) { 622 595 private: … … 757 730 } 758 731 writeln("Passed Perm test."); 759 }760 761 private template CombRet(T) {762 static if(isForwardRange!(T)) {763 alias Comb!(Unqual!(ElementType!(T))) CombRet;764 } else static if(is(T : uint)) {765 alias Comb!uint CombRet;766 } else static assert(0, "comb can only be created with range or uint.");767 }768 769 /**Create a Comb struct from a range or of a set of bounds.770 *771 * Note: CombRet is just a template to figure out what this should return.772 * I would use auto if not for bug 2251.773 *774 * Examples:775 * ---776 * auto p = comb([1,2,3]);777 * auto p = comb(5); // Permutations of integers on range [0, 5].778 * ---779 */780 CombRet!(T) comb(T)(T stuff, uint r) {781 alias typeof(return) rt;782 static if(isForwardRange!(T)) {783 return rt(stuff, r);784 } else {785 return rt(seq(0U, cast(uint) stuff), r);786 }787 732 } 788 733 … … 909 854 } 910 855 856 private template CombRet(T) { 857 static if(isForwardRange!(T)) { 858 alias Comb!(Unqual!(ElementType!(T))) CombRet; 859 } else static if(is(T : uint)) { 860 alias Comb!uint CombRet; 861 } else static assert(0, "comb can only be created with range or uint."); 862 } 863 864 /**Create a Comb struct from a range or of a set of bounds. 865 * 866 * Note: CombRet is just a template to figure out what this should return. 867 * I would use auto if not for bug 2251. 868 * 869 * Examples: 870 * --- 871 * auto p = comb([1,2,3]); 872 * auto p = comb(5); // Permutations of integers on range [0, 5]. 873 * --- 874 */ 875 CombRet!(T) comb(T)(T stuff, uint r) { 876 alias typeof(return) rt; 877 static if(isForwardRange!(T)) { 878 return rt(stuff, r); 879 } else { 880 return rt(seq(0U, cast(uint) stuff), r); 881 } 882 } 883 911 884 unittest { 912 885 // Test indexing verison first. trunk/cor.d
r87 r88 3 3 * Author: David Simcha*/ 4 4 /* 5 * You may use this software under your choice of either of the following 6 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 7 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 8 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 9 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 10 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 11 * GET A LIFE. 12 * 13 * ---------------------Phobos License: --------------------------------------- 14 * 15 * Copyright (C) 2008-2009 by David Simcha. 16 * 17 * This software is provided 'as-is', without any express or implied 18 * warranty. In no event will the authors be held liable for any damages 19 * arising from the use of this software. 20 * 21 * Permission is granted to anyone to use this software for any purpose, 22 * including commercial applications, and to alter it and redistribute it 23 * freely, in both source and binary form, subject to the following 24 * restrictions: 25 * 26 * o The origin of this software must not be misrepresented; you must not 27 * claim that you wrote the original software. If you use this software 28 * in a product, an acknowledgment in the product documentation would be 29 * appreciated but is not required. 30 * o Altered source versions must be plainly marked as such, and must not 31 * be misrepresented as being the original software. 32 * o This notice may not be removed or altered from any source 33 * distribution. 34 * 35 * --------------------BSD License: ----------------------------------------- 36 * 37 * Copyright (c) 2008-2009, David Simcha 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions are met: 42 * 43 * * Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 46 * * Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * * Neither the name of the authors nor the 51 * names of its contributors may be used to endorse or promote products 52 * derived from this software without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 55 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 57 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 58 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 60 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 61 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5 * License: 6 * Boost Software License - Version 1.0 - August 17th, 2003 7 * 8 * Permission is hereby granted, free of charge, to any person or organization 9 * obtaining a copy of the software and accompanying documentation covered by 10 * this license (the "Software") to use, reproduce, display, distribute, 11 * execute, and transmit the Software, and to prepare derivative works of the 12 * Software, and to permit third-parties to whom the Software is furnished to 13 * do so, all subject to the following: 14 * 15 * The copyright notices in the Software and this entire statement, including 16 * the above license grant, this restriction and the following disclaimer, 17 * must be included in all copies of the Software, in whole or in part, and 18 * all derivative works of the Software, unless such copies or derivative 19 * works are solely in the form of machine-executable object code generated by 20 * a source language processor. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 25 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 26 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 * DEALINGS IN THE SOFTWARE. 64 29 */ 65 30 … … 80 45 } 81 46 82 /**Pearson correlation. When the term correlation is used unqualified, it is 47 /**Convenience function for calculating Pearson correlation. 48 * When the term correlation is used unqualified, it is 83 49 * usually referring to this quantity. This is a parametric correlation 84 50 * metric and should not be used with extremely ill-behaved data. 85 51 * This function works with any pair of input ranges. If they are of different 86 * lengths, it uses the first min(input1.length, input2.length) elements.*/ 87 real pcor(T, U)(T input1, U input2) 52 * lengths, it uses the first min(input1.length, input2.length) elements. 53 * 54 * Note: The Pcor struct returned by this function is alias this'd to the 55 * correlation coefficient. Therefore, the result from this function can 56 * be treated simply as a floating point number. 57 */ 58 Pcor pcor(T, U)(T input1, U input2) 88 59 if(realInput!(T) && realInput!(U)) { 89 OnlinePcor corCalc;60 Pcor corCalc; 90 61 while(!input1.empty && !input2.empty) { 91 62 corCalc.put(input1.front, input2.front); … … 94 65 } 95 66 96 return corCalc .cor;67 return corCalc; 97 68 } 98 69 … … 128 99 * Getters for stdev, var, cov, cor cost floating point division ops. Getters 129 100 * for means cost a single branch to check for N == 0. This struct uses O(1) 130 * space.*/ 131 struct OnlinePcor { 101 * space. 102 * 103 * Pcor.cor is alias this'd, so if this struct is used as a real, it will 104 * be converted to a simple correlation coefficient automatically. 105 */ 106 struct Pcor { 132 107 private: 133 108 real _mean1 = 0, _mean2 = 0, _var1 = 0, _var2 = 0, _cov = 0, _k = 0; 134 109 public: 110 alias cor this; 111 135 112 /// 136 113 void put(T, U)(T elem1, U elem2) { … … 193 170 real covariance(T, U)(T input1, U input2) 194 171 if(realInput!(T) && realInput!(U)) { 195 OnlinePcor covCalc;172 Pcor covCalc; 196 173 while(!input1.empty && !input2.empty) { 197 174 covCalc.put(input1.front, input2.front); … … 259 236 averageTies(iDup2, i2Ranks, perms); 260 237 261 return pcor(i1Ranks, i2Ranks) ;238 return pcor(i1Ranks, i2Ranks).cor; 262 239 } 263 240 trunk/distrib.d
r85 r88 73 73 * because in Tango/MathExtra they represent P(X > x) while in dstats they 74 74 * represent P(X >= x). 75 * 75 76 * 76 77 * Copyright (c) 2008-2009, David Simcha and Don Clugston … … 106 107 import dstats.base, std.algorithm; 107 108 108 enum SQ2PI = 2.5066282746310005024157652848110452530069867406099383166299L;109 enum SQ2PI = sqrt(2 * PI); 109 110 110 111 version(unittest) { … … 179 180 assert(stdNormal(2.5) == normalCDF(2.5, 0, 1)); 180 181 } 181 182 182 183 183 /// … … 574 574 assert(x <= n); 575 575 } body { 576 if(x > n1 || x < (n - n2)) 576 if(x > n1 || x < (n - n2)) { 577 577 return 0; 578 } 578 579 real result = logNcomb(n1, x) + logNcomb(n2, n - x) - logNcomb(n1 + n2, n); 579 580 return exp(result); … … 652 653 assert(feqrel(chiSqrCDFR(invChiSqCDFR(3.5L, 0.1L), 3.5L), 0.1L)>=real.mant_dig-3); 653 654 assert(chiSqrCDF(0.4L, 19.02L) + chiSqrCDFR(0.4L, 19.02L) ==1.0L); 655 assert(approxEqual( invChiSqCDFR( 3, chiSqrCDFR(1, 3)), 1)); 654 656 writeln("Passed chi-square unittest."); 655 657 } … … 1120 1122 } 1121 1123 1122 /* 1124 /** 1123 1125 * Inverse of complemented Fisher distribution 1124 1126 * … … 1425 1427 1426 1428 /// 1429 real laplacePDF(real x, real mu = 0, real b = 1) pure nothrow { 1430 return (exp(-abs(x - mu) / b)) / (2 * b); 1431 } 1432 1433 unittest { 1434 // Values from Maxima. 1435 assert(approxEqual(laplacePDF(3, 2, 1), 0.18393972058572)); 1436 assert(approxEqual(laplacePDF(-8, 6, 7), 0.0096668059454723)); 1437 } 1438 1439 /// 1427 1440 real laplaceCDF(real X, real mu = 0, real b = 1) pure nothrow { 1428 1441 real diff = (X - mu); trunk/infotheory.d
r83 r88 5 5 * Author: David Simcha*/ 6 6 /* 7 * You may use this software under your choice of either of the following 8 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 9 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 10 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 11 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 12 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 13 * GET A LIFE. 14 * 15 * ---------------------Phobos License: --------------------------------------- 16 * 17 * Copyright (C) 2008-2009 by David Simcha. 18 * 19 * This software is provided 'as-is', without any express or implied 20 * warranty. In no event will the authors be held liable for any damages 21 * arising from the use of this software. 22 * 23 * Permission is granted to anyone to use this software for any purpose, 24 * including commercial applications, and to alter it and redistribute it 25 * freely, in both source and binary form, subject to the following 26 * restrictions: 27 * 28 * o The origin of this software must not be misrepresented; you must not 29 * claim that you wrote the original software. If you use this software 30 * in a product, an acknowledgment in the product documentation would be 31 * appreciated but is not required. 32 * o Altered source versions must be plainly marked as such, and must not 33 * be misrepresented as being the original software. 34 * o This notice may not be removed or altered from any source 35 * distribution. 36 * 37 * --------------------BSD License: ----------------------------------------- 38 * 39 * Copyright (c) 2008-2009, David Simcha 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions are met: 44 * 45 * * Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 48 * * Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 52 * * Neither the name of the authors nor the 53 * names of its contributors may be used to endorse or promote products 54 * derived from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 57 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 58 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 59 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 60 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 61 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 62 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 63 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 64 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 65 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 7 * License: 8 * Boost Software License - Version 1.0 - August 17th, 2003 9 * 10 * Permission is hereby granted, free of charge, to any person or organization 11 * obtaining a copy of the software and accompanying documentation covered by 12 * this license (the "Software") to use, reproduce, display, distribute, 13 * execute, and transmit the Software, and to prepare derivative works of the 14 * Software, and to permit third-parties to whom the Software is furnished to 15 * do so, all subject to the following: 16 * 17 * The copyright notices in the Software and this entire statement, including 18 * the above license grant, this restriction and the following disclaimer, 19 * must be included in all copies of the Software, in whole or in part, and 20 * all derivative works of the Software, unless such copies or derivative 21 * works are solely in the form of machine-executable object code generated by 22 * a source language processor. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 27 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 28 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 30 * DEALINGS IN THE SOFTWARE. 66 31 */ 67 32 trunk/random.d
r83 r88 1003 1003 writeln("\tMean Expected: ", a / (a + b), " Observed: ", summ.mean); 1004 1004 writeln("\tMedian Expected: ?? Observed: ", median(observ)); 1005 writeln("\tStdev Expected: ", betaStdev(a, b), " Observed: ", summ. SD);1005 writeln("\tStdev Expected: ", betaStdev(a, b), " Observed: ", summ.stdev); 1006 1006 writeln("\tKurtosis Expected: ", betaKurtosis(a, b), " Observed: ", summ.kurtosis); 1007 writeln("\tSkewness Expected: ", betaSkew(a, b), " Observed: ", summ.skew );1007 writeln("\tSkewness Expected: ", betaSkew(a, b), " Observed: ", summ.skewness); 1008 1008 delete observ; 1009 1009 } trunk/regress.d
r75 r88 6 6 * 7 7 * Author: David Simcha*/ 8 /* 9 * You may use this software under your choice of either of the following 10 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 11 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 12 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 13 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 14 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 15 * GET A LIFE. 16 * 17 * ---------------------Phobos License: --------------------------------------- 18 * 19 * Copyright (C) 2009 by David Simcha. 20 * 21 * This software is provided 'as-is', without any express or implied 22 * warranty. In no event will the authors be held liable for any damages 23 * arising from the use of this software. 24 * 25 * Permission is granted to anyone to use this software for any purpose, 26 * including commercial applications, and to alter it and redistribute it 27 * freely, in both source and binary form, subject to the following 28 * restrictions: 29 * 30 * o The origin of this software must not be misrepresented; you must not 31 * claim that you wrote the original software. If you use this software 32 * in a product, an acknowledgment in the product documentation would be 33 * appreciated but is not required. 34 * o Altered source versions must be plainly marked as such, and must not 35 * be misrepresented as being the original software. 36 * o This notice may not be removed or altered from any source 37 * distribution. 38 * 39 * --------------------BSD License: ----------------------------------------- 40 * 41 * Copyright (c) 2009, David Simcha 42 * All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions are met: 46 * 47 * * Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 50 * * Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 54 * * Neither the name of the authors nor the 55 * names of its contributors may be used to endorse or promote products 56 * derived from this software without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 59 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 60 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 61 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 62 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 63 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 64 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 65 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 67 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 8 /* 9 * License: 10 * Boost Software License - Version 1.0 - August 17th, 2003 11 * 12 * Permission is hereby granted, free of charge, to any person or organization 13 * obtaining a copy of the software and accompanying documentation covered by 14 * this license (the "Software") to use, reproduce, display, distribute, 15 * execute, and transmit the Software, and to prepare derivative works of the 16 * Software, and to permit third-parties to whom the Software is furnished to 17 * do so, all subject to the following: 18 * 19 * The copyright notices in the Software and this entire statement, including 20 * the above license grant, this restriction and the following disclaimer, 21 * must be included in all copies of the Software, in whole or in part, and 22 * all derivative works of the Software, unless such copies or derivative 23 * works are solely in the form of machine-executable object code generated by 24 * a source language processor. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 29 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 30 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 32 * DEALINGS IN THE SOFTWARE. 68 33 */ 69 34 … … 175 140 foreach(i, row; mat) { 176 141 real absMax = 1.0L / reduce!(max)(map!(abs)(row[0..mat.length])); 177 // row[0..mat.length] *= absMax; // Bug 3006 178 foreach(ref elem; row[0..mat.length]) { 179 elem *= absMax; 180 } 142 row[0..mat.length] *= absMax; 181 143 row[i + mat.length] = absMax; 182 144 } … … 207 169 foreach(i; 0..mat.length) { 208 170 real diagVal = mat[i][i]; 209 //mat[i][] /= diagVal; // Bug 3006 210 foreach(ref elem; mat[i]) { 211 elem /= diagVal; 212 } 171 mat[i][] /= diagVal; 213 172 } 214 173 … … 466 425 real S = 0; 467 426 uint n = 0; 468 OnlinePcor R2Calc;427 Pcor R2Calc; 469 428 for(; !residuals.empty; residuals.popFront) { 470 429 real residual = residuals.front; trunk/sort.d
r83 r88 31 31 * Author: David Simcha*/ 32 32 /* 33 * You may use this software under your choice of either of the following 34 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 35 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 36 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 37 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 38 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 39 * GET A LIFE. 40 * 41 * ---------------------Phobos License: --------------------------------------- 42 * 43 * Copyright (C) 2008-2009 by David Simcha. 44 * 45 * This software is provided 'as-is', without any express or implied 46 * warranty. In no event will the authors be held liable for any damages 47 * arising from the use of this software. 48 * 49 * Permission is granted to anyone to use this software for any purpose, 50 * including commercial applications, and to alter it and redistribute it 51 * freely, in both source and binary form, subject to the following 52 * restrictions: 53 * 54 * o The origin of this software must not be misrepresented; you must not 55 * claim that you wrote the original software. If you use this software 56 * in a product, an acknowledgment in the product documentation would be 57 * appreciated but is not required. 58 * o Altered source versions must be plainly marked as such, and must not 59 * be misrepresented as being the original software. 60 * o This notice may not be removed or altered from any source 61 * distribution. 62 * 63 * --------------------BSD License: ----------------------------------------- 64 * 65 * Copyright (c) 2008-2009, David Simcha 66 * All rights reserved. 67 * 68 * Redistribution and use in source and binary forms, with or without 69 * modification, are permitted provided that the following conditions are met: 70 * 71 * * Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 74 * * Redistributions in binary form must reproduce the above copyright 75 * notice, this list of conditions and the following disclaimer in the 76 * documentation and/or other materials provided with the distribution. 77 * 78 * * Neither the name of the authors nor the 79 * names of its contributors may be used to endorse or promote products 80 * derived from this software without specific prior written permission. 81 * 82 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 83 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 84 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 85 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 86 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 87 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 88 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 89 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 90 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 91 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * License: 34 * Boost Software License - Version 1.0 - August 17th, 2003 35 * 36 * Permission is hereby granted, free of charge, to any person or organization 37 * obtaining a copy of the software and accompanying documentation covered by 38 * this license (the "Software") to use, reproduce, display, distribute, 39 * execute, and transmit the Software, and to prepare derivative works of the 40 * Software, and to permit third-parties to whom the Software is furnished to 41 * do so, all subject to the following: 42 * 43 * The copyright notices in the Software and this entire statement, including 44 * the above license grant, this restriction and the following disclaimer, 45 * must be included in all copies of the Software, in whole or in part, and 46 * all derivative works of the Software, unless such copies or derivative 47 * works are solely in the form of machine-executable object code generated by 48 * a source language processor. 49 * 50 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 51 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 52 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 53 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 54 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 55 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 56 * DEALINGS IN THE SOFTWARE. 92 57 */ 93 58 trunk/stats.d
r70 r88 1 1 /**Convenience module that simply publicly imports everything else.*/ 2 /* 3 * License: 4 * If you think this module is even copyrightable, you have issues. 5 */ 2 6 3 7 module dstats.stats; trunk/summary.d
r87 r88 12 12 * Author: David Simcha*/ 13 13 /* 14 * You may use this software under your choice of either of the following 15 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 16 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 17 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 18 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 19 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 20 * GET A LIFE. 21 * 22 * ---------------------Phobos License: --------------------------------------- 23 * 24 * Copyright (C) 2008-2009 by David Simcha. 25 * 26 * This software is provided 'as-is', without any express or implied 27 * warranty. In no event will the authors be held liable for any damages 28 * arising from the use of this software. 29 * 30 * Permission is granted to anyone to use this software for any purpose, 31 * including commercial applications, and to alter it and redistribute it 32 * freely, in both source and binary form, subject to the following 33 * restrictions: 34 * 35 * o The origin of this software must not be misrepresented; you must not 36 * claim that you wrote the original software. If you use this software 37 * in a product, an acknowledgment in the product documentation would be 38 * appreciated but is not required. 39 * o Altered source versions must be plainly marked as such, and must not 40 * be misrepresented as being the original software. 41 * o This notice may not be removed or altered from any source 42 * distribution. 43 * 44 * --------------------BSD License: ----------------------------------------- 45 * 46 * Copyright (c) 2008-2009, David Simcha 47 * All rights reserved. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions are met: 51 * 52 * * Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 55 * * Redistributions in binary form must reproduce the above copyright 56 * notice, this list of conditions and the following disclaimer in the 57 * documentation and/or other materials provided with the distribution. 58 * 59 * * Neither the name of the authors nor the 60 * names of its contributors may be used to endorse or promote products 61 * derived from this software without specific prior written permission. 62 * 63 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 72 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 * License: 15 * Boost Software License - Version 1.0 - August 17th, 2003 16 * 17 * Permission is hereby granted, free of charge, to any person or organization 18 * obtaining a copy of the software and accompanying documentation covered by 19 * this license (the "Software") to use, reproduce, display, distribute, 20 * execute, and transmit the Software, and to prepare derivative works of the 21 * Software, and to permit third-parties to whom the Software is furnished to 22 * do so, all subject to the following: 23 * 24 * The copyright notices in the Software and this entire statement, including 25 * the above license grant, this restriction and the following disclaimer, 26 * must be included in all copies of the Software, in whole or in part, and 27 * all derivative works of the Software, unless such copies or derivative 28 * works are solely in the form of machine-executable object code generated by 29 * a source language processor. 30 * 31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 33 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 34 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 35 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 36 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 37 * DEALINGS IN THE SOFTWARE. 73 38 */ 74 39 … … 226 191 real mean(T)(T data) 227 192 if(realIterable!(T)) { 228 OnlineMean meanCalc;193 Mean meanCalc; 229 194 foreach(element; data) { 230 195 meanCalc.put(element); … … 239 204 * Examples: 240 205 * --- 241 * OnlineMean summ;206 * Mean summ; 242 207 * summ.put(1); 243 208 * summ.put(2); … … 247 212 * assert(summ.mean == 3); 248 213 * ---*/ 249 struct OnlineMean {214 struct Mean { 250 215 private: 251 216 real result = 0; … … 277 242 278 243 /// 279 struct OnlineGeometricMean {244 struct GeometricMean { 280 245 private: 281 OnlineMean m;246 Mean m; 282 247 public: 283 248 ///Allow implicit casting to real, by returning current geometric mean. … … 305 270 } 306 271 307 308 309 272 /// 310 273 real geometricMean(T)(T data) 311 274 if(realIterable!(T)) { 312 OnlineGeometricMean m;275 GeometricMean m; 313 276 foreach(elem; data) { 314 277 m.put(elem); … … 353 316 * for stdev, var cost a few floating point ops. Getter for mean costs 354 317 * a single branch to check for N == 0. Relatively expensive floating point 355 * ops, if you only need mean, try OnlineMean. This struct uses O(1) space and318 * ops, if you only need mean, try Mean. This struct uses O(1) space and 356 319 * does *NOT* store the individual elements. 357 320 * 358 321 * Examples: 359 322 * --- 360 * OnlineMeanSD summ;323 * MeanSD summ; 361 324 * summ.put(1); 362 325 * summ.put(2); … … 368 331 * assert(summ.var == 2.5); 369 332 * ---*/ 370 struct OnlineMeanSD {333 struct MeanSD { 371 334 private: 372 335 real _mean = 0; … … 396 359 } 397 360 361 // Undocumented on purpose b/c it's for internal use only. 398 362 real mse() const { 399 363 return (_k < 2) ? real.nan : (_var - _mean * _mean); … … 407 371 /// 408 372 string toString() { 409 return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ",373 return text("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 410 374 var, "\nStdev = ", stdev); 411 375 } 412 376 } 413 377 414 /**Simple holder for mean, stdev/variance. Plain old data, accessing is 415 * cheap.*/ 416 struct MeanSD { 417 /// 418 real mean; 419 /// 420 real SD; 421 422 string toString() { 423 return format("Mean = ", mean, "\nStdev = ", SD); 424 } 378 /**Convenience function that puts all elements of data into a MeanSD struct, 379 * then returns this struct.*/ 380 MeanSD meanStdev(T)(T data) 381 if(realIterable!(T)) { 382 MeanSD ret; 383 foreach(elem; data) { 384 ret.put(elem); 385 } 386 return ret; 425 387 } 426 388 … … 429 391 real variance(T)(T data) 430 392 if(realIterable!(T)) { 431 return meanVariance(data).SD; 432 } 433 434 /**Calculates both mean and variance of an input range, returns a MeanSD 435 * struct.*/ 436 MeanSD meanVariance(T)(T data) 437 if(realIterable!(T)) { 438 OnlineMeanSD meanSDCalc; 439 foreach(element; data) { 440 meanSDCalc.put(element); 441 } 442 443 return MeanSD(meanSDCalc.mean, meanSDCalc.var); 444 } 445 446 /**Calculates both mean and standard deviation of an input range, returns a 447 * MeanSD struct.*/ 448 MeanSD meanStdev(T)(T data) 449 if(realIterable!(T)) { 450 auto ret = meanVariance(data); 451 ret.SD = sqrt(ret.SD); 452 return ret; 393 return meanStdev(data).var; 453 394 } 454 395 … … 457 398 real stdev(T)(T data) 458 399 if(realIterable!(T)) { 459 return meanStdev(data). SD;400 return meanStdev(data).stdev; 460 401 } 461 402 462 403 unittest { 463 404 auto res = meanStdev(cast(int[]) [3, 1, 4, 5]); 464 assert(approxEqual(res. SD, 1.7078));405 assert(approxEqual(res.stdev, 1.7078)); 465 406 assert(approxEqual(res.mean, 3.25)); 466 407 res = meanStdev(cast(double[]) [1.0, 2.0, 3.0, 4.0, 5.0]); 467 assert(approxEqual(res. SD, 1.5811));408 assert(approxEqual(res.stdev, 1.5811)); 468 409 assert(approxEqual(res.mean, 3)); 469 410 writefln("Passed variance/standard deviation unittest."); … … 472 413 /**Output range to compute mean, stdev, variance, skewness, kurtosis, min, and 473 414 * max online. Using this struct is relatively expensive, so if you just need 474 * mean and/or stdev, try OnlineMeanSD or OnlineMean. Getter methods for stdev,415 * mean and/or stdev, try MeanSD or Mean. Getter methods for stdev, 475 416 * var cost a few floating point ops. Getter for mean costs a single branch to 476 417 * check for N == 0. Getters for skewness and kurtosis cost a whole bunch of … … 480 421 * Examples: 481 422 * --- 482 * OnlineSummary summ;423 * Summary summ; 483 424 * summ.put(1); 484 425 * summ.put(2); … … 494 435 * assert(summ.max == 5); 495 436 * ---*/ 496 struct OnlineSummary {437 struct Summary { 497 438 private: 498 439 real _mean = 0; … … 563 504 /// 564 505 string toString() { 565 return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ",506 return text("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 566 507 var, "\nStdev = ", stdev, "\nSkewness = ", skewness, 567 508 "\nKurtosis = ", kurtosis, "\nMin = ", _min, "\nMax = ", _max); … … 576 517 real kurtosis(T)(T data) 577 518 if(realIterable!(T)) { 578 OnlineSummary kCalc;519 Summary kCalc; 579 520 foreach(elem; data) { 580 521 kCalc.put(elem); … … 598 539 real skewness(T)(T data) 599 540 if(realIterable!(T)) { 600 OnlineSummary sCalc;541 Summary sCalc; 601 542 foreach(elem; data) { 602 543 sCalc.put(elem); … … 618 559 } 619 560 620 /**Plain old data struct for holding results of summary(). Accessing members 621 * is cheap.*/ 622 struct Summary { 623 /// 624 ulong N; 625 626 /// 627 real mean; 628 629 /// 630 real var; 631 632 /// 633 real SD; 634 635 /// 636 real skew; 637 638 /// 639 real kurtosis; 640 641 /// 642 real min; 643 644 /// 645 real max; 646 647 /// 648 string toString() { 649 return format("N = ", N, "\nMean = ", mean, "\nVariance = ", 650 var, "\nStdev = ", SD, "\nSkewness = ", skew, 651 "\nKurtosis = ", kurtosis, "\nMin = ", min, "\nMax = ", max); 652 } 653 } 654 655 /**Calculates all summary stats (mean, variance, standard dev., skewness 656 * and kurtosis) on an input range with elements that can be implicitly 657 * converted to real. Returns the results in a Summary struct.*/ 561 /**Convenience function. Puts all elements of data into a Summary struct, 562 * and returns this struct.*/ 658 563 Summary summary(T)(T data) 659 564 if(realIterable!(T)) { 660 OnlineSummary summ;565 Summary summ; 661 566 foreach(elem; data) { 662 567 summ.put(elem); 663 568 } 664 real variance = summ.var; 665 return Summary(lrint(summ.N), summ.mean, variance, sqrt(variance), 666 summ.skewness, summ.kurtosis, summ.min, summ.max); 569 return summ; 667 570 } 668 571 // Just a convenience function for a well-tested struct. No unittest really … … 685 588 auto msd = meanStdev(range); 686 589 this.mean = msd.mean; 687 this.sdNeg1 = 1.0L / msd. SD;590 this.sdNeg1 = 1.0L / msd.stdev; 688 591 } 689 592 trunk/tests.d
r84 r88 4 4 * Author: David Simcha*/ 5 5 /* 6 * You may use this software under your choice of either of the following 7 * licenses. YOU NEED ONLY OBEY THE TERMS OF EXACTLY ONE OF THE TWO LICENSES. 8 * IF YOU CHOOSE TO USE THE PHOBOS LICENSE, YOU DO NOT NEED TO OBEY THE TERMS OF 9 * THE BSD LICENSE. IF YOU CHOOSE TO USE THE BSD LICENSE, YOU DO NOT NEED 10 * TO OBEY THE TERMS OF THE PHOBOS LICENSE. IF YOU ARE A LAWYER LOOKING FOR 11 * LOOPHOLES AND RIDICULOUSLY NON-EXISTENT AMBIGUITIES IN THE PREVIOUS STATEMENT, 12 * GET A LIFE. 13 * 14 * ---------------------Phobos License: --------------------------------------- 15 * 16 * Copyright (C) 2008-2009 by David Simcha. 17 * 18 * This software is provided 'as-is', without any express or implied 19 * warranty. In no event will the authors be held liable for any damages 20 * arising from the use of this software. 21 * 22 * Permission is granted to anyone to use this software for any purpose, 23 * including commercial applications, and to alter it and redistribute it 24 * freely, in both source and binary form, subject to the following 25 * restrictions: 26 * 27 * o The origin of this software must not be misrepresented; you must not 28 * claim that you wrote the original software. If you use this software 29 * in a product, an acknowledgment in the product documentation would be 30 * appreciated but is not required. 31 * o Altered source versions must be plainly marked as such, and must not 32 * be misrepresented as being the original software. 33 * o This notice may not be removed or altered from any source 34 * distribution. 35 * 36 * --------------------BSD License: ----------------------------------------- 37 * 38 * Copyright (c) 2008-2009, David Simcha 39 * All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions are met: 43 * 44 * * Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 47 * * Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 51 * * Neither the name of the authors nor the 52 * names of its contributors may be used to endorse or promote products 53 * derived from this software without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 56 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 57 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 58 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 59 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 60 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 61 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6 * License: 7 * Boost Software License - Version 1.0 - August 17th, 2003 8 * 9 * Permission is hereby granted, free of charge, to any person or organization 10 * obtaining a copy of the software and accompanying documentation covered by 11 * this license (the "Software") to use, reproduce, display, distribute, 12 * execute, and transmit the Software, and to prepare derivative works of the 13 * Software, and to permit third-parties to whom the Software is furnished to 14 * do so, all subject to the following: 15 * 16 * The copyright notices in the Software and this entire statement, including 17 * the above license grant, this restriction and the following disclaimer, 18 * must be included in all copies of the Software, in whole or in part, and 19 * all derivative works of the Software, unless such copies or derivative 20 * works are solely in the form of machine-executable object code generated by 21 * a source language processor. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 26 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 27 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 29 * DEALINGS IN THE SOFTWARE. 65 30 */ 66 31 module dstats.tests; 67 32 68 33 import dstats.base, dstats.distrib, dstats.alloc, dstats.summary, dstats.sort, 69 dstats.cor, std.algorithm, std.functional, std.range, std.c.stdlib, 70 std.conv; 34 dstats.cor, std.algorithm, std.functional, std.range, std.conv; 71 35 72 36 version(unittest) { 73 37 import std.stdio, dstats.random; 74 75 Random gen;76 77 static this() {78 gen.seed(unpredictableSeed);79 }80 81 38 void main(){} 82 39 } … … 140 97 } 141 98 99 /**Tests whether a struct/class has the necessary information for calculating 100 * a T-test. It must have a property .mean (mean), .stdev (stdandard deviation), 101 * .var (variance), and .N (sample size). 102 */ 103 template isSummary(T) { 104 enum bool isSummary = is(typeof(T.init.mean)) && is(typeof(T.init.stdev)) && 105 is(typeof(T.init.var)) && is(typeof(T.init.N)); 106 } 107 142 108 /**One-sample Student's T-test for difference between mean of data and 143 109 * a fixed value. Alternatives are Alt.LESS, meaning mean(data) < testMean, … … 150 116 real confLevel = 0.95) 151 117 if(realIterable!(T)) { 152 return pairedTTest(data, repeat(0), testMean, alt, confLevel); 153 118 return studentsTTest( meanStdev(data), testMean, alt, confLevel); 119 120 } 121 122 /**Calculate the test directly from summary statistics of the data.*/ 123 ConfInt studentsTTest(T)(T summary, real testMean = 0, Alt alt = Alt.TWOSIDE, 124 real confLevel = 0.95) 125 if(isSummary!T) { 126 return pairedTTest(summary, testMean, alt, confLevel); 154 127 } 155 128 … … 188 161 Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 189 162 if(realIterable!(T) && realIterable!(U)) { 190 size_t n1, n2; 191 OnlineMeanSD s1summ, s2summ; 192 foreach(elem; sample1) { 193 s1summ.put(elem); 194 n1++; 195 } 196 foreach(elem; sample2) { 197 s2summ.put(elem); 198 n2++; 199 } 163 return studentsTTest( meanStdev(sample1), meanStdev(sample2), 164 testMean, alt, confLevel); 165 } 166 167 /**Calculate the test directly from summary statistics for each sample.*/ 168 ConfInt studentsTTest(T, U)(T sample1Summary, U sample2Summary, 169 real testMean = 0, Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 170 if(isSummary!T && isSummary!U) { 171 // Save some typing. 172 alias sample1Summary s1summ; 173 alias sample2Summary s2summ; 174 175 real n1 = s1summ.N, 176 n2 = s2summ.N; 200 177 201 178 real sx1x2 = sqrt(((n1 - 1) * s1summ.var + (n2 - 1) * s2summ.var) / … … 272 249 Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 273 250 if(realIterable!(T) && realIterable!(U)) { 274 size_t n1, n2; 275 OnlineMeanSD s1summ, s2summ; 276 foreach(elem; sample1) { 277 s1summ.put(elem); 278 n1++; 279 } 280 foreach(elem; sample2) { 281 s2summ.put(elem); 282 n2++; 283 } 251 return welchTTest( meanStdev(sample1), meanStdev(sample2), 252 testMean, alt, confLevel); 253 } 254 255 /**Calculate the test directly from summary statistics for each sample.*/ 256 ConfInt welchTTest(T, U)(T sample1Summary, U sample2Summary, real testMean = 0, 257 Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 258 if(isSummary!(T) && isSummary!(U)) { 259 // Save typing. 260 alias sample1Summary s1summ; 261 alias sample2Summary s2summ; 262 263 real n1 = s1summ.N, 264 n2 = s2summ.N; 284 265 285 266 auto v1 = s1summ.var, v2 = s2summ.var; … … 358 339 Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 359 340 if(realInput!(T) && realInput!(U)) { 360 OnlineMeanSD msd; 361 size_t len = 0; 341 MeanSD msd; 362 342 while(!before.empty && !after.empty) { 363 343 real diff = cast(real) before.front - cast(real) after.front; … … 365 345 after.popFront; 366 346 msd.put(diff); 367 len++; 368 } 347 } 348 349 return pairedTTest(msd, testMean, alt, confLevel); 350 } 351 352 /**Compute the test directly from summary statistics of the differences between 353 * corresponding samples.*/ 354 ConfInt pairedTTest(T)(T diffSummary, real testMean = 0, 355 Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 356 if(isSummary!T) { 357 // Save typing. 358 alias diffSummary msd; 369 359 370 360 ConfInt ret; 371 ret.testStat = (msd.mean - testMean) / msd.stdev * sqrt( cast(real) len);361 ret.testStat = (msd.mean - testMean) / msd.stdev * sqrt(msd.N); 372 362 auto sampleMean = msd.mean; 373 363 auto sampleSd = msd.stdev; 374 real normSd = sampleSd / sqrt( cast(real) len);364 real normSd = sampleSd / sqrt(msd.N); 375 365 ret.testStat = (sampleMean - testMean) / normSd; 376 366 … … 378 368 return ret; 379 369 } else if(alt == Alt.LESS) { 380 ret.p = studentsTCDF(ret.testStat, len- 1);381 real delta = invStudentsTCDF(1 - confLevel, len- 1) * normSd;370 ret.p = studentsTCDF(ret.testStat, msd.N - 1); 371 real delta = invStudentsTCDF(1 - confLevel, msd.N - 1) * normSd; 382 372 ret.lowerBound = -real.infinity; 383 373 ret.upperBound = sampleMean - delta; 384 374 } else if(alt == Alt.GREATER) { 385 ret.p = studentsTCDF(-ret.testStat, len- 1);386 real delta = invStudentsTCDF(1 - confLevel, len- 1) * normSd;375 ret.p = studentsTCDF(-ret.testStat, msd.N - 1); 376 real delta = invStudentsTCDF(1 - confLevel, msd.N - 1) * normSd; 387 377 ret.lowerBound = sampleMean + delta; 388 378 ret.upperBound = real.infinity; 389 379 } else { 390 ret.p = 2 * min(studentsTCDF(ret.testStat, len- 1),391 studentsTCDF(-ret.testStat, len- 1));392 real delta = invStudentsTCDF(0.5 * (1 - confLevel), len- 1) * normSd;380 ret.p = 2 * min(studentsTCDF(ret.testStat, msd.N - 1), 381 studentsTCDF(-ret.testStat, msd.N - 1)); 382 real delta = invStudentsTCDF(0.5 * (1 - confLevel), msd.N - 1) * normSd; 393 383 ret.lowerBound = sampleMean + delta; 394 384 ret.upperBound = sampleMean - delta; … … 429 419 mixin(newFrame); 430 420 auto data = tempdup(dataIn[0]); 431 auto withins = newStack! OnlineMeanSD(data.length);432 withins[] = OnlineMeanSD.init;421 auto withins = newStack!MeanSD(data.length); 422 withins[] = MeanSD.init; 433 423 } else { 434 424 enum len = dataIn.length; 435 425 alias dataIn data; 436 OnlineMeanSD[len] withins;437 } 438 439 OnlineMean overallMean;426 MeanSD[len] withins; 427 } 428 429 Mean overallMean; 440 430 uint DFGroups = data.length - 1; 441 431 uint DFDataPoints = 0; … … 510 500 * 511 501 * References: "Concepts and Applications of Inferrential Statistics". 512 * Richard Lowry. Vassar College. Onlineversion.502 * Richard Lowry. Vassar College. version. 513 503 * http://faculty.vassar.edu/lowry/webtext.html 514 504 */ … … 518 508 mixin(newFrame); 519 509 auto data = tempdup(dataIn[0]); 520 auto withins = newStack! OnlineMeanSD(data.length);521 withins[] = OnlineMeanSD.init;510 auto withins = newStack!MeanSD(data.length); 511 withins[] = MeanSD.init; 522 512 } else { 523 513 enum len = dataIn.length; 524 514 alias dataIn data; 525 OnlineMeanSD[len] withins;526 } 527 OnlineMeanSD overallSumm;515 MeanSD[len] withins; 516 } 517 MeanSD overallSumm; 528 518 real nGroupNeg1 = 1.0L / data.length; 529 519 … … 618 608 mixin(newFrame); 619 609 size_t N = 0; 610 620 611 static if(dataIn.length == 1 && isInputRange!(typeof(dataIn[0].front))) { 621 612 auto data = tempdup(dataIn[0]); … … 671 662 real rBar = 0.5L * (N + 1); 672 663 foreach(meanI, l; lengths) { 673 OnlineMean groupStats;664 Mean groupStats; 674 665 foreach(i; index..index + l) { 675 666 groupStats.put( ranks[i]); … … 749 740 auto ranks = newStack!float(data.length); 750 741 auto dataPoints = newStack!real(data.length); 751 auto colMeans = newStack! OnlineMean(data.length);752 colMeans[] = OnlineMean.init;742 auto colMeans = newStack!Mean(data.length); 743 colMeans[] = Mean.init; 753 744 } else { 754 745 enum len = dataIn.length; … … 756 747 float[len] ranks; 757 748 real[len] dataPoints; 758 OnlineMean[len] colMeans;749 Mean[len] colMeans; 759 750 } 760 751 real rBar = cast(real) data.length * (data.length + 1.0L) / 2.0L; 761 OnlineMeanSD overallSumm;752 MeanSD overallSumm; 762 753 763 754 bool someEmpty() { … … 917 908 } 918 909 919 real wilcoxonRankSumW(T)(T sample1, T sample2, real* tieSum = null)910 private real wilcoxonRankSumW(T)(T sample1, T sample2, real* tieSum = null) 920 911 if(isInputRange!(T) && dstats.base.hasLength!(T)) { 921 912 uint n1 = sample1.length, n2 = sample2.length, N = n1 + n2; … … 954 945 } 955 946 947 private 956 948 real wilcoxonRankSumPval(T)(T w, ulong n1, ulong n2, Alt alt = Alt.TWOSIDE, 957 949 real tieSum = 0, uint exactThresh = 50) { 950 if(alt == Alt.NONE) { 951 return real.nan; 952 } 953 958 954 ulong N = n1 + n2; 959 955 … … 990 986 * programming to count the number of combinations of numbers [1..N] that sum 991 987 * of length n1 that sum to <= W in O(N * W * n1) time.*/ 992 real wilcoxRSPExact(uint W, uint n1, uint n2, Alt alt = Alt.TWOSIDE) {988 private real wilcoxRSPExact(uint W, uint n1, uint n2, Alt alt = Alt.TWOSIDE) { 993 989 uint N = n1 + n2; 994 990 uint expected2 = n1 * n2; … … 1230 1226 } 1231 1227 1232 1233 real wilcoxonSignedRankPval(T)(T W, ulong N, Alt alt = Alt.TWOSIDE, 1228 private real wilcoxonSignedRankPval(T)(T W, ulong N, Alt alt = Alt.TWOSIDE, 1234 1229 real tieSum = 0, uint exactThresh = 50) 1235 1230 in { … … 1237 1232 assert(tieSum >= 0 || isNaN(tieSum)); 1238 1233 } body { 1234 if(alt == Alt.NONE) { 1235 return real.nan; 1236 } 1237 1239 1238 if(tieSum == 0 && !isNaN(tieSum) && N <= exactThresh) { 1240 1239 return wilcoxSRPExact(cast(uint) W, cast(uint) N, alt); … … 1263 1262 * it would be more trouble than it's worth to write one generalized 1264 1263 * function.*/ 1265 real wilcoxSRPExact(uint W, uint N, Alt alt = Alt.TWOSIDE) {1264 private real wilcoxSRPExact(uint W, uint N, Alt alt = Alt.TWOSIDE) { 1266 1265 uint expected2 = N * (N + 1) / 2; 1267 1266 switch(alt) { … … 1570 1569 real multiplier = 1; 1571 1570 1571 // Normalize proportions to add up to the sum of the data. 1572 1572 if(countProp == Expected.PROPORTION) { 1573 1573 real expectSum = 0; … … 1712 1712 * 1713 1713 * Yates's continuity correction is never used in this implementation. If 1714 * you want something conservative, use fisherExact().1714 * you want something that's guaranteed to be conservative, use fisherExact(). 1715 1715 * 1716 1716 * This is, for all practical purposes, an inherently non-directional test. … … 2182 2182 } 2183 2183 2184 real ksTestD(T, U)(T F, U Fprime)2184 private real ksTestD(T, U)(T F, U Fprime) 2185 2185 if(isInputRange!(T) && isInputRange!(U)) { 2186 2186 auto TAState = TempAlloc.getState; … … 2192 2192 } 2193 2193 2194 real ksTestDDestructive(T, U)(T F, U Fprime)2194 private real ksTestDDestructive(T, U)(T F, U Fprime) 2195 2195 if(isArrayLike!(T) && isArrayLike!(U)) { 2196 2196 qsort(F); … … 2217 2217 } 2218 2218 2219 real ksTestD(T, Func)(T Femp, Func F)2219 private real ksTestD(T, Func)(T Femp, Func F) 2220 2220 if(realInput!(T) && is(ReturnType!Func : real)) { 2221 2221 scope(exit) TempAlloc.free; … … 2223 2223 } 2224 2224 2225 real ksTestDDestructive(T, Func)(T Femp, Func F)2225 private real ksTestDDestructive(T, Func)(T Femp, Func F) 2226 2226 if(isArrayLike!(T) && is(ReturnType!Func : real)) { 2227 2227 qsort(Femp); … … 2237 2237 } 2238 2238 2239 real ksPval(ulong N, ulong Nprime, real D)2239 private real ksPval(ulong N, ulong Nprime, real D) 2240 2240 in { 2241 2241 assert(D >= -1); … … 2245 2245 } 2246 2246 2247 real ksPval(ulong N, real D)2247 private real ksPval(ulong N, real D) 2248 2248 in { 2249 2249 assert(D >= -1); … … 2275 2275 real runsTest(alias positive = "a > 0", T)(T obs, Alt alt = Alt.TWOSIDE) 2276 2276 if(isIterable!(T)) { 2277 OnlineRunsTest!(positive, IterType!(T)) r;2277 RunsTest!(positive, IterType!(T)) r; 2278 2278 foreach(elem; obs) { 2279 2279 r.put(elem); 2280 2280 } 2281 return r.p Val(alt);2281 return r.p(alt); 2282 2282 } 2283 2283 … … 2295 2295 /**Runs test as in runsTest(), except calculates online instead of from stored 2296 2296 * array elements.*/ 2297 struct OnlineRunsTest(alias positive = "a > 0", T) {2297 struct RunsTest(alias positive = "a > 0", T) { 2298 2298 private: 2299 2299 uint nPos; … … 2336 2336 2337 2337 /// 2338 real p Val(Alt alt = Alt.TWOSIDE) {2338 real p(Alt alt = Alt.TWOSIDE) { 2339 2339 uint N = nPos + nNeg; 2340 2340 real expected = 2.0L * nPos * nNeg / N + 1; … … 2361 2361 ConfInt pcorTest(T, U)(T range1, U range2, Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 2362 2362 if(realInput!(T) && realInput!(U)) { 2363 OnlinePcor res;2363 Pcor res; 2364 2364 while(!range1.empty && !range2.empty) { 2365 2365 res.put(range1.front, range2.front); … … 2440 2440 real sqN = sqrt(N - 3); 2441 2441 real z = sqN * atanh(cor); 2442 switch(alt) { 2442 2443 final switch(alt) { 2443 2444 case Alt.NONE : 2444 2445 return ret; … … 2461 2462 ret.upperBound = 1; 2462 2463 break; 2463 default:2464 assert(0);2465 2464 } 2466 2465 return ret; … … 2501 2500 } 2502 2501 2503 switch(alt) {2502 final switch(alt) { 2504 2503 case Alt.NONE : 2505 2504 return TestRes(tau); … … 2511 2510 case Alt.GREATER: 2512 2511 return TestRes(tau, normalCDFR(s - cc, 0, sd)); 2513 default:2514 assert(0);2515 2512 } 2516 2513 } … … 2518 2515 // Dynamic programming algorithm for computing exact Kendall tau P-values. 2519 2516 // Thanks to ShreevatsaR from StackOverflow. 2520 real kcorExactP(uint N, uint swaps, Alt alt) {2517 private real kcorExactP(uint N, uint swaps, Alt alt) { 2521 2518 uint maxSwaps = N * (N - 1) / 2; 2522 2519 assert(swaps <= maxSwaps); … … 2674 2671 { 2675 2672 auto summ = summary(range); 2676 sqrtb1 = summ.skew ;2673 sqrtb1 = summ.skewness; 2677 2674 b2 = summ.kurtosis + 3; 2678 2675 n = summ.N;
