<s> '''<STR_LIT>''' <EOL> from __future__ import print_function <EOL> from statsmodels . compat . python import lrange <EOL> import numpy as np <EOL> from statsmodels . iolib . table import SimpleTable <EOL> class StatTestMC ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dgp , statistic ) : <EOL> self . dgp = dgp <EOL> self . statistic = statistic <EOL> def run ( self , nrepl , statindices = None , dgpargs = [ ] , statsargs = [ ] ) : <EOL> '''<STR_LIT>''' <EOL> self . nrepl = nrepl <EOL> self . statindices = statindices <EOL> self . dgpargs = dgpargs <EOL> self . statsargs = statsargs <EOL> dgp = self . dgp <EOL> statfun = self . statistic <EOL> mcres0 = statfun ( dgp ( * dgpargs ) , * statsargs ) <EOL> self . nreturn = nreturns = len ( np . ravel ( mcres0 ) ) <EOL> if statindices is None : <EOL> mcres = np . zeros ( nrepl ) <EOL> mcres [ <NUM_LIT:0> ] = mcres0 <EOL> for ii in range ( <NUM_LIT:1> , repl - <NUM_LIT:1> , nreturns ) : <EOL> x = dgp ( * dgpargs ) <EOL> mcres [ ii ] = statfun ( x , * statsargs ) <EOL> else : <EOL> self . nreturn = nreturns = len ( statindices ) <EOL> self . mcres = mcres = np . zeros ( ( nrepl , nreturns ) ) <EOL> mcres [ <NUM_LIT:0> ] = [ mcres0 [ i ] for i in statindices ] <EOL> for ii in range ( <NUM_LIT:1> , nrepl - <NUM_LIT:1> ) : <EOL> x = dgp ( * dgpargs ) <EOL> ret = statfun ( x , * statsargs ) <EOL> mcres [ ii ] = [ ret [ i ] for i in statindices ] <EOL> self . mcres = mcres <EOL> def histogram ( self , idx = None , critval = None ) : <EOL> '''<STR_LIT>''' <EOL> if self . mcres . ndim == <NUM_LIT:2> : <EOL> if not idx is None : <EOL> mcres = self . mcres [ : , idx ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> mcres = self . mcres <EOL> if critval is None : <EOL> histo = np . histogram ( mcres , bins = <NUM_LIT:10> ) <EOL> else : <EOL> if not critval [ <NUM_LIT:0> ] == - np . inf : <EOL> bins = np . r_ [ - np . inf , critval , np . inf ] <EOL> if not critval [ <NUM_LIT:0> ] == - np . inf : <EOL> bins = np . r_ [ bins , np . inf ] <EOL> histo = np . histogram ( mcres , <EOL> bins = np . r_ [ - np . inf , critval , np . inf ] ) <EOL> self . histo = histo <EOL> self . cumhisto = np . cumsum ( histo [ <NUM_LIT:0> ] ) * <NUM_LIT:1.> / self . nrepl <EOL> self . cumhistoreversed = np . cumsum ( histo [ <NUM_LIT:0> ] [ : : - <NUM_LIT:1> ] ) [ : : - <NUM_LIT:1> ] * <NUM_LIT:1.> / self . nrepl <EOL> return histo , self . cumhisto , self . cumhistoreversed <EOL> def get_mc_sorted ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . mcressort = np . sort ( self . mcres , axis = <NUM_LIT:0> ) <EOL> return self . mcressort <EOL> def quantiles ( self , idx = None , frac = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) : <EOL> '''<STR_LIT>''' <EOL> if self . mcres . ndim == <NUM_LIT:2> : <EOL> if not idx is None : <EOL> mcres = self . mcres [ : , idx ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> mcres = self . mcres <EOL> self . frac = frac = np . asarray ( frac ) <EOL> mc_sorted = self . get_mc_sorted ( ) [ : , idx ] <EOL> return frac , mc_sorted [ ( self . nrepl * frac ) . astype ( int ) ] <EOL> def cdf ( self , x , idx = None ) : <EOL> '''<STR_LIT>''' <EOL> idx = np . atleast_1d ( idx ) . tolist ( ) <EOL> mc_sorted = self . get_mc_sorted ( ) <EOL> x = np . asarray ( x ) <EOL> if x . ndim > <NUM_LIT:1> and x . shape [ <NUM_LIT:1> ] == len ( idx ) : <EOL> use_xi = True <EOL> else : <EOL> use_xi = False <EOL> x_ = x <EOL> probs = [ ] <EOL> for i , ix in enumerate ( idx ) : <EOL> if use_xi : <EOL> x_ = x [ : , i ] <EOL> probs . append ( np . searchsorted ( mc_sorted [ : , ix ] , x_ ) / float ( self . nrepl ) ) <EOL> probs = np . asarray ( probs ) . T <EOL> return x , probs <EOL> def plot_hist ( self , idx , distpdf = None , bins = <NUM_LIT:50> , ax = None , kwds = None ) : <EOL> '''<STR_LIT>''' <EOL> if kwds is None : <EOL> kwds = ( { } , { } ) <EOL> if self . mcres . ndim == <NUM_LIT:2> : <EOL> if not idx is None : <EOL> mcres = self . mcres [ : , idx ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> mcres = self . mcres <EOL> lsp = np . linspace ( mcres . min ( ) , mcres . max ( ) , <NUM_LIT:100> ) <EOL> import matplotlib . pyplot as plt <EOL> fig = plt . figure ( ) <EOL> plt . hist ( mcres , bins = bins , normed = True , ** kwds [ <NUM_LIT:0> ] ) <EOL> plt . plot ( lsp , distpdf ( lsp ) , '<STR_LIT:r>' , ** kwds [ <NUM_LIT:1> ] ) <EOL> def summary_quantiles ( self , idx , distppf , frac = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] , <EOL> varnames = None , title = None ) : <EOL> '''<STR_LIT>''' <EOL> idx = np . atleast_1d ( idx ) <EOL> quant , mcq = self . quantiles ( idx , frac = frac ) <EOL> crit = distppf ( np . atleast_2d ( quant ) . T ) <EOL> mml = [ ] <EOL> for i , ix in enumerate ( idx ) : <EOL> mml . extend ( [ mcq [ : , i ] , crit [ : , i ] ] ) <EOL> mmlar = np . column_stack ( [ quant ] + mml ) <EOL> if title : <EOL> title = title + '<STR_LIT>' <EOL> else : <EOL> title = '<STR_LIT>' <EOL> if varnames is None : <EOL> varnames = [ '<STR_LIT>' % i for i in range ( mmlar . shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) ] <EOL> headers = [ '<STR_LIT>' ] + [ '<STR_LIT>' % ( i , t ) for i in varnames for t in [ '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> return SimpleTable ( mmlar , <EOL> txt_fmt = { '<STR_LIT>' : [ "<STR_LIT>" ] + [ "<STR_LIT>" ] * ( mmlar . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ) } , <EOL> title = title , <EOL> headers = headers ) <EOL> def summary_cdf ( self , idx , frac , crit , varnames = None , title = None ) : <EOL> '''<STR_LIT>''' <EOL> idx = np . atleast_1d ( idx ) <EOL> mml = [ ] <EOL> for i in range ( len ( idx ) ) : <EOL> mml . append ( self . cdf ( crit [ : , i ] , [ idx [ i ] ] ) [ <NUM_LIT:1> ] . ravel ( ) ) <EOL> mmlar = np . column_stack ( [ frac ] + mml ) <EOL> if title : <EOL> title = title + '<STR_LIT>' <EOL> else : <EOL> title = '<STR_LIT>' <EOL> if varnames is None : <EOL> varnames = [ '<STR_LIT>' % i for i in range ( mmlar . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ) ] <EOL> headers = [ '<STR_LIT>' ] + varnames <EOL> return SimpleTable ( mmlar , <EOL> txt_fmt = { '<STR_LIT>' : [ "<STR_LIT>" ] + [ "<STR_LIT>" ] * ( np . array ( mml ) . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> ) } , <EOL> title = title , <EOL> headers = headers ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from scipy import stats <EOL> from statsmodels . iolib . table import SimpleTable <EOL> from statsmodels . sandbox . stats . diagnostic import ( <EOL> acorr_ljungbox , unitroot_adf ) <EOL> def randwalksim ( nobs = <NUM_LIT:100> , drift = <NUM_LIT:0.0> ) : <EOL> return ( drift + np . random . randn ( nobs ) ) . cumsum ( ) <EOL> def normalnoisesim ( nobs = <NUM_LIT> , loc = <NUM_LIT:0.0> ) : <EOL> return ( loc + np . random . randn ( nobs ) ) <EOL> def adf20 ( x ) : <EOL> return unitroot_adf ( x , <NUM_LIT:2> , trendorder = <NUM_LIT:0> , autolag = None ) <EOL> print ( '<STR_LIT>' ) <EOL> from statsmodels . sandbox . stats . diagnostic import acorr_ljungbox <EOL> def lb4 ( x ) : <EOL> s , p = acorr_ljungbox ( x , lags = <NUM_LIT:4> ) <EOL> return s [ - <NUM_LIT:1> ] , p [ - <NUM_LIT:1> ] <EOL> def lb1 ( x ) : <EOL> s , p = acorr_ljungbox ( x , lags = <NUM_LIT:1> ) <EOL> return s [ <NUM_LIT:0> ] , p [ <NUM_LIT:0> ] <EOL> def lb ( x ) : <EOL> s , p = acorr_ljungbox ( x , lags = <NUM_LIT:4> ) <EOL> return np . r_ [ s , p ] <EOL> print ( '<STR_LIT>' ) <EOL> mc1 = StatTestMC ( normalnoisesim , lb ) <EOL> mc1 . run ( <NUM_LIT> , statindices = lrange ( <NUM_LIT:8> ) ) <EOL> print ( mc1 . histogram ( <NUM_LIT:1> , critval = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] ) ) <EOL> print ( mc1 . quantiles ( <NUM_LIT:1> ) ) <EOL> print ( mc1 . quantiles ( <NUM_LIT:0> ) ) <EOL> print ( mc1 . histogram ( <NUM_LIT:0> ) ) <EOL> print ( mc1 . summary_quantiles ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , stats . chi2 ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) . ppf , <EOL> varnames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> title = '<STR_LIT>' ) ) <EOL> print ( mc1 . cdf ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> print ( mc1 . cdf ( <NUM_LIT> , <NUM_LIT:3> ) ) <EOL> print ( mc1 . cdf ( <NUM_LIT> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> frac = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> ] <EOL> crit = stats . chi2 ( [ <NUM_LIT:2> , <NUM_LIT:4> ] ) . ppf ( np . atleast_2d ( frac ) . T ) <EOL> print ( mc1 . summary_cdf ( [ <NUM_LIT:1> , <NUM_LIT:3> ] , frac , crit , title = '<STR_LIT>' ) ) <EOL> crit = stats . chi2 ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) . ppf ( np . atleast_2d ( frac ) . T ) <EOL> print ( mc1 . summary_cdf ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , frac , crit , <EOL> varnames = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> title = '<STR_LIT>' ) ) <EOL> print ( mc1 . cdf ( crit , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) [ <NUM_LIT:1> ] . shape ) <EOL> '''<STR_LIT>''' <EOL> doplot = <NUM_LIT:0> <EOL> if doplot : <EOL> import matplotlib . pyplot as plt <EOL> mc1 . plot_hist ( <NUM_LIT:0> , stats . chi2 ( <NUM_LIT:2> ) . pdf ) <EOL> plt . show ( ) </s>
