In [1]:
from applpy import *


-----------------
Welcome to ApplPy
-----------------
ApplPy Procedures

Procedure Notation

Capital letters are random variables
Lower case letters are number
Greek letters are parameters
gX indicates a function
n and r are positive integers where n>=r
Square brackets [] denote a list
Curly bracks {} denote an optional variable


RV Class Procedures
X.variate(n,x),X.verifyPDF()

Functional Form Conversion
CDF(X,{x}),CHF(X,{x}),HF(X,{x}),IDF(X,{x})
PDF(X,{x}),SF(X,{x}),BootstrapRV([data])
Convert(X,{x})

Procedures on One Random Variable
ConvolutionIID(X,n),CoefOfVar(X),ExpectedValue(X,gx)
Kurtosis(X),MaximumIID(X,n),Mean(X),MGF(X)
MinimumIID(X,n),OrderStat(X,n,r),ProductIID(X,n)
Skewness(X),Transform(X,gX),Truncate(X,[x1,x2])
Variance(X)

Procedures on Two Random Variables
Convolution(X,Y),Maximum(X,Y),Minimum(X,Y)
Mixture([p1,p2],[X,Y]),Product(X,Y)

Statistics Procedures
KSTest(X,[sample]), MOM(X,[sample],[parameters])
MLE(X,[sample],[parameters],censor)

Utilities
PlotDist(X,{[x1,x2]}),PlotDisplay([plotlist],{[x1,x2]})
PPPlot(X,[sample]),QQPlot(X,[sample])

Bayesian Procedures
CredibleSet(X,alpha), JeffreysPrior(X,low,high,param)
Posterior(X,Y,[data],param)
PosteriorPredictive(X,Y,[data],param)

Continuous Distributions
ArcSinRV(),ArcTanRV(alpha,phi),BetaRV(alpha,beta)
CauchyRV(a,alpha)ChiRV(N),ChiSquareRV(N),ErlangRV(theta,N)
ErrorRV(mu,alpha,d),ErrorIIRV(a,b,c),ExponentialRV(theta)
ExponentialPowerRV(theta,kappa),ExtremeValueRV(alpha,beta)
FRV(n1,n2),GammaRV(theta,kappa),GompertzRV(theta,kappa)
GeneralizedParetoRV(theta,delta,kappa),IDBRV(theta,delta,kappa)
InverseGaussianRV(theta,mu),InverseGammaRV(alpha,beta)
KSRV(n),LaPlaceRV(omega,theta), LogGammaRV(alpha,beta)
LogisticRV(kappa,theta),LogLogisticRV(theta,kappa)
LogNormalRV(mu,sigma),LomaxRV(kappa,theta)
MakehamRV(theta,delta,kappa),MuthRV(kappa),NormalRV(mu,sigma)
ParetoRV(theta,kappa),RayleighRV(theta),TriangularRV(a,b,c)
TRV(N),UniformRV(a,b),WeibullRV(theta,kappa)

Discrete Distributions
BenfordRV(),BinomialRV(n,p),GeometricRV(p),PoissonRV(theta)
IPython console for SymPy 0.7.5 (Python 2.7.6-64-bit) (ground types: python)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

Documentation can be found at http://www.sympy.org
WARNING: Hook shutdown_hook is deprecated. Use the atexit module instead.

In [2]:
# Exactness in Lieu of CLT

In [3]:
X=ExponentialRV(1)

In [4]:
n=3

In [5]:
Y=ConvolutionIID(X,n)

In [6]:
Exact=Transform(Y,[[x/n],[0,oo]])

In [8]:
Exact.display()


continuous pdf with support [0, oo]:
Out[8]:
$$\begin{bmatrix}\frac{27 x^{2}}{2} e^{- 3 x}\end{bmatrix}$$

In [10]:
Exact.variate(s=.90)


Out[10]:
$$\begin{bmatrix}1.77410677927807\end{bmatrix}$$

In [11]:
CLT=NormalRV(1,1/sqrt(n))

In [13]:
CLT.variate(s=.90)


Out[13]:
$$\begin{bmatrix}1.73990414134756\end{bmatrix}$$

In [15]:
X=ExponentialRV(1)
sample_size=range(1,31)
percentiles=[.90,.95,.99]
header=['n','Percentile','Difference']
table=[]
for n in sample_size:
    Y=ConvolutionIID(X,n)
    Exact=Transform(Y,[[x/n],[0,oo]])
    CLT=NormalRV(1,1/sqrt(n))
    for i in percentiles:
        exact_value=Exact.variate(s=i)[0]
        clt_value=CLT.variate(s=i)[0]
        difference=exact_value-clt_value
        table.append([n,i,float(difference)])

In [17]:
import pandas as pd

In [18]:
exact_minus_clt=pd.DataFrame(table,columns=header)

In [22]:
difference_by_pct=exact_minus_clt.pivot_table('Difference',rows='n',cols='Percentile',aggfunc='sum')

In [24]:
difference_by_pct[:10]


Out[24]:
Percentile 0.9 0.95 0.99
n
1 0.021034 0.350879 1.278822
2 0.038666 0.208845 0.674200
3 0.034203 0.148941 0.458865
4 0.029420 0.115987 0.348105
5 0.025591 0.095103 0.280551
6 0.022588 0.080664 0.235020
7 0.020201 0.070075 0.202240
8 0.018267 0.061971 0.177507
9 0.016673 0.055565 0.158179
10 0.015337 0.050373 0.142656

10 rows × 3 columns


In [27]:
difference_by_pct.plot(title='Convergence of CLT by Sample Size and Percentile')


Out[27]:
<matplotlib.axes.AxesSubplot at 0xf41f890>

In [ ]: