Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Repository for a workshop on Bayesian statistics

1430 views


'j�Z�%�@sdZddlmZmZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlZddlZddlZddlZddlmZddlmZddlmZdd	lmZdd
lmZejd�Zdd
�Zdd�Zdd�Zdd�ZGdd�de �Z!dZ"Gdd�de �Z#Gdd�de#�Z$Gdd�de#�Z%Gdd�de%�Z&dd �Z'dd!d"�Z(dd#d$�Z)dd%d&�Z*dd'd(�Z+dd)d*�Z,dd+d,�Z-d-d.d/�Z.d0d1�Z/Gd2d3�d3�Z0dd4d5�Z1dd6d7�Z2dd8d9�Z3dd:d;�Z4dd<d=�Z5Gd>d?�d?e6�Z7Gd@dA�dAe%�Z8ddBdC�Z9ddDdE�Z:ddFdG�Z;GdHdI�dIe �Z<GdJdK�dKe<�Z=GdLdM�dMe<�Z>GdNdO�dOe<�Z?dPdQdR�Z@dSdT�ZAdUdV�ZBdWdX�ZCdYdZ�ZDd[d\�ZEd]d^�ZFd_d`da�ZGdbdc�ZHddde�ZIdfdg�ZJdhdi�ZKddjdk�ZLddldmdn�ZMdodp�ZNdqdr�ZOdsdtdu�ZPdvdw�ZQdxdy�ZRdzd{d|�ZSd}d~�ZTdd��ZUdzd�d��ZVd�d��ZWd�d�d��ZXd�d��ZYddsd�d��ZZddsd�d��Z[ddsd�d��Z\d�d�d��Z]d�d�d��Z^d�d�d��Z_Gd�d��d��Z`Gd�d��d�e �Zad�d��Zbd�d��Zcdd�d��Zdd�d�d��Zed�d�d��Zfd�d��Zgddd�d��Zhddd�d��Zidd�d��Zjd�d�d��Zkd�d�d��Zld�d�d��Zmd�d��Znddd�d��Zod�d��Zpdsd�d��Zqd�d��Zrd�d��Zsd�d��Ztd�d��Zud�d��Zvd�d��Zwd�d��Zxd�d��Zyd�d��Zzd�d��Z{d�d��Z|d�d��Z}d�d��Z~d�d��Zd�d��Z�Gd�d��d�e �Z�d�d��Z�dd�d��Z�d�d�d��Z�d�d��Z�d�d�d��Z�d�d��Z�d�d��Z�dd�d��Z�Gd�d��d�e �Z�d�d��Z�e�d�kr{e��dS)�z�This file contains code for use with "Think Stats" and
"Think Bayes", both by Allen B. Downey, available from greenteapress.com

Copyright 2014 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
�)�print_function�divisionN)�Counter)�
itemgetter)�stats)�special)�ndimage)�gamma)�open�cCs!tj|�tjj|�dS)zEInitialize the random and np.random generators.

    x: int seed
    N)�random�seed�np)�x�r�)/home/user/BayesMadeSimple/thinkbayes2.py�
RandomSeed2s
rcCs"|dkrtd�S|d|S)aaComputes odds for a given probability.

    Example: p=0.75 means 75 for and 25 against, or 3:1 odds in favor.

    Note: when p=1, the formula for odds divides by zero, which is
    normally undefined.  But I think it is reasonable to define Odds(1)
    to be infinity, so that's what this function does.

    p: float 0-1

    Returns: float odds
    ��inf)�float)�prrr�Odds;s

rcCs||dS)z�Computes the probability corresponding to given odds.

    Example: o=2 means 2:1 odds in favor, or 2/3 probability

    o: float odds, strictly positive

    Returns: float probability
    rr)�orrr�ProbabilityMs	rcCs|||S)z�Computes the probability corresponding to given odds.

    Example: yes=2, no=1 means 2:1 odds in favor, or 2/3 probability.
    
    yes, no: int or float odds in favor
    r)�yes�norrr�Probability2Ysrc@sFeZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�Interpolatorz�Represents a mapping between sorted sequences; performs linear interp.

    Attributes:
        xs: sorted list
        ys: sorted list
    cCs||_||_dS)N)�xs�ys)�selfrrrrr�__init__ks	zInterpolator.__init__cCs|j||j|j�S)z4Looks up x and returns the corresponding value of y.)�_Bisectrr)r rrrr�LookuposzInterpolator.LookupcCs|j||j|j�S)z4Looks up y and returns the corresponding value of x.)r"rr)r �yrrr�ReversesszInterpolator.ReversecCs�||dkr|dS||dkr0|dStj||�}d|||d||||d}||d|d||||d}|S)zHelper function.rrg�?�����r&)�bisect)r rrr�i�fracr$rrrr"ws**zInterpolator._BisectN)�__name__�
__module__�__qualname__�__doc__r!r#r%r"rrrrrcs
r�
_nolegend_c@s�eZdZdZdddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zddd�Zdd�Zddd �Zdd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zdd/d0�Zd1d2�Zd3d4d5�Zd6d7d8�Zd9d:�Zd;d<�Zd=d>�Z d?d@�Z!dAdBdC�Z"dAdDdE�Z#dS)F�_DictWrapperz%An object that contains a dictionary.NcCsB|dk	r|nt|_i|_d|_|dkr=dSt|tttf�rs|dk	rg|n|j|_t|t�r�|jj	|j
��nxt|tttf�r�|jj	|j��nGt|tj
�r�|jj	|j�j��n|jj	t|��t|�dkr>t|t�r>|j�dS)z�Initializes the distribution.

        obj: Hist, Pmf, Cdf, Pdf, dict, pandas Series, list of pairs
        label: string label
        NFr)�
DEFAULT_LABEL�label�d�log�
isinstancer/�Cdf�Pdf�dict�update�items�Items�pandas�SeriesZvalue_counts�	iteritemsr�len�Pmf�	Normalize)r �objr1rrrr!�s 		!z_DictWrapper.__init__cCs
t|�S)N)�id)r rrr�__hash__�sz_DictWrapper.__hash__cCs=|jj}|jtkr2d|t|j�fS|jSdS)Nz%s(%s))�	__class__r*r1r0�strr2)r �clsrrr�__str__�sz_DictWrapper.__str__cCsY|jj}|jtkr2d|t|j�fSd|t|j�t|j�fSdS)Nz%s(%s)z
%s(%s, %s))rDr*r1r0�reprr2)r rFrrr�__repr__�sz_DictWrapper.__repr__cCs1y|j|jkSWntk
r,dSYnXdS)NF)r2�AttributeError)r �otherrrr�__eq__�s
z_DictWrapper.__eq__cCs
t|j�S)N)r>r2)r rrr�__len__�sz_DictWrapper.__len__cCs
t|j�S)N)�iterr2)r rrr�__iter__�sz_DictWrapper.__iter__cCs
t|j�S)zReturns an iterator over keys.)rNr2)r rrr�iterkeys�sz_DictWrapper.iterkeyscCs
||jkS)N)r2)r �valuerrr�__contains__�sz_DictWrapper.__contains__cCs|jj|d�S)Nr)r2�get)r rQrrr�__getitem__�sz_DictWrapper.__getitem__cCs||j|<dS)N)r2)r rQ�probrrr�__setitem__�sz_DictWrapper.__setitem__cCs|j|=dS)N)r2)r rQrrr�__delitem__�sz_DictWrapper.__delitem__cCsFtj|�}tj|j�|_|dk	r6|n|j|_|S)z�Returns a copy.

        Make a shallow copy of d.  If you want a deep copy of d,
        use copy.deepcopy on the whole object.

        label: string label for the new Hist

        returns: new _DictWrapper with the same type
        N)�copyr2r1)r r1�newrrr�Copy�s
z_DictWrapper.CopycCsN|j�}|jj�x.|j�D] \}}|j|||�q&W|S)zmMultiplies the values by a factor.

        factor: what to multiply by

        Returns: new object
        )rZr2�clearr:�Set)r �factorrY�valrUrrr�Scale�s

z_DictWrapper.ScalecCs�|jrtd��d|_|dkr6|j�}xP|jj�D]?\}}|rx|j|tj||��qF|j|�qFWdS)z�Log transforms the probabilities.
        
        Removes values with probability 0.

        Normalizes so that the largest logprob is 0.
        z&Pmf/Hist already under a log transformTN)r3�
ValueError�MaxLiker2r9r\�math�Remove)r �mrrrrr�Log�s		 z_DictWrapper.LogcCsw|jstd��d|_|dkr6|j�}x:|jj�D])\}}|j|tj||��qFWdS)z�Exponentiates the probabilities.

        m: how much to shift the ps before exponentiating

        If m is None, normalizes so that the largest prob is 1.
        z"Pmf/Hist not under a log transformFN)r3r`rar2r9r\rb�exp)r rdrrrrr�Exps		z_DictWrapper.ExpcCs|jS)zGets the dictionary.)r2)r rrr�GetDictsz_DictWrapper.GetDictcCs
||_dS)zSets the dictionary.N)r2)r r2rrr�SetDictsz_DictWrapper.SetDictcCs
|jj�S)z�Gets an unsorted sequence of values.

        Note: one source of confusion is that the keys of this
        dictionary are the values of the Hist/Pmf, and the
        values of the dictionary are frequencies/probabilities.
        )r2�keys)r rrr�Values!sz_DictWrapper.ValuescCs
|jj�S)z6Gets an unsorted sequence of (value, freq/prob) pairs.)r2r9)r rrrr:*sz_DictWrapper.Itemscs�dd��t�fdd�|j�D��rDd}tj|�yt|jj��SWntk
r||jj�SYnXdS)zvGets a sorted sequence of (value, freq/prob) pairs.

        It items are unsortable, the result is unsorted.
        cSs.ytj|�SWntk
r)dSYnXdS)NF)rb�isnan�	TypeError)rrrrrl3s
z'_DictWrapper.SortedItems.<locals>.isnancsg|]}�|��qSrr)�.0r)rlrr�
<listcomp>9s	z,_DictWrapper.SortedItems.<locals>.<listcomp>z)Keys contain NaN, may not sort correctly.N)�anyrk�logging�warning�sortedr2r9rm)r �msgr)rlr�SortedItems.s%

z_DictWrapper.SortedItemscKst|j��S)z�Generates a sequence of points suitable for plotting.

        Note: options are ignored

        Returns:
            tuple of (sorted value sequence, freq/prob sequence)
        )�zipru)r �optionsrrr�RenderBsz_DictWrapper.RendercCs+|dk	r|n|j}t|d|�S)zMakes a Cdf.Nr1)r1r5)r r1rrr�MakeCdfLsz_DictWrapper.MakeCdfcCs.x'|j�D]\}}t||�q
WdS)z5Prints the values and freqs/probs in ascending order.N)ru�print)r r^rUrrr�PrintQsz_DictWrapper.PrintrcCs||j|<dS)z�Sets the freq/prob associated with the value x.

        Args:
            x: number value
            y: number freq or prob
        N)r2)r rr$rrrr\Vsz_DictWrapper.SetrcCs$|jj|d�||j|<dS)z�Increments the freq/prob associated with the value x.

        Args:
            x: number value
            term: how much to increment by
        rN)r2rS)r r�termrrr�Incr_sz_DictWrapper.IncrcCs$|jj|d�||j|<dS)z�Scales the freq/prob associated with the value x.

        Args:
            x: number value
            factor: how much to multiply by
        rN)r2rS)r rr]rrr�Multhsz_DictWrapper.MultcCs|j|=dS)zRemoves a value.

        Throws an exception if the value is not there.

        Args:
            x: value to remove
        N)r2)r rrrrrcqsz_DictWrapper.RemovecCst|jj��}|S)z>Returns the total of the frequencies/probabilities in the map.)�sumr2�values)r �totalrrr�Total{sz_DictWrapper.TotalcCst|jj��S)z5Returns the largest frequency/probability in the map.)�maxr2r�)r rrrra�sz_DictWrapper.MaxLike�
cCs#t|jj�dd�d|�S)zhReturns the largest n values, with frequency/probability.

        n: number of items to return
        �reverseTN)rsr2r9)r �nrrr�Largest�sz_DictWrapper.LargestcCs#t|jj�dd�d|�S)ziReturns the smallest n values, with frequency/probability.

        n: number of items to return
        r�FN)rsr2r9)r r�rrr�Smallest�sz_DictWrapper.Smallest)$r*r+r,r-r!rCrGrIrLrMrOrPrRrTrVrWrZr_rergrhrirkr:rurxryr{r\r}r~rcr�rar�r�rrrrr/�sB	
			
r/c@sFeZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�Histz�Represents a histogram, which is a map from values to frequencies.

    Values can be any hashable type; frequencies are integer counters.
    cCs|jj|d�S)z�Gets the frequency associated with the value x.

        Args:
            x: number value

        Returns:
            int frequency
        r)r2rS)r rrrr�Freq�s	z	Hist.Freqcs�fdd�|D�S)z*Gets frequencies for a sequence of values.csg|]}�j|��qSr)r�)rnr)r rrro�s	zHist.Freqs.<locals>.<listcomp>r)r rr)r r�Freqs�sz
Hist.FreqscCs:x3|j�D]%\}}||j|�kr
dSq
WdS)zfChecks whether the values in this histogram are a subset of
        the values in the given histogram.FT)r:r�)r rKr^�freqrrr�IsSubset�sz
Hist.IsSubsetcCs2x+|j�D]\}}|j||�q
WdS)z@Subtracts the values in the given histogram from this histogram.N)r:r})r rKr^r�rrr�Subtract�sz
Hist.SubtractN)r*r+r,r-r�r�r�r�rrrrr��s
r�c@s�eZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	ddd�Z
dd�Zdd�Zdd�Z
dd�Zddd�Zdd�Zddd �Zd!d"�ZeZeZd#d$d%�Zd&d'�ZeZd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�ZeZ d8d9�Z!d:d;�Z"dS)<r?z�Represents a probability mass function.
    
    Values can be any hashable type; probabilities are floating-point.
    Pmfs are not necessarily normalized.
    rcCs|jj||�S)z�Gets the probability associated with the value x.

        Args:
            x: number value
            default: value to return if the key is not there

        Returns:
            float probability
        )r2rS)r r�defaultrrr�Prob�s
zPmf.Probcs�fdd�|D�S)z,Gets probabilities for a sequence of values.csg|]}�j|��qSr)r�)rnr)r rrro�s	zPmf.Probs.<locals>.<listcomp>r)r rr)r r�Probs�sz	Pmf.ProbscCsQ|d}d}x:t|j��D]&\}}||7}||kr#|Sq#WdS)z�Computes a percentile of a given Pmf.

        Note: this is not super efficient.  If you are planning
        to compute more than a few percentiles, compute the Cdf.

        percentage: float 0-100

        returns: value from the Pmf
        �drN)rsr:)r �
percentagerr�r^rUrrr�
Percentile�s


zPmf.PercentilecsLt�t�rt|��S�fdd�|jj�D�}t|�SdS)zrProbability that a sample from this Pmf exceeds x.

        x: number

        returns: float probability
        cs(g|]\}}|�kr|�qSrr)rnr^rU)rrrro�s	z#Pmf.ProbGreater.<locals>.<listcomp>N)r4r/�PmfProbGreaterr2r9r)r r�tr)rr�ProbGreater�s
"zPmf.ProbGreatercsLt�t�rt|��S�fdd�|jj�D�}t|�SdS)zwProbability that a sample from this Pmf is less than x.

        x: number

        returns: float probability
        cs(g|]\}}|�kr|�qSrr)rnr^rU)rrrro�s	z Pmf.ProbLess.<locals>.<listcomp>N)r4r/�PmfProbLessr2r9r)r rr�r)rr�ProbLess�s
"zPmf.ProbLesscCs(t|t�rt||�S||SdS)zuProbability that a sample from this Pmf is exactly x.

        x: number

        returns: float probability
        N)r4r/�PmfProbEqual)r rrrr�	ProbEqual�s
z
Pmf.ProbEqualrcCsl|jrtd��|j�}|dkr9td��||}x"|jD]}|j||9<qMW|S)z�Normalizes this PMF so the sum of all probs is fraction.

        Args:
            fraction: what the total should be after normalization

        Returns: the total probability before normalizing
        z'Normalize: Pmf is under a log transformrz%Normalize: total probability is zero.)r3r`r�r2)r �fractionr�r]rrrrr@s	
z
Pmf.NormalizecCs\tj�}d}x7|jj�D]&\}}||7}||kr"|Sq"Wtd��dS)z�Chooses a random element from this PMF.

        Note: this is not very efficient.  If you plan to call
        this more than a few times, consider converting to a CDF.

        Returns:
            float value from the Pmf
        rz$Random: Pmf might not be normalized.N)rr2r9r`)r �targetr�rrrrr�Randoms	
z
Pmf.RandomcCs|j�j|�S)z�Generates a random sample from this distribution.
        
        n: int length of the sample
        returns: NumPy array
        )ry�Sample)r r�rrrr�/sz
Pmf.SamplecCstdd�|j�D��S)zMComputes the mean of a PMF.

        Returns:
            float mean
        css|]\}}||VqdS)Nr)rnrrrrr�	<genexpr>=szPmf.Mean.<locals>.<genexpr>)rr:)r rrr�Mean7szPmf.MeancCs|j�jd�S)zQComputes the median of a PMF.

        Returns:
            float median
        �2)ryr�)r rrr�Median?sz
Pmf.MedianNcs;�dkr|j��t�fdd�|j�D��S)z�Computes the variance of a PMF.

        mu: the point around which the variance is computed;
                if omitted, computes the mean

        returns: float variance
        Nc3s'|]\}}||�dVqdS)rNr)rnrr)�murrr�RszPmf.Var.<locals>.<genexpr>)r�rr:)r r�r)r�r�VarGszPmf.Varcs&tj�fdd�|j�D��S)zWComputes the expectation of func(x).

        Returns:
            expectation
        c3s%|]\}}|�|�VqdS)Nr)rnrr)�funcrrr�ZszPmf.Expect.<locals>.<genexpr>)rrr:)r r�r)r�r�ExpectTsz
Pmf.ExpectcCs|j|�}tj|�S)z�Computes the standard deviation of a PMF.

        mu: the point around which the variance is computed;
                if omitted, computes the mean

        returns: float standard deviation
        )r�rb�sqrt)r r��varrrr�Std\szPmf.StdcCs)tdd�|j�D��\}}|S)z\Returns the value with the highest probability.

        Returns: float probability
        css!|]\}}||fVqdS)Nr)rnr^rUrrrr�lszPmf.Mode.<locals>.<genexpr>)r�r:)r �_r^rrr�Modegs%zPmf.Mode�ZcCs|j�}|j|�S)z�Computes the central credible interval.

        If percentage=90, computes the 90% CI.

        Args:
            percentage: float between 0 and 100

        Returns:
            sequence of two floats, low and high
        )ry�CredibleInterval)r r��cdfrrrr�vszPmf.CredibleIntervalcCs7y|j|�SWntk
r2|j|�SYnXdS)z�Computes the Pmf of the sum of values drawn from self and other.

        other: another Pmf or a scalar

        returns: new Pmf
        N)�AddPmfrJ�AddConstant)r rKrrr�__add__�s
zPmf.__add__cCs]t�}xM|j�D]?\}}x0|j�D]"\}}|||||7<q/WqW|S)zComputes the Pmf of the sum of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        )r?r:)r rK�pmf�v1�p1�v2�p2rrrr��s
	z
Pmf.AddPmfcCsT|dkr|j�St�}x.|j�D] \}}|j|||�q,W|S)zxComputes the Pmf of the sum a constant and values from self.

        other: a number

        returns: new Pmf
        r)rZr?r:r\)r rKr�r�r�rrrr��s
	zPmf.AddConstantcCs8y|j|�SWn tk
r3|j|�SYnXdS)z�Computes the Pmf of the diff of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        N)�SubPmfrJr�)r rKrrr�__sub__�s
zPmf.__sub__cCs_t�}xO|j�D]A\}}x2|j�D]$\}}|j||||�q/WqW|S)z�Computes the Pmf of the diff of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        )r?r:r})r rKr�r�r�r�r�rrrr��s
	 z
Pmf.SubPmfcCs7y|j|�SWntk
r2|j|�SYnXdS)z�Computes the Pmf of the product of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        N)�MulPmfrJ�MulConstant)r rKrrr�__mul__�s
zPmf.__mul__cCs_t�}xO|j�D]A\}}x2|j�D]$\}}|j||||�q/WqW|S)z�Computes the Pmf of the diff of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        )r?r:r})r rKr�r�r�r�r�rrrr��s
	 z
Pmf.MulPmfcCs>t�}x.|j�D] \}}|j|||�qW|S)zComputes the Pmf of the product of a constant and values from self.

        other: a number

        returns: new Pmf
        )r?r:r\)r rKr�r�r�rrrr��s	zPmf.MulConstantcCs;y|j|�SWn#tk
r6|jd|�SYnXdS)z�Computes the Pmf of the ratio of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        rN)�DivPmfrJr�)r rKrrr�__div__�s
zPmf.__div__cCs_t�}xO|j�D]A\}}x2|j�D]$\}}|j||||�q/WqW|S)z�Computes the Pmf of the ratio of values drawn from self and other.

        other: another Pmf

        returns: new Pmf
        )r?r:r})r rKr�r�r�r�r�rrrr��s
	 z
Pmf.DivPmfcCs|j�}|j|C_|S)zrComputes the CDF of the maximum of k selections from this dist.

        k: int

        returns: new Cdf
        )ry�ps)r �kr�rrr�MaxszPmf.Max)#r*r+r,r-r�r�r�r�r�r�r@r�r�r�r�r�r�r�r��MAPZMaximumLikelihoodr�r��__radd__r�r�r�r�r�r�r�r��__truediv__r�r�rrrrr?�s@


	



r?c@sCeZdZdZddd�Zddd�Zddd	�ZdS)
�JointzTRepresents a joint distribution.

    The values are sequences (usually tuples)
    NcCsDtd|�}x.|j�D] \}}|j|||�qW|S)z�Gets the marginal distribution of the indicated variable.

        i: index of the variable we want

        Returns: Pmf
        r1)r?r:r})r r(r1r��vsrUrrr�MarginalszJoint.MarginalcCsatd|�}xA|j�D]3\}}|||kr;q|j|||�qW|j�|S)a!Gets the conditional distribution of the indicated variable.

        Distribution of vs[i], conditioned on vs[j] = val.

        i: index of the variable we want
        j: which variable is conditioned on
        val: the value the jth variable has to have

        Returns: Pmf
        r1)r?r:r}r@)r r(�jr^r1r�r�rUrrr�Conditional%s
zJoint.Conditionalr�cCsxg}d}dd�|j�D�}|jdd�x<|D]4\}}|j|�||7}||dkr<Pq<W|S)aReturns the maximum-likelihood credible interval.

        If percentage=90, computes a 90% CI containing the values
        with the highest likelihoods.

        percentage: float between 0 and 100

        Returns: list of values from the suite
        rcSs"g|]\}}||f�qSrr)rnr^rUrrrroFs	z)Joint.MaxLikeInterval.<locals>.<listcomp>r�Tr�)r:�sort�append)r r��intervalr�r�rUr^rrr�MaxLikeInterval9s


zJoint.MaxLikeInterval)r*r+r,r-r�r�r�rrrrr�sr�cCsat�}xQ|j�D]C\}}x4|j�D]&\}}|j||f||�q/WqW|S)z�Joint distribution of values from pmf1 and pmf2.

    Assumes that the PMFs represent independent random variables.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        Joint pmf of value pairs
    )r�r:r\)�pmf1�pmf2Zjointr�r�r�r�rrr�	MakeJointRs
	"r�cCst|d|�S)z�Makes a histogram from an unsorted sequence of values.

    Args:
        t: sequence of numbers
        label: string label for this histogram

    Returns:
        Hist object
    r1)r�)r�r1rrr�MakeHistFromListes
r�cCs
t||�S)z�Makes a histogram from a map from values to frequencies.

    Args:
        d: dictionary that maps values to frequencies
        label: string label for this histogram

    Returns:
        Hist object
    )r�)r2r1rrr�MakeHistFromDictrs
r�cCst|d|�S)z�Makes a PMF from an unsorted sequence of values.

    Args:
        t: sequence of numbers
        label: string label for this PMF

    Returns:
        Pmf object
    r1)r?)r�r1rrr�MakePmfFromLists
r�cCst|d|�S)z�Makes a PMF from a map from values to probabilities.

    Args:
        d: dictionary that maps values to probabilities
        label: string label for this PMF

    Returns:
        Pmf object
    r1)r?)r2r1rrr�MakePmfFromDict�s
r�cCstt|�d|�S)z�Makes a PMF from a sequence of value-probability pairs

    Args:
        t: sequence of value-probability pairs
        label: string label for this PMF

    Returns:
        Pmf object
    r1)r?r7)r�r1rrr�MakePmfFromItems�s
r�cCs%|dkr|j}t|d|�S)z�Makes a normalized PMF from a Hist object.

    Args:
        hist: Hist object
        label: string label

    Returns:
        Pmf object
    Nr1)r1r?)�histr1rrr�MakePmfFromHist�s
	r��mixcCs_td|�}xI|j�D];\}}x,|j�D]\}}||||7<q5WqW|S)z�Make a mixture distribution.

    Args:
      metapmf: Pmf that maps from Pmfs to probs.
      label: string label for the new Pmf.

    Returns: Pmf object.
    r1)r?r:)Zmetapmfr1r�r�r�rr�rrr�MakeMixture�s
	r�cCsGt�}x-tj|||�D]}|j|d�qW|j�|S)zwMake a uniform Pmf.

    low: lowest value (inclusive)
    high: highest value (inclusize)
    n: number of values
    r)r?r�linspacer\r@)�low�highr�r�rrrr�MakeUniformPmf�s
	
r�c@s�eZdZdZddddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zddd�Z
ddd�Zdd�Zdd�Zdd�Zdd �Zd!d"�ZeZd#d$�Zdd%d&�ZeZd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6d7�ZeZ d8d9d:�Z!d;d<�Z"d=d>�Z#dS)?r5z�Represents a cumulative distribution function.

    Attributes:
        xs: sequence of values
        ps: sequence of probabilities
        label: string used as a graph label.
    NcCs�|dk	r|nt|_t|tttf�rW|sW|dk	rK|n|j|_|dkr�tjg�|_tjg�|_	|dk	r�t
jd�dS|dk	r�t|t�r�t
jd�tj|�|_tj|�|_	dSt|t�r1t
j
|j�|_t
j
|j	�|_	dSt|t�rI|}nt|�}t|�dkr�tjg�|_tjg�|_	dStt|j���\}}tj|�|_tj|dtj�|_	|j	|j	d_	dS)z�Initializes.
        
        If ps is provided, obj must be the corresponding list of values.

        obj: Hist, Pmf, Cdf, Pdf, dict, pandas Series, list of pairs
        ps: list of cumulative probabilities
        label: string label
        Nz+Cdf: can't pass ps without also passing xs.zCdf: ps can't be a stringr�dtyperr&)r0r1r4r/r5r6r�asarrayrr�rqrrrErXr�r>rvrsr:�cumsumr)r rAr�r1�dwr�freqsrrrr!�s>	

	zCdf.__init__cCsI|jj}|jtkr>d|t|j�t|j�fS|jSdS)Nz
%s(%s, %s))rDr*r1r0rErr�)r rFrrrrGs#zCdf.__str__cCsq|jj}|jtkr>d|t|j�t|j�fSd|t|j�t|j�t|j�fSdS)Nz
%s(%s, %s)z%s(%s, %s, %s))rDr*r1r0rErr�rH)r rFrrrrIs
#zCdf.__repr__cCs
t|j�S)N)r>r)r rrrrM szCdf.__len__cCs
|j|�S)N)r�)r rrrrrT#szCdf.__getitem__cCs
t��dS)N)�UnimplementedMethodException)r rrrrV&szCdf.__setitem__cCs
t��dS)N)r�)r rrrrW)szCdf.__delitem__cCs4tj|j|jk�o3tj|j|jk�S)N)r�allrr�)r rKrrrrL,sz
Cdf.__eq__cCs7x0t|j|j�D]\}}t||�qWdS)z5Prints the values and freqs/probs in ascending order.N)rvrr�rz)r r^rUrrrr{/s"z	Cdf.PrintcCs:|dkr|j}tt|j�t|j�d|�S)zQReturns a copy of this Cdf.

        label: string label for the new Cdf
        Nr1)r1r5�listrr�)r r1rrrrZ4s	zCdf.CopycCs%|dkr|j}t|d|�S)zMakes a Pmf.Nr1)r1r?)r r1rrr�MakePmf=s	zCdf.MakePmfcCs9|j}tj|d�}d|d<t|j||�S)zqReturns a sorted sequence of (value, probability) pairs.

        Note: in Python3, returns an iterator.
        rr)r�r�rollrvr)r �a�brrrr:Cs	
z	Cdf.ItemscCs |j�}|j||_|S)z>Adds a term to the xs.

        term: how much to add
        )rZr)r r|rYrrr�ShiftMsz	Cdf.ShiftcCs |j�}|j||_|S)zLMultiplies the xs by a factor.

        factor: what to multiply by
        )rZr)r r]rYrrrr_Wsz	Cdf.ScalecCsA||jdkrdStj|j|�}|j|d}|S)z�Returns CDF(x), the probability that corresponds to value x.

        Args:
            x: number

        Returns:
            float probability
        rr)rr'r�)r r�indexrrrrr�as
	zCdf.ProbcCsVtj|�}tj|j|dd�}|j|d}d|||jdk<|S)z�Gets probabilities for a sequence of values.

        xs: any sequence that can be converted to NumPy array

        returns: NumPy array of cumulative probabilities
        �side�rightrr)rr��searchsortedrr�)r rr�r�rrrr�ps
z	Cdf.ProbscCsD|dks|dkr$td��tj|j|�}|j|S)z�Returns InverseCDF(p), the value that corresponds to probability p.

        Args:
            p: number in the range [0, 1]

        Returns:
            number value
        rrz%Probability p must be in range [0, 1])r`r'�bisect_leftr�r)r rr�rrr�Values	z	Cdf.ValuecCs~|dkr|jStj|�}tj|dk�sLtj|dk�rXtd��tj|j|dd�}|j|S)z�Returns InverseCDF(p), the value that corresponds to probability p.

        If ps is not provided, returns all values.

        Args:
            ps: NumPy array of numbers in the range [0, 1]

        Returns:
            NumPy array of values
        Nrrz%Probability p must be in range [0, 1]r��left)rrr�rpr`r�r�)r r�r�rrrrk�s*z
Cdf.ValuescCs|j|d�S)z�Returns the value that corresponds to percentile p.

        Args:
            p: number in the range [0, 100]

        Returns:
            number value
        r�)r�)r rrrrr��s	zCdf.PercentilecCs tj|�}|j|d�S)z�Returns the value that corresponds to percentiles ps.

        Args:
            ps: numbers in the range [0, 100]

        Returns:
            array of values
        r�)rr�rk)r r�rrr�Percentiles�s	zCdf.PercentilescCs|j|�dS)z�Returns the percentile rank of the value x.

        x: potential value in the CDF

        returns: percentile rank in the range 0 to 100
        r�)r�)r rrrr�PercentileRank�szCdf.PercentileRankcCs|jt�dS)z�Returns the percentile ranks of the values in xs.

        xs: potential value in the CDF

        returns: array of percentile ranks in the range 0 to 100
        r�)r�r)r rrrr�PercentileRanks�szCdf.PercentileRankscCs|jtj��S)z.Chooses a random value from this distribution.)r�r)r rrrr��sz
Cdf.RandomcCstjj|�}|j|�S)z�Generates a random sample from this distribution.
        
        n: int length of the sample
        returns: NumPy array
        )rr�
ValueArray)r r�r�rrrr��sz
Cdf.SamplecCsTd}d}xAt|j|j�D]*\}}||}|||7}|}q"W|S)zMComputes the mean of a CDF.

        Returns:
            float mean
        r)rvrr�)r Zold_pr�rZnew_prrrrr��s"

zCdf.Meanr�cCs8d|dd}|j|�|jd|�f}|S)z�Computes the central credible interval.

        If percentage=90, computes the 90% CI.

        Args:
            percentage: float between 0 and 100

        Returns:
            sequence of two floats, low and high
        rr�r)r�)r r�rUr�rrrr��s"zCdf.CredibleIntervali�cCs
t��dS)a
        An entry is added to the cdf only if the percentile differs
        from the previous value in a significant digit, where the number
        of significant digits is determined by multiplier.  The
        default is 1000, which keeps log10(1000) = 3 significant digits.
        N)r�)r Z
multiplierrrr�_Round�sz
Cdf._RoundcKshdd�}tj|j�}|||�}tj|jd�}d|d<|||j�}||fS)z�Generates a sequence of points suitable for plotting.

        An empirical CDF is a step function; linear interpolation
        can be misleading.

        Note: options are ignored

        Returns:
            tuple of (xs, ps)
        cSsKtj|jd|jd�}||ddd�<||ddd�<|S)Nrrr)r�empty�shape)r�r��crrr�
interleaves!zCdf.Render.<locals>.interleaverr)r�arrayrr�r�)r rwr�r�rZshift_psr�rrrrxs
z
Cdf.RendercCs|j�}|j|C_|S)zrComputes the CDF of the maximum of k selections from this dist.

        k: int

        returns: new Cdf
        )rZr�)r r�r�rrrr�szCdf.Max)$r*r+r,r-r!rGrIrMrTrVrWrLr{rZr�r:r�r_r�r�Z	ProbArrayr�rkr�r�r�r�r�r�r�r�r�ZConfidenceIntervalr�rxr�rrrrr5�sB5	



			
r5cCstt|�d|�S)z�Makes a cdf from an unsorted sequence of (value, frequency) pairs.

    Args:
        items: unsorted sequence of (value, frequency) pairs
        label: string label for this CDF

    Returns:
        cdf: list of (value, fraction) pairs
    r1)r5r7)r9r1rrr�MakeCdfFromItems(s
r�cCst|d|�S)z�Makes a CDF from a dictionary that maps values to frequencies.

    Args:
       d: dictionary that maps values to frequencies.
       label: string label for the data.

    Returns:
        Cdf object
    r1)r5)r2r1rrr�MakeCdfFromDict5s
r�cCst|d|�S)z�Creates a CDF from an unsorted sequence.

    Args:
        seq: unsorted sequence of sortable values
        label: string label for the cdf

    Returns:
       Cdf object
    r1)r5)�seqr1rrr�MakeCdfFromListBs
r�cCs%|dkr|j}t|d|�S)z�Makes a CDF from a Hist object.

    Args:
       hist: Pmf.Hist object
       label: string label for the data.

    Returns:
        Cdf object
    Nr1)r1r5)r�r1rrr�MakeCdfFromHistOs
	rcCs%|dkr|j}t|d|�S)z�Makes a CDF from a Pmf object.

    Args:
       pmf: Pmf.Pmf object
       label: string label for the data.

    Returns:
        Cdf object
    Nr1)r1r5)r�r1rrr�MakeCdfFromPmf_s
	rc@seZdZdZdS)r�z>Exception if someone calls a method that should be overridden.N)r*r+r,r-rrrrr�osr�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�Suitez9Represents a suite of hypotheses and their probabilities.cCsCx6|j�D](}|j||�}|j||�q
W|j�S)z�Updates each hypothesis based on the data.

        data: any representation of the data

        returns: the normalizing constant
        )rk�
Likelihoodr~r@)r �data�hypo�likerrr�UpdatevszSuite.UpdatecCs=x6|j�D](}|j||�}|j||�q
WdS)aUpdates a suite of hypotheses based on new data.

        Modifies the suite directly; if you want to keep the original, make
        a copy.

        Note: unlike Update, LogUpdate does not normalize.

        Args:
            data: any representation of the data
        N)rk�
LogLikelihoodr})r rrrrrr�	LogUpdate�szSuite.LogUpdatecCsTxG|D]?}x6|j�D](}|j||�}|j||�qWqW|j�S)aWUpdates each hypothesis based on the dataset.

        This is more efficient than calling Update repeatedly because
        it waits until the end to Normalize.

        Modifies the suite directly; if you want to keep the original, make
        a copy.

        dataset: a sequence of data

        returns: the normalizing constant
        )rkrr~r@)r �datasetrrrrrr�	UpdateSet�s


zSuite.UpdateSetcCs"x|D]}|j|�qWdS)z�Updates each hypothesis based on the dataset.

        Modifies the suite directly; if you want to keep the original, make
        a copy.

        dataset: a sequence of data

        returns: None
        N)r	)r r
rrrr�LogUpdateSet�s

zSuite.LogUpdateSetcCs
t��dS)z�Computes the likelihood of the data under the hypothesis.

        hypo: some representation of the hypothesis
        data: some representation of the data
        N)r�)r rrrrrr�szSuite.LikelihoodcCs
t��dS)z�Computes the log likelihood of the data under the hypothesis.

        hypo: some representation of the hypothesis
        data: some representation of the data
        N)r�)r rrrrrr�szSuite.LogLikelihoodcCs4x-t|j��D]\}}t||�qWdS)z.Prints the hypotheses and their probabilities.N)rsr:rz)r rrUrrrr{�szSuite.PrintcCsMxF|j�D]8\}}|r8|j|t|��q
|j|�q
WdS)zXTransforms from probabilities to odds.

        Values with prob=0 are removed.
        N)r:r\rrc)r rrUrrr�MakeOdds�szSuite.MakeOddscCs7x0|j�D]"\}}|j|t|��q
WdS)z&Transforms from odds to probabilities.N)r:r\r)r rZoddsrrr�	MakeProbs�szSuite.MakeProbsN)
r*r+r,r-rr	rrrrr{r
rrrrrrss
rcCs(t|d|�}|j�}t|�S)z�Makes a suite from an unsorted sequence of values.

    Args:
        t: sequence of numbers
        label: string label for this suite

    Returns:
        Suite object
    r1)r�rh�MakeSuiteFromDict)r�r1r�r2rrr�MakeSuiteFromList�s
rcCs4|dkr|j}t|j��}t||�S)z�Makes a normalized suite from a Hist object.

    Args:
        hist: Hist object
        label: string label

    Returns:
        Suite object
    N)r1r7rhr)r�r1r2rrr�MakeSuiteFromHist�s
	rcCs*td|�}|j|�|j�|S)z�Makes a suite from a map from values to probabilities.

    Args:
        d: dictionary that maps values to probabilities
        label: string label for this suite

    Returns:
        Suite object
    r1)rrir@)r2r1�suiterrrr�s


rc@sReZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
r6z0Represents a probability density function (PDF).cCs
t��dS)z_Evaluates this Pdf at x.

        Returns: float or NumPy array of probability density
        N)r�)r rrrr�DensityszPdf.DensitycCs
t��dS)zvGet a linspace for plotting.

        Not all subclasses of Pdf implement this.

        Returns: numpy array
        N)r�)r rrr�GetLinspaceszPdf.GetLinspacecKsF|jdd�}|j|�\}}ttt||��d|�S)z�Makes a discrete version of this Pdf.

        options can include
        label: string
        low: low end of range
        high: high end of range
        n: number of places to evaluate

        Returns: new Pmf
        r1�)�poprxr?r7rv)r rwr1r�dsrrrr�szPdf.MakePmfcKs�|jdd�|jdd�}}|dk	rg|dk	rg|jdd�}tj|||�}n*|jdd�}|dkr�|j�}|j|�}||fS)a�Generates a sequence of points suitable for plotting.

        If options includes low and high, it must also include n;
        in that case the density is evaluated an n locations between
        low and high, including both.

        If options includes xs, the density is evaluate at those location.

        Otherwise, self.GetLinspace is invoked to provide the locations.

        Returns:
            tuple of (xs, densities)
        r�Nr�r��er)rrr�rr)r rwr�r�r�rrrrrrx*s%z
Pdf.RendercCst|j��S)z<Generates a sequence of (value, probability) pairs.
        )rvrx)r rrrr:Dsz	Pdf.ItemsN)	r*r+r,r-rrr�rxr:rrrrr6s	r6c@sOeZdZdZddddd�Zdd�Zd	d
�Zdd�ZdS)
�	NormalPdfz,Represents the PDF of a Normal distribution.rrNcCs1||_||_|dk	r$|nd|_dS)z�Constructs a Normal Pdf with given mu and sigma.

        mu: mean
        sigma: standard deviation
        label: string
        Nr.)r��sigmar1)r r�rr1rrrr!Ms		zNormalPdf.__init__cCsd|j|jfS)NzNormalPdf(%f, %f))r�r)r rrrrGXszNormalPdf.__str__cCs<|jd|j|jd|j}}tj||d�S)zCGet a linspace for plotting.

        Returns: numpy array
        �r)r�rrr�)r r�r�rrrr[s)zNormalPdf.GetLinspacecCstjj||j|j�S)z�Evaluates this Pdf at xs.

        xs: scalar or sequence of floats

        returns: float or NumPy array of probability density
        )r�norm�pdfr�r)r rrrrrcszNormalPdf.Density)r*r+r,r-r!rGrrrrrrrJs
rc@sLeZdZdZdddd�Zdd�Zdd	�Zd
d�ZdS)�ExponentialPdfz2Represents the PDF of an exponential distribution.rNcCs(||_|dk	r|nd|_dS)zoConstructs an exponential Pdf with given parameter.

        lam: rate parameter
        label: string
        Nr.)�lamr1)r rr1rrrr!ps	zExponentialPdf.__init__cCsd|jS)NzExponentialPdf(%f))r)r rrrrGyszExponentialPdf.__str__cCs'dd|j}}tj||d�S)zCGet a linspace for plotting.

        Returns: numpy array
        rg@r)rrr�)r r�r�rrrr|szExponentialPdf.GetLinspacecCstjj|dd|j�S)z�Evaluates this Pdf at xs.

        xs: scalar or sequence of floats

        returns: float or NumPy array of probability density
        �scaleg�?)rZexponrr)r rrrrr�szExponentialPdf.Density)r*r+r,r-r!rGrrrrrrrms
	rc@sUeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�ZdS)
�EstimatedPdfz"Represents a PDF estimated by KDE.NcCsa|dk	r|nd|_tj|�|_t|�}t|�}tj||d�|_dS)zrEstimates the density function based on a sample.

        sample: sequence of data
        label: string
        Nr.r)r1rZgaussian_kde�kde�minr�rr�)r �sampler1r�r�rrrr!�s
zEstimatedPdf.__init__cCsdt|j�S)NzEstimatedPdf(label=%s))rEr1)r rrrrG�szEstimatedPdf.__str__cCs|jS)zCGet a linspace for plotting.

        Returns: numpy array
        )r�)r rrrr�szEstimatedPdf.GetLinspacecCs|jj|�S)z`Evaluates this Pdf at xs.

        returns: float or NumPy array of probability density
        )r"�evaluate)r rrrrr�szEstimatedPdf.DensitycCs|jj|�j�S)zUGenerates a random sample from the estimated Pdf.

        n: size of sample
        )r"�resample�flatten)r r�rrrr��szEstimatedPdf.Sample)	r*r+r,r-r!rGrrr�rrrrr!�sr!r�cCsD|j�}d|dd}|j|�|jd|�f}|S)aComputes a credible interval for a given distribution.

    If percentage=90, computes the 90% CI.

    Args:
        pmf: Pmf object representing a posterior distribution
        percentage: float between 0 and 100

    Returns:
        sequence of two floats, low and high
    rr�r)ryr�)r�r�r�rUr�rrrr��s"r�cCs^d}xQ|j�D]C\}}x4|j�D]&\}}||kr,|||7}q,WqW|S)z�Probability that a value from pmf1 is less than a value from pmf2.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        float probability
    r)r:)r�r�r�r�r�r�r�rrrr��s
r�cCs^d}xQ|j�D]C\}}x4|j�D]&\}}||kr,|||7}q,WqW|S)z�Probability that a value from pmf1 is less than a value from pmf2.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        float probability
    r)r:)r�r�r�r�r�r�r�rrrr��s
r�cCs^d}xQ|j�D]C\}}x4|j�D]&\}}||kr,|||7}q,WqW|S)z�Probability that a value from pmf1 equals a value from pmf2.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        float probability
    r)r:)r�r�r�r�r�r�r�rrrr��s
r�cCstdd�|D��}|S)z�Chooses a random value from each dist and returns the sum.

    dists: sequence of Pmf or Cdf objects

    returns: numerical sum
    css|]}|j�VqdS)N)r�)rn�distrrrr�szRandomSum.<locals>.<genexpr>)r)�distsr�rrr�	RandomSumsr*cs)t�fdd�t|�D��}|S)z�Draws a sample of sums from a list of distributions.

    dists: sequence of Pmf or Cdf objects
    n: sample size

    returns: new Pmf of sums
    c3s|]}t��VqdS)N)r*)rnr()r)rrr�szSampleSum.<locals>.<genexpr>)r?�range)r)r�r�r)r)r�	SampleSums%r,cCstjj|||�S)z�Computes the unnormalized PDF of the normal distribution.

    x: value
    mu: mean
    sigma: standard deviation
    
    returns: float probability density
    )rrr)rr�rrrr�
EvalNormalPdfs	r-��c	Csut�}|||}|||}x?tj|||�D](}t|||�}|j||�q;W|j�|S)z�Makes a PMF discrete approx to a Normal distribution.
    
    mu: float mean
    sigma: float standard deviation
    num_sigmas: how many sigmas to extend in each direction
    n: number of values in the Pmf

    returns: normalized Pmf
    )r?rr�r-r\r@)	r�rZ
num_sigmasr�r�r�r�rrrrr�
MakeNormalPmf#s
	
r/cCstjj|||�S)zjEvaluates the binomial PMF.

    Returns the probabily of k successes in n trials with probability p.
    )r�binomr�)r�r�rrrr�EvalBinomialPmf8sr1cCsDt�}x4t|d�D]"}tjj|||�||<qW|S)zkEvaluates the binomial PMF.

    Returns the distribution of successes in n trials with probability p.
    r)r?r+rr0r�)r�rr�r�rrr�MakeBinomialPmf@s	 r2cCs$||dtj|�t|�S)z�Computes the Gamma PDF.

    x: where to evaluate the PDF
    a: parameter of the gamma distribution

    returns: float probability
    r)rrfr	)rr�rrr�EvalGammaPdfKsr3cCsGtj|�}t||�}ttt||���}|j�|S)z�Makes a PMF discrete approx to a Gamma distribution.

    lam: parameter lambda in events per unit time
    xs: upper bound of the Pmf

    returns: normalized Pmf
    )rr�r3r?r7rvr@)rr�r�r�rrr�MakeGammaPmfVs

r4cCstjj||d|�S)z�Evaluates the geometric PMF.

    With loc=0: Probability of `k` trials to get one success.
    With loc=-1: Probability of `k` trials before first success.

    k: number of trials
    p: probability of success on each trial
    �loc)r�geomr�)r�rr5rrr�EvalGeometricPmfes	r7r�cCsMt�}x3t|�D]%}tjj||d|�||<qW|j�|S)z�Evaluates the binomial PMF.

    With loc=0: PMF of trials to get one success.
    With loc=-1: PMF of trials before first success.

    p: probability of success
    high: upper bound where PMF is truncated
    r5)r?r+rr6r�r@)rr5r�r�r�rrr�MakeGeometricPmfqs
		#
r8cCstjj||||�S)z�Evaluates the hypergeometric PMF.

    Returns the probabily of k successes in n trials from a population
    N with K successes in it.
    )rZ	hypergeomr�)r��N�Kr�rrr�EvalHypergeomPmf�sr;cCstjj||�S)z�Computes the Poisson PMF.

    k: number of events
    lam: parameter lambda in events per unit time

    returns: float probability
    )r�poissonr�)r�rrrr�EvalPoissonPmf�sr=rcCs]t�}xCtd|d|�D]+}tjj||�}|j||�q W|j�|S)z�Makes a PMF discrete approx to a Poisson distribution.

    lam: parameter lambda in events per unit time
    high: upper bound of the Pmf

    returns: normalized Pmf
    rr)r?r+rr<r�r\r@)rr��stepr�r�rrrr�MakePoissonPmf�s	
r?cCs|tj||�S)z�Computes the exponential PDF.

    x: value
    lam: parameter lambda in events per unit time

    returns: float probability density
    )rbrf)rrrrr�EvalExponentialPdf�sr@cCsdtj||�S)zAEvaluates CDF of the exponential distribution with parameter lam.r)rbrf)rrrrr�EvalExponentialCdf�srA��cCsVt�}x<tjd||�D]%}t||�}|j||�qW|j�|S)z�Makes a PMF discrete approx to an exponential distribution.

    lam: parameter lambda in events per unit time
    high: upper bound
    n: number of values in the Pmf

    returns: normalized Pmf
    r)r?rr�r@r\r@)rr�r�r�rrrrr�MakeExponentialPmf�s		
rCcCs0||}||||dtj||�S)z�Computes the Weibull PDF.

    x: value
    lam: parameter lambda in events per unit time
    k: parameter

    returns: float probability density
    r)rrf)rrr��argrrr�EvalWeibullPdf�s	
rEcCs ||}dtj||�S)z*Evaluates CDF of the Weibull distribution.r)rrf)rrr�rDrrr�EvalWeibullCdf�s
rFcCs@tjd||�}t|||�}ttt||���S)z�Makes a PMF discrete approx to a Weibull distribution.

    lam: parameter lambda in events per unit time
    k: parameter
    high: upper bound
    n: number of values in the Pmf

    returns: normalized Pmf
    r)rr�rEr?r7rv)rr�r�r�rr�rrr�MakeWeibullPmf�s
rGcCstjj||d|�S)z�Computes the Pareto.

    xm: minimum value (scale parameter)
    alpha: shape parameter

    returns: float probability density
    r )r�paretor)r�xm�alpharrr�
EvalParetoPdf�srKrcCsOtj|||�}tjj||d|�}ttt||���}|S)z�Makes a PMF discrete approx to a Pareto distribution.

    xm: minimum value (scale parameter)
    alpha: shape parameter
    high: upper bound value
    num: number of values

    returns: normalized Pmf
    r )rr�rrHrr?r7rv)rIrJr��numrr�r�rrr�
MakeParetoPmf�s
rMcCstj|t�ddS)z�Evaluates the CDF of the standard Normal distribution.
    
    See http://en.wikipedia.org/wiki/Normal_distribution
    #Cumulative_distribution_function

    Args:
        x: float
                
    Returns:
        float
    rr)rb�erf�ROOT2)rrrr�StandardNormalCdfsrPcCstjj|d|d|�S)z�Evaluates the CDF of the normal distribution.
    
    Args:
        x: float

        mu: mean parameter
        
        sigma: standard deviation parameter
                
    Returns:
        float
    r5r )rrr�)rr�rrrr�
EvalNormalCdfs
rQcCstjj|d|d|�S)a!Evaluates the inverse CDF of the normal distribution.

    See http://en.wikipedia.org/wiki/Normal_distribution#Quantile_function  

    Args:
        p: float

        mu: mean parameter
        
        sigma: standard deviation parameter
                
    Returns:
        float
    r5r )rrZppf)rr�rrrr�EvalNormalCdfInverse!srRcCstjj|d|d|�S)z�Evaluates the CDF of the lognormal distribution.
    
    x: float or sequence
    mu: mean parameter
    sigma: standard deviation parameter
                
    Returns: float or sequence
    r5r )rZlognormr�)rr�rrrr�EvalLognormalCdf3s	rScCs7tj|||�}dtj||�}||fS)z�Generates sequences of xs and ps for an exponential CDF.

    lam: parameter
    low: float
    high: float
    n: number of points to render

    returns: numpy arrays (xs, ps)
    r)rr�rf)rr�r�r�rr�rrr�
RenderExpoCdf?s
rTcCs7tj|||�}tjj|||�}||fS)z�Generates sequences of xs and ps for a Normal CDF.

    mu: parameter
    sigma: parameter
    low: float
    high: float
    n: number of points to render

    returns: numpy arrays (xs, ps)
    )rr�rrr�)r�rr�r�r�rr�rrr�RenderNormalCdfOsrUr�cCsD||kr|}tj|||�}d|||}||fS)z�Generates sequences of xs and ps for a Pareto CDF.

    xmin: parameter
    alpha: parameter
    low: float
    high: float
    n: number of points to render

    returns: numpy arrays (xs, ps)
    r)rr�)�xminrJr�r�r�rr�rrr�RenderParetoCdf_s
rWc@s�eZdZdZddddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dddd�Zddd�Zdd�Z
dS)�Betaz\Represents a Beta distribution.

    See http://en.wikipedia.org/wiki/Beta_distribution
    rNcCs1||_||_|dk	r$|nd|_dS)z Initializes a Beta distribution.Nr.)rJ�betar1)r rJrYr1rrrr!ws		z
Beta.__init__cCs.|\}}|j|7_|j|7_dS)zOUpdates a Beta distribution.

        data: pair of int (heads, tails)
        N)rJrY)r r�headsZtailsrrrr}szBeta.UpdatecCs|j|j|jS)z'Computes the mean of this distribution.)rJrY)r rrrr��sz	Beta.MeancCs&|jd}|jd}|||S)z7Computes the value with maximum a posteori probability.r)rJrY)r r�r�rrrr��s

zBeta.MAPcCstj|j|j�S)z2Generates a random variate from this distribution.)r�betavariaterJrY)r rrrr��szBeta.RandomcCs%|f}tjj|j|j|�S)zVGenerates a random sample from this distribution.

        n: int sample size
        )rrrYrJ)r r��sizerrrr��s	zBeta.SamplecCs"||jdd||jdS)zEvaluates the PDF at x.r)rJrY)r rrrr�EvalPdf�szBeta.EvalPdfrcs�|dkr$�jdk	r$�j}�jdksB�jdkr^�j�}|j�}|S�fdd�t��D�}�fdd�|D�}ttt||��d|�}|S)a�Returns a Pmf of this distribution.

        Note: Normally, we just evaluate the PDF at a sequence
        of points and treat the probability density as a probability
        mass.

        But if alpha or beta is less than one, we have to be
        more careful because the PDF goes to infinity at x=0
        and x=1.  In that case we evaluate the CDF and compute
        differences.

        The result is a little funny, because the values at 0 and 1
        are not symmetric.  Nevertheless, it is a reasonable discrete
        model of the continuous distribution, and behaves well as
        the number of values increases.
        Nrcsg|]}|�d�qS)g�?r)rnr()�stepsrrro�s	z Beta.MakePmf.<locals>.<listcomp>csg|]}�j|��qSr)r])rnr)r rrro�s	r1)	r1rJrYryr�r+r?r7rv)r r^r1r�r�rZprobsr)r r^rr��s	!zBeta.MakePmfcsM�fdd�t��D�}tj|j|j|�}t||�}|S)z%Returns the CDF of this distribution.csg|]}|�d�qS)g�?r)rnr()r^rrro�s	z Beta.MakeCdf.<locals>.<listcomp>)r+rZbetaincrJrYr5)r r^rr�r�r)r^rry�szBeta.MakeCdfcCs2tj|�d}tj|j|j|�}|S)zmReturns the given percentiles from this distribution.

        ps: scalar, array, or list of [0-100]
        r�)rr�rZ
betaincinvrJrY)r r�rrrrr��szBeta.Percentile)r*r+r,r-r!rr�r�r�r�r]r�ryr�rrrrrXrs	rXc@sseZdZdZdddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	ddd�Z
dS)�	DirichletzfRepresents a Dirichlet distribution.

    See http://en.wikipedia.org/wiki/Dirichlet_distribution
    rNcCs_|dkrtd��||_tj|dtj�||_|dk	rR|nd|_dS)z�Initializes a Dirichlet distribution.

        n: number of dimensions
        conc: concentration parameter (smaller yields more concentration)
        label: string label
        rz0A Dirichlet distribution with n<2 makes no senser�Nr.)r`r�r�onesr�paramsr1)r r�Zconcr1rrrr!�s
	zDirichlet.__init__cCs't|�}|jd|�|7<dS)ztUpdates a Dirichlet distribution.

        data: sequence of observations, in order corresponding to params
        N)r>ra)r rrdrrrr�szDirichlet.UpdatecCs#tjj|j�}||j�S)zlGenerates a random variate from this distribution.

        Returns: normalized vector of fractions
        )rrr	rar)r rrrrr��szDirichlet.RandomcCsOt|�}|j|krdS|}|j�}|d|�|}|j�S)z�Computes the likelihood of the data.

        Selects a random vector of probabilities from this distribution.

        Returns: float probability
        rN)r>r�r��prod)r rrdrr�qrrrr�szDirichlet.LikelihoodcCsXt|�}|j|kr%td�S|j�}tj|d|��|}|j�S)z�Computes the log likelihood of the data.

        Selects a random vector of probabilities from this distribution.

        Returns: float log probability
        z-infN)r>r�rr�rr3r)r rrdrr$rrrr	s
zDirichlet.LogLikelihoodcCs-|jj�}|j|}t|||�S)z�Computes the marginal distribution of the ith element.

        See http://en.wikipedia.org/wiki/Dirichlet_distribution
        #Marginal_distributions

        i: int

        Returns: Beta object
        )rarrX)r r(�alpha0rJrrr�MarginalBeta	s

zDirichlet.MarginalBetacCs5|jj�}|j|}tt||�d|�S)z�Makes a predictive distribution.

        xs: values to go into the Pmf

        Returns: Pmf that maps from x to the mean prevalence of x
        r1)rarr?rv)r rr1rdr�rrr�
PredictivePmf!	s
zDirichlet.PredictivePmf)r*r+r,r-r!rr�rrrerfrrrrr_�sr_cCstjj||�S)z{Compute the binomial coefficient "n choose k".

    n: number of trials
    k: number of successes

    Returns: float
    )�scipy�miscZcomb)r�r�rrr�BinomialCoef-	sricCs;|tj|�|tj|�||tj||�S)z�Computes the log of the binomial coefficient.

    http://math.stackexchange.com/questions/64716/
    approximating-the-logarithm-of-the-binomial-coefficient

    n: number of trials
    k: number of successes

    Returns: float
    )rbr3)r�r�rrr�LogBinomialCoef8	srjcCsit|�}tjjdd|�}|j�|rFt||�}ntj|�}|j�||fS)z�Generates data for a normal probability plot.

    ys: sequence of values
    jitter: float magnitude of jitter added to the ys 

    returns: numpy arrays xs, ys
    rr)r>rr�normalr��Jitterr�)r�jitterr�rrrr�NormalProbabilityF	s

rng�?cCs&t|�}tjjd||�|S)z�Jitters the values by adding a uniform variate in (-jitter, jitter).

    values: sequence
    jitter: scalar magnitude of jitter
    
    returns: new numpy array
    r)r>rrrk)r�rmr�rrrrl[	srlz0.8c	Ks�t|�\}}t|�\}}tj|�}t|||�}tj|d|dd�t|�\}}tj|||�dS)z�Makes a normal probability plot with a fitted line.

    sample: sequence of numbers
    fit_color: color string for the fitted line
    options: passed along to Plot
    �colorr1�modelN)rn�MeanVarrbr��FitLine�	thinkplot�Plot)	r$Z	fit_colorrwrr�meanr��std�fitrrr�NormalProbabilityPlotg	srxcCs
tj|�S)zHComputes mean.

    xs: sequence of values

    returns: float mean
    )rru)rrrrr�y	sr�cCsOtj|�}|dkr'|j�}||}tj||�t|�|S)z�Computes variance.

    xs: sequence of values
    mu: option known mean
    ddof: delta degrees of freedom

    returns: float
    N)rr�ru�dotr>)rr��ddofrrrrr��	s
	
r�cCst|||�}tj|�S)z�Computes standard deviation.

    xs: sequence of values
    mu: option known mean
    ddof: delta degrees of freedom

    returns: float
    )r�rbr�)rr�rzr�rrrr��	s	r�cCs7tj|�}|j�}t|||�}||fS)z�Computes mean and variance.

    Based on http://stackoverflow.com/questions/19391149/
    numpy-mean-and-variance-from-single-function

    xs: sequence of values
    ddof: delta degrees of freedom
    
    returns: pair of float, mean and var
    )rr�rur�)rrzru�s2rrrrq�	srqg{�G�z�?cCs1t|t|��}t|�||�}|S)z�Trims the largest and smallest elements of t.

    Args:
        t: sequence of numbers
        p: fraction of values to trim off each end

    Returns:
        sequence of values
    )�intr>rs)r�rr�rrr�Trim�	s
r}cCst||�}t|�S)z�Computes the trimmed mean of a sequence of numbers.

    Args:
        t: sequence of numbers
        p: fraction of values to trim off each end

    Returns:
        float
    )r}r�)r�rrrr�TrimmedMean�	s
r~cCs+t||�}t|�\}}||fS)z�Computes the trimmed mean and variance of a sequence of numbers.

    Side effect: sorts the list.

    Args:
        t: sequence of numbers
        p: fraction of values to trim off each end

    Returns:
        float
    )r}rq)r�rr�r�rrr�TrimmedMeanVar�	src	Csx|j�|j�}t|�t|�}}|j�}|j�}||||||}|tj|�}|S)zpCompute Cohen's d.

    group1: Series or NumPy array
    group2: Series or NumPy array

    returns: float
    )rur>r�rbr�)	Zgroup1Zgroup2�diff�n1�n2Zvar1Zvar2Z
pooled_varr2rrr�CohenEffectSize�	sr�cCs|tj|�}tj|�}|dkr9tj|�}|dkrTtj|�}tj||||�t|�}|S)z�Computes Cov(X, Y).

    Args:
        xs: sequence of values
        ys: sequence of values
        meanx: optional float mean of xs
        meany: optional float mean of ys

    Returns:
        Cov(X, Y)
    N)rr�ruryr>)rr�meanx�meany�covrrr�Cov�	s$r�cCsltj|�}tj|�}t|�\}}t|�\}}t||||�tj||�}|S)z�Computes Corr(X, Y).

    Args:
        xs: sequence of values
        ys: sequence of values

    Returns:
        Corr(X, Y)
    )rr�rqr�rbr�)rrr��varxr�Zvary�corrrrr�Corr
s
&r�cCs<||d�}|j|�|d�}t||�}|S)z�Computes the serial correlation of a series.

    series: Series
    lag: integer number of intervals to shift

    returns: float correlation
    N)�shiftr�)�series�lagrrr�rrr�
SerialCorr!
sr�cCs7tj|�j�}tj|�j�}t||�S)z�Computes Spearman's rank correlation.

    Args:
        xs: sequence of values
        ys: sequence of values

    Returns:
        float Spearman's correlation
    )r;r<�rankr�)rrZxranksZyranksrrr�SpearmanCorr/
s
r�cCs_t|�}t|dtd��}t|�}t|ddd��}dd�|D�}|S)z�Returns a list of ranks corresponding to the elements in t.

    Args:
        t: sequence of numbers
    
    Returns:
        list of integer ranks, starting at 1
    �keyrcSs|ddS)Nrrr)�triprrr�<lambda>Q
szMapToRanks.<locals>.<lambda>cSsg|]}|dd�qS)rrr)rnr�rrrroT
s	zMapToRanks.<locals>.<listcomp>)�	enumeratersr)r��pairsZsorted_pairsZrankedZresortedZranksrrr�
MapToRanks>
s
r�cCsOt|�\}}t|�}t||||�|}|||}||fS)z�Computes a linear least squares fit for ys as a function of xs.

    Args:
        xs: sequence of values
        ys: sequence of values

    Returns:
        tuple of (intercept, slope)
    )rqr�r�)rrr�r�r��slope�interrrr�LeastSquaresX
s

r�cCs'tj|�}|||}||fS)zrFits a line to the given data.

    xs: sequence of x

    returns: tuple of numpy arrays (sorted xs, fit ys)
    )rr�)rr�r�Zfit_xsZfit_ysrrrrrk
srrcCs4tj|�}tj|�}||||}|S)z�Computes residuals for a linear fit with parameters inter and slope.

    Args:
        xs: independent variable
        ys: dependent variable
        inter: float intercept
        slope: float slope

    Returns:
        list of residuals
    )rr�)rrr�r��resrrr�	Residualsw
sr�cCsdt|�t|�S)z�Computes the coefficient of determination (R^2) for given residuals.

    Args:
        ys: dependent variable
        res: residuals
        
    Returns:
        float coefficient of determination
    r)r�)rr�rrr�CoefDetermination�
s
r�ccsTtjdd�}|Vtjd|d�}xtj|||�}|Vq1WdS)z�Generates standard normal variates with serial correlation.

    rho: target coefficient of correlation

    Returns: iterable
    rrrN)r�gaussrbr�)�rhorrrrr�CorrelatedGenerator�
sr�ccs(x!t|�D]}|||Vq
WdS)z�Generates normal variates with serial correlation.

    mu: mean of variate
    sigma: standard deviation of variate
    rho: target coefficient of correlation

    Returns: iterable
    N)r�)r�rr�rrrr�CorrelatedNormalGenerator�
s	r�cs't�fdd�|D��t|�S)z'Computes the kth raw moment of xs.
    c3s|]}|�VqdS)Nr)rnr)r�rrr��
szRawMoment.<locals>.<genexpr>)rr>)rr�r)r�r�	RawMoment�
sr�cs9t|d��t��fdd�|D��t|�S)z+Computes the kth central moment of xs.
    rc3s|]}|��VqdS)Nr)rnr)r�rurrr��
sz CentralMoment.<locals>.<genexpr>)r�rr>)rr�r)r�rur�
CentralMoment�
sr�cCs3t|d�}tj|�}t||�||S)z0Computes the kth standardized moment of xs.
    r)r�rbr�)rr�r�rvrrr�StandardizedMoment�
sr�cCs
t|d�S)zComputes skewness.
    r)r�)rrrr�Skewness�
sr�cCst|�}|jd�S)z�Computes the median (50th percentile) of a sequence.

    xs: sequence or anything else that can initialize a Cdf

    returns: float
    g�?)r5r�)rr�rrrr��
sr�cCs(t|�}|jd�|jd�fS)z�Computes the interquartile of a sequence.

    xs: sequence or anything else that can initialize a Cdf

    returns: pair of floats
    g�?g�?)r5r�)rr�rrr�IQR�
sr�cCsOt|�}t|d�}t|d�}tj|�}d|||}|S)z*Computes the Pearson median skewness.
    rrr)r�r�r�rbr�)r�medianrur�rv�gprrr�PearsonMedianSkewness�
sr�c@s1eZdZdZddd�Zdd�ZdS)�FixedWidthVariablesz4Represents a set of variables in a fixed width file.rcCsR||_|ddg||_|jjtj�jj�|_|d|_dS)z�Initializes.

        variables: DataFrame
        index_base: are the indices 0 or 1 based?

        Attributes:
        colspecs: list of (start, end) index tuples
        names: list of string variable names
        �start�end�nameN)�	variables�colspecs�astyperr|r��tolist�names)r r��
index_baserrrr!�
s
	!zFixedWidthVariables.__init__cKs(tj|d|jd|j|�}|S)zgReads a fixed width ASCII file.

        filename: string filename

        returns: DataFrame
        r�r�)r;Zread_fwfr�r�)r �filenamerw�dfrrr�ReadFixedWidths
		z"FixedWidthVariables.ReadFixedWidthN)r*r+r,r-r!r�rrrrr��
sr�cKs�tdtdtdtdtdt�}g}t||���}x�|D]�}tjd|�}|sgqFt|jd��}|j�}|dd�\}	}
}|
j�}
|	j	d	�r�t
}	n
||	}	d
j|dd��jd�}|j
||	|
||f�qFWWdQRXd
ddddg}
tj|d|
�}|jjd�|d<d|jt|�ddf<t|dd�}|S)z�Reads a Stata dictionary file.

    dct_file: string filename
    options: dict of options passed to open()

    returns: FixedWidthVariables object
    �byter|�longr�doublez_column\(([^)]*)\)r�rE� N�"r��typer��fstring�desc�columnsr�rr�r&)r7r|rr
�re�search�group�split�lower�
startswithrE�join�stripr�r;�	DataFramer�r�r5r>r�)Zdct_filerw�type_mapZvar_info�f�line�matchr�r��vtyper�r�Z	long_descr�r��dctrrr�ReadStataDcts,'
	
"'r�cCs1|dkrt|�}tjj||dd�S)z�Draw a sample from xs with the same length as xs.

    xs: sequence
    n: sample size (default: len(xs))

    returns: NumPy array
    N�replaceT)r>rr�choice)rr�rrr�Resample9sr�FcCs/tjj|j|d|�}|j|}|S)z�Choose a sample of rows from a DataFrame.

    df: DataFrame
    nrows: number of rows
    replace: whether to sample with replacement

    returns: DataDf
    r�)rrr�r�r5)r��nrowsr��indicesr$rrr�
SampleRowsFs	
r�cCst|t|�dd�S)zPResamples rows from a DataFrame.

    df: DataFrame

    returns: DataFrame
    r�T)r�r>)r�rrr�ResampleRowsTsr�ZfinalwgtcCsB||}tt|��}|jt|��}|j|}|S)z�Resamples a DataFrame using probabilities proportional to given column.

    df: DataFrame
    column: string column name to use as weights

    returns: DataFrame
    )r5r7r�r>r5)r��column�weightsr�r�r$rrr�ResampleRowsWeighted^s


r�cCs.|j\}}t||d�}||fS)z|Selects the row from a sorted array that maps to percentile p.

    p: float 0--100

    returns: NumPy array (one row)
    r�)r�r|)r�r�rows�colsr�rrr�
PercentileRowmsr�cs�t|�}t|d�}tj||f��x't|�D]\}}|�|f<q>Wtj�dd���fdd�|D�}|S)a�Given a collection of lines, selects percentiles along vertical axis.

    For example, if ys_seq contains simulation results like ys as a
    function of time, and percents contains (5, 95), the result would
    be a 90% CI for each vertical slice of the simulation results.

    ys_seq: sequence of lines (y values)
    percents: list of percentiles (0-100) to select

    returns: list of NumPy arrays, one for each percentile
    r�axiscsg|]}t�|��qSr)r�)rnr)r�rrro�s	z"PercentileRows.<locals>.<listcomp>)r>r�zerosr�r�)Zys_seq�percentsr��ncolsr(rr�r)r�r�PercentileRowsysr�cKstjj|||�S)zpSmooths a NumPy array with a Gaussian filter.

    xs: sequence
    sigma: standard deviation of the filter
    )r�filtersZgaussian_filter1d)rrrwrrr�Smooth�sr�c@speZdZdZdd�Zddd�Zdd�Zd	d
d�Zdd
�Zdd�Z	dd�Z
d	S)�HypothesisTestzRepresents a hypothesis test.cCs;||_|j�|j|�|_d|_d|_dS)zFInitializes.

        data: data in whatever form is relevant
        N)r�	MakeModel�
TestStatistic�actual�
test_stats�test_cdf)r rrrrr!�s
	
	zHypothesisTest.__init__i�cs^�fdd�t|�D��_t�j��_t�fdd��jD��}||S)z�Computes the distribution of the test statistic and p-value.

        iters: number of iterations

        returns: float p-value
        cs%g|]}�j�j���qSr)r��RunModel)rnr�)r rrro�s	z)HypothesisTest.PValue.<locals>.<listcomp>c3s$|]}|�jkrdVqdS)rN)r�)rnr)r rrr��sz(HypothesisTest.PValue.<locals>.<genexpr>)r+r�r5r�r)r �iters�countr)r r�PValue�s
"zHypothesisTest.PValuecCs
t|j�S)zDReturns the largest test statistic seen during simulations.
        )r�r�)r rrr�MaxTestStat�szHypothesisTest.MaxTestStatNcCs3dd�}||j�tj|jd|�dS)zCDraws a Cdf with vertical lines at the observed test stat.
        cSs&tj||gddgdd�dS)zDraws a vertical line at x.rrroz0.8N)rsrt)rrrr�VertLine�sz(HypothesisTest.PlotCdf.<locals>.VertLiner1N)r�rsr5r�)r r1r�rrr�PlotCdf�s
zHypothesisTest.PlotCdfcCs
t��dS)z^Computes the test statistic.

        data: data in whatever form is relevant        
        N)r�)r rrrrr��szHypothesisTest.TestStatisticcCsdS)z.Build a model of the null hypothesis.
        Nr)r rrrr��szHypothesisTest.MakeModelcCs
t��dS)zORun the model of the null hypothesis.

        returns: simulated data
        N)r�)r rrrr��szHypothesisTest.RunModel)r*r+r,r-r!r�r�r�r�r�r�rrrrr��s
r�cCsdS)Nrrrrr�main�sr��__main__)�r-�
__future__rrr'rXrqrbrr��collectionsr�operatorrrs�numpyrr;rgrrrZ
scipy.specialr	�ior
r�rOrrrr�objectrr0r/r�r?r�r�r�r�r�r�r�r�r�r�r5r�r�r�rr�	Exceptionr�rrrrr6rrr!r�r�r�r�r*r,r-r/r1r2r3r4r7r8r;r=r?r@rArCrErFrGrKrMrPrQrRrSrTrUrWrXr_rirjrnrlrxr�r�r�rqr}r~rr�r�r�r�r�r�r�rrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r*rrrr�<module>s	
"�"�_?




�U


dB#!*	
]^



$&

	?