;ò
Å¿Ac           @   sJ  d  Z  d k Z d k Z d k Z d k Z d k Td Z d f  d „  ƒ  YZ d „  Z d „  Z	 d „  Z
 d	 d
 „ Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e d „ Z d e d d „ Z d e d „ Z d e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d  „  Z d! „  Z  d" „  Z! d# „  Z" d$ „  Z# d% d& „ Z$ d' „  Z% d( „  Z& d) „  Z' d* „  Z( d+ „  Z) d, „  Z* d d- d. d/ „ Z+ d d0 d1 d. d2 „ Z, d d3 d4 d. d5 „ Z- e d6 „ Z. d7 „  Z/ d8 „  Z0 d9 „  Z1 d: „  Z2 d; „  Z3 d< „  Z4 d= „  Z5 d> „  Z6 d? „  Z7 d@ „  Z8 dA „  Z9 dB „  Z: dC „  Z; dD „  Z< dE „  Z= dF „  Z> dG „  Z? dH dI dJ „ Z@ dK „  ZA dL „  ZB dM „  ZC dN „  ZD dO „  ZE dP „  ZF dQ „  ZG dR „  ZH dS „  ZI dT „  ZJ dU „  ZK e e eL eM f f ƒ ZN e e	 eL eM f f ƒ ZO e e
 eL eM f f ƒ ZP e e eL eM f f ƒ ZQ e e eL eM f f ƒ ZR e e eL eM f f ƒ ZS e e eL eM f f ƒ ZT e e eL eM f f ƒ ZU e e eL eM f f ƒ ZV e e eL eM f f ƒ ZW e e eL eM f f ƒ ZX e e eL eM f f ƒ ZY e e eL eM f f ƒ ZZ e e eL eM f f ƒ Z[ e e eL eM f f ƒ Z\ e e eL eM f f ƒ Z] e e eL eM f f ƒ Z^ e e eL eM f f ƒ Z_ e e eL eM f f ƒ Z` e e eL eM f f ƒ Za e e eL eM f f ƒ Zb e e eL eM f f ƒ Zc e e eL eM f f ƒ Zd e e  eL eM f f ƒ Ze e e! eL eM f f ƒ Zf e e" eL eM f f ƒ Zg e e# eL eM f f ƒ Zh e e$ eL eM f f ƒ Zi e e% eL eM f f ƒ Zj e e& eL eM f f ƒ Zk e e' eL eM f f ƒ Zl e e( eL eM f f ƒ Zm e e) eL eM f f ƒ Zn e e* eL eM f f ƒ Zo e e+ eL eM f f ƒ Zp e e, eL eM f f ƒ Zq e e- eL eM f f ƒ Zr e e. eL eM f f ƒ Zs e e/ eL eM f f ƒ Zt e e0 eL eM f f ƒ Zu e e2 eL eM f f ƒ Zv e e1 eL eM f f ƒ Zw e e3 eL eM f f ƒ Zx e e4 eL eM f f ƒ Zy e e5 eL eM f f ƒ Zz e e6 e{ e| f f ƒ Z} e e8 e{ e| f f ƒ Z~ e e9 e{ e| f f ƒ Z e e: e{ e| f f ƒ Z€ e e; e{ e| f f ƒ Z e e= e{ e| f f ƒ Z‚ e e7 e{ e| f f ƒ Zƒ e e< e{ e| f f ƒ Z„ e e> eL eM f f ƒ Z… e e? eL eM f f ƒ Z† e eA eL eM f f ƒ Z‡ e eB eL eM f f ƒ Zˆ e eC eL eM f f ƒ Z‰ e eD eL eM f f ƒ ZŠ e eE eL eM f f ƒ Z‹ e eG eL eM f f ƒ ZŒ e eF eL eM f f ƒ Z e eH eL eM f f ƒ ZŽ e eI eL eM f f ƒ Z e eK eL eM f f ƒ Z y%d k‘ Z‘ e‘ Z’ d k“ Z“ e“ Z” e d dV „ Z• e d dW „ Z– e d dX „ Z— d	 dY „ Z˜ e dZ „ Z™ e d[ „ Zš e d d f d\ „ Z› e d d f d] „ Zœ e e d d^ „ Z e d d_ „ Zž e d d f d` „ ZŸ e d d f da „ Z  d e db „ Z¡ e dc „ Z¢ e dd „ Z£ e de „ Z¤ e df „ Z¥ e dg „ Z¦ e dh „ Z§ e di „ Z¨ dj „  Z© dk „  Zª d e dl „ Z« d e d dm „ Z¬ d e dn „ Z­ d e do „ Z® dp „  Z¯ e d dq „ Z° e d dr „ Z± d ds „ Z² e d dt „ Z³ e d du „ Z´ e d dv „ Zµ e d dw „ Z¶ dx „  Z· dy „  Z¸ d dz „ Z¹ d d{ „ Zº e e d d| „ Z» d} „  Z¼ d% d~ „ Z½ d „  Z¾ d€ „  Z¿ d „  ZÀ d d‚ „ ZÁ dƒ „  ZÂ d„ „  ZÃ d… „  ZÄ d† „  ZÅ d d- d. d‡ „ ZÆ e d d0 d1 d. dˆ „ ZÇ e d d0 d1 d. d‰ „ ZÈ e dŠ „ ZÉ d‹ „  ZÊ dŒ „  ZË d „  ZÌ dŽ „  ZÍ d „  ZÎ d „  ZÏ d‘ „  ZÐ d’ „  ZÑ d“ „  ZÒ d” „  ZÓ d• „  ZÔ d– „  ZÕ d d— „ ZÖ d˜ „  Z× d d™ „ ZØ d k“ Z“ d kÙ ZÙ e“ Z” dš „  ZÚ d› „  ZÛ dœ „  ZÜ d „  ZÝ dž „  ZÞ dŸ „  Zß e d d  „ Zà e d¡ „ Zá e d d¢ „ Zâ e d d£ „ Zã e d d¤ „ Zä e d d¥ „ Zå d¦ „  Zæ d§ „  Zç d¨ „  Zè e e eL eM f f e• e’ ié f f ƒ ZN e e	 eL eM f f e– e’ ié f f ƒ ZO e e
 eL eM f f e— e’ ié f f ƒ ZP e e eL eM f f e˜ e’ ié f f ƒ ZQ e e eL eM f f e™ e’ ié f f ƒ ZR e e eL eM f f eš e’ ié f f ƒ ZS e e› e’ ié f f ƒ Zê e eœ e’ ié f f ƒ Zë e eŸ e’ ié f f ƒ Zì e e  e’ ié f f ƒ Zí e e eL eM f f e¡ e’ ié f f ƒ ZT e e eL eM f f e¢ e’ ié f f ƒ ZU e e eL eM f f e£ e’ ié f f ƒ ZV e e eL eM f f e¤ e’ ié f f ƒ ZW e e eL eM f f e¥ e’ ié f f ƒ ZX e e¦ eL eM f f e¦ e’ ié f f ƒ Zî e e§ eL eM f f e§ e’ ié f f ƒ Zï e e¨ eL eM f f e¨ e’ ié f f ƒ Zð e e eL eM f f e© e’ ié f f ƒ ZY e e eL eM f f eª e’ ié f f ƒ ZZ e e eL eM f f e« e’ ié f f ƒ Z[ e e eL eM f f e¬ e’ ié f f ƒ Z\ e e eL eM f f e­ e’ ié f f ƒ Z] e e eL eM f f e® e’ ié f f ƒ Z^ e e eL eM f f e¯ e’ ié f f ƒ Z_ e e eL eM f f e° e’ ié f f ƒ Z` e e eL eM f f e± e’ ié f f ƒ Za e e² e’ ié f f ƒ Zñ e e eL eM f f e³ e’ ié f f ƒ Zb e e eL eM f f e´ e’ ié f f ƒ Zc e e eL eM f f eµ e’ ié f f ƒ Zd e e  eL eM f f e¶ e’ ié f f ƒ Ze e e! eL eM f f e· e’ ié f f ƒ Zf e e" eL eM f f e¸ e’ ié f f ƒ Zg e e» e’ ié f f ƒ Zò e e# eL eM f f e¼ e’ ié f f ƒ Zh e e$ eL eM f f e½ e’ ié f f ƒ Zi e e% eL eM f f eÀ e’ ié f f ƒ Zj e e& eL eM f f eÁ e’ ié f f ƒ Zk e e' eL eM f f eÂ e’ ié f f ƒ Zl e e( eL eM f f eÃ e’ ié f f ƒ Zm e e) eL eM f f eÄ e’ ié f f ƒ Zn e e* eL eM f f eÅ e’ ié f f ƒ Zo e e+ eL eM f f eÆ e’ ié f f ƒ Zp e e, eL eM f f eÇ e’ ié f f ƒ Zq e e- eL eM f f eÈ e’ ié f f ƒ Zr e e. eL eM f f eÉ e’ ié f f ƒ Zs e e/ eL eM f f eÊ e’ ié f f ƒ Zt e e0 eL eM f f eË e’ ié f f ƒ Zu e e1 eL eM f f eÌ e’ ié f f ƒ Zw e e2 eL eM f f eÍ e’ ié f f ƒ Zv e e3 eL eM f f eÎ e’ ié f f ƒ Zx e e4 eL eM f f eÏ e’ ié f f ƒ Zy e e5 eL eM f f eÐ e’ ié f f ƒ Zz e e6 e{ e| f f eÑ e’ ié f f ƒ Z} e e8 e{ e| f f eÓ e’ ié f f ƒ Z~ e e9 e{ e| f f eÔ e’ ié f f ƒ Z e e: e{ e| f f eÕ e’ ié f f ƒ Z€ e e; e{ e| f f eÖ e’ ié f f ƒ Z e e= e{ e| f f eØ e’ ié f f ƒ Z‚ e e7 e{ e| f f eÒ e’ ié f f ƒ Zƒ e e< e{ e| f f e× e’ ié f f ƒ Z„ e e> eL eM f f eÛ e’ ié f f ƒ Z… e e? eL eM f f eÜ e’ ié f f ƒ Z† e eA eL eM e’ ié f f ƒ Z‡ e eB eL eM f f eà e’ ié f f ƒ Zˆ e eC eL eM f f eá e’ ié f f ƒ Z‰ e eD eL eM f f eâ e’ ié f f ƒ ZŠ e eE eL eM f f eã e’ ié f f ƒ Z‹ e eG eL eM f f eä e’ ié f f ƒ ZŒ e eF eL eM f f eå e’ ié f f ƒ Z e eH eL eM f f eæ e’ ié f f ƒ ZŽ e eI eL eM f f eç e’ ié f f ƒ Z e eK eL eM f f eè e’ ié f f ƒ Z Wn eó j
 o n Xd S(©   sq  
stats.py module

(Requires pstat.py module.)

#################################################
#######  Written by:  Gary Strangman  ###########
#######  Last modified:  May 10, 2002 ###########
#################################################

A collection of basic statistical functions for python.  The function
names appear below.

IMPORTANT:  There are really *3* sets of functions.  The first set has an 'l'
prefix, which can be used with list or tuple arguments.  The second set has
an 'a' prefix, which can accept NumPy array arguments.  These latter
functions are defined only when NumPy is available on the system.  The third
type has NO prefix (i.e., has the name that appears below).  Functions of
this set are members of a "Dispatch" class, c/o David Ascher.  This class
allows different functions to be called depending on the type of the passed
arguments.  Thus, stats.mean is a member of the Dispatch class and
stats.mean(range(20)) will call stats.lmean(range(20)) while
stats.mean(Numeric.arange(20)) will call stats.amean(Numeric.arange(20)).
This is a handy way to keep consistent function names when different
argument types require different functions to be called.  Having
implementated the Dispatch class, however, means that to get info on
a given function, you must use the REAL function name ... that is
"print stats.lmean.__doc__" or "print stats.amean.__doc__" work fine,
while "print stats.mean.__doc__" will print the doc for the Dispatch
class.  NUMPY FUNCTIONS ('a' prefix) generally have more argument options
but should otherwise be consistent with the corresponding list functions.

Disclaimers:  The function list is obviously incomplete and, worse, the
functions are not optimized.  All functions have been tested (some more
so than others), but they are far from bulletproof.  Thus, as with any
free software, no warranty or guarantee is expressed or implied. :-)  A
few extra functions that don't appear in the list below can be found by
interested treasure-hunters.  These functions don't necessarily have
both list and array versions but were deemed useful

CENTRAL TENDENCY:  geometricmean
                   harmonicmean
                   mean
                   median
                   medianscore
                   mode

MOMENTS:  moment
          variation
          skew
          kurtosis
          skewtest   (for Numpy arrays only)
          kurtosistest (for Numpy arrays only)
          normaltest (for Numpy arrays only)

ALTERED VERSIONS:  tmean  (for Numpy arrays only)
                   tvar   (for Numpy arrays only)
                   tmin   (for Numpy arrays only)
                   tmax   (for Numpy arrays only)
                   tstdev (for Numpy arrays only)
                   tsem   (for Numpy arrays only)
                   describe

FREQUENCY STATS:  itemfreq
                  scoreatpercentile
                  percentileofscore
                  histogram
                  cumfreq
                  relfreq

VARIABILITY:  obrientransform
              samplevar
              samplestdev
              signaltonoise (for Numpy arrays only)
              var
              stdev
              sterr
              sem
              z
              zs
              zmap (for Numpy arrays only)

TRIMMING FCNS:  threshold (for Numpy arrays only)
                trimboth
                trim1
                round (round all vals to 'n' decimals; Numpy only)

CORRELATION FCNS:  covariance  (for Numpy arrays only)
                   correlation (for Numpy arrays only)
                   paired
                   pearsonr
                   spearmanr
                   pointbiserialr
                   kendalltau
                   linregress

INFERENTIAL STATS:  ttest_1samp
                    ttest_ind
                    ttest_rel
                    chisquare
                    ks_2samp
                    mannwhitneyu
                    ranksums
                    wilcoxont
                    kruskalwallish
                    friedmanchisquare

PROBABILITY CALCS:  chisqprob
                    erfcc
                    zprob
                    ksprob
                    fprob
                    betacf
                    gammln 
                    betai

ANOVA FUNCTIONS:  F_oneway
                  F_value

SUPPORT FUNCTIONS:  writecc
                    incr
                    sign  (for Numpy arrays only)
                    sum
                    cumsum
                    ss
                    summult
                    sumdiffsquared
                    square_of_sums
                    shellsort
                    rankdata
                    outputpairedstats
                    findwithin
N(   s   *f0.59999999999999998s   Dispatchc           B   s    t  Z d  Z d „  Z d „  Z RS(   s‹  
The Dispatch class, care of David Ascher, allows different functions to
be called depending on the argument types.  This way, there can be one
function name regardless of the argument type.  To access function doc
in stats.py module, prefix the function with an 'l' or 'a' for list or
array arguments, respectively.  That is, print stats.lmean.__doc__ or
print stats.amean.__doc__ or whatever.
c         G   s   h  |  _ x_ | D]W \ } } xH | D]@ } | |  i i ƒ  j o t d t | ƒ ‚ n | |  i | <q# Wq W|  i i ƒ  |  _	 d  S(   Ns   can't have two dispatches on (
   s   selfs	   _dispatchs   tupless   funcs   typess   ts   keyss
   ValueErrors   strs   _types(   s   selfs   tupless   ts   funcs   types(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   __init__ë   s    	  c         O   sU   t  | ƒ |  i j o t d t  | ƒ ‚ n t |  i t  | ƒ | f | | ƒ Sd  S(   Ns'   don't know how to dispatch %s arguments(	   s   types   arg1s   selfs   _typess	   TypeErrors   applys	   _dispatchs   argss   kw(   s   selfs   arg1s   argss   kw(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   __call__ô   s    (   s   __name__s
   __module__s   __doc__s   __init__s   __call__(    (    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   Dispatchá   s    		c         C   sB   d } d t |  ƒ } x! |  D] } | t | | ƒ } q W| Sd S(   s¥   
Calculates the geometric mean of the values in the passed list.
That is:  n-th root of (x1 * x2 * ... * xn).  Assumes a '1D' list.

Usage:   lgeometricmean(inlist)
f1.0N(   s   mults   lens   inlists
   one_over_ns   items   pow(   s   inlists
   one_over_ns   items   mult(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lgeometricmean  s      c         C   s7   d } x |  D] } | d | } q Wt |  ƒ | Sd S(   s    
Calculates the harmonic mean of the values in the passed list.
That is:  n / (1/x1 + 1/x2 + ... + 1/xn).  Assumes a '1D' list.

Usage:   lharmonicmean(inlist)
i    f1.0N(   s   sums   inlists   items   len(   s   inlists   items   sum(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lharmonicmean  s      c         C   s9   d } x |  D] } | | } q W| t t |  ƒ ƒ Sd S(   sž   
Returns the arithematic mean of the values in the passed list.
Assumes a '1D' list, but will function on the 1st dim of an array(!).

Usage:   lmean(inlist)
i    N(   s   sums   inlists   items   floats   len(   s   inlists   items   sum(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lmean  s      iè  c         C   sÄ   t  |  | ƒ \ }
 } }	 } t |
 ƒ } x@ t	 t
 | ƒ ƒ D], } | | t
 |  ƒ d j o | } Pq: q: W| |	 | } | | d } t |
 | ƒ } | t
 |  ƒ d | t | ƒ |	 } | Sd S(   sD  
Returns the computed median value of a list of numbers, given the
number of bins to use for the histogram (more bins brings the computed value
closer to the median score, default number of bins = 1000).  See G.W.
Heiman's Basic Stats (1st Edition), or CRC Probability & Statistics.

Usage:   lmedian (inlist, numbins=1000)
f2.0i   N(   s	   histograms   inlists   numbinss   hists   smallests   binsizes   extrass   cumsums   cumhists   ranges   lens   is   cfbins   LRLs   cfbelows   floats   freqs   median(   s   inlists   numbinss   LRLs   smallests   cfbins   is   cfbelows   cumhists   medians   binsizes   hists   extrass   freq(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lmedian,  s      	&c         C   s†   t  i |  ƒ } | i ƒ  t | ƒ d d j o4 t | ƒ d } t | | | | d ƒ d } n t | ƒ d } | | } | Sd S(   s£   
Returns the 'middle' score of the passed list.  If there is an even
number of scores, the mean of the 2 middle scores is returned.

Usage:   lmedianscore(inlist)
i   i    i   N(	   s   copys   deepcopys   inlists   newlists   sorts   lens   indexs   floats   median(   s   inlists   indexs   newlists   median(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lmedianscoreB  s     
$
c         C   sÉ   t  i |  ƒ } | i ƒ  g  } x$ | D] } | i |  i | ƒ ƒ q& Wt	 | ƒ } g  } d } xZ | oR y2 | i | ƒ } | i | | ƒ | | =| | =Wqa t j
 o d } qa Xqa W| | f Sd S(   s  
Returns a list of the modal (most common) score(s) in the passed
list.  If there is more than one such score, all are returned.  The
bin-count for the mode(s) is also returned.

Usage:   lmode(inlist)
Returns: bin-count for mode(s), a list of modal value(s)
i   i    N(   s   pstats   uniques   inlists   scoress   sorts   freqs   items   appends   counts   maxs   maxfreqs   modes	   stillmores   indexs   indxs
   ValueError(   s   inlists	   stillmores   scoress   indxs   items   modes   maxfreqs   freq(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lmodeU  s(     
  i   c         C   sh   | d j o d SnP t |  ƒ } t |  ƒ } d } x  |  D] } | | | | } q: W| t | ƒ Sd S(   sú   
Calculates the nth moment about the mean for a sample (defaults to
the 1st moment).  Used to calculate coefficients of skewness and kurtosis.

Usage:   lmoment(inlist,moment=1)
Returns: appropriate moment (r) from ... 1/n * SUM((inlist(i)-mean)**r)
i   f0.0i    N(	   s   moments   means   inlists   mns   lens   ns   ss   xs   float(   s   inlists   moments   mns   ss   ns   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lmomentv  s      c         C   s"   d t  |  ƒ t t |  ƒ ƒ Sd S(   s€   
Returns the coefficient of variation, as defined in CRC Standard
Probability and Statistics, p.6.

Usage:   lvariation(inlist)
f100.0N(   s   samplestdevs   inlists   floats   mean(   s   inlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lvariation‰  s     c         C   s'   t  |  d ƒ t t  |  d ƒ d ƒ Sd S(   s¤   
Returns the skewness of a distribution, as defined in Numerical
Recipies (alternate defn in CRC Standard Probability and Statistics, p.6.)

Usage:   lskew(inlist)
i   i   f1.5N(   s   moments   inlists   pow(   s   inlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lskew“  s     c         C   s'   t  |  d ƒ t t  |  d ƒ d ƒ Sd S(   s¨   
Returns the kurtosis of a distribution, as defined in Numerical
Recipies (alternate defn in CRC Standard Probability and Statistics, p.6.)

Usage:   lkurtosis(inlist)
i   i   f2.0N(   s   moments   inlists   pow(   s   inlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   lkurtosis  s     c         C   sn   t  |  ƒ } t |  ƒ t |  ƒ f } t |  ƒ } t |  ƒ } t
 |  ƒ } t |  ƒ } | | | | | | f Sd S(   s   
Returns some descriptive statistics of the passed list (assumed to be 1D).

Usage:   ldescribe(inlist)
Returns: n, mean, standard deviation, skew, kurtosis
N(   s   lens   inlists   ns   mins   maxs   mms   means   ms   stdevs   sds   skews   sks   kurtosiss   kurt(   s   inlists   mms   ms   ns   sks   sds   kurt(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   ldescribe§  s     c         C   sZ   t  i |  ƒ } | i ƒ  g  } x$ | D] } | i |  i | ƒ ƒ q& Wt  i	 | | ƒ Sd S(   sç   
Returns a list of pairs.  Each pair consists of one of the scores in inlist
and it's frequency count.  Assumes a 1D list is passed.

Usage:   litemfreq(inlist)
Returns: a 2D frequency table (col [0:n-1]=scores, col n=frequencies)
N(
   s   pstats   uniques   inlists   scoress   sorts   freqs   items   appends   counts   abut(   s   inlists   items   freqs   scores(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   litemfreq»  s     
 c   
      C   sÈ   | d j o d GH| d } n | t |  ƒ } t |  ƒ \ } } } }	 t	 t
 i | ƒ ƒ } x0 t t | ƒ ƒ D] } | | | j o Pqp qp W| | | | d t | | ƒ | | | } | Sd S(   s„   
Returns the score at a given percentile relative to the distribution
given by inlist.

Usage:   lscoreatpercentile(inlist,percent)
i   s4   
Dividing percent>1 by 100 in lscoreatpercentile().
f100.0N(   s   percents   lens   inlists   targetcfs	   histograms   hs   lrls   binsizes   extrass   cumsums   copys   deepcopys   cumhists   ranges   is   floats   score(
   s   inlists   percents   scores   is   hs   cumhists   lrls   binsizes   targetcfs   extras(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lscoreatpercentileË  s      	0i
   c         C   s™   t  |  | | ƒ \ } } }	 }
 t t	 i
 | ƒ ƒ } t | | t |	 ƒ ƒ } | | d | | |	 | t |	 ƒ | | t t |  ƒ ƒ d } | Sd S(   sß   
Returns the percentile value of a score relative to the distribution
given by inlist.  Formula depends on the values used to histogram the data(!).

Usage:   lpercentileofscore(inlist,score,histbins=10,defaultlimits=None)
i   id   N(   s	   histograms   inlists   histbinss   defaultlimitss   hs   lrls   binsizes   extrass   cumsums   copys   deepcopys   cumhists   ints   scores   floats   is   lens   pct(   s   inlists   scores   histbinss   defaultlimitss   is   hs   cumhists   lrls   pcts   binsizes   extras(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lpercentileofscoreß  s     Di    c         C   s  | t j or t | ƒ t t g j p t | ƒ d j o | }
 d t |  ƒ }	 n | d }
 | d }	 |	 |
 t
 | ƒ } n] t |  ƒ t |  ƒ t
 | ƒ d } t |  ƒ t |  ƒ | t
 | ƒ } t |  ƒ | d }
 d g | } d } xq |  D]i } yO | |
 d j  o | d } n- t | |
 t
 | ƒ ƒ } | | d | | <Wqõ | d } qõ Xqõ W| d j o
 | d j o d G| GHn | |
 | | f Sd S(   sá  
Returns (i) a list of histogram bin counts, (ii) the smallest value
of the histogram binning, and (iii) the bin width (the last 2 are not
necessarily integers).  Default number of bins is 10.  If no sequence object
is given for defaultreallimits, the routine picks (usually non-pretty) bins
spanning all the numbers in the inlist.

Usage:   lhistogram (inlist, numbins=10, defaultreallimits=None,suppressoutput=0)
Returns: list of bin values, lowerreallimit, binsize, extrapoints
i   f1.0001i    i   s'   
Points outside given histogram range =N(   s   defaultreallimitss   Nones   types   ListTypes	   TupleTypes   lens   lowerreallimits   maxs   inlists   upperreallimits   floats   numbinss   binsizes   mins   estbinwidths   binss   extrapointss   nums   ints   bintoincrements   printextras(   s   inlists   numbinss   defaultreallimitss   printextrass   bintoincrements   nums   estbinwidths   binsizes   extrapointss   upperreallimits   lowerreallimits   bins(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lhistogramî  s2    
 ,

$$ c         C   sG   t  |  | | ƒ \ } } } } t t	 i
 | ƒ ƒ } | | | | f Sd S(   sÐ   
Returns a cumulative frequency histogram, using the histogram function.

Usage:   lcumfreq(inlist,numbins=10,defaultreallimits=None)
Returns: list of cumfreq bin values, lowerreallimit, binsize, extrapoints
N(   s	   histograms   inlists   numbinss   defaultreallimitss   hs   ls   bs   es   cumsums   copys   deepcopys   cumhist(   s   inlists   numbinss   defaultreallimitss   bs   es   hs   cumhists   l(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lcumfreq  s     c         C   sm   t  |  | | ƒ \ } } } } x8 t t	 | ƒ ƒ D]$ } | | t t	 |  ƒ ƒ | | <q1 W| | | | f Sd S(   sÎ   
Returns a relative frequency histogram, using the histogram function.

Usage:   lrelfreq(inlist,numbins=10,defaultreallimits=None)
Returns: list of cumfreq bin values, lowerreallimit, binsize, extrapoints
N(   s	   histograms   inlists   numbinss   defaultreallimitss   hs   ls   bs   es   ranges   lens   is   float(   s   inlists   numbinss   defaultreallimitss   bs   es   is   hs   l(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lrelfreq!  s      "c          G   sÊ  d }	 t |  ƒ } d g | } d g | }
 d g | } g  } xp t | ƒ D]b } | i
 t i |  | ƒ ƒ t t | | ƒ ƒ | | <t | | ƒ |
 | <t | | ƒ | | <qL Wx­ t | ƒ D]Ÿ } x– t | | ƒ D]„ } | | d | | | | | | | d } d |
 | | | d } | | d | | d } | | t | ƒ | | | <qÖ Wq¿ Wd } x= t | ƒ D]/ } |
 | t | | ƒ |	 j o
 d	 } ququW| d j o t d
 ‚ n | Sd S(   sù   
Computes a transform on input data (any number of columns).  Used to
test for homogeneity of variance prior to running one-way stats.  From
Maxwell and Delaney, p.112.

Usage:   lobrientransform(*args)
Returns: transformed data for use in an ANOVA
f1e-10f0.0f1.5i   f0.5f1.0f2.0i   i    s   Problem in obrientransform.N(   s   TINYs   lens   argss   ks   ns   vs   ms   nargss   ranges   is   appends   copys   deepcopys   floats   vars   means   js   t1s   t2s   t3s   checks
   ValueError(   s   argss   is   nargss   ks   ms   t3s   ns   t2s   js   TINYs   vs   t1s   check(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lobrientransform2  s:        .$ c         C   sX   t  |  ƒ } t |  ƒ } g  } x |  D] } | i | | ƒ q% Wt | ƒ t	 | ƒ Sd S(   sœ   
Returns the variance of the values in the passed list using
N for the denominator (i.e., DESCRIBES the sample variance only).

Usage:   lsamplevar(inlist)
N(
   s   lens   inlists   ns   means   mns
   deviationss   items   appends   sss   float(   s   inlists
   deviationss   mns   ns   item(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lsamplevarV  s      c         C   s   t  i t |  ƒ ƒ Sd S(   s¥   
Returns the standard deviation of the values in the passed list using
N for the denominator (i.e., DESCRIBES the sample stdev only).

Usage:   lsamplestdev(inlist)
N(   s   maths   sqrts	   samplevars   inlist(   s   inlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsamplestdeve  s     c         C   sv   t  |  ƒ } t |  ƒ } d g t  |  ƒ } x, t t  |  ƒ ƒ D] } |  | | | | <q> Wt | ƒ t	 | d ƒ Sd S(   s˜   
Returns the variance of the values in the passed list using N-1
for the denominator (i.e., for estimating population variance).

Usage:   lvar(inlist)
i    i   N(
   s   lens   inlists   ns   means   mns
   deviationss   ranges   is   sss   float(   s   inlists
   deviationss   is   mns   n(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lvaro  s      c         C   s   t  i t |  ƒ ƒ Sd S(   s   
Returns the standard deviation of the values in the passed list
using N-1 in the denominator (i.e., to estimate population stdev).

Usage:   lstdev(inlist)
N(   s   maths   sqrts   vars   inlist(   s   inlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lstdev~  s     c         C   s'   t  |  ƒ t t i t |  ƒ ƒ ƒ Sd S(   s¢   
Returns the standard error of the values in the passed list using N-1
in the denominator (i.e., to estimate population standard error).

Usage:   lsterr(inlist)
N(   s   stdevs   inlists   floats   maths   sqrts   len(   s   inlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsterrˆ  s     c         C   s-   t  |  ƒ } t |  ƒ } | t i | ƒ Sd S(   s‹   
Returns the estimated standard error of the mean (sx-bar) of the
values in the passed list.  sem = stdev / sqrt(n)

Usage:   lsem(inlist)
N(   s   stdevs   inlists   sds   lens   ns   maths   sqrt(   s   inlists   ns   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsem’  s     c         C   s"   | t |  ƒ t |  ƒ } | Sd S(   s²   
Returns the z-score for a given input score, given that score and the
list from which that score came.  Not appropriate for population calculations.

Usage:   lz(inlist, score)
N(   s   scores   means   inlists   samplestdevs   z(   s   inlists   scores   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lzž  s     c         C   s5   g  } x$ |  D] } | i t |  | ƒ ƒ q W| Sd S(   sZ   
Returns a list of z-scores, one for each score in the passed list.

Usage:   lzs(inlist)
N(   s   zscoress   inlists   items   appends   z(   s   inlists   zscoress   item(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lzs©  s      c         C   s5   t  | t |  ƒ ƒ } t |  ƒ | } |  | | !Sd S(   s‹  
Slices off the passed proportion of items from BOTH ends of the passed
list (i.e., with proportiontocut=0.1, slices 'leftmost' 10% AND 'rightmost'
10% of scores.  Assumes list is sorted by magnitude.  Slices off LESS if
proportion results in a non-integer slice index (i.e., conservatively
slices off proportiontocut).

Usage:   ltrimboth (l,proportiontocut)
Returns: trimmed version of list l
N(   s   ints   proportiontocuts   lens   ls   lowercuts   uppercut(   s   ls   proportiontocuts   uppercuts   lowercut(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   ltrimboth¹  s    
 s   rightc         C   sy   | d j o* d } t |  ƒ t | t |  ƒ ƒ } n4 | d j o& t | t |  ƒ ƒ } t |  ƒ } n |  | | !Sd S(   s  
Slices off the passed proportion of items from ONE end of the passed
list (i.e., if proportiontocut=0.1, slices off 'leftmost' or 'rightmost'
10% of scores).  Slices off LESS if proportion results in a non-integer
slice index (i.e., conservatively slices off proportiontocut).

Usage:   ltrim1 (l,proportiontocut,tail='right')  or set tail='left'
Returns: trimmed version of list l
s   righti    s   leftN(   s   tails   lowercuts   lens   ls   ints   proportiontocuts   uppercut(   s   ls   proportiontocuts   tails   uppercuts   lowercut(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ltrim1É  s    	 $c         C   s¯  d } x1 | d d d d d d g j o d Gt ƒ  } q	 W| d d d d g j oîd	 Gt |  | ƒ } t t i | d
 ƒ t i | d ƒ ƒ \ }	 } | d j  o d t t | d ƒ ƒ } n d } | GH| d d g j oâ | d
 d j o; t |  | d
 ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq=t |  ƒ d j p t | ƒ d j o8 t |  | ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq=t |  | ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq¢| d
 d j o; t |  | d
 ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq¢t |  | ƒ \ } } d Gt | d ƒ Gt | d ƒ GHnbd } x1 | d d d d d d g j o d Gt ƒ  } qJW| d d g j o‹ t |  | ƒ \ }
 } } } } d GHd d d d d g t |
 d ƒ t | d ƒ t | d ƒ t | d ƒ t | d ƒ g g } t i | ƒ nŠ | d d g j o= t |  | ƒ \ } } d GHd  Gt | d ƒ Gt | d ƒ GHn: t |  | ƒ \ } } d! GHd" Gt | d ƒ Gt | d ƒ GHd# GHt Sd$ S(%   s¾   
Interactively determines the type of data and then runs the
appropriated statistic for paired group data.

Usage:   lpaired(x,y)
Returns: appropriate statistic name, value, and probability
s    s   is   rs   Is   Rs   cs   Cs9   
Independent or related samples, or correlation (i,r,c): s   
Comparing variances ...i    i   f0.050000000000000003s   unequal, p=i   s   equals   es   
Independent samples t-test:  i   s(   
Rank Sums test (NONparametric, n>20):  s.   
Mann-Whitney U-test (NONparametric, ns<20):  s   
Related samples t-test:  s#   
Wilcoxon T-test (NONparametric):  s   ds   Ds9   
Is the data Continuous, Ranked, or Dichotomous (c,r,d): s/   
Linear regression for continuous variables ...s   Slopes	   Intercepts   Probs
   SEestimates%   
Correlation for ranked variables ...s   Spearman's r: s/   
Assuming x contains a dichotomous variable ...s   Point Biserial r: s   

N(    s   sampless	   raw_inputs   obrientransforms   xs   ys   rs   F_oneways   pstats   colexs   fs   ps   strs   rounds   vartypes	   ttest_inds   ts   lens   ranksumss   zs   mannwhitneyus   us	   ttest_rels   corrtypes
   linregresss   ms   bs   sees   lols   printccs	   spearmanrs   pointbiserialrs   None(   s   xs   ys   sees   vartypes   lols   rs   sampless   corrtypes   bs   fs   ms   ps   us   ts   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lpairedà  s^      -#&#### W#c         C   sE  d } t |  ƒ t | ƒ j o t d ‚ n t |  ƒ } t t |  ƒ }  t t | ƒ } t |  ƒ } t | ƒ } | t |  | ƒ t |  ƒ t | ƒ } t i | t |  ƒ t |  ƒ | t | ƒ t | ƒ ƒ } | | }	 | d } |	 t i | d |	 | d |	 | ƒ }
 t d | d | t | |
 |
 ƒ ƒ } |	 | f Sd S(   s  
Calculates a Pearson correlation coefficient and the associated
probability value.  Taken from Heiman's Basic Statistics for the Behav.
Sci (2nd), p.195.

Usage:   lpearsonr(x,y)      where x and y are equal-length lists
Returns: Pearson's r value, two-tailed p-value
f1.0000000000000001e-30s/   Input values not paired in pearsonr.  Aborting.i   f1.0f0.5N(   s   TINYs   lens   xs   ys
   ValueErrors   ns   maps   floats   means   xmeans   ymeans   summults   sums   r_nums   maths   sqrts   sss   square_of_sumss   r_dens   rs   dfs   ts   betais   prob(   s   xs   ys   r_nums   r_dens   TINYs   probs   dfs   ymeans   ns   rs   ts   xmean(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   lpearsonr   s      ';

+(c         C   sä   d } t |  ƒ t | ƒ j o t d ‚ n t |  ƒ } t |  ƒ } t | ƒ } t	 | | ƒ }	 d d |	 t | | d d ƒ } | t i | d | d d | ƒ }
 | d } t d | d | | |
 |
 ƒ } | | f Sd S(	   sð   
Calculates a Spearman rank-order correlation coefficient.  Taken
from Heiman's Basic Statistics for the Behav. Sci (1st), p.192.

Usage:   lspearmanr(x,y)      where x and y are equal-length lists
Returns: Spearman's r, two-tailed p-value
f1.0000000000000001e-30s0   Input values not paired in spearmanr.  Aborting.i   i   i   f1.0f0.5N(   s   TINYs   lens   xs   ys
   ValueErrors   ns   rankdatas   rankxs   rankys   sumdiffsquareds   dsqs   floats   rss   maths   sqrts   ts   dfs   betais   probrs(   s   xs   ys   rankxs   rss   dfs   rankys   ns   probrss   TINYs   dsqs   t(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lspearmanr:  s     $'
"c         C   sÄ  d } t |  ƒ t | ƒ j o t d ‚ n t i |  | ƒ } t i |  ƒ } t | ƒ d j o t d ‚ nTt i | t
 d ƒ ƒ }
 t i | |
 d ƒ } t i | d | d ƒ }  t i | d | d ƒ } t t i |  d ƒ ƒ } t t i | d ƒ ƒ }	 t | ƒ } t i t |  ƒ t | ƒ t | ƒ t | ƒ ƒ } |	 | t t i | d ƒ ƒ | } | d } | t i | d | | d | | ƒ } t d | d | | | | ƒ } | | f Sd	 S(
   s  
Calculates a point-biserial correlation coefficient and the associated
probability value.  Taken from Heiman's Basic Statistics for the Behav.
Sci (1st), p.194.

Usage:   lpointbiserialr(x,y)      where x,y are equal-length lists
Returns: Point-biserial r, two-tailed p-value
f1.0000000000000001e-30s5   INPUT VALUES NOT PAIRED IN pointbiserialr.  ABORTING.i   s3   Exactly 2 categories required for pointbiserialr().i    i   f1.0f0.5N(   s   TINYs   lens   xs   ys
   ValueErrors   pstats   abuts   datas   uniques
   categoriess   ranges   codemaps   recodes   recodeds   linexands   means   colexs   xmeans   ymeans   ns   maths   sqrts   floats   adjusts   samplestdevs   rpbs   dfs   ts   betais   prob(   s   xs   ys   rpbs   TINYs   dfs   recodeds   datas
   categoriess   probs   ymeans   codemaps   ns   adjusts   ts   xmean(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lpointbiserialrR  s*     3$
+"c         C   sb  d }
 d } d } xÏ t t |  ƒ d ƒ D]· } x® t | t | ƒ ƒ D]— } |  | |  | } | | | | }	 | |	 } | o= |
 d }
 | d } | d j o | d } qÜ | d } qE | o |
 d }
 qE | d } qE Wq) W| t i |
 | ƒ } d t |  ƒ d d t |  ƒ t |  ƒ d } | t i | ƒ } t t | ƒ d ƒ } | | f Sd S(   sÓ   
Calculates Kendall's tau ... correlation of ordinal data.  Adapted
from function kendl1 in Numerical Recipies.  Needs good test-routine.@@@

Usage:   lkendalltau(x,y)
Returns: Kendall's tau, two-tailed p-value
i    i   f4.0f10.0f9.0f1.4142136000000001N(   s   n1s   n2s   isss   ranges   lens   xs   js   ys   ks   a1s   a2s   aas   maths   sqrts   taus   svars   zs   erfccs   abss   prob(   s   xs   ys   aas   taus   isss   ks   js   svars   a1s   a2s   n1s   n2s   zs   prob(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lkendalltaur  s2       


0c         C   sÈ  d } t |  ƒ t | ƒ j o t d ‚ n t |  ƒ } t t |  ƒ }  t t | ƒ } t |  ƒ } t | ƒ }
 t | t |  | ƒ t |  ƒ t | ƒ ƒ } t i | t |  ƒ t |  ƒ | t | ƒ t | ƒ ƒ } | | } d t i d | | d | | ƒ } | d } | t i | d | | d | | ƒ } t d | d | | | | ƒ } | t | t |  ƒ t |  ƒ ƒ } |
 | | } t i d | | ƒ t | ƒ }	 | | | | |	 f Sd S(   s½   
Calculates a regression line on x,y pairs.  

Usage:   llinregress(x,y)      x,y are equal-length lists of x-y coordinates
Returns: slope, intercept, r, two-tailed prob, sterr-of-estimate
f9.9999999999999995e-21s1   Input values not paired in linregress.  Aborting.f0.5f1.0i   i   N(   s   TINYs   lens   xs   ys
   ValueErrors   ns   maps   floats   means   xmeans   ymeans   summults   sums   r_nums   maths   sqrts   sss   square_of_sumss   r_dens   rs   logs   zs   dfs   ts   betais   probs   slopes	   intercepts   samplestdevs   sterrest(   s   xs   ys   slopes   r_nums   r_dens   TINYs   dfs	   intercepts   probs   sterrests   ymeans   ns   rs   ts   xmeans   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   llinregress•  s(     -;
'
+"$!s   Samples   ac         C   sø   t  |  ƒ } t |  ƒ } t |  ƒ }	 |	 d } |	 d | t | ƒ } | | t i | d |	 ƒ }
 t d | d t | ƒ | |
 |
 ƒ } | d j oP d } t | | d d | d d d | |	 | | t |  ƒ t |  ƒ | |
 | ƒ n |
 | f Sd S(	   s’  
Calculates the t-obtained for the independent samples T-test on ONE group
of scores a, given a population mean.  If printit=1, results are printed
to the screen.  If printit='filename', the results are output to 'filename'
using the given writemode (default=append).  Returns t-value, and prob.

Usage:   lttest_1samp(a,popmean,Name='Sample',printit=0,writemode='a')
Returns: t-value, two-tailed prob
i   f1.0f0.5i    s   Single-sample T-test.s
   Populations   --N(   s   means   as   xs   vars   vs   lens   ns   dfs   floats   svars   popmeans   maths   sqrts   ts   betais   probs   printits   statnames   outputpairedstatss	   writemodes   names   mins   max(   s   as   popmeans   printits   names	   writemodes   statnames   svars   probs   dfs   ns   ts   vs   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lttest_1sampµ  s    	 
(	s   Samp1s   Samp2c         C   sB  t  |  ƒ } t  | ƒ } t |  ƒ d }
 t | ƒ d } t |  ƒ } t | ƒ } | | d }	 | d |
 | d | t |	 ƒ } | | t i | d | d | ƒ } t d |	 d |	 |	 | | ƒ } | d j o\ d } t | | | | | |
 t |  ƒ t |  ƒ | | | | t | ƒ t | ƒ | | | ƒ n | | f Sd S(   s™  
Calculates the t-obtained T-test on TWO INDEPENDENT samples of
scores a, and b.  From Numerical Recipies, p.483.  If printit=1, results
are printed to the screen.  If printit='filename', the results are output
to 'filename' using the given writemode (default=append).  Returns t-value,
and prob.

Usage:   lttest_ind(a,b,printit=0,name1='Samp1',name2='Samp2',writemode='a')
Returns: t-value, two-tailed prob
i   i   f1.0f0.5i    s   Independent samples T-test.N(   s   means   as   x1s   bs   x2s   stdevs   v1s   v2s   lens   n1s   n2s   dfs   floats   svars   maths   sqrts   ts   betais   probs   printits   statnames   outputpairedstatss	   writemodes   name1s   mins   maxs   name2(   s   as   bs   printits   name1s   name2s	   writemodes   statnames   svars   probs   dfs   v1s   v2s   x2s   x1s   ts   n1s   n2(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lttest_indÐ  s$    
 $'"	s   Sample1s   Sample2c         C   s‰  t  |  ƒ t  | ƒ j o t d ‚ n t |  ƒ } t | ƒ }
 t |  ƒ } t | ƒ }	 t  |  ƒ } d } x8 t t  |  ƒ ƒ D]$ } | |  | | | | |
 } q{ W| d } | t | ƒ } t i | |	 d | t | ƒ ƒ } | |
 | } t d | d | | | | ƒ } | d j o\ d } t | | | | | | t |  ƒ t |  ƒ | | |
 |	 t | ƒ t | ƒ | | | ƒ n | | f Sd S(   s™  
Calculates the t-obtained T-test on TWO RELATED samples of scores,
a and b.  From Numerical Recipies, p.483.  If printit=1, results are
printed to the screen.  If printit='filename', the results are output to
'filename' using the given writemode (default=append).  Returns t-value,
and prob.

Usage:   lttest_rel(a,b,printit=0,name1='Sample1',name2='Sample2',writemode='a')
Returns: t-value, two-tailed prob
s"   Unequal length lists in ttest_rel.i    i   f2.0f0.5s   Related samples T-test.N(   s   lens   as   bs
   ValueErrors   means   x1s   x2s   vars   v1s   v2s   ns   covs   ranges   is   dfs   floats   maths   sqrts   sds   ts   betais   probs   printits   statnames   outputpairedstatss	   writemodes   name1s   mins   maxs   name2(   s   as   bs   printits   name1s   name2s	   writemodes   statnames   dfs   v1s   v2s   x2s   x1s   probs   covs   is   ns   ts   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lttest_relï  s0    
  "
%"	c         C   s¦   t  |  ƒ } | t j o' t |  ƒ t | ƒ g t  |  ƒ } n d } xB t t  |  ƒ ƒ D]. } | |  | | | d t | | ƒ } qY W| t
 | | d ƒ f Sd S(   sD  
Calculates a one-way chi square for list of observed frequencies and returns
the result.  If no expected frequencies are given, the total N is assumed to
be equally distributed across all groups.

Usage:   lchisquare(f_obs, f_exp=None)   f_obs = list of observed cell freq.
Returns: chisquare-statistic, associated p-value
i    i   i   N(   s   lens   f_obss   ks   f_exps   Nones   sums   floats   chisqs   ranges   is	   chisqprob(   s   f_obss   f_exps   is   chisqs   k(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lchisquare  s     ' ,c         C   sz  d } d } d } d } t |  ƒ } t | ƒ } | }
 | } d }	 |  i ƒ  | i ƒ  x¼ | | j  o
 | | j  o¡ |  | } | | } | | j o | t |
 ƒ } | d } n | | j o | t | ƒ } | d } n | | } t i | ƒ t i |	 ƒ j o
 | }	 qY qY WyG t i |
 | t |
 | ƒ ƒ } t | d d | t |	 ƒ ƒ } Wn d } n X|	 | f Sd S(   sÚ   
Computes the Kolmogorov-Smirnof statistic on 2 samples.  From
Numerical Recipies in C, page 493.

Usage:   lks_2samp(data1,data2)   data1&2 are lists of values for 2 conditions
Returns: KS D-value, associated p-value
i    f0.0i   f0.12f0.11f1.0N(   s   j1s   j2s   fn1s   fn2s   lens   data1s   n1s   data2s   n2s   en1s   en2s   ds   sorts   d1s   d2s   floats   dts   maths   fabss   sqrts   ens   ksprobs   abss   prob(   s   data1s   data2s   probs   ens   fn2s   fn1s   d2s   j1s   j2s   ds   en1s   en2s   n1s   n2s   d1s   dt(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   lks_2samp%  s>     

 


!&
c         C   s  t  |  ƒ }
 t  | ƒ } t |  | ƒ } | d |
 !}	 | |
 } |
 | |
 |
 d d t	 |	 ƒ } |
 | | } t | | ƒ } t | | ƒ } t i t | ƒ ƒ } | d j o t d ‚ n t i | |
 | |
 | d d ƒ } t | |
 | d | ƒ } | d t | ƒ f Sd S(   sº  
Calculates a Mann-Whitney U statistic on the provided scores and
returns the result.  Use only when the n in each condition is < 20 and
you have 2 independent samples of ranks.  NOTE: Mann-Whitney U is
significant if the u-obtained is LESS THAN or equal to the critical
value of U found in the tables.  Equivalent to Kruskal-Wallis H with
just 2 groups.

Usage:   lmannwhitneyu(data)
Returns: u-statistic, one-tailed p-value (i.e., p(z(U)))
i    i   f2.0s*   All numbers are identical in lmannwhitneyuf12.0f1.0N(   s   lens   xs   n1s   ys   n2s   rankdatas   rankeds   rankxs   rankys   sums   u1s   u2s   maxs   bigus   mins   smallus   maths   sqrts
   tiecorrects   Ts
   ValueErrors   sds   abss   zs   zprob(   s   xs   ys   bigus   u1s   u2s   smallus   rankys   rankeds   Ts   rankxs   n1s   n2s   zs   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lmannwhitneyuL  s      
$'c         C   sì   t  |  ƒ \ } } t | ƒ } d } d } x› | | d j  o‰ | | | | d j ob d } xC | | d j  o | | | | d j o | d } | d } q` W| | d | } n | d } q- W| t	 | d | ƒ } d | Sd S(   s  
Corrects for ties in Mann Whitney U and Kruskal Wallis H tests.  See
Siegel, S. (1956) Nonparametric Statistics for the Behavioral Sciences.
New York: McGraw-Hill.  Code adapted from |Stat rankind.c code.

Usage:   ltiecorrect(rankvals)
Returns: T correction factor for U or H
f0.0i    i   i   f1.0N(
   s	   shellsorts   rankvalss   sorteds   posns   lens   ns   Ts   is   ntiess   float(   s   rankvalss   posns   ntiess   Ts   is   ns   sorted(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ltiecorrecti  s"       *
c   
      C   s·   t  |  ƒ } t  | ƒ } |  | }	 t |	 ƒ } | |  }  | | } t |  ƒ } | | | d d } | | t i | | | | d d ƒ } d d t t | ƒ ƒ } | | f Sd S(   só   
Calculates the rank sums statistic on the provided scores and
returns the result.  Use only when the n in each condition is > 20 and you
have 2 independent samples of ranks.

Usage:   lranksums(x,y)
Returns: a z-statistic, two-tailed p-value
i   f2.0f12.0i   f1.0N(   s   lens   xs   n1s   ys   n2s   alldatas   rankdatas   rankeds   sums   ss   expecteds   maths   sqrts   zs   zprobs   abss   prob(
   s   xs   ys   expecteds   ss   rankeds   n1s   n2s   zs   probs   alldata(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   lranksums‚  s     


+c         C   s  t  |  ƒ t  | ƒ j o t d ‚ n g  } xJ t t  |  ƒ ƒ D]6 } |  | | | } | d j o | i | ƒ q? q? Wt  | ƒ } t
 t | ƒ } t | ƒ }	 d } d } xK t t  | ƒ ƒ D]7 } | | d j  o | |	 | } q¿ | |	 | } q¿ Wt | | ƒ } | | d d } t i | | d d | d d ƒ } t i | | ƒ | } d	 d t t | ƒ ƒ }
 | |
 f Sd
 S(   s¶   
Calculates the Wilcoxon T-test for related samples and returns the
result.  A non-parametric T-test.

Usage:   lwilcoxont(x,y)
Returns: a t-statistic, two-tail probability estimate
s"   Unequal N in wilcoxont.  Aborting.i    f0.0i   f0.25f2.0f1.0f24.0i   N(   s   lens   xs   ys
   ValueErrors   ds   ranges   is   diffs   appends   counts   maps   abss   absds   rankdatas	   absrankeds   r_pluss   r_minuss   mins   wts   mns   maths   sqrts   ses   fabss   zs   zprobs   prob(   s   xs   ys   diffs   wts   counts   r_pluss   ds   is   mns	   absrankeds   probs   absds   zs   r_minuss   se(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lwilcoxont˜  s2       'c          G   sœ  t  |  ƒ }  d g t |  ƒ } g  } t t |  ƒ } x( t t |  ƒ ƒ D] } | |  | } qG Wt | ƒ } t
 | ƒ } x= t t |  ƒ ƒ D]) } | d | | !|  | <| d | | 5qŠ Wg  }
 xQ t t |  ƒ ƒ D]= } |
 i t |  | ƒ d ƒ |
 | t | | ƒ |
 | <qÐ Wt |
 ƒ } t | ƒ }	 d |	 |	 d | d |	 d } t |  ƒ d } | d j o t d ‚ n | t | ƒ } | t | | ƒ f Sd S(   sG  
The Kruskal-Wallis H-test is a non-parametric ANOVA for 3 or more
groups, requiring at least 5 subjects in each group.  This function
calculates the Kruskal-Wallis H-test for 3 or more independent samples
and returns the result.  

Usage:   lkruskalwallish(*args)
Returns: H-statistic (corrected for ties), associated p-value
i    i   f12.0i   i   s,   All numbers are identical in lkruskalwallishN(   s   lists   argss   lens   ns   alls   maps   ranges   is   rankdatas   rankeds
   tiecorrects   Ts   rsumss   appends   sums   floats   ssbns   totalns   hs   dfs
   ValueErrors	   chisqprob(   s   argss   alls   ssbns   is   hs   ns   dfs   rankeds   Ts   totalns   rsums(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lkruskalwallish¹  s6    	     "c          G   sù   t  |  ƒ } | d j  o t d ‚ n t  |  d ƒ } t t i t |  ƒ ƒ } x. t
 t  | ƒ ƒ D] } t | | ƒ | | <qa Wd } x, t
 | ƒ D] } | t |  | ƒ d } q’ Wd | | | d | d | | d } | t | | d ƒ f Sd S(   sÉ  
Friedman Chi-Square is a non-parametric, one-way within-subjects
ANOVA.  This function calculates the Friedman Chi-square test for repeated
measures and returns the result, along with the associated probability
value.  It assumes 3 or more repeated measures.  Only 3 levels requires a
minimum of 10 subjects in the study.  Four levels requires 5 subjects per
level(??).

Usage:   lfriedmanchisquare(*args)
Returns: chi-square statistic, associated p-value
i   s3   Less than 3 levels.  Friedman test not appropriate.i    i   f12.0i   N(   s   lens   argss   ks
   ValueErrors   ns   applys   pstats   abuts   tuples   datas   ranges   is   rankdatas   ssbns   sums   chisqs	   chisqprob(   s   argss   is   ks   chisqs   ns   datas   ssbn(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lfriedmanchisquareÜ  s       *c         C   s  d } d „  } |  d j p
 | d j  o d Sn d |  } | d d j o
 d } n d } | d j o | | ƒ }	 n | o
 |	 } n d t t	 i
 |  ƒ ƒ } | d j oJd | d }  | o
 d }
 n d }
 | | j o‘ | o
 d	 } n t	 i t	 i
 t	 i ƒ ƒ } t	 i | ƒ } xK |
 |  j o= t	 i |
 ƒ | } | | | |
 | | ƒ } |
 d }
 q"W| Sq | o
 d } n$ d t	 i
 t	 i ƒ t	 i
 | ƒ } d	 } x: |
 |  j o, | | t |
 ƒ } | | } |
 d }
 q²W| |	 | Sn | Sd
 S(   s®   
Returns the (1-tailed) probability value associated with the provided
chi-square value and df.  Adapted from chisq.c in Gary Perlman's |Stat.

Usage:   lchisqprob(chisq,df)
f20.0c         C   s-   d } |  | j  o d Sn t i |  ƒ Sd  S(   Nf20.0f0.0(   s   BIGs   xs   maths   exp(   s   xs   BIG(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ex  s    i    i   f1.0f0.5i   f2.0f0.0N(   s   BIGs   exs   chisqs   dfs   as   evens   ys   ss   zprobs   maths   sqrts   zs   es   logs   pis   cs   float(   s   chisqs   dfs   evens   as   es   BIGs   cs   ss   exs   ys   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lchisqprobú  sR     	




 
# 
c         C   s¥   t  |  ƒ } d d d | } | t i | | d | d | d | d | d | d | d	 | d
 | d | d ƒ } |  d j o | Sn	 d | Sd S(   s›   
Returns the complementary error function erfc(x) with fractional
error everywhere less than 1.2e-7.  Adapted from Numerical Recipies.

Usage:   lerfcc(x)
f1.0f0.5f1.2655122299999999f
1.00002368f
0.37409196f0.096784179999999997f0.18628806000000001f0.27886807000000002f-1.13520398f1.4885158700000001f0.82215223000000004f0.17087277000000001i    f2.0N(   s   abss   xs   zs   ts   maths   exps   ans(   s   xs   ts   anss   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lerfcc6  s     fc         C   sj  d } |  d j o
 d } nd t i |  ƒ } | | d j o
 d } në | d j  o\ | | } d | d | d | d | d	 | d
 | d | d | d | d } n‚ | d } d | d | d | d | d | d | d | d | d | d | d | d | d | d | d } |  d j o | d d } n d | d } | Sd S(   s(  
Returns the area under the normal curve 'to the left of' the given z value.
Thus, 
    for z<0, zprob(z) = 1-tail probability
    for z>0, 1.0-zprob(z) = 1-tail probability
    for any z, 2.0*(1.0-zprob(abs(z))) = 2-tail probability
Adapted from z.c in Gary Perlman's |Stat.

Usage:   lzprob(z)
f6.0f0.0f0.5f1.0f0.000124818987f0.001075204047f0.0051987750189999996f0.019198292004f0.059054035642f0.15196875136400001f0.31915293269400002f0.53192300729999997f0.79788456059299995f2.0f4.5255659000000002e-05f0.00015252929f1.9538131999999999e-05f0.00067690498600000005f0.0013906042840000001f0.00079462081999999998f0.0020342548740000001f0.0065497912140000001f0.010557625006f0.011630447319f0.0092794533410000008f0.0053535791080000002f0.0021412687410000001f0.00053531084899999996f0.99993665752399996N(   s   Z_MAXs   zs   xs   maths   fabss   ys   ws   prob(   s   zs   Z_MAXs   ws   ys   xs   prob(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lzprobF  s     
 


R
wc         C   s¹   d } d } d } d |  |  } xŽ t d d ƒ D]} } | t i | | | ƒ } | | } t i
 | ƒ d | j p t i
 | ƒ d | j  o | Sn | } t i
 | ƒ } q0 Wd Sd	 S(
   su   
Computes a Kolmolgorov-Smirnov t-test significance level.  Adapted from
Numerical Recipies.

Usage:   lksprob(alam)
f2.0f0.0f-2.0i   iÉ   f0.001f1e-08f1.0N(   s   facs   sums   termbfs   alams   a2s   ranges   js   maths   exps   terms   fabs(   s   alams   terms   sums   js   termbfs   a2s   fac(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lksprobp  s      
4c         C   s4   t  d | d |  | t | |  | ƒ ƒ } | Sd S(   sÿ   
Returns the (1-tailed) significance level (p-value) of an F
statistic given the degrees of freedom for the numerator (dfR-dfF) and
the degrees of freedom for the denominator (dfF).

Usage:   lfprob(dfnum, dfden, F)   where usually dfnum=dfbn, dfden=dfwn
f0.5N(   s   betais   dfdens   dfnums   floats   Fs   p(   s   dfnums   dfdens   Fs   p(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lfprob…  s     ,c         C   s\  d } d } d } }	 } |  | } |  d }
 |  d } d | | |
 } xt | d ƒ D]ô } t | d ƒ } | | } | | | | | | |  | } |	 | | } | | | } |  | | | | |
 | |  | } | | |	 } | | | } |	 } | | } | | } | | }	 d } t |	 | ƒ | t |	 ƒ j  o |	 Sq[ q[ Wd GHd S(   s    
This function evaluates the continued fraction form of the incomplete
Beta function, betai.  (Adapted from: Numerical Recipies in C.)

Usage:   lbetacf(a,b,x)
iÈ   f2.9999999999999999e-07f1.0i   s-   a or b too big, or ITMAX too small in Betacf.N(   s   ITMAXs   EPSs   bms   azs   ams   as   bs   qabs   qaps   qams   xs   bzs   ranges   is   floats   ems   tems   ds   aps   bps   apps   bpps   aolds   abs(   s   as   bs   xs   ems   tems   apps   ams   EPSs   aps   azs   qaps   qabs   qams   ITMAXs   bms   aolds   bps   bzs   ds   is   bpp(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lbetacf‘  s4     


 
"'


!c         C   s¡   d d d d d d g } |  d } | d } | | d	 t i | ƒ } d } x6 t t | ƒ ƒ D]" } | d
 } | | | | } qa W| t i d | ƒ Sd S(   sœ   
Returns the gamma function of xx.
    Gamma(z) = Integral(0,infinity) of t^(z-1)exp(-t) dt.
(Adapted from: Numerical Recipies in C.)

Usage:   lgammln(xx)
f76.180091730000001f-86.505320330000004f24.014098220000001f-1.231739516f0.00120858003f5.3638199999999999e-06f1.0f5.5f0.5i   f2.5066282746500002N(
   s   coeffs   xxs   xs   tmps   maths   logs   sers   ranges   lens   j(   s   xxs   tmps   sers   coeffs   js   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lgammln³  s     

 
c         C   s  | d j  p
 | d j o t d ‚ n | d j p
 | d j o
 d } nT t i t |  | ƒ t |  ƒ t | ƒ |  t i | ƒ | t i d | ƒ ƒ } | |  d |  | d j  o" | t	 |  | | ƒ t
 |  ƒ Sn' d | t	 | |  d | ƒ t
 | ƒ Sd S(   sW  
Returns the incomplete beta function:

    I-sub-x(a,b) = 1/B(a,b)*(Integral(0,x) of t^(a-1)(1-t)^(b-1) dt)

where a,b>0 and B(a,b) = G(a)*G(b)/(G(a+b)) where G(a) is the gamma
function of a.  The continued fraction formulation is implemented here,
using the betacf function.  (Adapted from: Numerical Recipies in C.)

Usage:   lbetai(a,b,x)
f0.0f1.0s   Bad x in lbetaif2.0N(   s   xs
   ValueErrors   bts   maths   exps   gammlns   as   bs   logs   betacfs   float(   s   as   bs   xs   bt(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lbetaiÈ  s     
S"c          G   s   t  |  ƒ } d g | } d g | } d g | } g  } t t i	 |  ƒ } t t | ƒ } t t | ƒ } t t  |  ƒ } x( t t  |  ƒ ƒ D] } | |  | } q‹ Wt i	 | ƒ } t  | ƒ } t | ƒ t | ƒ t | ƒ } d } x7 |  D]/ } | t t i	 | ƒ ƒ t t  | ƒ ƒ } që W| t | ƒ t | ƒ } | | } | d }
 | | }	 | t |
 ƒ } | t |	 ƒ } | | } t |
 |	 | ƒ } | | f Sd S(   s  
Performs a 1-way ANOVA, returning an F-value and probability given
any number of groups.  From Heiman, pp.394-7.

Usage:   F_oneway(*lists)    where *lists is any number of lists, one per
                                  treatment group
Returns: F value, one-tailed p-value
i    i   N(   s   lens   listss   as   meanss   varss   nss   alldatas   maps   Ns   arrays   tmps   ameans   avars   ranges   is   bigns   asss   asquare_of_sumss   floats   sstots   ssbns   lists   sswns   dfbns   dfwns   msbs   msws   fs   fprobs   prob(   s   listss   varss   tmps   sstots   msws   bigns   nss   msbs   meanss   dfwns   dfbns   as   fs   is   lists   probs   sswns   alldatas   ssbn(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   lF_onewayå  s:        -



c         C   s$   |  | t | ƒ | t | ƒ Sd S(   sr  
Returns an F-statistic given the following:
        ER  = error associated with the null hypothesis (the Restricted model)
        EF  = error associated with the alternate hypothesis (the Full model)
        dfR-dfF = degrees of freedom of the numerator
        dfF = degrees of freedom associated with the denominator/Full model

Usage:   lF_value(ER,EF,dfnum,dfden)
N(   s   ERs   EFs   floats   dfnums   dfden(   s   ERs   EFs   dfnums   dfden(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lF_value
  s    	 s   wi   c         C   sD  t  |  d ƒ t t g j o |  g }  n t | | ƒ } g  } t	 i
 |  ƒ } xa t t |  ƒ ƒ D]M }	 |  |	 d g j p |  |	 d j p |  |	 d j o | |	 g } qa qa W| i ƒ  x | D] } | | =qÃ Wd g t | d ƒ } x_ t t | d ƒ ƒ D]G } t i | | ƒ }
 t t i |
 ƒ }
 t t t |
 ƒ ƒ | | | <qWxâ |  D]Ú } | d g j p
 | d j o | i d ƒ nš | d g j p
 | d j oc d g t | ƒ } x0 t t | ƒ ƒ D] } d | | d | | <qËW| i t i | | ƒ ƒ n | i t i | | ƒ ƒ | i d ƒ qTW| i ƒ  t Sd S(   s  
Writes a list of lists to a file in columns, customized by the max
size of items within the columns (max size of items in col, +2 characters)
to specified file.  File-overwrite is the default.

Usage:   writecc (listoflists,file,writetype='w',extra=2)
Returns: None
i    s   
s   dashess   -i   N(    s   types   listoflistss   ListTypes	   TupleTypes   opens   files	   writetypes   outfiles
   rowstokills   copys   deepcopys
   list2prints   ranges   lens   is   reverses   rows   maxsizes   cols   pstats   colexs   itemss   maps   makestrs   maxs   extras   writes   dashess   js   lineincustcolss   closes   None(   s   listoflistss   files	   writetypes   extras   rows   maxsizes   dashess   outfiles
   rowstokills   is   itemss   js
   list2prints   col(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   writecc  sD      6
  !  
c         C   sÁ   |  d d |  d <x¤ t t |  ƒ ƒ D] } |  | | | j o | t |  ƒ d j  o( d |  | <|  | d d |  | d <q% |  | | | j o | t |  ƒ d j o
 d }  q% q% W|  Sd S(   sÅ   
Simulate a counting system from an n-dimensional list.

Usage:   lincr(l,cap)   l=list to increment, cap=max values for each list pos'n
Returns: next set of values for list l, OR -1 (if overflow)
i    i   iÿÿÿÿN(   s   ls   ranges   lens   is   cap(   s   ls   caps   i(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lincrC  s      ,
,c         C   s)   d } x |  D] } | | } q W| Sd S(   sI   
Returns the sum of the items in the passed list.

Usage:   lsum(inlist)
i    N(   s   ss   inlists   item(   s   inlists   items   s(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsumT  s      c         C   sQ   t  i |  ƒ } x7 t d t | ƒ ƒ D]  } | | | | d | | <q% W| Sd S(   sl   
Returns a list consisting of the cumulative sum of the items in the
passed list.

Usage:   lcumsum(inlist)
i   N(   s   copys   deepcopys   inlists   newlists   ranges   lens   i(   s   inlists   is   newlist(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lcumsum`  s      c         C   s-   d } x |  D] } | | | } q W| Sd S(   sl   
Squares each value in the passed list, adds up these squares and
returns the result.

Usage:   lss(inlist)
i    N(   s   sss   inlists   item(   s   inlists   sss   item(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lssm  s      c         C   se   t  |  ƒ t  | ƒ j o t d ‚ n d } x. t i |  | ƒ D] \ } } | | | } q? W| Sd S(   sµ   
Multiplies elements in list1 and list2, element by element, and
returns the sum of all resulting multiplications.  Must provide equal
length lists.

Usage:   lsummult(list1,list2)
s"   Lists not equal length in summult.i    N(	   s   lens   list1s   list2s
   ValueErrors   ss   pstats   abuts   item1s   item2(   s   list1s   list2s   item2s   item1s   s(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsummultz  s      c         C   sE   d } x4 t t |  ƒ ƒ D]  } | |  | | | d } q W| Sd S(   s¹   
Takes pairwise differences of the values in lists x and y, squares
these differences, and returns the sum of these squares.

Usage:   lsumdiffsquared(x,y)
Returns: sum[(x[i]-y[i])**2]
i    i   N(   s   sdss   ranges   lens   xs   is   y(   s   xs   ys   sdss   i(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsumdiffsquaredŠ  s      c         C   s   t  |  ƒ } t | ƒ | Sd S(   s‹   
Adds the values in the passed list, squares the sum, and returns
the result.

Usage:   lsquare_of_sums(inlist)
Returns: sum(inlist[i])**2
N(   s   sums   inlists   ss   float(   s   inlists   s(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lsquare_of_sums˜  s     c   	      C   s  t  |  ƒ } t i |  ƒ } t | ƒ } | d } xÚ | d j oÌ x» t | | ƒ D]ª } x¡ t | | d | ƒ D]ˆ } x | d j o | | | | | j oX | | } | | | | | <| | | | <| | } | | | | | <| | | | <qx Wqo WqQ W| d } q4 W| | f Sd S(   s†   
Shellsort algorithm.  Sorts a 1D-list.

Usage:   lshellsort(inlist)
Returns: sorted-inlist, sorting-index-vector (for original list)
i   i    iÿÿÿÿN(   s   lens   inlists   ns   copys   deepcopys   svecs   ranges   ivecs   gaps   is   js   temps   itemp(	   s   inlists   ivecs   temps   is   itemps   js   ns   gaps   svec(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   lshellsort¤  s*     
    &

c   
      C   sì   t  |  ƒ } t |  ƒ \ }	 } d } d } d g | } xª t	 | ƒ D]œ } | | } | d } | | d j p |	 | |	 | d j oX | t | ƒ d } x1 t	 | | d | d ƒ D] } | | | | <q¸ Wd } d } qD qD W| Sd S(   sÜ   
Ranks the data in inlist, dealing with ties appropritely.  Assumes
a 1D inlist.  Adapted from Gary Perlman's |Stat ranksort.

Usage:   lrankdata(inlist)
Returns: a list of length equal to inlist, containing rank scores
i    i   N(   s   lens   inlists   ns	   shellsorts   svecs   ivecs   sumrankss   dupcounts   newlists   ranges   is   floats   averanks   j(
   s   inlists   dupcounts   ivecs   js   averanks   is   newlists   ns   sumrankss   svec(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   lrankdata¾  s$      

* c         C   s…  d } y | i } | d } Wn n X| d j  o
 d } n/ | d j  o
 d } n | d j  o
 d } n d	 d
 d d d d g g } | | | t | d ƒ t t	 i
 | ƒ d ƒ | | g | |	 t |
 d ƒ t t	 i
 | ƒ d ƒ | | g g } t |  ƒ t j p t |  ƒ d j o‹ H| GHHt i | ƒ Hy@ | i f  j o | d } n | i f  j o | d } n Wn n Xd Gt | d ƒ Gd Gt | d ƒ G| GHHnÜ t |  | ƒ } | i  d | d ƒ | i! ƒ  t" | |  d ƒ t |  d ƒ } y@ | i f  j o | d } n | i f  j o | d } n Wn n X| i  t i# d t | d ƒ d t | d ƒ | d g ƒ ƒ | i! ƒ  t$ Sd S(   s—  
Prints or write to a file stats for two groups, using the name, n,
mean, sterr, min and max for each group, as well as the statistic name,
its value, and the associated p-value.

Usage:   outputpairedstats(fname,writemode,
                           name1,n1,mean1,stderr1,min1,max1,
                           name2,n2,mean2,stderr2,min2,max2,
                           statname,stat,prob)
Returns: None
s    i    f0.001s     ***f0.01s     **f0.050000000000000003s     *s   Names   Ns   Means   SDs   Mins   Maxi   s   Test statistic = s      p = s   
s   

s   as   
Test statistic = i   N(%   s   suffixs   probs   shapes   xs   titles   name1s   n1s   rounds   m1s   maths   sqrts   se1s   min1s   max1s   name2s   n2s   m2s   se2s   min2s   max2s   lofls   types   fnames
   StringTypes   lens   statnames   pstats   printccs   stats   opens	   writemodes   files   writes   closes   writeccs   list2strings   None(   s   fnames	   writemodes   name1s   n1s   m1s   se1s   min1s   max1s   name2s   n2s   m2s   se2s   min2s   max2s   statnames   stats   probs   suffixs   files   titles   lofls   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   outputpairedstats×  sZ     	 
 
 
j&'
:
c         C   sÑ   t  |  d ƒ d } d } x¬ t d | ƒ D]› } t i t i |  | ƒ ƒ d } t i
 |  | | ƒ } t i t i | d ƒ ƒ } t i t i |  d ƒ ƒ } t  | ƒ t  | ƒ j o | d | >} q* q* W| Sd S(   s   
Returns an integer representing a binary vector, where 1=within-
subject factor, 0=between.  Input equals the entire data 2D list (i.e.,
column 0=random factor, column -1=measured values (those two are skipped).
Note: input data is in |Stat format ... a list of lists ("2D list") with 
one row per measured value, first column=subject identifier, last column=
score, one in-between column per factor (these columns contain level
designations on each factor).  See also stats.anova.__doc__.

Usage:   lfindwithin(data)     data in |Stat format
i    i   N(   s   lens   datas   numfacts	   withinvecs   ranges   cols   pstats   uniques   colexs   examplelevels   linexands   rowss	   factsubjss   allsubjs(   s   datas   rowss	   factsubjss	   withinvecs   allsubjss   numfacts   examplelevels   col(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   lfindwithin  s      c   	      C   sÛ  t  i |  t  i ƒ }  | t j oG t  i |  ƒ }  t |  ƒ } t  i	 |  d | ƒ } t  i i | ƒ } nkt | ƒ t t g j ox |  i | } t  i	 |  d | ƒ } t  i i | | ƒ } | d j o/ t |  i ƒ } d | | <t  i | | ƒ } qÓnÚ t | ƒ } | i ƒ  | i ƒ  t  i t  i i t  i |  i | ƒ ƒ t  i ƒ } t  i	 |  d | ƒ } x# | D] } t  i i | | ƒ } qgW| d j o@ t |  i ƒ } x | D] } d | | <q©Wt  i | | ƒ } n | Sd S(   ss  
Calculates the geometric mean of the values in the passed array.
That is:  n-th root of (x1 * x2 * ... * xn).  Defaults to ALL values in
the passed array.  Use dimension=None to flatten array first.  REMEMBER: if
dimension=0, it collapses over dimension 0 ('rows' in a 2D array) only, and
if dimension is a sequence, it collapses over all specified dimensions.  If
keepdims is set to 1, the resulting array will have as many dimensions as
inarray, with only 1 'level' per dim that was collapsed over.

Usage:   ageometricmean(inarray,dimension=None,keepdims=0)
Returns: geometric mean computed over dim(s) listed in dimension
f1.0i   N(   s   Ns   arrays   inarrays   Floats	   dimensions   Nones   ravels   lens   sizes   powers   mults   multiplys   reduces   types   IntTypes	   FloatTypes   shapes   keepdimss   lists   shps   reshapes   sums   dimss   sorts   reverses   takes   dim(	   s   inarrays	   dimensions   keepdimss   shps   dims   sums   dimss   mults   size(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ageometricmean¤  s<     


0  c         C   sÀ  |  i t i ƒ }  | t j o5 t i |  ƒ }  t |  ƒ } t i	 i
 d |  ƒ } nat | ƒ t t g j ol t |  i | ƒ } t i	 i
 d |  | ƒ } | d j o/ t |  i ƒ } d | | <t i | | ƒ } q´nÜt | ƒ } | i ƒ  g  } x; t t |  i ƒ ƒ D]$ } | | j o | i | ƒ qqWt i |  | | ƒ } d g t | ƒ } | g  j oa t t i |  ƒ ƒ } t d |  ƒ } | d j o+ t i | g t i t |  i ƒ ƒ ƒ } q´në d | d <t i  | i d t | ƒ !ƒ d }
 t i" |
 d t i ƒ } x3 t# | |
 ƒ d j o t d | | ƒ | | <qWt i$ i
 t i% |  i | ƒ ƒ } | d j o@ t |  i ƒ } x | D] }	 d | |	 <qŠWt i | | ƒ } n | | Sd S(   sf  
Calculates the harmonic mean of the values in the passed array.
That is:  n / (1/x1 + 1/x2 + ... + 1/xn).  Defaults to ALL values in
the passed array.  Use dimension=None to flatten array first.  REMEMBER: if
dimension=0, it collapses over dimension 0 ('rows' in a 2D array) only, and
if dimension is a sequence, it collapses over all specified dimensions.  If
keepdims is set to 1, the resulting array will have as many dimensions as
inarray, with only 1 'level' per dim that was collapsed over.

Usage:   aharmonicmean(inarray,dimension=None,keepdims=0)
Returns: harmonic mean computed over dim(s) in dimension
f1.0i   i    iÿÿÿÿN('   s   inarrays   astypes   Ns   Floats	   dimensions   Nones   ravels   lens   sizes   adds   reduces   ss   types   IntTypes	   FloatTypes   floats   shapes   keepdimss   lists   shps   reshapes   dimss   sorts   nondimss   ranges   is   appends	   transposes   tinarrays   idxs   asums   oness   arrays   loopcaps   zeross   incrs   multiplys   takes   dim(   s   inarrays	   dimensions   keepdimss   nondimss   ss   sizes   tinarrays   dimss   shps   dims   loopcaps   idxs   i(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   aharmonicmeanÏ  sR     

 /
# ! c         C   sÊ  |  i ƒ  d d d g j o |  i t i ƒ }  n | t j o7 t i |  ƒ }  t i i	 |  ƒ } t t |  ƒ ƒ } nIt | ƒ t t g j ob t |  | ƒ } t |  i | ƒ } | d j o/ t |  i ƒ } d | | <t i | | ƒ } q¾nÎ t | ƒ } | i ƒ  | i ƒ  |  d } x# | D] } t i i	 | | ƒ } q"Wt i t i i	 t i |  i | ƒ ƒ t i ƒ } | d j o@ t |  i ƒ } x | D] } d | | <q”Wt i | | ƒ } n | | Sd S(   sa  
Calculates the arithmatic mean of the values in the passed array.
That is:  1/n * (x1 + x2 + ... + xn).  Defaults to ALL values in the
passed array.  Use dimension=None to flatten array first.  REMEMBER: if
dimension=0, it collapses over dimension 0 ('rows' in a 2D array) only, and
if dimension is a sequence, it collapses over all specified dimensions.  If
keepdims is set to 1, the resulting array will have as many dimensions as
inarray, with only 1 'level' per dim that was collapsed over.

Usage:   amean(inarray,dimension=None,keepdims=0)
Returns: arithematic mean calculated over dim(s) in dimension
s   ls   ss   bi   f1.0N(   s   inarrays   typecodes   astypes   Ns   Floats	   dimensions   Nones   ravels   adds   reduces   sums   floats   lens   denoms   types   IntTypes	   FloatTypes   asums   shapes   keepdimss   lists   shps   reshapes   dimss   sorts   reverses   dims   arrays   multiplys   take(   s   inarrays	   dimensions   keepdimss   shps   dims   sums   dimss   denom(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   amean  s:     



 0 c         C   sÏ   t  i |  ƒ }  t |  | ƒ \ } }	 } } t  i	 | ƒ } t  i | t |  ƒ d ƒ } t | ƒ } | i d ƒ } |	 | | }
 t  i i | d | !ƒ } | | } |
 t |  ƒ d | t | ƒ | } | Sd S(   sÂ  
Calculates the COMPUTED median value of an array of numbers, given the
number of bins to use for the histogram (more bins approaches finding the
precise median value of the array; default number of bins = 1000).  From
G.W. Heiman's Basic Stats, or CRC Probability & Statistics.
NOTE:  THIS ROUTINE ALWAYS uses the entire passed array (flattens it first).

Usage:   amedian(inarray,numbins=1000)
Returns: median calculated over ALL values in inarray
f2.0i   i    N(   s   Ns   ravels   inarrays
   ahistograms   numbinss   hists   smallests   binsizes   extrass   cumsums   cumhists   greater_equals   lens	   otherbinss   lists   indexs   cfbins   LRLs   adds   reduces   cfbelows   freqs   floats   median(   s   inarrays   numbinss	   otherbinss   cfbins   cfbelows   cumhists   binsizes   hists   medians   smallests   LRLs   freqs   extras(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   amedian/  s    
 
&c         C   sÚ   | t j o t i |  ƒ }  d } n t i |  | ƒ }  |  i | d d j o8 |  i | d } t i |  | |  | d ƒ d } nK |  i | d } t i
 |  | g | ƒ } | i d f j o | d } n | Sd S(   s›  
Returns the 'middle' score of the passed array.  If there is an even
number of scores, the mean of the 2 middle scores is returned.  Can function
with 1D arrays, or on the FIRST dimension of 2D arrays (i.e., dimension can
be None, to pre-flatten the array, or else dimension must equal 0).

Usage:   amedianscore(inarray,dimension=None)
Returns: 'middle' score of the array, or the mean of the 2 middle scores
i    i   i   f2.0N(   s	   dimensions   Nones   Ns   ravels   inarrays   sorts   shapes   indxs   asarrays   medians   take(   s   inarrays	   dimensions   medians   indx(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   amedianscoreG  s    	 
'c   
      C   s   | t j o t i |  ƒ }  d } n t i t i |  ƒ ƒ } t |  i	 ƒ }	 d |	 | <t i |	 ƒ } t i |	 ƒ } xz | D]r } t i |  | ƒ } t | | d ƒ } t i t i | | ƒ | | ƒ } t i t i | | ƒ | | ƒ } | } q| W| | f Sd S(   st  
Returns an array of the modal (most common) score in the passed array.
If there is more than one such score, ONLY THE FIRST is returned.
The bin-count for the modal values is also returned.  Operates on whole
array (dimension=None), or on a given dimension.

Usage:   amode(a, dimension=None)
Returns: array of bin-counts for mode(s), array of corresponding modal values
i    i   N(   s	   dimensions   Nones   Ns   ravels   as   pstats   auniques   scoress   lists   shapes	   testshapes   zeross   oldmostfreqs	   oldcountss   scores   equals   templates   asums   countss   wheres   greaters   mostfrequent(
   s   as	   dimensions   scores   mostfrequents   oldmostfreqs   countss	   oldcountss   templates   scoress	   testshape(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   amode`  s"    	 

 !!
c         C   s  |  i ƒ  d d d g j o |  i t i ƒ }  n | t j o t |  ƒ Sn t | ƒ t	 t
 t i g j p
 t d ‚ | d o t i } n
 t i } | d o t i } n
 t i } | d t i i t i |  ƒ ƒ j p# | d t i i t i |  ƒ ƒ j  o t d ‚ n½ | d t j o | d t j o | |  | d ƒ } n„ | d t j o | d t j o | |  | d ƒ } nK | d t j o | d t j o( | |  | d ƒ | |  | d ƒ } n t t i i t i |  | ƒ ƒ ƒ } t t i i t i | ƒ ƒ ƒ } | | Sd S(	   s“  
Returns the arithmetic mean of all values in an array, ignoring values
strictly outside the sequence passed to 'limits'.   Note: either limit
in the sequence, or the value of limits itself, can be set to None.  The
inclusive list/tuple determines whether the lower and upper limiting bounds
(respectively) are open/exclusive (0) or closed/inclusive (1).

Usage:   atmean(a,limits=None,inclusive=(1,1))
s   ls   ss   bs   Wrong type for limits in atmeani    i   s-   No array values within given limits (atmean).N(   s   as   typecodes   astypes   Ns   Floats   limitss   Nones   means   types   ListTypes	   TupleTypes	   ArrayTypes   AssertionErrors	   inclusives   greater_equals   lowerfcns   greaters
   less_equals   upperfcns   lesss   maximums   reduces   ravels   minimums
   ValueErrors   masks   floats   adds   ss   n(   s   as   limitss	   inclusives   lowerfcns   masks   upperfcns   ss   n(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atmean|  s.    	 ) 	 	L"""(%!c   	      C   s¼  |  i t i ƒ }  | t j p | t t g j o~ t i i t i |  |  ƒ ƒ } t
 t t i |  ƒ ƒ ƒ d } t i i t i |  ƒ ƒ d | } | G| G| GH| | | Sn t | ƒ t t t i g j p
 t d ‚ | d o t i } n
 t i } | d o t i } n
 t i } | d t i i t i |  ƒ ƒ j p# | d t i i t i |  ƒ ƒ j  o t d ‚ n½ | d t j o | d t j o | |  | d ƒ } n„ | d t j o | d t j o | |  | d ƒ } nK | d t j o | d t j o( | |  | d ƒ | |  | d ƒ } n t i i t i |  |  | ƒ ƒ } t
 t i i t i | ƒ ƒ ƒ d } t i i t i |  | ƒ ƒ d | } | G| G| GH| | | Sd S(   s¡  
Returns the sample variance of values in an array, (i.e., using N-1),
ignoring values strictly outside the sequence passed to 'limits'.  
Note: either limit in the sequence, or the value of limits itself,
can be set to None.  The inclusive list/tuple determines whether the lower
and upper limiting bounds (respectively) are open/exclusive (0) or
closed/inclusive (1).

Usage:   atvar(a,limits=None,inclusive=(1,1))
i   i   s   Wrong type for limits in atvari    s,   No array values within given limits (atvar).N(   s   as   astypes   Ns   Floats   limitss   Nones   adds   reduces   ravels   term1s   floats   lens   ns   term2s   types   ListTypes	   TupleTypes	   ArrayTypes   AssertionErrors	   inclusives   greater_equals   lowerfcns   greaters
   less_equals   upperfcns   lesss   maximums   minimums
   ValueErrors   mask(	   s   as   limitss	   inclusives   term1s   lowerfcns   masks   upperfcns   ns   term2(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atvarœ  s8    
  #) 	 	L"""(#%'c         C   sÃ   | o t i } n
 t i } | t j o t i |  ƒ }  d } n | t j o# t i
 i t i |  ƒ ƒ d } n t i i t i |  ƒ ƒ } t i | |  | ƒ |  | ƒ } t i
 i | | ƒ Sd S(   sú   
Returns the minimum value of a, along dimension, including only values less
than (or equal to, if inclusive=1) lowerlimit.  If the limit is set to None,
all values in the array are used.

Usage:   atmin(a,lowerlimit=None,dimension=None,inclusive=1)
i    i   N(   s	   inclusives   Ns   greaters   lowerfcns   greater_equals	   dimensions   Nones   ravels   as
   lowerlimits   minimums   reduces   maximums   biggests   wheres   ta(   s   as
   lowerlimits	   dimensions	   inclusives   lowerfcns   biggests   ta(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atminÂ  s      	
#c         C   sÃ   | o t i } n
 t i } | t j o t i |  ƒ }  d } n | t j o# t i
 i t i |  ƒ ƒ d } n t i i t i |  ƒ ƒ } t i | |  | ƒ |  | ƒ } t i
 i | | ƒ Sd S(   s  
Returns the maximum value of a, along dimension, including only values greater
than (or equal to, if inclusive=1) upperlimit.  If the limit is set to None,
a limit larger than the max value in the array is used.

Usage:   atmax(a,upperlimit,dimension=None,inclusive=1)
i    i   N(   s	   inclusives   Ns   lesss   upperfcns
   less_equals	   dimensions   Nones   ravels   as
   upperlimits   maximums   reduces   minimums   smallests   wheres   ta(   s   as
   upperlimits	   dimensions	   inclusives   upperfcns   smallests   ta(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atmaxÖ  s      	
#c         C   s   t  i t |  | | ƒ ƒ Sd S(   s—  
Returns the standard deviation of all values in an array, ignoring values
strictly outside the sequence passed to 'limits'.   Note: either limit
in the sequence, or the value of limits itself, can be set to None.  The
inclusive list/tuple determines whether the lower and upper limiting bounds
(respectively) are open/exclusive (0) or closed/inclusive (1).

Usage:   atstdev(a,limits=None,inclusive=(1,1))
N(   s   Ns   sqrts   tvars   as   limitss	   inclusive(   s   as   limitss	   inclusive(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atstdevê  s    	 c   	      C   s*  t  |  | | ƒ } | t j p | t t g j o t t t i	 |  ƒ ƒ ƒ } n t | ƒ t t t i g j p
 t d ‚ | d o t i } n
 t i } | d o t i } n
 t i } | d t i i t i	 |  ƒ ƒ j p# | d t i i t i	 |  ƒ ƒ j  o t d ‚ n½ | d t j o | d t j o | |  | d ƒ } n„ | d t j o | d t j o | |  | d ƒ } nK | d t j o | d t j o( | |  | d ƒ | |  | d ƒ } n t i i t i	 |  |  | ƒ ƒ } t t i i t i	 | ƒ ƒ ƒ } | t i | ƒ Sd S(   sÃ  
Returns the standard error of the mean for the values in an array,
(i.e., using N for the denominator), ignoring values strictly outside
the sequence passed to 'limits'.   Note: either limit in the sequence,
or the value of limits itself, can be set to None.  The inclusive list/tuple
determines whether the lower and upper limiting bounds (respectively) are
open/exclusive (0) or closed/inclusive (1).

Usage:   atsem(a,limits=None,inclusive=(1,1))
s   Wrong type for limits in atsemi    i   s,   No array values within given limits (atsem).N(   s   tstdevs   as   limitss	   inclusives   sds   Nones   floats   lens   Ns   ravels   ns   types   ListTypes	   TupleTypes	   ArrayTypes   AssertionErrors   greater_equals   lowerfcns   greaters
   less_equals   upperfcns   lesss   maximums   reduces   minimums
   ValueErrors   masks   adds   term1s   maths   sqrt(	   s   as   limitss	   inclusives   term1s   masks   ns   lowerfcns   upperfcns   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atsem÷  s,    
  ) 	 	L"""(#!c         C   st   | t j o t i |  ƒ }  d } n | d j o d Sn6 t |  | d ƒ } t i |  | | ƒ } t | | ƒ Sd S(   s‹  
Calculates the nth moment about the mean for a sample (defaults to the
1st moment).  Generally used to calculate coefficients of skewness and
kurtosis.  Dimension can equal None (ravel array first), an integer
(the dimension over which to operate), or a sequence (operate over
multiple dimensions).

Usage:   amoment(a,moment=1,dimension=None)
Returns: appropriate moment along given dimension
i    i   f0.0N(
   s	   dimensions   Nones   Ns   ravels   as   moments   ameans   mns   powers   s(   s   as   moments	   dimensions   mns   s(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   amoment	  s    
 
c         C   s"   d t  |  | ƒ t |  | ƒ Sd S(   s  
Returns the coefficient of variation, as defined in CRC Standard
Probability and Statistics, p.6. Dimension can equal None (ravel array
first), an integer (the dimension over which to operate), or a
sequence (operate over multiple dimensions).

Usage:   avariation(a,dimension=None)
f100.0N(   s   asamplestdevs   as	   dimensions   amean(   s   as	   dimension(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   avariation1	  s     c         C   s   t  i t |  d | ƒ d ƒ } t  i | d ƒ } t | ƒ t  i	 j o t
 | ƒ d j o d Gt
 | ƒ GHn | | } t  i | d t |  d | ƒ | ƒ Sd S(   s’   
Returns the skewness of a distribution (normal ==> 0.0; >0 means extra
weight in left tail).  Use askewtest() to see if it's close enough.
Dimension can equal None (ravel array first), an integer (the
dimension over which to operate), or a sequence (operate over multiple
dimensions).

Usage:   askew(a, dimension=None)
Returns: skew of vals in a along dimension, returning ZERO where all vals equal
i   f1.5i    s   Number of zeros in askew: i   N(   s   Ns   powers   amoments   as	   dimensions   denoms   equals   zeros   types	   ArrayTypes   asums   where(   s   as	   dimensions   zeros   denom(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   askew=	  s    
 )
c         C   s   t  i t |  d | ƒ d ƒ } t  i | d ƒ } t | ƒ t  i	 j o t
 | ƒ d j o d Gt
 | ƒ GHn | | } t  i | d t |  d | ƒ | ƒ Sd S(   s­  
Returns the kurtosis of a distribution (normal ==> 3.0; >3 means
heavier in the tails, and usually more peaked).  Use akurtosistest()
to see if it's close enough.  Dimension can equal None (ravel array
first), an integer (the dimension over which to operate), or a
sequence (operate over multiple dimensions).

Usage:   akurtosis(a,dimension=None)
Returns: kurtosis of values in a along dimension, and ZERO where all vals equal
i   i    s   Number of zeros in akurtosis: i   N(   s   Ns   powers   amoments   as	   dimensions   denoms   equals   zeros   types	   ArrayTypes   asums   where(   s   as	   dimensions   zeros   denom(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   akurtosisP	  s    
 )
c         C   s­   | t j o t i |  ƒ }  d } n |  i | } t i i |  ƒ t i	 i |  ƒ f } t |  | ƒ } t |  | ƒ } t |  | ƒ } t |  | ƒ } | | | | | | f Sd S(   s<  
Returns several descriptive statistics of the passed array.  Dimension
can equal None (ravel array first), an integer (the dimension over
which to operate), or a sequence (operate over multiple dimensions).

Usage:   adescribe(inarray,dimension=None)
Returns: n, (min,max), mean, standard deviation, skew, kurtosis
i    N(   s	   dimensions   Nones   Ns   ravels   inarrays   shapes   ns   minimums   reduces   maximums   mms   ameans   ms   astdevs   sds   askews   skews	   akurtosiss   kurt(   s   inarrays	   dimensions   mms   skews   ms   ns   kurts   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   adescribec	  s     
$c   
      C   s  | t j o t i |  ƒ }  d } n t |  | ƒ } t |  i | ƒ } | t i
 | d | d d | d ƒ } d | | d | d | d | d | d	 | d
 | d | d } d t i
 d | d ƒ }	 d t i
 t i t i
 |	 ƒ ƒ ƒ } t i
 d |	 d ƒ } t i t i | d ƒ d | ƒ } | t i | | t i
 | | d d ƒ ƒ } | d t | ƒ d f Sd S(   s2  
Tests whether the skew is significantly different from a normal
distribution.  Dimension can equal None (ravel array first), an
integer (the dimension over which to operate), or a sequence (operate
over multiple dimensions).

Usage:   askewtest(a,dimension=None)
Returns: z-score and 2-tail z-probability
i    i   i   f6.0i   f3.0i   iF   f2.0i   i   i	   iÿÿÿÿf1.0N(   s	   dimensions   Nones   Ns   ravels   as   askews   b2s   floats   shapes   ns   sqrts   ys   beta2s   W2s   logs   deltas   alphas   wheres   equals   Zs   zprob(
   s   as	   dimensions   Zs   beta2s   b2s   deltas   ys   alphas   ns   W2(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   askewtest|	  s    	 
+J%!0c         C   s>  | t j o t i |  ƒ }  d } n t |  i | ƒ } | d j  o d G| GHn t |  | ƒ }	 d | d | d } d | | d | d | d | d | d | d	 } |	 | t i | ƒ } d
 | | d	 | d | d | d t i d
 | d | d	 | | d | d ƒ } d
 d | d | t i d d | d ƒ } d d d | } d | t i d | d ƒ }
 t i t i |
 d ƒ d |
 ƒ }
 t i t i |
 d ƒ | t i d d | |
 d d ƒ ƒ } | | t i d d | ƒ } t i t i |
 d ƒ d | ƒ } | d t | ƒ d f Sd S(   sa  
Tests whether a dataset has normal kurtosis (i.e.,
kurtosis=3(n-1)/(n+1)) Valid only for n>20.  Dimension can equal None
(ravel array first), an integer (the dimension over which to operate),
or a sequence (operate over multiple dimensions).

Usage:   akurtosistest(a,dimension=None)
Returns: z-score and 2-tail z-probability, returns 0 for bad pixels
i    i   s<   akurtosistest only valid for n>=20 ... continuing anyway, n=f3.0i   f24.0i   i   i   f6.0i   i	   f8.0f2.0f4.0f9.0ic   f1.0N(   s	   dimensions   Nones   Ns   ravels   as   floats   shapes   ns	   akurtosiss   b2s   Es   varb2s   sqrts   xs	   sqrtbeta1s   As   term1s   denoms   wheres   lesss   equals   powers   term2s   Zs   zprob(   s   as	   dimensions   As   Zs   Es   term2s   term1s   varb2s   ns   b2s   denoms   xs	   sqrtbeta1(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   akurtosistest•	  s(    	 
:[/!=!c         C   s‰   | t j o t i |  ƒ }  d } n t |  | ƒ \ } } t |  | ƒ \ } } t i
 | d ƒ t i
 | d ƒ } | t | d ƒ f Sd S(   sT  
Tests whether skew and/OR kurtosis of dataset differs from normal
curve.  Can operate over multiple dimensions.  Dimension can equal
None (ravel array first), an integer (the dimension over which to
operate), or a sequence (operate over multiple dimensions).

Usage:   anormaltest(a,dimension=None)
Returns: z-score and 2-tail probability
i    i   N(   s	   dimensions   Nones   Ns   ravels   as	   askewtests   ss   ps   akurtosistests   ks   powers   k2s
   achisqprob(   s   as	   dimensions   k2s   ps   ss   k(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   anormaltestµ	  s    	 
"c         C   s“   t  i |  ƒ } t i | ƒ } t i t | ƒ ƒ } x@ t	 t | ƒ ƒ D], } t i i t i |  | | ƒ ƒ | | <qF Wt i t  i | | ƒ ƒ Sd S(   sð   
Returns a 2D array of item frequencies.  Column 1 contains item values,
column 2 contains their respective counts.  Assumes a 1D array is passed.

Usage:   aitemfreq(a)
Returns: a 2D frequency table (col [0:n-1]=scores, col n=frequencies)
N(   s   pstats   auniques   as   scoress   Ns   sorts   zeross   lens   freqs   ranges   is   adds   reduces   equals   arrays   aabut(   s   as   is   scoress   freq(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   aitemfreqÌ	  s      *c   
      C   s­   | d } | t |  ƒ } t |  ƒ \ } } } }	 t	 | d ƒ } x0 t t | ƒ ƒ D] } | | | j o PqU qU W| | | | d t | | ƒ | | | } | Sd S(   sƒ   
Usage:   ascoreatpercentile(inarray,percent)   0<percent<100
Returns: score at given percentile, relative to inarray distribution
f100.0i   N(   s   percents   lens   inarrays   targetcfs	   histograms   hs   lrls   binsizes   extrass   cumsums   cumhists   ranges   is   floats   score(
   s   inarrays   percents   scores   is   hs   cumhists   lrls   binsizes   targetcfs   extras(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ascoreatpercentileÜ	  s     
 	0c         C   s”   t  |  | | ƒ \ } } }	 }
 t | d ƒ } t
 | | t |	 ƒ ƒ } | | d | | |	 | t |	 ƒ | | t t |  ƒ ƒ d } | Sd S(   sá   
Note: result of this function depends on the values used to histogram
the data(!).

Usage:   apercentileofscore(inarray,score,histbins=10,defaultlimits=None)
Returns: percentile-position of score (0-100) relative to inarray
i   id   N(   s	   histograms   inarrays   histbinss   defaultlimitss   hs   lrls   binsizes   extrass   cumsums   cumhists   ints   scores   floats   is   lens   pct(   s   inarrays   scores   histbinss   defaultlimitss   is   hs   cumhists   lrls   pcts   binsizes   extras(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   apercentileofscoreì	  s     Dc         C   st  t  i |  ƒ }  | t j o, | d } | d }
 |
 | t | ƒ } ni t  i
 i |  ƒ } t  i i |  ƒ } t | | ƒ t | ƒ d } | | | t | ƒ } | | d } t  i | ƒ } d }	 xq |  D]i } yO | | d j  o |	 d }	 n- t | | t | ƒ ƒ } | | d | | <WqÌ |	 d }	 qÌ XqÌ W|	 d j o
 | d j o d G|	 GHn | | | |	 f Sd S(   s<  
Returns (i) an array of histogram bin counts, (ii) the smallest value
of the histogram binning, and (iii) the bin width (the last 2 are not
necessarily integers).  Default number of bins is 10.  Defaultlimits
can be None (the routine picks bins spanning all the numbers in the
inarray) or a 2-sequence (lowerlimit, upperlimit).  Returns all of the
following: array of bin values, lowerreallimit, binsize, extrapoints.

Usage:   ahistogram(inarray,numbins=10,defaultlimits=None,printextras=1)
Returns: (array of bin counts, bin-minimum, min-width, #-points-outside-range)
i    i   f2.0s'   
Points outside given histogram range =N(   s   Ns   ravels   inarrays   defaultlimitss   Nones   lowerreallimits   upperreallimits   floats   numbinss   binsizes   minimums   reduces   Mins   maximums   Maxs   estbinwidths   zeross   binss   extrapointss   nums   ints   bintoincrements   printextras(   s   inarrays   numbinss   defaultlimitss   printextrass   bintoincrements   nums   Mins   Maxs   estbinwidths   extrapointss   upperreallimits   lowerreallimits   binsizes   bins(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   ahistogramû	  s2     

 c         C   sB   t  |  | | ƒ \ } } } } t | d ƒ } | | | | f Sd S(   sB  
Returns a cumulative frequency histogram, using the histogram function.
Defaultreallimits can be None (use all data), or a 2-sequence containing
lower and upper limits on values to include.

Usage:   acumfreq(a,numbins=10,defaultreallimits=None)
Returns: array of cumfreq bin values, lowerreallimit, binsize, extrapoints
i   N(
   s	   histograms   as   numbinss   defaultreallimitss   hs   ls   bs   es   cumsums   cumhist(   s   as   numbinss   defaultreallimitss   bs   es   hs   cumhists   l(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   acumfreq"
  s     c         C   sR   t  |  | | ƒ \ } } } } t i	 | t
 |  i d ƒ ƒ } | | | | f Sd S(   s@  
Returns a relative frequency histogram, using the histogram function.
Defaultreallimits can be None (use all data), or a 2-sequence containing
lower and upper limits on values to include.

Usage:   arelfreq(a,numbins=10,defaultreallimits=None)
Returns: array of cumfreq bin values, lowerreallimit, binsize, extrapoints
i    N(   s	   histograms   as   numbinss   defaultreallimitss   hs   ls   bs   es   Ns   arrays   floats   shape(   s   as   numbinss   defaultreallimitss   bs   es   hs   l(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   arelfreq0
  s      c          G   sî  d } t |  ƒ } t i | t i ƒ }
 t i | t i ƒ }	 t i | t i ƒ } g  } xs t | ƒ D]e } | i |  | i t i ƒ ƒ t t | | ƒ ƒ |
 | <t | | ƒ |	 | <t | | ƒ | | <qd Wx­ t | ƒ D]Ÿ } x– t |
 | ƒ D]„ } |
 | d |
 | | | | | | d } d |	 | |
 | d } |
 | d |
 | d } | | t | ƒ | | | <qñ WqÚ Wd } x= t | ƒ D]/ } |	 | t | | ƒ | j o
 d } qqW| d j o t d	 ‚ n t i | ƒ Sd
 S(   s³  
Computes a transform on input data (any number of columns).  Used to
test for homogeneity of variance prior to running one-way stats.  Each
array in *args is one level of a factor.  If an F_oneway() run on the
transformed data and found significant, variances are unequal.   From
Maxwell and Delaney, p.112.

Usage:   aobrientransform(*args)    *args = 1D arrays, one per level of factor
Returns: transformed data for use in an ANOVA
f1e-10f1.5i   f0.5f1.0f2.0i   i    s'   Lack of convergence in obrientransform.N(   s   TINYs   lens   argss   ks   Ns   zeross   Floats   ns   vs   ms   nargss   ranges   is   appends   astypes   floats   vars   means   js   t1s   t2s   t3s   checks
   ValueErrors   array(   s   argss   is   nargss   ks   ms   t3s   t2s   js   TINYs   vs   ns   t1s   check(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   aobrientransformB
  s:    
    .$ c         C   sè   | t j o t i |  ƒ }  d } n | d j o& t |  | ƒ d d … t i f } n t |  | d d ƒ} |  | } t	 | ƒ t
 j o, d } x0 | D] } | |  i | } q˜ Wn |  i | } t | | | ƒ t | ƒ } | Sd S(   sw  
Returns the sample standard deviation of the values in the passed
array (i.e., using N).  Dimension can equal None (ravel array first),
an integer (the dimension over which to operate), or a sequence
(operate over multiple dimensions).  Set keepdims=1 to return an array
with the same number of dimensions as inarray.

Usage:   asamplevar(inarray,dimension=None,keepdims=0)
i    i   Ns   keepdims(   s	   dimensions   Nones   Ns   ravels   inarrays   ameans   NewAxiss   mns
   deviationss   types   ListTypes   ns   ds   shapes   asss   keepdimss   floats   svar(   s   inarrays	   dimensions   keepdimss   ds   mns
   deviationss   svars   n(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   asamplevarh
  s     	 
&
 c         C   s   t  i t |  | | ƒ ƒ Sd S(   sy  
Returns the sample standard deviation of the values in the passed
array (i.e., using N).  Dimension can equal None (ravel array first),
an integer (the dimension over which to operate), or a sequence
(operate over multiple dimensions).  Set keepdims=1 to return an array
with the same number of dimensions as inarray.

Usage:   asamplestdev(inarray,dimension=None,keepdims=0)
N(   s   Ns   sqrts
   asamplevars   inarrays	   dimensions   keepdims(   s   inarrays	   dimensions   keepdims(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asamplestdev„
  s    	 c         C   sE   t  |  | ƒ } t |  | ƒ } t i t i | d ƒ d | | ƒ Sd S(   s?  
Calculates signal-to-noise.  Dimension can equal None (ravel array
first), an integer (the dimension over which to operate), or a
sequence (operate over multiple dimensions).

Usage:   asignaltonoise(instack,dimension=0):
Returns: array containing the value of (mean/stdev) along dimension,
         or 0 when stdev=0
i    N(	   s   means   instacks	   dimensions   ms   stdevs   sds   Ns   wheres   equal(   s   instacks	   dimensions   ms   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asignaltonoise‘
  s    	 c         C   s¶   | t j o t i |  ƒ }  d } n t |  | d ƒ } |  | } t | ƒ t	 j o, d } x0 | D] } | |  i | } qb Wn |  i | } t | | | ƒ t | d ƒ } | Sd S(   sq  
Returns the estimated population variance of the values in the passed
array (i.e., N-1).  Dimension can equal None (ravel array first), an
integer (the dimension over which to operate), or a sequence (operate
over multiple dimensions).  Set keepdims=1 to return an array with the
same number of dimensions as inarray.

Usage:   avar(inarray,dimension=None,keepdims=0)
i    i   N(   s	   dimensions   Nones   Ns   ravels   inarrays   ameans   mns
   deviationss   types   ListTypes   ns   ds   shapes   asss   keepdimss   floats   var(   s   inarrays	   dimensions   keepdimss   ds   mns
   deviationss   vars   n(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   avar 
  s    	 

  c         C   s   t  i t |  | | ƒ ƒ Sd S(   s}  
Returns the estimated population standard deviation of the values in
the passed array (i.e., N-1).  Dimension can equal None (ravel array
first), an integer (the dimension over which to operate), or a
sequence (operate over multiple dimensions).  Set keepdims=1 to return
an array with the same number of dimensions as inarray.

Usage:   astdev(inarray,dimension=None,keepdims=0)
N(   s   Ns   sqrts   avars   inarrays	   dimensions   keepdims(   s   inarrays	   dimensions   keepdims(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   astdev¹
  s    	 c         C   sT   | t j o t i |  ƒ }  d } n t |  | | ƒ t t i |  i	 | ƒ ƒ Sd S(   sy  
Returns the estimated population standard error of the values in the
passed array (i.e., N-1).  Dimension can equal None (ravel array
first), an integer (the dimension over which to operate), or a
sequence (operate over multiple dimensions).  Set keepdims=1 to return
an array with the same number of dimensions as inarray.

Usage:   asterr(inarray,dimension=None,keepdims=0)
i    N(
   s	   dimensions   Nones   Ns   ravels   inarrays   astdevs   keepdimss   floats   sqrts   shape(   s   inarrays	   dimensions   keepdims(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asterrÆ
  s
    	 
c         C   s   | t j o t i |  ƒ }  d } n t | ƒ t j o, d } x0 | D] } | |  i	 | } qF Wn |  i	 | } t
 |  | | ƒ t i | d ƒ } | Sd S(   ss  
Returns the standard error of the mean (i.e., using N) of the values
in the passed array.  Dimension can equal None (ravel array first), an
integer (the dimension over which to operate), or a sequence (operate
over multiple dimensions).  Set keepdims=1 to return an array with the
same number of dimensions as inarray.

Usage:   asem(inarray,dimension=None, keepdims=0)
i    i   N(   s	   dimensions   Nones   Ns   ravels   inarrays   types   ListTypes   ns   ds   shapes   asamplestdevs   keepdimss   sqrts   s(   s   inarrays	   dimensions   keepdimss   ds   ss   n(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asemÖ
  s    	 
 #c         C   s"   | t |  ƒ t |  ƒ } | Sd S(   s²   
Returns the z-score of a given input score, given thearray from which
that score came.  Not appropriate for population calculations, nor for
arrays > 1D.

Usage:   az(a, score)
N(   s   scores   ameans   as   asamplestdevs   z(   s   as   scores   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   azí
  s     c         C   s>   g  } x$ |  D] } | i t |  | ƒ ƒ q Wt i | ƒ Sd S(   s   
Returns a 1D array of z-scores, one for each score in the passed array,
computed relative to the passed array.

Usage:   azs(a)
N(   s   zscoress   as   items   appends   zs   Ns   array(   s   as   zscoress   item(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   azsù
  s      c         C   s.   t  | | ƒ } t | d ƒ } |  | | Sd S(   sÚ   
Returns an array of z-scores the shape of scores (e.g., [x,y]), compared to
array passed to compare (e.g., [time,x,y]).  Assumes collapsing over dim 0
of the compare array.

Usage:   azs(scores, compare, dimension=0)
i    N(   s   ameans   compares	   dimensions   mnss   asamplestdevs   sstds   scores(   s   scoress   compares	   dimensions   mnss   sstd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   azmap  s     c         C   sK  | d „ } t |  ƒ t i j o3 y t i |  ƒ }  WqU t i |  d ƒ }  qU Xn |  i } |  i	 ƒ  d d d d g j o4 t i
 |  ƒ } t i t | | ƒ ƒ } | | _ n“ |  i	 ƒ  d d g j oo t i
 |  ƒ d } xL t t | ƒ ƒ D]8 } t | | ƒ t j o t | | | ƒ | | <qð qð W| | _ n |  d } | Sd	 S(
   s’   
Rounds all values in array a to 'digits' decimal places.

Usage:   around(a,digits)
Returns: a, where each value is rounded to 'digits' decimals
c         C   s   t  |  | ƒ Sd  S(   N(   s   rounds   xs   d(   s   xs   d(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ar  s    s   Os   fs   Fs   ds   Ds   oi   N(   s   digitss   ars   types   as   Ns	   ArrayTypes   arrays   shapes   shps   typecodes   ravels   bs   maps   ranges   lens   is	   FloatTypes   round(   s   as   digitss   bs   is   shps   ar(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   around  s*     	 
c         C   sª   t  i |  i ƒ } | t j o) | t  i t  i |  | ƒ d d ƒ } n | t j o) | t  i t  i
 |  | ƒ d d ƒ } n t  i | d d ƒ } t  i | | |  ƒ Sd S(   s  
Like Numeric.clip() except that values <threshmid or >threshmax are replaced
by newval instead of by threshmin/threshmax (respectively).

Usage:   athreshold(a,threshmin=None,threshmax=None,newval=0)
Returns: a, with values <threshmin or >threshmax replaced with newval
i   i    N(   s   Ns   zeross   as   shapes   masks	   threshmins   Nones   wheres   lesss	   threshmaxs   greaters   clips   newval(   s   as	   threshmins	   threshmaxs   newvals   mask(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   athreshold6  s     ))c         C   s5   t  | t |  ƒ ƒ } t |  ƒ | } |  | | !Sd S(   s£  
Slices off the passed proportion of items from BOTH ends of the passed
array (i.e., with proportiontocut=0.1, slices 'leftmost' 10% AND
'rightmost' 10% of scores.  You must pre-sort the array if you want
"proper" trimming.  Slices off LESS if proportion results in a
non-integer slice index (i.e., conservatively slices off
proportiontocut).

Usage:   atrimboth (a,proportiontocut)
Returns: trimmed version of array a
N(   s   ints   proportiontocuts   lens   as   lowercuts   uppercut(   s   as   proportiontocuts   uppercuts   lowercut(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   atrimbothG  s     c         C   s‹   t  i | ƒ d j o* d } t |  ƒ t | t |  ƒ ƒ } n= t  i | ƒ d j o& t | t |  ƒ ƒ } t |  ƒ } n |  | | !Sd S(   s€  
Slices off the passed proportion of items from ONE end of the passed
array (i.e., if proportiontocut=0.1, slices off 'leftmost' or 'rightmost'
10% of scores).  Slices off LESS if proportion results in a non-integer
slice index (i.e., conservatively slices off proportiontocut).

Usage:   atrim1(a,proportiontocut,tail='right')  or set tail='left'
Returns: trimmed version of array a
s   righti    s   leftN(	   s   strings   lowers   tails   lowercuts   lens   as   ints   proportiontocuts   uppercut(   s   as   proportiontocuts   tails   uppercuts   lowercut(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atrim1X  s    	 $c         C   sy   t  |  i ƒ d j o t d ‚ n |  i d } t |  d ƒ } t i t i	 |  ƒ |  ƒ t
 | ƒ t i i | | ƒ Sd S(   s…   
Computes the covariance matrix of a matrix X.  Requires a 2D matrix input.

Usage:   acovariance(X)
Returns: covariance matrix of X
i   s    acovariance requires 2D matricesi    N(   s   lens   Xs   shapes	   TypeErrors   ns   ameans   mXs   Ns   dots	   transposes   floats   multiplys   outer(   s   Xs   ns   mX(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   acovarianceo  s     c         C   s?   t  |  ƒ } t i | ƒ } | t i t i i | | ƒ ƒ Sd S(   sˆ   
Computes the correlation matrix of a matrix X.  Requires a 2D matrix input.

Usage:   acorrelation(X)
Returns: correlation matrix of X
N(	   s   acovariances   Xs   Cs   Ns   diagonals   Vs   sqrts   multiplys   outer(   s   Xs   Cs   V(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   acorrelation}  s     c         C   s²  d } x1 | d d d d d d g j o d Gt ƒ  } q	 W| d d d d g j oñd	 Gt |  | ƒ } t t i | d
 ƒ t i | d ƒ ƒ \ }	 } | d j  o d t t | d ƒ ƒ } n d } | GH| d d g j oå | d
 d j o> t |  | t d
 ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq@t |  ƒ d j p t | ƒ d j o8 t |  | ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq@t |  | ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq¥| d
 d j o; t |  | d
 ƒ \ } } d Gt | d ƒ Gt | d ƒ GHq¥t |  | ƒ \ } } d Gt | d ƒ Gt | d ƒ GHnbd } x1 | d d d d d d g j o d Gt ƒ  } qMW| d d g j o‹ t |  | ƒ \ }
 } } } } d GHd d d d d g t |
 d ƒ t | d ƒ t | d ƒ t | d ƒ t | d ƒ g g } t i | ƒ nŠ | d d g j o= t |  | ƒ \ } } d GHd  Gt | d ƒ Gt | d ƒ GHn: t |  | ƒ \ } } d! GHd" Gt | d ƒ Gt | d ƒ GHd# GHt Sd$ S(%   sü   
Interactively determines the type of data in x and y, and then runs the
appropriated statistic for paired group data.

Usage:   apaired(x,y)     x,y = the two arrays of values to be compared
Returns: appropriate statistic name, value, and probability
s    s   is   rs   Is   Rs   cs   Cs9   
Independent or related samples, or correlation (i,r,c): s   
Comparing variances ...i    i   f0.050000000000000003s   unequal, p=i   s   equals   es   
Independent samples t-test:  i   s(   
Rank Sums test (NONparametric, n>20):  s.   
Mann-Whitney U-test (NONparametric, ns<20):  s   
Related samples t-test:  s#   
Wilcoxon T-test (NONparametric):  s   ds   Ds9   
Is the data Continuous, Ranked, or Dichotomous (c,r,d): s/   
Linear regression for continuous variables ...s   Slopes	   Intercepts   Probs
   SEestimates%   
Correlation for ranked variables ...s   Spearman's r: s/   
Assuming x contains a dichotomous variable ...s   Point Biserial r: s   

N(    s   sampless	   raw_inputs   obrientransforms   xs   ys   rs   F_oneways   pstats   colexs   fs   ps   strs   rounds   vartypes	   ttest_inds   Nones   ts   lens   ranksumss   zs   mannwhitneyus   us	   ttest_rels   corrtypes
   linregresss   ms   bs   sees   lols   printccs	   spearmanrs   pointbiserialr(   s   xs   ys   sees   vartypes   lols   rs   sampless   corrtypes   bs   fs   ms   ps   us   ts   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   apaired‰  s^      -#&#### W#c         C   s  d } t |  ƒ } t |  ƒ }	 t | ƒ } | t i	 i
 |  | ƒ t i	 i
 |  ƒ t i	 i
 | ƒ } t i | t |  ƒ t |  ƒ | t | ƒ t | ƒ ƒ } | | } | d } | t i | d | | d | | ƒ }
 t d | d | | |
 |
 | ƒ } | | f Sd S(   s÷   
Calculates a Pearson correlation coefficient and returns p.  Taken
from Heiman's Basic Statistics for the Behav. Sci (2nd), p.195.

Usage:   apearsonr(x,y,verbose=1)      where x,y are equal length arrays
Returns: Pearson's r, two-tailed p-value
f9.9999999999999995e-21i   f1.0f0.5N(   s   TINYs   lens   xs   ns   ameans   xmeans   ys   ymeans   Ns   adds   reduces   r_nums   maths   sqrts   asss   asquare_of_sumss   r_dens   rs   dfs   ts   abetais   verboses   prob(   s   xs   ys   verboses   r_dens   ymeans   ns   dfs   rs   TINYs   xmeans   ts   probs   r_num(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   apearsonrÉ  s     :;

+%c         C   sÉ   d } t |  ƒ } t |  ƒ } t | ƒ } t i	 i
 | | d ƒ }	 d d |	 t | | d d ƒ } | t i | d | d d | ƒ }
 | d } t d | d | | |
 |
 ƒ } | | f Sd S(   sí   
Calculates a Spearman rank-order correlation coefficient.  Taken
from Heiman's Basic Statistics for the Behav. Sci (1st), p.192.

Usage:   aspearmanr(x,y)      where x,y are equal-length arrays
Returns: Spearman's r, two-tailed p-value
f1.0000000000000001e-30i   i   i   f1.0f0.5N(   s   TINYs   lens   xs   ns   rankdatas   rankxs   ys   rankys   Ns   adds   reduces   dsqs   floats   rss   maths   sqrts   ts   dfs   abetais   probrs(   s   xs   ys   rankxs   rss   dfs   rankys   ns   probrss   TINYs   dsqs   t(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   aspearmanrÞ  s     $'
"c         C   s¡  d } t i |  ƒ } t i |  | ƒ } t | ƒ d j o t	 d ‚ nWt i | t
 i d ƒ ƒ }
 t i | |
 d ƒ } t i | d | d ƒ }  t i | d | d ƒ } t t i |  d ƒ ƒ } t t i | d ƒ ƒ }	 t | ƒ } t i t |  ƒ t | ƒ t | ƒ t | ƒ ƒ } |	 | t t i | d ƒ ƒ | } | d } | t i | d | | d | | ƒ } t d | d | | | | ƒ } | | f Sd S(	   s  
Calculates a point-biserial correlation coefficient and the associated
probability value.  Taken from Heiman's Basic Statistics for the Behav.
Sci (1st), p.194.

Usage:   apointbiserialr(x,y)      where x,y are equal length arrays
Returns: Point-biserial r, two-tailed p-value
f1.0000000000000001e-30i   s:   Exactly 2 categories required (in x) for pointbiserialr().i    i   f1.0f0.5N(   s   TINYs   pstats   auniques   xs
   categoriess   aabuts   ys   datas   lens
   ValueErrors   Ns   aranges   codemaps   arecodes   recodeds	   alinexands   ameans   acolexs   xmeans   ymeans   ns   maths   sqrts   floats   adjusts   asamplestdevs   rpbs   dfs   ts   abetais   prob(   s   xs   ys   rpbs   TINYs   dfs   recodeds   datas
   categoriess   probs   ymeans   codemaps   ns   adjusts   ts   xmean(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   apointbiserialrô  s&     3$
+"c         C   sb  d }
 d } d } xÏ t t |  ƒ d ƒ D]· } x® t | t | ƒ ƒ D]— } |  | |  | } | | | | }	 | |	 } | o= |
 d }
 | d } | d j o | d } qÜ | d } qE | o |
 d }
 qE | d } qE Wq) W| t i |
 | ƒ } d t |  ƒ d d t |  ƒ t |  ƒ d } | t i | ƒ } t t | ƒ d ƒ } | | f Sd S(   sÑ   
Calculates Kendall's tau ... correlation of ordinal data.  Adapted
from function kendl1 in Numerical Recipies.  Needs good test-cases.@@@

Usage:   akendalltau(x,y)
Returns: Kendall's tau, two-tailed p-value
i    i   f4.0f10.0f9.0f1.4142136000000001N(   s   n1s   n2s   isss   ranges   lens   xs   js   ys   ks   a1s   a2s   aas   maths   sqrts   taus   svars   zs   erfccs   abss   prob(   s   xs   ys   aas   taus   isss   ks   js   svars   a1s   a2s   n1s   n2s   zs   prob(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   akendalltau  s2       


0c          G   s  d } t |  ƒ d j oe |  d }  t |  ƒ d j o |  d } |  d } q’ |  d d … d f } |  d d … d f } n |  d } |  d } t | ƒ }
 t | ƒ } t | ƒ }	 |
 t	 i
 i | | ƒ t	 i
 i | ƒ t	 i
 i | ƒ } t i |
 t | ƒ t | ƒ |
 t | ƒ t | ƒ ƒ } | | } d t i d | | d | | ƒ } |
 d } | t i | d | | d | | ƒ } t d | d | | | | ƒ } | t |
 ƒ t | ƒ t | ƒ } |	 | | } t i d | | ƒ t | ƒ } | | | | | f Sd S(   sW  
Calculates a regression line on two arrays, x and y, corresponding to x,y
pairs.  If a single 2D array is passed, alinregress finds dim with 2 levels
and splits data into x,y pairs along that dim.

Usage:   alinregress(*args)    args=2 equal-length arrays, or one 2D array
Returns: slope, intercept, r, two-tailed prob, sterr-of-the-estimate
f9.9999999999999995e-21i   i    i   Nf0.5f1.0(   s   TINYs   lens   argss   xs   ys   ns   ameans   xmeans   ymeans   Ns   adds   reduces   r_nums   maths   sqrts   asss   asquare_of_sumss   r_dens   rs   logs   zs   dfs   ts   abetais   probs   floats   slopes	   intercepts   asamplestdevs   sterrest(   s   argss   slopes   r_nums   r_dens   TINYs   dfs   probs	   intercepts   sterrests   ymeans   ns   rs   ts   xmeans   ys   xs   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   alinregress5  s2     



:;
'
+"$!c         C   s9  t  |  ƒ t i j o t i |  ƒ }  n t |  ƒ } t |  ƒ } t	 |  ƒ }	 |	 d } |	 d | t | ƒ } | | t i | d |	 ƒ }
 t d | d | | |
 |
 ƒ } | d j on d } t | | d d | d d d | |	 | | t i i t i |  ƒ ƒ t i i t i |  ƒ ƒ | |
 | ƒ n |
 | f Sd S(	   s’  
Calculates the t-obtained for the independent samples T-test on ONE group
of scores a, given a population mean.  If printit=1, results are printed
to the screen.  If printit='filename', the results are output to 'filename'
using the given writemode (default=append).  Returns t-value, and prob.

Usage:   attest_1samp(a,popmean,Name='Sample',printit=0,writemode='a')
Returns: t-value, two-tailed prob
i   f1.0f0.5i    s   Single-sample T-test.s
   Populations   --N(   s   types   as   Ns	   ArrayTypes   arrays   ameans   xs   avars   vs   lens   ns   dfs   floats   svars   popmeans   maths   sqrts   ts   abetais   probs   printits   statnames   outputpairedstatss	   writemodes   names   minimums   reduces   ravels   maximum(   s   as   popmeans   printits   names	   writemodes   statnames   svars   probs   dfs   ns   ts   vs   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   attest_1samp^  s$    	 
"	$c         C   s—  | t j o( t i |  ƒ }  t i | ƒ } d } n t |  | ƒ } t | | ƒ } t	 |  | ƒ } t	 | | ƒ } |  i | } | i | } | | d }
 | d | | d | t |
 ƒ }	 t i |	 d ƒ } t i | d |	 ƒ }	 | | t i |	 d | d | ƒ } t i | d | ƒ } t d |
 d t |
 ƒ |
 | | ƒ } t | ƒ t i j o t i | | i ƒ } n t | ƒ d j o | d } n | d j oä t | ƒ t i j o | d } n t | ƒ t i j o | d } n d } t | | | | | | t i" i# t i |  ƒ ƒ t i$ i# t i |  ƒ ƒ | | | | t i" i# t i | ƒ ƒ t i$ i# t i | ƒ ƒ | | | ƒ d Sn | | f Sd S(   s  
Calculates the t-obtained T-test on TWO INDEPENDENT samples of scores
a, and b.  From Numerical Recipies, p.483.  If printit=1, results are
printed to the screen.  If printit='filename', the results are output
to 'filename' using the given writemode (default=append).  Dimension
can equal None (ravel array first), or an integer (the dimension over
which to operate on a and b).

Usage:   attest_ind (a,b,dimension=None,printit=0,
                     Name1='Samp1',Name2='Samp2',writemode='a')
Returns: t-value, two-tailed p-value
i    i   i   f1.0f0.5s   Independent samples T-test.N(&   s	   dimensions   Nones   Ns   ravels   as   bs   ameans   x1s   x2s   avars   v1s   v2s   shapes   n1s   n2s   dfs   floats   svars   equals   zerodivproblems   wheres   sqrts   ts   abetais   probss   types	   ArrayTypes   reshapes   lens   printits   statnames   outputpairedstatss	   writemodes   name1s   minimums   reduces   maximums   name2(   s   as   bs	   dimensions   printits   name1s   name2s	   writemodes   statnames   probss   svars   dfs   v1s   v2s   x2s   zerodivproblems   x1s   ts   n1s   n2(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   attest_ind|  sH     
$'(	$$c         C   sŒ  | t j o( t i |  ƒ }  t i | ƒ } d } n t |  ƒ t | ƒ j o t d ‚ n t |  | ƒ } t | | ƒ } t |  | ƒ } t | | ƒ } |  i | } t | d ƒ }
 |  | i d ƒ } t i | t i i | | | ƒ t i i | | ƒ d |
 ƒ }	 t i |	 d ƒ } t i | d |	 ƒ }	 t i i | | ƒ |	 } t i | d | ƒ } t d |
 d t |
 ƒ |
 | | ƒ } t | ƒ t i j o t i  | | i ƒ } n t | ƒ d j o | d } n | d j oœ d } t# | | | | | | t i& i t i |  ƒ ƒ t i' i t i |  ƒ ƒ | | | | t i& i t i | ƒ ƒ t i' i t i | ƒ ƒ | | | ƒ d	 Sn | | f Sd	 S(
   s  
Calculates the t-obtained T-test on TWO RELATED samples of scores, a
and b.  From Numerical Recipies, p.483.  If printit=1, results are
printed to the screen.  If printit='filename', the results are output
to 'filename' using the given writemode (default=append).  Dimension
can equal None (ravel array first), or an integer (the dimension over
which to operate on a and b).

Usage:   attest_rel(a,b,dimension=None,printit=0,
                    name1='Samp1',name2='Samp2',writemode='a')
Returns: t-value, two-tailed p-value
i    s   Unequal length arrays.i   s   di   f1.0f0.5s   Related samples T-test.N()   s	   dimensions   Nones   Ns   ravels   as   bs   lens
   ValueErrors   ameans   x1s   x2s   avars   v1s   v2s   shapes   ns   floats   dfs   astypes   ds   sqrts   adds   reduces   denoms   equals   zerodivproblems   wheres   ts   abetais   probss   types	   ArrayTypes   reshapes   printits   statnames   outputpairedstatss	   writemodes   name1s   minimums   maximums   name2(   s   as   bs	   dimensions   printits   name1s   name2s	   writemodes   statnames   probss   denoms   dfs   v1s   v2s   x2s   zerodivproblems   x1s   ds   ns   t(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   attest_rel°  sD     
A(	$$c         C   sš   t  |  ƒ } | t j o6 t i t |  ƒ t | ƒ g t  |  ƒ t i	 ƒ } n | i
 t i	 ƒ } t i i |  | d | ƒ } | t | | d ƒ f Sd S(   sF  
Calculates a one-way chi square for array of observed frequencies and returns
the result.  If no expected frequencies are given, the total N is assumed to
be equally distributed across all groups.

Usage:   achisquare(f_obs, f_exp=None)   f_obs = array of observed cell freq.
Returns: chisquare-statistic, associated p-value
i   i   N(   s   lens   f_obss   ks   f_exps   Nones   Ns   arrays   sums   floats   Floats   astypes   adds   reduces   chisqs	   chisqprob(   s   f_obss   f_exps   ks   chisq(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   achisquareâ  s     6c         C   s§  d } d }	 d } d } |  i d } | i d } | d } | d } t i |  i d t i ƒ }
 t i |  d ƒ }  t i | d ƒ } x¶ | | j  o
 |	 | j  o› |  | } | |	 } | | j o | t | ƒ } | d } n | | j o |	 t | ƒ } |	 d }	 n | | } t | ƒ t |
 ƒ j o
 | }
 q‰ q‰ WyJ t i | | t | | ƒ ƒ } t | d d | t i |
 ƒ ƒ } Wn d } n X|
 | f Sd S(   sû   
Computes the Kolmogorov-Smirnof statistic on 2 samples.  Modified from
Numerical Recipies in C, page 493.  Returns KS D-value, prob.  Not ufunc-
like.

Usage:   aks_2samp(data1,data2)  where data1 and data2 are 1D arrays
Returns: KS D-value, p-value
i    f0.0i   f0.12f0.11f1.0N(   s   j1s   j2s   fn1s   fn2s   data1s   shapes   n1s   data2s   n2s   en1s   en2s   Ns   zeross   Floats   ds   sorts   d1s   d2s   floats   dts   abss   maths   sqrts   ens   aksprobs   fabss   prob(   s   data1s   data2s   probs   ens   fn2s   fn1s   d1s   d2s   j1s   j2s   ds   en1s   en2s   n1s   n2s   dt(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   aks_2sampô  s>     

 


!)
c         C   s$  t  |  ƒ }
 t  | ƒ } t t i |  | f ƒ ƒ } | d |
 !}	 | |
 } |
 | |
 |
 d d t |	 ƒ } |
 | | } t | | ƒ } t | | ƒ } t i t | ƒ ƒ } | d j o t d ‚ n t i | |
 | |
 | d d ƒ } t | |
 | d | ƒ } | d t | ƒ f Sd S(   s©  
Calculates a Mann-Whitney U statistic on the provided scores and
returns the result.  Use only when the n in each condition is < 20 and
you have 2 independent samples of ranks.  REMEMBER: Mann-Whitney U is
significant if the u-obtained is LESS THAN or equal to the critical
value of U.

Usage:   amannwhitneyu(x,y)     where x,y are arrays of values for 2 conditions
Returns: u-statistic, one-tailed p-value (i.e., p(z(U)))
i    i   f2.0s*   All numbers are identical in amannwhitneyuf12.0f1.0N(   s   lens   xs   n1s   ys   n2s   rankdatas   Ns   concatenates   rankeds   rankxs   rankys   sums   u1s   u2s   maxs   bigus   mins   smallus   maths   sqrts
   tiecorrects   Ts
   ValueErrors   sds   abss   zs   zprob(   s   xs   ys   u2s   u1s   bigus   smallus   rankys   rankeds   Ts   rankxs   n1s   n2s   zs   sd(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   amannwhitneyu  s     
 
$'c         C   sõ   t  t i |  ƒ ƒ \ } } t | ƒ } d } d } x› | | d j  o‰ | | | | d j ob d } xC | | d j  o | | | | d j o | d } | d } qi W| | d | } n | d } q6 W| t | d | ƒ } d | Sd S(   s  
Tie-corrector for ties in Mann Whitney U and Kruskal Wallis H tests.
See Siegel, S. (1956) Nonparametric Statistics for the Behavioral
Sciences.  New York: McGraw-Hill.  Code adapted from |Stat rankind.c
code.

Usage:   atiecorrect(rankvals)
Returns: T correction factor for U or H
f0.0i    i   i   f1.0N(   s
   ashellsorts   Ns   arrays   rankvalss   sorteds   posns   lens   ns   Ts   is   ntiess   float(   s   rankvalss   posns   ntiess   Ts   is   ns   sorted(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   atiecorrect8  s"    	   *
c   
      C   sÂ   t  |  ƒ } t  | ƒ } t i |  | f ƒ }	 t |	 ƒ } | |  }  | | } t
 |  ƒ } | | | d d } | | t i | | | | d d ƒ } d d t t | ƒ ƒ } | | f Sd S(   sÉ   
Calculates the rank sums statistic on the provided scores and returns
the result.

Usage:   aranksums(x,y)     where x,y are arrays of values for 2 conditions
Returns: z-statistic, two-tailed p-value
i   f2.0f12.0i   f1.0N(   s   lens   xs   n1s   ys   n2s   Ns   concatenates   alldatas	   arankdatas   rankeds   sums   ss   expecteds   maths   sqrts   zs   zprobs   abss   prob(
   s   xs   ys   probs   expecteds   ss   rankeds   n1s   n2s   zs   alldata(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   aranksumsR  s     

+c         C   sj  t  |  ƒ t  | ƒ j o t d ‚ n |  | } t i t i | d ƒ | ƒ } t  | ƒ } t	 | ƒ }
 t |
 ƒ }	 d } d } xK t t  |
 ƒ ƒ D]7 } | | d j  o | |	 | } q‘ | |	 | } q‘ Wt | | ƒ } | | d d } t i | | d d | d d ƒ } t i | | ƒ | } t i | | ƒ | } d	 d t t	 | ƒ ƒ } | | f Sd
 S(   sà   
Calculates the Wilcoxon T-test for related samples and returns the
result.  A non-parametric T-test.

Usage:   awilcoxont(x,y)     where x,y are equal-length arrays for 2 conditions
Returns: t-statistic, two-tailed p-value
s#   Unequal N in awilcoxont.  Aborting.i    f0.0i   f0.25f2.0f1.0f24.0i   N(   s   lens   xs   ys
   ValueErrors   ds   Ns   compresss	   not_equals   counts   abss   absds	   arankdatas	   absrankeds   r_pluss   r_minuss   ranges   is   mins   wts   mns   maths   sqrts   ses   fabss   zs   zprobs   prob(   s   xs   ys   probs   wts   counts   r_pluss   ds   is   mns	   absrankeds   absds   zs   r_minuss   se(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   awilcoxontg  s,     
 'c          G   s¿  t  |  ƒ d j p
 t d ‚ t |  ƒ }  d g t  |  ƒ } t t  |  ƒ } g  } x. t t  |  ƒ ƒ D] } | |  | i	 ƒ  } qd Wt
 | ƒ } t | ƒ } x= t t  |  ƒ ƒ D]) } | d | | !|  | <| d | | 5q­ Wg  }
 xQ t t  |  ƒ ƒ D]= } |
 i t |  | ƒ d ƒ |
 | t | | ƒ |
 | <qó Wt |
 ƒ } t | ƒ }	 d |	 |	 d | d |	 d } t  |  ƒ d } | d j o t d ‚ n | t | ƒ } | t | | ƒ f Sd S(	   so  
The Kruskal-Wallis H-test is a non-parametric ANOVA for 3 or more
groups, requiring at least 5 subjects in each group.  This function
calculates the Kruskal-Wallis H and associated p-value for 3 or more
independent samples.

Usage:   akruskalwallish(*args)     args are separate arrays for 3+ conditions
Returns: H-statistic (corrected for ties), associated p-value
i   s1   Need at least 3 groups in stats.akruskalwallish()i    i   f12.0i   s,   All numbers are identical in akruskalwallishN(   s   lens   argss   AssertionErrors   lists   ns   maps   alls   ranges   is   tolists   rankdatas   rankeds
   tiecorrects   Ts   rsumss   appends   sums   floats   ssbns   totalns   hs   dfs
   ValueErrors	   chisqprob(   s   argss   alls   ssbns   is   hs   ns   dfs   rankeds   Ts   totalns   rsums(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   akruskalwallish†  s8    	     "c          G   sé   t  |  ƒ } | d j  o t d ‚ n t  |  d ƒ } t t i |  ƒ } | i	 t
 i ƒ } x. t t  | ƒ ƒ D] } t | | ƒ | | <qm Wt t |  d ƒ d ƒ } d | | | d | d | | d } | t | | d ƒ f Sd S(   sö  
Friedman Chi-Square is a non-parametric, one-way within-subjects
ANOVA.  This function calculates the Friedman Chi-square test for
repeated measures and returns the result, along with the associated
probability value.  It assumes 3 or more repeated measures.  Only 3
levels requires a minimum of 10 subjects in the study.  Four levels
requires 5 subjects per level(??).

Usage:   afriedmanchisquare(*args)   args are separate arrays for 2+ conditions
Returns: chi-square statistic, associated p-value
i   s5   
Less than 3 levels.  Friedman test not appropriate.
i    i   i   f12.0N(   s   lens   argss   ks
   ValueErrors   ns   applys   pstats   aabuts   datas   astypes   Ns   Floats   ranges   is	   arankdatas   asums   ssbns   chisqs	   chisqprob(   s   argss   is   ks   chisqs   ns   datas   ssbn(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   afriedmanchisquareª  s      *c         C   s¤  d } d „  } t |  ƒ t i j o
 d } n d } t i |  g ƒ }  | d j  o t i	 |  i
 t i ƒ Sn t i |  i
 t i ƒ } t i t i |  d ƒ d | ƒ } d |  }
 | d j o | |
 ƒ } n | d d j o d } | d } | d } n+ d } d t t i |  ƒ ƒ } | d } | d j ogd | d }  | o t i	 | i
 t i ƒ } n d t i	 | i
 t i ƒ } | o t i | i
 t i ƒ } n2 t i t i t i ƒ ƒ t i	 | i
 t i ƒ } t i |
 ƒ } t i | i
 ƒ } t i |
 | ƒ } d	 t i	 | i
 t i ƒ } t i! i" t i | i
 ƒ ƒ }	 x¥ t$ | ƒ |	 j o‘ t i | ƒ | } | | | | |
 | ƒ } | d } t i | |  ƒ } t i | t i& | d ƒ | | | ƒ } t i' | | d d ƒ } qFW| o4 t i	 | i
 t i ƒ } t i	 | i
 t i ƒ } nV d t i	 | i
 t i ƒ } d t i t i ƒ t i |
 ƒ t i	 | i
 t i ƒ } d
 } t i | i
 ƒ } d	 t i	 | i
 t i ƒ } x¦ t$ | ƒ |	 j o’ | |
 | i) t i ƒ } | | } | d } t i | |  ƒ } t i | t i& | d ƒ d | | | | | ƒ } t i' | | d d ƒ } q²Wt i t i& | d ƒ d t i t i |
 | ƒ | | ƒ ƒ } | Sn | Sd S(   s  
Returns the (1-tail) probability value associated with the provided chi-square
value and df.  Heavily modified from chisq.c in Gary Perlman's |Stat.  Can
handle multiple dimensions.

Usage:   achisqprob(chisq,df)    chisq=chisquare stat., df=degrees of freedom
f200.0c         C   s:   d } t i t i |  | ƒ | |  ƒ } t i | ƒ Sd  S(   Nf200.0(   s   BIGs   Ns   wheres   lesss   xs	   exponentss   exp(   s   xs	   exponentss   BIG(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   exÐ  s    #i   i    f1.0f0.5i   f2.0iÿÿÿÿf0.0N(*   s   BIGs   exs   types   chisqs   Ns	   ArrayTypes	   arrayflags   arrays   dfs   oness   shapes   floats   zeross   Floats   probss   wheres
   less_equals   as   ys   evens   ss   s2s   azprobs   sqrts   zs   es   logs   pis   cs   masks   greaters   a_bigs   a_big_frozens   multiplys   reduces   totalelementss   asums   newmasks   equals   clips   a_notbig_frozens   astype(   s   chisqs   dfs   exs   probss   evens   BIGs   a_notbig_frozens	   arrayflags   newmasks   totalelementss   as   cs   a_bigs   es   masks   a_big_frozens   ss   s2s   ys   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   achisqprobÇ  s~     	
!


1 
)9 

!$c         C   s«   t  |  ƒ } d d d | } | t i | | d | d | d | d | d | d | d	 | d
 | d | d ƒ } t i t i |  d ƒ | d | ƒ Sd S(   s¼   
Returns the complementary error function erfc(x) with fractional error
everywhere less than 1.2e-7.  Adapted from Numerical Recipies.  Can
handle multiple dimensions.

Usage:   aerfcc(x)
f1.0f0.5f1.2655122299999999f
1.00002368f
0.37409196f0.096784179999999997f0.18628806000000001f0.27886807000000002f-1.13520398f1.4885158700000001f0.82215223000000004f0.17087277000000001i    f2.0N(	   s   abss   xs   zs   ts   Ns   exps   anss   wheres   greater_equal(   s   xs   ts   anss   z(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   aerfcc  s
     fc         C   sÖ   d „  } d „  } d } t i |  i t i ƒ } d t i	 |  ƒ } t i t i | d ƒ | | | ƒ | | d ƒ ƒ } t i t i | | d ƒ d | ƒ } t i t i |  d ƒ | d d d | d ƒ } | Sd	 S(
   sa  
Returns the area under the normal curve 'to the left of' the given z value.
Thus, 
    for z<0, zprob(z) = 1-tail probability
    for z>0, 1.0-zprob(z) = 1-tail probability
    for any z, 2.0*(1.0-zprob(abs(z))) = 2-tail probability
Adapted from z.c in Gary Perlman's |Stat.  Can handle multiple dimensions.

Usage:   azprob(z)    where z is a z-value
c         C   s   d |  d |  d |  d |  d |  d |  d |  d |  d	 |  d
 |  d |  d |  d |  d |  d } | Sd  S(   Nf4.5255659000000002e-05f0.00015252929f1.9538131999999999e-05f0.00067690498600000005f0.0013906042840000001f0.00079462081999999998f0.0020342548740000001f0.0065497912140000001f0.010557625006f0.011630447319f0.0092794533410000008f0.0053535791080000002f0.0021412687410000001f0.00053531084899999996f0.99993665752399996(   s   ys   x(   s   ys   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   yfunc2  s    wc         C   s_   d |  d |  d |  d |  d |  d |  d |  d |  d	 t i |  ƒ d
 } | Sd  S(   Nf0.000124818987f0.001075204047f0.0051987750189999996f0.019198292004f0.059054035642f0.15196875136400001f0.31915293269400002f0.53192300729999997f0.79788456059299995f2.0(   s   ws   Ns   sqrts   x(   s   ws   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   wfunc=  s    Wf6.0f0.5f1.0f2.0i    i   N(   s   yfuncs   wfuncs   Z_MAXs   Ns   zeross   zs   shapes   Floats   xs   fabss   ys   wheres   lesss   greaters   prob(   s   zs   wfuncs   Z_MAXs   yfuncs   ys   xs   prob(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   azprob'  s    
 		5%1c         C   s€  t  |  ƒ t i j o8 d t i |  i t i ƒ } |  i t i ƒ }  d } n% t i
 d ƒ } t i
 |  t i ƒ }  t i |  i ƒ } d t i |  i t i ƒ } t i |  i t i ƒ } t i |  i t i ƒ }
 t i
 d |  |  t i ƒ } t i i t i
 | i ƒ ƒ } x!t d d ƒ D]} t | ƒ | j o Pn | | | } t i | d ƒ }	 t i |	 d | ƒ } | |	 } | t i | ƒ } | | } t i t i t | ƒ d	 |
 ƒ t i t | ƒ d
 | ƒ d d ƒ } t i | t i! | d ƒ | | ƒ } t i" | | d d ƒ } | } t | ƒ }
 qW| o# t i t i! | d ƒ d | ƒ Sn$ t i t i! | d ƒ d | ƒ d Sd S(   s¤   
Returns the probability value for a K-S statistic computed via ks_2samp.
Adapted from Numerical Recipies.  Can handle multiple dimensions.

Usage:   aksprob(alam)
iÿÿÿÿi   f-1.0f2.0f-2.0iÉ   iýÿÿi    f0.001f1e-08f1.0N(#   s   types   alams   Ns	   ArrayTypes   oness   shapes   Float64s   frozens   astypes	   arrayflags   arrays   zeross   masks   Floats   facs   sums   termbfs   a2s   multiplys   reduces   totalelementss   ranges   js   asums	   exponentss   lesss   overflowmasks   wheres   exps   terms
   less_equals   abss   newmasks   equals   clip(   s   alams   newmasks   terms   totalelementss   frozens   masks   sums   js	   arrayflags   overflowmasks   termbfs   a2s   facs	   exponents(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   aksprobN  s@     
 

9%#c         C   sp   t  | ƒ t i j o, t d | d |  | d | |  | ƒ Sn+ t d | d |  | t | |  | ƒ ƒ Sd S(   s  
Returns the 1-tailed significance level (p-value) of an F statistic
given the degrees of freedom for the numerator (dfR-dfF) and the degrees
of freedom for the denominator (dfF).  Can handle multiple dims for F.

Usage:   afprob(dfnum, dfden, F)   where usually dfnum=dfbn, dfden=dfwn
f0.5f1.0N(   s   types   Fs   Ns	   ArrayTypes   abetais   dfdens   dfnums   float(   s   dfnums   dfdens   F(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   afprobw  s     ,c         C   s’  d } d } d } t | ƒ t i j o  t i | i t i	 ƒ d } n+ d } t i d g ƒ } t i | g ƒ } t i | i ƒ } d } }
 } |  | } |  d } |  d } d | | | } xut | d ƒ D]c} t i t i t i | d ƒ ƒ ƒ d j o Pn t | d ƒ } | | } | | | | | | |  | } |
 | | } | | | } |  | | | | | | |  | } | | |
 } | | | } |
 d } | | } | | } | | }
 d } t i% t& |
 | ƒ | t& |
 ƒ ƒ } t i( | t i | d ƒ |
 | ƒ } t i) | | d d ƒ } qÓ Wt* t i | d ƒ ƒ }	 |	 d j o | o d G|	 Gd GHn | o | Sn	 | d Sd	 S(
   sÃ   
Evaluates the continued fraction form of the incomplete Beta function,
betai.  (Adapted from: Numerical Recipies in C.)  Can handle multiple
dimensions for x.

Usage:   abetacf(a,b,x,verbose=1)
iÈ   f2.9999999999999999e-07i   iÿÿÿÿi    f1.0s1   a or b too big, or ITMAX too small in Betacf for s	    elementsN(-   s   ITMAXs   EPSs	   arrayflags   types   xs   Ns	   ArrayTypes   oness   shapes   Floats   frozens   arrays   zeross   masks   bms   azs   ams   as   bs   qabs   qaps   qams   bzs   ranges   is   sums   ravels   equals   floats   ems   tems   ds   aps   bps   apps   bpps   aolds   lesss   abss   newmasks   wheres   clips   asums
   noconverges   verbose(   s   as   bs   xs   verboses   ems   apps   ams   EPSs   aps
   noconverges   azs   tems   qaps   bpps   qabs   qams   ITMAXs   bms	   arrayflags   newmasks   aolds   bps   bzs   ds   frozens   masks   i(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   abetacf…  sR      


 +
"'



&%c         C   s¡   d d d d d d g } |  d } | d } | | d	 t i | ƒ } d } x6 t t | ƒ ƒ D]" } | d
 } | | | | } qa W| t i d | ƒ Sd S(   sß   
Returns the gamma function of xx.
    Gamma(z) = Integral(0,infinity) of t^(z-1)exp(-t) dt.
Adapted from: Numerical Recipies in C.  Can handle multiple dims ... but
probably doesn't normally have to.

Usage:   agammln(xx)
f76.180091730000001f-86.505320330000004f24.014098220000001f-1.231739516f0.00120858003f5.3638199999999999e-06f1.0f5.5f0.5i   f2.5066282746500002N(
   s   coeffs   xxs   xs   tmps   Ns   logs   sers   ranges   lens   j(   s   xxs   tmps   sers   coeffs   js   x(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   agammln¹  s     

 
c      
   C   sQ  d } t |  ƒ t i j o@ t t i | d ƒ t i | d ƒ ƒ d j o t	 d ‚ q\ n t i
 t i | d ƒ | | ƒ } t i
 t i | d ƒ d | | ƒ } t i
 t i | d ƒ t i | d ƒ d d ƒ } t |  | ƒ t |  ƒ t | ƒ |  t i | ƒ | t i d | ƒ } t i
 t i | d ƒ d | ƒ } t i | ƒ } t | ƒ t i j ow t i
 t i | |  d |  | d ƒ | t |  | | | ƒ t |  ƒ d | t | |  d | | ƒ t | ƒ ƒ } np | |  d |  | d j  o' | t |  | | | ƒ t |  ƒ } n, d | t | |  d | | ƒ t | ƒ } | Sd	 S(
   s‚  
Returns the incomplete beta function:

    I-sub-x(a,b) = 1/B(a,b)*(Integral(0,x) of t^(a-1)(1-t)^(b-1) dt)

where a,b>0 and B(a,b) = G(a)*G(b)/(G(a+b)) where G(a) is the gamma
function of a.  The continued fraction formulation is implemented
here, using the betacf function.  (Adapted from: Numerical Recipies in
C.)  Can handle multiple dimensions.

Usage:   abetai(a,b,x,verbose=1)
f1.0000000000000001e-15i    i   s   Bad x in abetaif1.0iÿÿÿÿiýÿÿf2.0N(   s   TINYs   types   as   Ns	   ArrayTypes   asums   lesss   xs   greaters
   ValueErrors   wheres   equals   bts   gammlns   bs   logs	   exponentss   exps   abetacfs   verboses   floats   ans(   s   as   bs   xs   verboses   TINYs   bts   anss	   exponents(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   abetaiÎ  s&     /!%1J!% 2'+c         C   sÏ  t  | ƒ t  |  ƒ j o d GHd Sn t  | ƒ }	 t i | ƒ }
 t i |	 t  |
 ƒ f ƒ } x@ t
 t  |
 ƒ ƒ D], } t i | |
 | ƒ | d d … | f <qo Wt i t i t i t i t i | ƒ | ƒ ƒ t i | ƒ ƒ |  ƒ } |  t i | | ƒ } d |	 t  |
 ƒ t i t i | ƒ | ƒ } t  |
 ƒ d j o‘ t i d d g ƒ } |	 d } t d t | d ƒ ƒ } t i | | ƒ t i | | ƒ } t d | d t | ƒ | | | ƒ } | | f Sn d S(	   s=  
Calculates a linear model fit ... anova/ancova/lin-regress/t-test/etc. Taken
from:
    Peterson et al. Statistical limitations in functional neuroimaging
    I. Non-inferential methods and statistical models.  Phil Trans Royal Soc
    Lond B 354: 1239-1260.

Usage:   aglm(data,para)
Returns: statistic, p-value ???
s)   data and para must be same length in aglmNf1.0i   i   iÿÿÿÿi    f0.5(   s   lens   paras   datas   ns   pstats   auniques   ps   Ns   zeross   xs   ranges   ls   equals   dots   LAs   inverses	   transposes   bs   diffss   s_sqs   arrays   cs   dfs   asums   facts   sqrts   ts   abetais   floats   probs(   s   datas   paras   probss   dfs   s_sqs   diffss   cs   bs   ls   ns   ps   ts   xs   fact(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   aglmû  s,    
  *-	-
#(c          G   su  t  |  ƒ } d g | }	 d g | } d g | } g  } t t i	 |  ƒ } t t | ƒ }	 t t | ƒ } t t  |  ƒ } t i |  ƒ } t  | ƒ } t | ƒ t | ƒ t | ƒ } d } x7 |  D]/ } | t t i	 | ƒ ƒ t t  | ƒ ƒ } qÀ W| t | ƒ t | ƒ } | | } | d } | | }
 | t | ƒ } | t |
 ƒ } | | } t | |
 | ƒ } | | f Sd S(   s
  
Performs a 1-way ANOVA, returning an F-value and probability given
any number of groups.  From Heiman, pp.394-7.

Usage:   aF_oneway (*args)    where *args is 2 or more arrays, one per
                                  treatment group
Returns: f-value, probability
i    i   N(   s   lens   argss   nas   meanss   varss   nss   alldatas   maps   Ns   arrays   tmps   ameans   avars   concatenates   bigns   asss   asquare_of_sumss   floats   sstots   ssbns   as   sswns   dfbns   dfwns   msbs   msws   fs   fprobs   prob(   s   argss   varss   tmps   sstots   msws   nas   bigns   nss   msbs   meanss   dfwns   dfbns   as   fs   probs   sswns   alldatas   ssbn(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   aF_oneway  s4       -



c         C   s(   |  | t | | ƒ | t | ƒ Sd S(   sF  
Returns an F-statistic given the following:
        ER  = error associated with the null hypothesis (the Restricted model)
        EF  = error associated with the alternate hypothesis (the Full model)
        dfR = degrees of freedom the Restricted model
        dfF = degrees of freedom associated with the Restricted model
N(   s   ERs   EFs   floats   dfRs   dfF(   s   ERs   EFs   dfRs   dfF(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   aF_value@  s     c   	      C   s/  t  |  d ƒ }  t  | d ƒ } t  |  d ƒ } t  | d ƒ } t  | d ƒ } t  | d ƒ } d } | d j  o
 d } n/ | d j  o
 d } n | d j  o
 d } n d	 d
 d d d d g g } | |  | t  |  t	 | ƒ d ƒ | | | g | | t  | t	 | ƒ d ƒ d d d g g } t i | ƒ d  Sd  S(   Ni   s    f0.001s     ***f0.01s     **f0.050000000000000003s     *s   EF/ERs   DFs   Mean Squares   F-values   prob(   s   rounds   Enums   Edens   dfnums   dfdens   fs   probs   suffixs   titles   floats   lofls   pstats   printcc(	   s   Enums   Edens   dfnums   dfdens   fs   probs   suffixs   lofls   title(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   outputfstatsK  s"     
 
 
Zc         C   s¯   t  |  ƒ t t g j o t i |  g g ƒ }  n t  | ƒ t t g j o t i | g g ƒ } n t i |  ƒ t i | ƒ t	 | ƒ } t i | ƒ t	 | ƒ } | | Sd S(   s†  
Returns an F-statistic given the following:
        ER  = error associated with the null hypothesis (the Restricted model)
        EF  = error associated with the alternate hypothesis (the Full model)
        dfR = degrees of freedom the Restricted model
        dfF = degrees of freedom associated with the Restricted model
where ER and EF are matrices from a multivariate F calculation.
N(   s   types   ERs   IntTypes	   FloatTypes   Ns   arrays   EFs   LAs   determinants   floats   dfnums   n_ums   dfdens   d_en(   s   ERs   EFs   dfnums   dfdens   n_ums   d_en(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   F_value_multivariate]  s     &c         C   s§   t  i |  ƒ }  t |  ƒ t d ƒ j p t |  ƒ t d ƒ j o, |  |  t  i |  d ƒ t  i |  d ƒ Sn7 t  i t  i |  ƒ ƒ t  i |  d ƒ t  i |  d ƒ Sd S(   sR   
Usage:   asign(a)
Returns: array shape of a, with -1 where a<0 and +1 where a>=0
f1.3999999999999999i   i    N(   s   Ns   asarrays   as   types   lesss   greaters   zeross   shape(   s   a(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asigns  s
     2,c         C   s„  t  |  ƒ t i j o |  i ƒ  d d d g j o |  i t i ƒ }  n | t j o t i	 t i
 |  ƒ ƒ } nt  | ƒ t t g j oU t i i |  | ƒ } | d j o/ t |  i ƒ } d | | <t i | | ƒ } q|nž t | ƒ } | i ƒ  | i ƒ  |  d } x# | D] } t i i | | ƒ } qW| d j o@ t |  i ƒ } x | D] } d | | <qRWt i | | ƒ } n | Sd S(   s3  
An alternative to the Numeric.add.reduce function, which allows one to
(1) collapse over multiple dimensions at once, and/or (2) to retain
all dimensions in the original array (squashing one down to size.
Dimension can equal None (ravel array first), an integer (the
dimension over which to operate), or a sequence (operate over multiple
dimensions).  If keepdims=1, the resulting array will have as many
dimensions as the input array.

Usage:   asum(a, dimension=None, keepdims=0)
Returns: array summed along 'dimension'(s), same _number_ of dims if keepdims=1
s   ls   ss   bi   f1.0N(   s   types   as   Ns	   ArrayTypes   typecodes   astypes   Floats	   dimensions   Nones   sums   ravels   ss   IntTypes	   FloatTypes   adds   reduces   keepdimss   lists   shapes   shps   reshapes   dimss   sorts   reverses   dim(   s   as	   dimensions   keepdimss   shps   dimss   ss   dim(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asum  s2     2



  c         C   sª   | t j o t i |  ƒ }  d } n t | ƒ t t t i g j oN t	 | ƒ } | i
 ƒ  | i ƒ  x# | D] } t i i |  | ƒ }  ql W|  Sn t i i |  | ƒ Sd S(   s7  
Returns an array consisting of the cumulative sum of the items in the
passed array.  Dimension can equal None (ravel array first), an
integer (the dimension over which to operate), or a sequence (operate
over multiple dimensions, but this last one just barely makes sense).

Usage:   acumsum(a,dimension=None)
i    N(   s	   dimensions   Nones   Ns   ravels   as   types   ListTypes	   TupleTypes	   ArrayTypes   lists   sorts   reverses   ds   adds
   accumulate(   s   as	   dimensions   d(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   acumsum¥  s     


 c         C   s>   | t j o t i |  ƒ }  d } n t |  |  | | ƒ Sd S(   sÑ  
Squares each value in the passed array, adds these squares & returns
the result.  Unfortunate function name. :-) Defaults to ALL values in
the array.  Dimension can equal None (ravel array first), an integer
(the dimension over which to operate), or a sequence (operate over
multiple dimensions).  Set keepdims=1 to maintain the original number
of dimensions.

Usage:   ass(inarray, dimension=None, keepdims=0)
Returns: sum-along-'dimension' for (inarray*inarray)
i    N(   s	   dimensions   Nones   Ns   ravels   inarrays   asums   keepdims(   s   inarrays	   dimensions   keepdims(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ass¼  s
     
c         C   sM   | t j o( t i |  ƒ }  t i | ƒ } d } n t |  | | | ƒ Sd S(   s‹  
Multiplies elements in array1 and array2, element by element, and
returns the sum (along 'dimension') of all resulting multiplications.
Dimension can equal None (ravel array first), an integer (the
dimension over which to operate), or a sequence (operate over multiple
dimensions).  A trivial function, but included for completeness.

Usage:   asummult(array1,array2,dimension=None,keepdims=0)
i    N(   s	   dimensions   Nones   Ns   ravels   array1s   array2s   asums   keepdims(   s   array1s   array2s	   dimensions   keepdims(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asummultÎ  s    	 
c         C   sx   | t j o t i |  ƒ }  d } n t |  | | ƒ } t | ƒ t i	 j o | i
 t i ƒ | Sn t | ƒ | Sd S(   s¶  
Adds the values in the passed array, squares that sum, and returns the
result.  Dimension can equal None (ravel array first), an integer (the
dimension over which to operate), or a sequence (operate over multiple
dimensions).  If keepdims=1, the returned array will have the same
NUMBER of dimensions as the original.

Usage:   asquare_of_sums(inarray, dimension=None, keepdims=0)
Returns: the square of the sum over dim(s) in dimension
i    N(   s	   dimensions   Nones   Ns   ravels   inarrays   asums   keepdimss   ss   types	   ArrayTypes   astypes   Floats   float(   s   inarrays	   dimensions   keepdimss   s(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asquare_of_sumsß  s    
 
c         C   sB   | t j o t i |  ƒ } d } n t |  | d | | ƒ Sd S(   sŒ  
Takes pairwise differences of the values in arrays a and b, squares
these differences, and returns the sum of these squares.  Dimension
can equal None (ravel array first), an integer (the dimension over
which to operate), or a sequence (operate over multiple dimensions).
keepdims=1 means the return shape = len(a.shape) = len(b.shape)

Usage:   asumdiffsquared(a,b)
Returns: sum[ravel(a-b)**2]
i    i   N(	   s	   dimensions   Nones   Ns   ravels   as   inarrays   asums   bs   keepdims(   s   as   bs	   dimensions   keepdimss   inarray(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   asumdiffsquaredô  s
    
 
c   	      C   s  t  |  ƒ } |  d } t | ƒ } | d } xÚ | d j oÌ x» t | | ƒ D]ª } x¡ t | | d | ƒ D]ˆ } x | d j o | | | | | j oX | | } | | | | | <| | | | <| | } | | | | | <| | | | <qs Wqj WqL W| d } q/ W| | f Sd S(   sŠ   
Shellsort algorithm.  Sorts a 1D-array.

Usage:   ashellsort(inarray)
Returns: sorted-inarray, sorting-index-vector (for original array)
f1.0i   i    iÿÿÿÿN(   s   lens   inarrays   ns   svecs   ranges   ivecs   gaps   is   js   temps   itemp(	   s   inarrays   ivecs   temps   is   itemps   js   ns   gaps   svec(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys
   ashellsort  s*     

    &

c   
      C   sô   t  |  ƒ } t |  ƒ \ }	 } d } d } t i	 | t i
 ƒ } xª t | ƒ D]œ } | | } | d } | | d j p |	 | |	 | d j oX | t | ƒ d } x1 t | | d | d ƒ D] } | | | | <qÀ Wd } d } qL qL W| Sd S(   sß   
Ranks the data in inarray, dealing with ties appropritely.  Assumes
a 1D inarray.  Adapted from Gary Perlman's |Stat ranksort.

Usage:   arankdata(inarray)
Returns: array of length equal to inarray, containing rank scores
i    i   N(   s   lens   inarrays   ns
   ashellsorts   svecs   ivecs   sumrankss   dupcounts   Ns   zeross   Floats   newarrays   ranges   is   floats   averanks   j(
   s   inarrays   dupcounts   ivecs   averanks   is   ns   js   newarrays   sumrankss   svec(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys	   arankdata  s$      

* c         C   sµ   t  |  d ƒ d } d g | } x‰ t d | d ƒ D]t } t i |  | t i t i	 |  d ƒ ƒ d ƒ } t  t i t i	 | d ƒ ƒ ƒ t  | ƒ j  o d | | d <q5 q5 W| Sd S(   sÕ   
Returns a binary vector, 1=within-subject factor, 0=between.  Input
equals the entire data array (i.e., column 1=random factor, last
column = measured values.

Usage:   afindwithin(data)     data in |Stat format
i    i   i   N(   s   lens   datas   numfacts	   withinvecs   ranges   cols   pstats   linexands   uniques   colexs   rows(   s   datas   rowss	   withinvecs   numfacts   col(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   afindwithin8  s      ..(ô   s   __doc__s   pstats   maths   strings   copys   typess   __version__s   Dispatchs   lgeometricmeans   lharmonicmeans   lmeans   lmedians   lmedianscores   lmodes   lmoments
   lvariations   lskews	   lkurtosiss	   ldescribes	   litemfreqs   lscoreatpercentiles   Nones   lpercentileofscores
   lhistograms   lcumfreqs   lrelfreqs   lobrientransforms
   lsamplevars   lsamplestdevs   lvars   lstdevs   lsterrs   lsems   lzs   lzss	   ltrimboths   ltrim1s   lpaireds	   lpearsonrs
   lspearmanrs   lpointbiserialrs   lkendalltaus   llinregresss   lttest_1samps
   lttest_inds
   lttest_rels
   lchisquares	   lks_2samps   lmannwhitneyus   ltiecorrects	   lranksumss
   lwilcoxonts   lkruskalwallishs   lfriedmanchisquares
   lchisqprobs   lerfccs   lzprobs   lksprobs   lfprobs   lbetacfs   lgammlns   lbetais	   lF_oneways   lF_values   writeccs   lincrs   lsums   lcumsums   lsss   lsummults   lsumdiffsquareds   lsquare_of_sumss
   lshellsorts	   lrankdatas   outputpairedstatss   lfindwithins   ListTypes	   TupleTypes   geometricmeans   harmonicmeans   means   medians   medianscores   modes   moments	   variations   skews   kurtosiss   describes   itemfreqs   scoreatpercentiles   percentileofscores	   histograms   cumfreqs   relfreqs   obrientransforms	   samplevars   samplestdevs   vars   stdevs   sterrs   sems   zs   zss   trimboths   trim1s   paireds   pearsonrs	   spearmanrs   pointbiserialrs
   kendalltaus
   linregresss   ttest_1samps	   ttest_inds	   ttest_rels	   chisquares   ks_2samps   mannwhitneyus   ranksumss
   tiecorrects	   wilcoxonts   kruskalwallishs   friedmanchisquares   IntTypes	   FloatTypes	   chisqprobs   zprobs   ksprobs   fprobs   betacfs   betais   erfccs   gammlns   F_oneways   F_values   incrs   sums   cumsums   sss   summults   square_of_sumss   sumdiffsquareds	   shellsorts   rankdatas
   findwithins   Numerics   Ns   LinearAlgebras   LAs   ageometricmeans   aharmonicmeans   ameans   amedians   amedianscores   amodes   atmeans   atvars   atmins   atmaxs   atstdevs   atsems   amoments
   avariations   askews	   akurtosiss	   adescribes	   askewtests   akurtosistests   anormaltests	   aitemfreqs   ascoreatpercentiles   apercentileofscores
   ahistograms   acumfreqs   arelfreqs   aobrientransforms
   asamplevars   asamplestdevs   asignaltonoises   avars   astdevs   asterrs   asems   azs   azss   azmaps   arounds
   athresholds	   atrimboths   atrim1s   acovariances   acorrelations   apaireds	   apearsonrs
   aspearmanrs   apointbiserialrs   akendalltaus   alinregresss   attest_1samps
   attest_inds
   attest_rels
   achisquares	   aks_2samps   amannwhitneyus   atiecorrects	   aranksumss
   awilcoxonts   akruskalwallishs   afriedmanchisquares
   achisqprobs   aerfccs   azprobs   aksprobs   afprobs   abetacfs   agammlns   abetais   operators   aglms	   aF_oneways   aF_values   outputfstatss   F_value_multivariates   asigns   asums   acumsums   asss   asummults   asquare_of_sumss   asumdiffsquareds
   ashellsorts	   arankdatas   afindwithins	   ArrayTypes   tmeans   tvars   tstdevs   tsems   skewtests   kurtosistests
   normaltests   signaltonoises	   thresholds   ImportError(ë   s   betacfs   pearsonrs	   ltrimboths   gammlns
   avariations   abetacfs   outputpairedstatss   square_of_sumss   lsss   asignaltonoises   mannwhitneyus	   thresholds   lscoreatpercentiles
   lspearmanrs   lsamplestdevs   lharmonicmeans
   athresholds   lmedians   copys   F_oneways
   ashellsorts   apointbiserialrs   zs   avars	   aranksumss   ltiecorrects   atvars   skewtests   cumfreqs   kurtosistests   lpercentileofscores   lzs   aksprobs   sterrs   strings   atstdevs   lfindwithins   asquare_of_sumss   lzss   azmaps   rankdatas   atiecorrects
   attest_inds   lsumdiffsquareds   asterrs   pstats	   litemfreqs   lsums   lmeans   atmaxs   amodes
   lttest_rels	   ttest_rels   tsems   aobrientransforms   lsems	   lpearsonrs   medians   Dispatchs   tmeans   itemfreqs   lmedianscores   modes	   shellsorts   fprobs   means	   aitemfreqs   amoments   lgammlns   ranksumss   arounds   lfriedmanchisquares   lcumsums   Numerics   sems   amedianscores
   lttest_inds   harmonicmeans   zss	   spearmanrs   akruskalwallishs   cumsums   sums   azss   outputfstatss   sumdiffsquareds
   lvariations   lpaireds   erfccs   lbetais   geometricmeans   maths   asss   apercentileofscores	   adescribes
   ahistograms
   lshellsorts   atmeans   lmoments   lsquare_of_sumss   asumdiffsquareds	   lkurtosiss   akendalltaus   stdevs   atmins
   asamplevars
   tiecorrects   lF_values	   chisqprobs
   lsamplevars	   lks_2samps   summults   ksprobs
   normaltests   lkendalltaus   askews
   lhistograms   lerfccs	   aks_2samps   lrelfreqs   lincrs   lmannwhitneyus   skews	   aF_oneways	   lF_oneways	   ttest_inds   lsummults   amedians
   achisquares   ameans   operators
   attest_rels   aharmonicmeans   anormaltests   akurtosistests   lmodes   lpointbiserialrs   ageometricmeans   trimboths   asigns   LinearAlgebras	   apearsonrs   vars
   kendalltaus   F_values	   ldescribes   azprobs
   awilcoxonts	   samplevars   afprobs	   histograms   lstdevs
   achisqprobs   llinregresss   agammlns   acumsums   kurtosiss   amannwhitneyus   lskews   apaireds   zprobs   lsterrs   tstdevs	   askewtests   astdevs   atrim1s   betais   lobrientransforms   acumfreqs   paireds   lttest_1samps
   aspearmanrs
   lwilcoxonts   ltrim1s   lcumfreqs	   akurtosiss
   linregresss   samplestdevs   ttest_1samps   tvars   signaltonoises	   atrimboths   describes   friedmanchisquares   lgeometricmeans	   chisquares   pointbiserialrs   percentileofscores   incrs   azs   obrientransforms   kruskalwallishs   ks_2samps   lkruskalwallishs	   lranksumss   aF_values   aerfccs   trim1s   __version__s   relfreqs   atsems   medianscores   asummults
   findwithins   lbetacfs   afindwithins	   lrankdatas   attest_1samps   aglms   alinregresss	   variations   acorrelations   writeccs   moments   asems   acovariances   lfprobs	   arankdatas   Ns
   lchisqprobs	   wilcoxonts   asamplestdevs   lksprobs   lzprobs   asums   sss   ascoreatpercentiles   LAs   arelfreqs   scoreatpercentiles   afriedmanchisquares   F_value_multivariates   lvars   abetais
   lchisquare(    (    s5   /afs/cs.wisc.edu/u/n/a/naze/gametheory/sexes/stats.pys   ?œ   sä  <	#					!	
	
	
			'	$		
		
	
					@			 	#	 $	'				!	#		<		*			"			%	(										9	 		+6* &$ 		'	&						@			#	)42	(					$		R		'	)	4	*	"	#				&			