Changeset 88

Show
Ignore:
Timestamp:
10/18/09 06:57:55 (3 years ago)
Author:
dsimcha
Message:

Misc. code cleanup, refactoring.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • docs/alloc.html

    r87 r88  
    798798            Page was generated with 
    799799            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    800             on Tue Oct 13 20:16:25 2009 
     800            on Sun Oct 18 13:42:20 2009 
    801801 
    802802        </td></tr> 
  • docs/base.html

    r87 r88  
    266266 <font color=blue>assert</font>(s == [0, 1, 2, 3, 4]); 
    267267</pre> 
     268  
    268269<br><br> 
    269270 
     
    404405 
    405406 
    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, 
    408408 and can be iterated over with foreach.  Note that permutations are 
    409409 output in undefined order.  Also note that the returned permutations 
     
    427427  <font color=blue>assert</font>(res.length == 6); 
    428428</pre> 
     429  
    429430<br><br> 
    430431 
     
    508509 <font color=blue>auto</font> p = <u>perm</u>(5);  <font color=green>// Permutations of integers on range [0, 5]. 
    509510</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]. 
    536511</font></pre> 
    537512  
     
    653628 
    654629<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> 
     642CombRet 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> 
    655656<dt><span class="decl">T[]  
    656657<span class="currsymbol">intersect</span> 
     
    807808            Page was generated with 
    808809            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    809             on Tue Oct 13 20:16:25 2009 
     810            on Sun Oct 18 13:42:20 2009 
    810811 
    811812        </td></tr> 
  • docs/cor.html

    r87 r88  
    2424<dl> 
    2525<script>explorer.outline.writeEnabled = true;</script> 
    26 <dt><span class="decl">real  
     26<dt><span class="decl">Pcor  
    2727<span class="currsymbol">pcor</span> 
    2828<script>explorer.outline.addDecl('pcor');</script> 
     
    3333 
    3434 
    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 
    3637 usually referring to this quantity.  This is a parametric correlation 
    3738 metric and should not be used with extremely ill-behaved data. 
     
    3940 lengths, it uses the first min(input1.length, input2.length) elements. 
    4041<br><br> 
     42<b>Note:</b><br> 
     43The 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> 
    4148 
    4249</dd> 
     
    4451<script>explorer.outline.writeEnabled = true;</script> 
    4552<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> 
    4855 
    4956; 
     
    5865<br><br> 
    5966 
     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 
    6075 
    6176<script>explorer.outline.incSymbolLevel();</script> 
     
    274289            Page was generated with 
    275290            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    276             on Tue Oct 13 20:16:25 2009 
     291            on Sun Oct 18 13:42:20 2009 
    277292 
    278293        </td></tr> 
  • docs/distrib.html

    r87 r88  
    572572 
    573573(real <span class="funcparam">x</span>, real <span class="funcparam">df1</span>, real <span class="funcparam">df2</span>); 
    574 <br>real  
    575 <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>); 
    579574</span></dt> 
    580575<script>explorer.outline.writeEnabled = false;</script> 
     
    626621<script>explorer.outline.writeEnabled = true;</script> 
    627622<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> 
     633Finds 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  
    628659<span class="currsymbol">negBinomPMF</span> 
    629660<script>explorer.outline.addDecl('negBinomPMF');</script> 
     
    810841<script>explorer.outline.writeEnabled = true;</script> 
    811842<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  
    812856<span class="currsymbol">laplaceCDF</span> 
    813857<script>explorer.outline.addDecl('laplaceCDF');</script> 
     
    869913            Page was generated with 
    870914            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    871             on Tue Oct 13 20:16:26 2009 
     915            on Sun Oct 18 13:42:20 2009 
    872916 
    873917        </td></tr> 
  • docs/gamma.html

    r87 r88  
    242242            Page was generated with 
    243243            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    244             on Tue Oct 13 20:16:26 2009 
     244            on Sun Oct 18 13:42:20 2009 
    245245 
    246246        </td></tr> 
  • docs/infotheory.html

    r87 r88  
    208208            Page was generated with 
    209209            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    210             on Tue Oct 13 20:16:26 2009 
     210            on Sun Oct 18 13:42:20 2009 
    211211 
    212212        </td></tr> 
  • docs/random.html

    r87 r88  
    312312            Page was generated with 
    313313            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    314             on Tue Oct 13 20:16:26 2009 
     314            on Sun Oct 18 13:42:20 2009 
    315315 
    316316        </td></tr> 
  • docs/regress.html

    r87 r88  
    408408            Page was generated with 
    409409            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    410             on Tue Oct 13 20:16:26 2009 
     410            on Sun Oct 18 13:42:20 2009 
    411411 
    412412        </td></tr> 
  • docs/sort.html

    r87 r88  
    406406            Page was generated with 
    407407            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    408             on Tue Oct 13 20:16:26 2009 
     408            on Sun Oct 18 13:42:21 2009 
    409409 
    410410        </td></tr> 
  • docs/stats.html

    r87 r88  
    2121            Page was generated with 
    2222            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    23             on Tue Oct 13 20:16:26 2009 
     23            on Sun Oct 18 13:42:21 2009 
    2424 
    2525        </td></tr> 
  • docs/summary.html

    r87 r88  
    127127<script>explorer.outline.writeEnabled = true;</script> 
    128128<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> 
    131131 
    132132; 
     
    140140<br><br> 
    141141<b>Examples:</b><br> 
    142 <pre class="d_code"> <u>OnlineMean</u> summ; 
     142<pre class="d_code"> <u>Mean</u> summ; 
    143143 summ.put(1); 
    144144 summ.put(2); 
     
    214214<script>explorer.outline.writeEnabled = true;</script> 
    215215<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> 
    218218 
    219219; 
     
    320320<script>explorer.outline.writeEnabled = true;</script> 
    321321<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> 
    324324 
    325325; 
     
    331331 for stdev, var cost a few floating point ops.  Getter for mean costs 
    332332 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 and 
     333 ops, if you only need mean, try Mean.  This struct uses O(1) space and 
    334334 does *NOT* store the individual elements. 
    335335<br><br> 
    336336<b>Examples:</b><br> 
    337 <pre class="d_code"> <u>OnlineMeanSD</u> summ; 
     337<pre class="d_code"> <u>MeanSD</u> summ; 
    338338 summ.put(1); 
    339339 summ.put(2); 
     
    434434 
    435435<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> 
    480448 
    481449</dd> 
     
    502470 
    503471<script>explorer.outline.writeEnabled = true;</script> 
    504 <dt><span class="decl">MeanSD  
    505 <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 MeanSD 
    514  struct. 
    515 <br><br> 
    516  
    517 </dd> 
    518  
    519 <script>explorer.outline.writeEnabled = true;</script> 
    520 <dt><span class="decl">MeanSD  
    521 <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 a 
    530  MeanSD struct. 
    531 <br><br> 
    532  
    533 </dd> 
    534  
    535 <script>explorer.outline.writeEnabled = true;</script> 
    536472<dt><span class="decl">real  
    537473<span class="currsymbol">stdev</span> 
     
    551487<script>explorer.outline.writeEnabled = true;</script> 
    552488<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> 
    555491 
    556492; 
     
    561497<dd>Output range to compute mean, stdev, variance, skewness, kurtosis, min, and 
    562498 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, 
    564500 var cost a few floating point ops.  Getter for mean costs a single branch to 
    565501 check for N == 0.  Getters for skewness and kurtosis cost a whole bunch of 
     
    568504<br><br> 
    569505<b>Examples:</b><br> 
    570 <pre class="d_code"> <u>OnlineSummary</u> summ; 
     506<pre class="d_code"> <u>Summary</u> summ; 
    571507 summ.put(1); 
    572508 summ.put(2); 
     
    789725 
    790726<script>explorer.outline.writeEnabled = true;</script> 
    791 <dt><span class="decl">struct  
    792 <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 members 
    801  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">ulong  
    809 <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">real  
    822 <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">real  
    835 <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">real  
    848 <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">real  
    861 <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">real  
    874 <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">real  
    887 <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">real  
    900 <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">string  
    913 <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> 
    930727<dt><span class="decl">Summary  
    931728<span class="currsymbol">summary</span> 
     
    937734 
    938735 
    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. 
    946738<br><br> 
    947739 
     
    1116908            Page was generated with 
    1117909            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    1118             on Tue Oct 13 20:16:26 2009 
     910            on Sun Oct 18 13:42:21 2009 
    1119911 
    1120912        </td></tr> 
  • docs/tests.html

    r87 r88  
    247247 
    248248<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> 
    249266<dt><span class="decl">ConfInt  
    250267<span class="currsymbol">studentsTTest</span> 
     
    264281A ConfInt containing T, the P-value and the boundaries of 
    265282 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. 
    266298<br><br> 
    267299 
     
    294326<script>explorer.outline.writeEnabled = true;</script> 
    295327<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  
    296343<span class="currsymbol">welchTTest</span> 
    297344<script>explorer.outline.addDecl('welchTTest');</script> 
     
    311358 boundaries of the confidence interval for the difference between means 
    312359 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. 
    313375<br><br> 
    314376 
     
    336398 boundaries of the confidence interval for the mean difference between 
    337399 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. 
    338416<br><br> 
    339417 
     
    404482<b>References:</b><br> 
    405483"Concepts and Applications of Inferrential Statistics". 
    406               Richard Lowry.  Vassar College.  Online version. 
     484              Richard Lowry.  Vassar College.  version. 
    407485<br><br> 
    408486<b>http:</b><br> 
     
    810888 
    811889 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(). 
    813891<br><br> 
    814892 
     
    10491127<script>explorer.outline.writeEnabled = true;</script> 
    10501128<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> 
    10531131 
    10541132(alias positive = "a &gt; 0",T); 
     
    10921170<script>explorer.outline.writeEnabled = true;</script> 
    10931171<dt><span class="decl">real  
    1094 <span class="currsymbol">pVal</span> 
    1095 <script>explorer.outline.addDecl('pVal');</script> 
     1172<span class="currsymbol">p</span> 
     1173<script>explorer.outline.addDecl('p');</script> 
    10961174 
    10971175(Alt <span class="funcparam">alt</span> = Alt.TWOSIDE); 
     
    14081486            Page was generated with 
    14091487            <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 
    1410             on Tue Oct 13 20:16:26 2009 
     1488            on Sun Oct 18 13:42:21 2009 
    14111489 
    14121490        </td></tr> 
  • trunk/alloc.d

    r86 r88  
    33 * Author:  David Simcha*/ 
    44 /* 
    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 
    127 * 
    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: 
    1414 * 
    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. 
    1621 * 
    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. 
    6429 */ 
    6530 
  • trunk/base.d

    r86 r88  
    66 * Author:  David Simcha*/ 
    77 /* 
    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. 
    6732 */ 
    68  
    6933module dstats.base; 
    7034 
     
    7236private import dstats.sort, std.c.stdlib, std.bigint, std.typecons, 
    7337               std.functional, std.algorithm, std.range, std.bitmanip, 
    74                std.stdio
     38               std.stdio, std.contracts
    7539 
    7640import std.string : strip; 
     
    7943immutable real[] staticLogFacTable; 
    8044 
    81 enum : size_t { 
    82     staticFacTableLen = 10_000, 
    83 
     45private enum size_t staticFacTableLen = 10_000; 
    8446 
    8547static this() { 
     
    262224if(isForwardRange!(T) && realInput!(T) && isIntegral!(Ret)) 
    263225in { 
    264     assert(nbin <= Ret.max + 1); 
    265226    assert(nbin > 0); 
    266227} 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 
    267234    alias ElementType!(T) E; 
    268235    Unqual!(E) min = data.front, max = data.front; 
     
    306273    assert(res == [cast(ubyte) 0, 0, 0, 0, 1, 3, 5, 7, 8, 9]); 
    307274    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 
    308284    writeln("Passed bin unittest."); 
    309285} 
     
    326302    assert(nbin > 0); 
    327303    assert(nbin <= data.length); 
    328     assert(nbin <= Ret.max + 1); 
    329304} 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 
    330311    Ret[] result; 
    331312    if(alloc == Alloc.HEAP) { 
     
    365346    assert(res == [cast(ubyte) 1, 0, 1, 0, 2, 3, 0, 2, 2, 1, 1, 3, 3, 2, 3, 0]); 
    366347    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 
    367357    writeln("Passed frqBin unittest."); 
    368358} 
     
    375365 * auto s = seq(0, 5); 
    376366 * assert(s == [0, 1, 2, 3, 4]); 
    377  * ---*/ 
    378  
     367 * --- 
     368 */ 
    379369T[] seq(T)(T start, T end, T increment = 1, Alloc alloc = Alloc.HEAP) { 
    380370    T[] output; 
     
    463453} 
    464454 
    465 // Used internally by rank()
     455// Used internally by rank() and dstats.cor.scor()
    466456void averageTies(T, U)(T sortedInput, U[] ranks, uint[] perms) nothrow 
    467457in { 
     
    538528/// 
    539529/*Values up to 9,999 are pre-calculated and stored in 
    540  * an invariant global array, for performance.  After this point, the gamma 
     530 * an immutable global array, for performance.  After this point, the gamma 
    541531 * function is used, because caching would take up too much memory, and if 
    542532 * done lazily, would cause threading issues.*/ 
     
    577567    writefln("Passed logNcomb unit test."); 
    578568} 
    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 //} 
    597569 
    598570/**A struct that generates all possible permutations of a sequence, 
     
    618590 *  assert(res.canFind([3.0, 2.0, 1.0])); 
    619591 *  assert(res.length == 6); 
    620  *  ---*/ 
     592 *  --- 
     593 */ 
    621594struct Perm(T = uint) { 
    622595private: 
     
    757730    } 
    758731    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     } 
    787732} 
    788733 
     
    909854} 
    910855 
     856private 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 */ 
     875CombRet!(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 
    911884unittest { 
    912885    // Test indexing verison first. 
  • trunk/cor.d

    r87 r88  
    33 * Author:  David Simcha*/ 
    44 /* 
    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. 
    6429 */ 
    6530 
     
    8045} 
    8146 
    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 
    8349 * usually referring to this quantity.  This is a parametric correlation 
    8450 * metric and should not be used with extremely ill-behaved data. 
    8551 * 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 */ 
     58Pcor pcor(T, U)(T input1, U input2) 
    8859if(realInput!(T) && realInput!(U)) { 
    89     OnlinePcor corCalc; 
     60    Pcor corCalc; 
    9061    while(!input1.empty && !input2.empty) { 
    9162        corCalc.put(input1.front, input2.front); 
     
    9465    } 
    9566 
    96     return corCalc.cor
     67    return corCalc
    9768} 
    9869 
     
    12899 * Getters for stdev, var, cov, cor cost floating point division ops.  Getters 
    129100 * 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 */ 
     106struct Pcor { 
    132107private: 
    133108    real _mean1 = 0, _mean2 = 0, _var1 = 0, _var2 = 0, _cov = 0, _k = 0; 
    134109public: 
     110    alias cor this; 
     111 
    135112    /// 
    136113    void put(T, U)(T elem1, U elem2) { 
     
    193170real covariance(T, U)(T input1, U input2) 
    194171if(realInput!(T) && realInput!(U)) { 
    195     OnlinePcor covCalc; 
     172    Pcor covCalc; 
    196173    while(!input1.empty && !input2.empty) { 
    197174        covCalc.put(input1.front, input2.front); 
     
    259236    averageTies(iDup2, i2Ranks, perms); 
    260237 
    261     return pcor(i1Ranks, i2Ranks)
     238    return pcor(i1Ranks, i2Ranks).cor
    262239} 
    263240 
  • trunk/distrib.d

    r85 r88  
    7373 * because in Tango/MathExtra they represent P(X > x) while in dstats they 
    7474 * represent P(X >= x). 
     75 * 
    7576 * 
    7677 * Copyright (c) 2008-2009, David Simcha and Don Clugston 
     
    106107import dstats.base, std.algorithm; 
    107108 
    108 enum SQ2PI = 2.5066282746310005024157652848110452530069867406099383166299L
     109enum SQ2PI = sqrt(2 * PI)
    109110 
    110111version(unittest) { 
     
    179180    assert(stdNormal(2.5) == normalCDF(2.5, 0, 1)); 
    180181} 
    181  
    182182 
    183183/// 
     
    574574    assert(x <= n); 
    575575} body { 
    576     if(x > n1 || x < (n - n2)) 
     576    if(x > n1 || x < (n - n2)) { 
    577577        return 0; 
     578    } 
    578579    real result = logNcomb(n1, x) + logNcomb(n2, n - x) - logNcomb(n1 + n2, n); 
    579580    return exp(result); 
     
    652653  assert(feqrel(chiSqrCDFR(invChiSqCDFR(3.5L, 0.1L), 3.5L), 0.1L)>=real.mant_dig-3); 
    653654  assert(chiSqrCDF(0.4L, 19.02L) + chiSqrCDFR(0.4L, 19.02L) ==1.0L); 
     655  assert(approxEqual( invChiSqCDFR( 3, chiSqrCDFR(1, 3)), 1)); 
    654656  writeln("Passed chi-square unittest."); 
    655657} 
     
    11201122} 
    11211123 
    1122 /* 
     1124/** 
    11231125 * Inverse of complemented Fisher distribution 
    11241126 * 
     
    14251427 
    14261428/// 
     1429real laplacePDF(real x, real mu = 0, real b = 1) pure nothrow { 
     1430    return (exp(-abs(x - mu) / b)) / (2 * b); 
     1431} 
     1432 
     1433unittest { 
     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/// 
    14271440real laplaceCDF(real X, real mu = 0, real b = 1) pure nothrow { 
    14281441    real diff = (X - mu); 
  • trunk/infotheory.d

    r83 r88  
    55 * Author:  David Simcha*/ 
    66 /* 
    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. 
    6631 */ 
    6732 
  • trunk/random.d

    r83 r88  
    10031003        writeln("\tMean Expected: ", a / (a + b), " Observed:  ", summ.mean); 
    10041004        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); 
    10061006        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); 
    10081008        delete observ; 
    10091009    } 
  • trunk/regress.d

    r75 r88  
    66 * 
    77 * 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. 
    6833 */ 
    6934 
     
    175140    foreach(i, row; mat) { 
    176141        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; 
    181143        row[i + mat.length] = absMax; 
    182144    } 
     
    207169    foreach(i; 0..mat.length) { 
    208170        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; 
    213172    } 
    214173 
     
    466425    real S = 0; 
    467426    uint n = 0; 
    468     OnlinePcor R2Calc; 
     427    Pcor R2Calc; 
    469428    for(; !residuals.empty; residuals.popFront) { 
    470429        real residual = residuals.front; 
  • trunk/sort.d

    r83 r88  
    3131 * Author:  David Simcha*/ 
    3232 /* 
    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. 
    9257 */ 
    9358 
  • trunk/stats.d

    r70 r88  
    11/**Convenience module that simply publicly imports everything else.*/ 
     2/* 
     3 * License: 
     4 * If you think this module is even copyrightable, you have issues. 
     5 */ 
    26 
    37module dstats.stats; 
  • trunk/summary.d

    r87 r88  
    1212 * Author:  David Simcha*/ 
    1313 /* 
    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. 
    7338 */ 
    7439 
     
    226191real mean(T)(T data) 
    227192if(realIterable!(T)) { 
    228     OnlineMean meanCalc; 
     193    Mean meanCalc; 
    229194    foreach(element; data) { 
    230195        meanCalc.put(element); 
     
    239204 * Examples: 
    240205 * --- 
    241  * OnlineMean summ; 
     206 * Mean summ; 
    242207 * summ.put(1); 
    243208 * summ.put(2); 
     
    247212 * assert(summ.mean == 3); 
    248213 * ---*/ 
    249 struct OnlineMean { 
     214struct Mean { 
    250215private: 
    251216    real result = 0; 
     
    277242 
    278243/// 
    279 struct OnlineGeometricMean { 
     244struct GeometricMean { 
    280245private: 
    281     OnlineMean m; 
     246    Mean m; 
    282247public: 
    283248    ///Allow implicit casting to real, by returning current geometric mean. 
     
    305270} 
    306271 
    307  
    308  
    309272/// 
    310273real geometricMean(T)(T data) 
    311274if(realIterable!(T)) { 
    312     OnlineGeometricMean m; 
     275    GeometricMean m; 
    313276    foreach(elem; data) { 
    314277        m.put(elem); 
     
    353316 * for stdev, var cost a few floating point ops.  Getter for mean costs 
    354317 * 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 and 
     318 * ops, if you only need mean, try Mean.  This struct uses O(1) space and 
    356319 * does *NOT* store the individual elements. 
    357320 * 
    358321 * Examples: 
    359322 * --- 
    360  * OnlineMeanSD summ; 
     323 * MeanSD summ; 
    361324 * summ.put(1); 
    362325 * summ.put(2); 
     
    368331 * assert(summ.var == 2.5); 
    369332 * ---*/ 
    370 struct OnlineMeanSD { 
     333struct MeanSD { 
    371334private: 
    372335    real _mean = 0; 
     
    396359    } 
    397360 
     361    // Undocumented on purpose b/c it's for internal use only. 
    398362    real mse() const { 
    399363        return (_k < 2) ? real.nan : (_var - _mean * _mean); 
     
    407371    /// 
    408372    string toString() { 
    409         return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 
     373        return text("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 
    410374               var, "\nStdev = ", stdev); 
    411375    } 
    412376} 
    413377 
    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.*/ 
     380MeanSD meanStdev(T)(T data) 
     381if(realIterable!(T)) { 
     382    MeanSD ret; 
     383    foreach(elem; data) { 
     384        ret.put(elem); 
     385    } 
     386    return ret; 
    425387} 
    426388 
     
    429391real variance(T)(T data) 
    430392if(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; 
    453394} 
    454395 
     
    457398real stdev(T)(T data) 
    458399if(realIterable!(T)) { 
    459     return meanStdev(data).SD
     400    return meanStdev(data).stdev
    460401} 
    461402 
    462403unittest { 
    463404    auto res = meanStdev(cast(int[]) [3, 1, 4, 5]); 
    464     assert(approxEqual(res.SD, 1.7078)); 
     405    assert(approxEqual(res.stdev, 1.7078)); 
    465406    assert(approxEqual(res.mean, 3.25)); 
    466407    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)); 
    468409    assert(approxEqual(res.mean, 3)); 
    469410    writefln("Passed variance/standard deviation unittest."); 
     
    472413/**Output range to compute mean, stdev, variance, skewness, kurtosis, min, and 
    473414 * 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, 
    475416 * var cost a few floating point ops.  Getter for mean costs a single branch to 
    476417 * check for N == 0.  Getters for skewness and kurtosis cost a whole bunch of 
     
    480421 * Examples: 
    481422 * --- 
    482  * OnlineSummary summ; 
     423 * Summary summ; 
    483424 * summ.put(1); 
    484425 * summ.put(2); 
     
    494435 * assert(summ.max == 5); 
    495436 * ---*/ 
    496 struct OnlineSummary { 
     437struct Summary { 
    497438private: 
    498439    real _mean = 0; 
     
    563504    /// 
    564505    string toString() { 
    565         return format("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 
     506        return text("N = ", cast(ulong) _k, "\nMean = ", mean, "\nVariance = ", 
    566507               var, "\nStdev = ", stdev, "\nSkewness = ", skewness, 
    567508               "\nKurtosis = ", kurtosis, "\nMin = ", _min, "\nMax = ", _max); 
     
    576517real kurtosis(T)(T data) 
    577518if(realIterable!(T)) { 
    578     OnlineSummary kCalc; 
     519    Summary kCalc; 
    579520    foreach(elem; data) { 
    580521        kCalc.put(elem); 
     
    598539real skewness(T)(T data) 
    599540if(realIterable!(T)) { 
    600     OnlineSummary sCalc; 
     541    Summary sCalc; 
    601542    foreach(elem; data) { 
    602543        sCalc.put(elem); 
     
    618559} 
    619560 
    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.*/ 
    658563Summary summary(T)(T data) 
    659564if(realIterable!(T)) { 
    660     OnlineSummary summ; 
     565    Summary summ; 
    661566    foreach(elem; data) { 
    662567        summ.put(elem); 
    663568    } 
    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; 
    667570} 
    668571// Just a convenience function for a well-tested struct.  No unittest really 
     
    685588        auto msd = meanStdev(range); 
    686589        this.mean = msd.mean; 
    687         this.sdNeg1 = 1.0L / msd.SD
     590        this.sdNeg1 = 1.0L / msd.stdev
    688591    } 
    689592 
  • trunk/tests.d

    r84 r88  
    44 * Author:  David Simcha*/ 
    55 /* 
    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. 
    6530 */ 
    6631module dstats.tests; 
    6732 
    6833import 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; 
    7135 
    7236version(unittest) { 
    7337    import std.stdio, dstats.random; 
    74  
    75     Random gen; 
    76  
    77     static this() { 
    78         gen.seed(unpredictableSeed); 
    79     } 
    80  
    8138    void main(){} 
    8239} 
     
    14097} 
    14198 
     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 */ 
     103template 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 
    142108/**One-sample Student's T-test for difference between mean of data and 
    143109 * a fixed value.  Alternatives are Alt.LESS, meaning mean(data) < testMean, 
     
    150116    real confLevel = 0.95) 
    151117if(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.*/ 
     123ConfInt studentsTTest(T)(T summary, real testMean = 0, Alt alt = Alt.TWOSIDE, 
     124    real confLevel = 0.95) 
     125if(isSummary!T) { 
     126    return pairedTTest(summary, testMean, alt, confLevel); 
    154127} 
    155128 
     
    188161    Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
    189162if(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.*/ 
     168ConfInt studentsTTest(T, U)(T sample1Summary, U sample2Summary, 
     169    real testMean = 0, Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
     170if(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; 
    200177 
    201178    real sx1x2 = sqrt(((n1 - 1) * s1summ.var + (n2 - 1) * s2summ.var) / 
     
    272249    Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
    273250if(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.*/ 
     256ConfInt welchTTest(T, U)(T sample1Summary, U sample2Summary, real testMean = 0, 
     257    Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
     258if(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; 
    284265 
    285266    auto v1 = s1summ.var, v2 = s2summ.var; 
     
    358339    Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
    359340if(realInput!(T) && realInput!(U)) { 
    360     OnlineMeanSD msd; 
    361     size_t len = 0; 
     341    MeanSD msd; 
    362342    while(!before.empty && !after.empty) { 
    363343        real diff = cast(real) before.front - cast(real) after.front; 
     
    365345        after.popFront; 
    366346        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.*/ 
     354ConfInt pairedTTest(T)(T diffSummary, real testMean = 0, 
     355    Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
     356if(isSummary!T) { 
     357    // Save typing. 
     358    alias diffSummary msd; 
    369359 
    370360    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); 
    372362    auto sampleMean = msd.mean; 
    373363    auto sampleSd = msd.stdev; 
    374     real normSd = sampleSd / sqrt(cast(real) len); 
     364    real normSd = sampleSd / sqrt(msd.N); 
    375365    ret.testStat = (sampleMean - testMean) / normSd; 
    376366 
     
    378368        return ret; 
    379369    } 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; 
    382372        ret.lowerBound = -real.infinity; 
    383373        ret.upperBound = sampleMean - delta; 
    384374    } 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; 
    387377        ret.lowerBound = sampleMean + delta; 
    388378        ret.upperBound = real.infinity; 
    389379    } 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; 
    393383        ret.lowerBound = sampleMean + delta; 
    394384        ret.upperBound = sampleMean - delta; 
     
    429419        mixin(newFrame); 
    430420        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; 
    433423    } else { 
    434424        enum len = dataIn.length; 
    435425        alias dataIn data; 
    436         OnlineMeanSD[len] withins; 
    437     } 
    438  
    439     OnlineMean overallMean; 
     426        MeanSD[len] withins; 
     427    } 
     428 
     429    Mean overallMean; 
    440430    uint DFGroups = data.length - 1; 
    441431    uint DFDataPoints = 0; 
     
    510500 * 
    511501 * References:  "Concepts and Applications of Inferrential Statistics". 
    512  *              Richard Lowry.  Vassar College.  Online version. 
     502 *              Richard Lowry.  Vassar College.  version. 
    513503 *              http://faculty.vassar.edu/lowry/webtext.html 
    514504 */ 
     
    518508        mixin(newFrame); 
    519509        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; 
    522512    } else { 
    523513        enum len = dataIn.length; 
    524514        alias dataIn data; 
    525         OnlineMeanSD[len] withins; 
    526     } 
    527     OnlineMeanSD overallSumm; 
     515        MeanSD[len] withins; 
     516    } 
     517    MeanSD overallSumm; 
    528518    real nGroupNeg1 = 1.0L / data.length; 
    529519 
     
    618608    mixin(newFrame); 
    619609    size_t N = 0; 
     610 
    620611    static if(dataIn.length == 1 && isInputRange!(typeof(dataIn[0].front))) { 
    621612        auto data = tempdup(dataIn[0]); 
     
    671662    real rBar = 0.5L * (N + 1); 
    672663    foreach(meanI, l; lengths) { 
    673         OnlineMean groupStats; 
     664        Mean groupStats; 
    674665        foreach(i; index..index + l) { 
    675666            groupStats.put( ranks[i]); 
     
    749740        auto ranks = newStack!float(data.length); 
    750741        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; 
    753744    } else { 
    754745        enum len = dataIn.length; 
     
    756747        float[len] ranks; 
    757748        real[len] dataPoints; 
    758         OnlineMean[len] colMeans; 
     749        Mean[len] colMeans; 
    759750    } 
    760751    real rBar = cast(real) data.length * (data.length + 1.0L) / 2.0L; 
    761     OnlineMeanSD overallSumm; 
     752    MeanSD overallSumm; 
    762753 
    763754    bool someEmpty() { 
     
    917908} 
    918909 
    919 real wilcoxonRankSumW(T)(T sample1, T sample2, real* tieSum = null) 
     910private real wilcoxonRankSumW(T)(T sample1, T sample2, real* tieSum = null) 
    920911if(isInputRange!(T) && dstats.base.hasLength!(T)) { 
    921912    uint n1 = sample1.length, n2 = sample2.length, N = n1 + n2; 
     
    954945} 
    955946 
     947private 
    956948real wilcoxonRankSumPval(T)(T w, ulong n1, ulong n2, Alt alt = Alt.TWOSIDE, 
    957949                           real tieSum = 0,  uint exactThresh = 50) { 
     950    if(alt == Alt.NONE) { 
     951        return real.nan; 
     952    } 
     953 
    958954    ulong N = n1 + n2; 
    959955 
     
    990986 * programming to count the number of combinations of numbers [1..N] that sum 
    991987 * 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) { 
     988private real wilcoxRSPExact(uint W, uint n1, uint n2, Alt alt = Alt.TWOSIDE) { 
    993989    uint N = n1 + n2; 
    994990    uint expected2 = n1 * n2; 
     
    12301226} 
    12311227 
    1232  
    1233 real wilcoxonSignedRankPval(T)(T W, ulong N, Alt alt = Alt.TWOSIDE, 
     1228private real wilcoxonSignedRankPval(T)(T W, ulong N, Alt alt = Alt.TWOSIDE, 
    12341229     real tieSum = 0, uint exactThresh = 50) 
    12351230in { 
     
    12371232    assert(tieSum >= 0 || isNaN(tieSum)); 
    12381233} body { 
     1234    if(alt == Alt.NONE) { 
     1235        return real.nan; 
     1236    } 
     1237 
    12391238    if(tieSum == 0 && !isNaN(tieSum) && N <= exactThresh) { 
    12401239        return wilcoxSRPExact(cast(uint) W, cast(uint) N, alt); 
     
    12631262 * it would be more trouble than it's worth to write one generalized 
    12641263 * function.*/ 
    1265 real wilcoxSRPExact(uint W, uint N, Alt alt = Alt.TWOSIDE) { 
     1264private real wilcoxSRPExact(uint W, uint N, Alt alt = Alt.TWOSIDE) { 
    12661265    uint expected2 = N * (N + 1) / 2; 
    12671266    switch(alt) { 
     
    15701569    real multiplier = 1; 
    15711570 
     1571    // Normalize proportions to add up to the sum of the data. 
    15721572    if(countProp == Expected.PROPORTION) { 
    15731573        real expectSum = 0; 
     
    17121712 * 
    17131713 * 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(). 
    17151715 * 
    17161716 * This is, for all practical purposes, an inherently non-directional test. 
     
    21822182} 
    21832183 
    2184 real ksTestD(T, U)(T F, U Fprime) 
     2184private real ksTestD(T, U)(T F, U Fprime) 
    21852185if(isInputRange!(T) && isInputRange!(U)) { 
    21862186    auto TAState = TempAlloc.getState; 
     
    21922192} 
    21932193 
    2194 real ksTestDDestructive(T, U)(T F, U Fprime) 
     2194private real ksTestDDestructive(T, U)(T F, U Fprime) 
    21952195if(isArrayLike!(T) && isArrayLike!(U)) { 
    21962196    qsort(F); 
     
    22172217} 
    22182218 
    2219 real ksTestD(T, Func)(T Femp, Func F) 
     2219private real ksTestD(T, Func)(T Femp, Func F) 
    22202220if(realInput!(T) && is(ReturnType!Func : real)) { 
    22212221    scope(exit) TempAlloc.free; 
     
    22232223} 
    22242224 
    2225 real ksTestDDestructive(T, Func)(T Femp, Func F) 
     2225private real ksTestDDestructive(T, Func)(T Femp, Func F) 
    22262226if(isArrayLike!(T) && is(ReturnType!Func : real)) { 
    22272227    qsort(Femp); 
     
    22372237} 
    22382238 
    2239 real ksPval(ulong N, ulong Nprime, real D) 
     2239private real ksPval(ulong N, ulong Nprime, real D) 
    22402240in { 
    22412241    assert(D >= -1); 
     
    22452245} 
    22462246 
    2247 real ksPval(ulong N, real D) 
     2247private real ksPval(ulong N, real D) 
    22482248in { 
    22492249    assert(D >= -1); 
     
    22752275real runsTest(alias positive = "a > 0", T)(T obs, Alt alt = Alt.TWOSIDE) 
    22762276if(isIterable!(T)) { 
    2277     OnlineRunsTest!(positive, IterType!(T)) r; 
     2277    RunsTest!(positive, IterType!(T)) r; 
    22782278    foreach(elem; obs) { 
    22792279        r.put(elem); 
    22802280    } 
    2281     return r.pVal(alt); 
     2281    return r.p(alt); 
    22822282} 
    22832283 
     
    22952295/**Runs test as in runsTest(), except calculates online instead of from stored 
    22962296 * array elements.*/ 
    2297 struct OnlineRunsTest(alias positive = "a > 0", T) { 
     2297struct RunsTest(alias positive = "a > 0", T) { 
    22982298private: 
    22992299    uint nPos; 
     
    23362336 
    23372337    /// 
    2338     real pVal(Alt alt = Alt.TWOSIDE) { 
     2338    real p(Alt alt = Alt.TWOSIDE) { 
    23392339        uint N = nPos + nNeg; 
    23402340        real expected = 2.0L * nPos * nNeg / N + 1; 
     
    23612361ConfInt pcorTest(T, U)(T range1, U range2, Alt alt = Alt.TWOSIDE, real confLevel = 0.95) 
    23622362if(realInput!(T) && realInput!(U)) { 
    2363     OnlinePcor res; 
     2363    Pcor res; 
    23642364    while(!range1.empty && !range2.empty) { 
    23652365        res.put(range1.front, range2.front); 
     
    24402440    real sqN = sqrt(N - 3); 
    24412441    real z = sqN * atanh(cor); 
    2442     switch(alt) { 
     2442 
     2443    final switch(alt) { 
    24432444        case Alt.NONE : 
    24442445            return ret; 
     
    24612462            ret.upperBound = 1; 
    24622463            break; 
    2463         default: 
    2464             assert(0); 
    24652464    } 
    24662465    return ret; 
     
    25012500    } 
    25022501 
    2503     switch(alt) { 
     2502    final switch(alt) { 
    25042503        case Alt.NONE : 
    25052504            return TestRes(tau); 
     
    25112510        case Alt.GREATER: 
    25122511            return TestRes(tau, normalCDFR(s - cc, 0, sd)); 
    2513         default: 
    2514             assert(0); 
    25152512    } 
    25162513} 
     
    25182515// Dynamic programming algorithm for computing exact Kendall tau P-values. 
    25192516// Thanks to ShreevatsaR from StackOverflow. 
    2520 real kcorExactP(uint N, uint swaps, Alt alt) { 
     2517private real kcorExactP(uint N, uint swaps, Alt alt) { 
    25212518    uint maxSwaps = N * (N - 1) / 2; 
    25222519    assert(swaps <= maxSwaps); 
     
    26742671    { 
    26752672        auto summ = summary(range); 
    2676         sqrtb1 = summ.skew
     2673        sqrtb1 = summ.skewness
    26772674        b2 = summ.kurtosis + 3; 
    26782675        n = summ.N;