<s> from __future__ import division , print_function , absolute_import <EOL> import warnings <EOL> import numpy as np <EOL> from numpy . polynomial . hermite_e import HermiteE <EOL> from numpy . testing import ( TestCase , run_module_suite , assert_equal , <EOL> assert_raises , assert_allclose ) <EOL> import numpy . testing as npt <EOL> from scipy . misc import factorial , factorial2 <EOL> from scipy . special import gamma <EOL> import scipy . stats as stats <EOL> from statsmodels . distributions . edgeworth import ( _faa_di_bruno_partitions , <EOL> cumulant_from_moments , ExpandedNormal ) <EOL> class TestFaaDiBruno ( TestCase ) : <EOL> def test_neg_arg ( self ) : <EOL> assert_raises ( ValueError , _faa_di_bruno_partitions , - <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , _faa_di_bruno_partitions , <NUM_LIT:0> ) <EOL> def test_small_vals ( self ) : <EOL> for n in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> for ks in _faa_di_bruno_partitions ( n ) : <EOL> lhs = sum ( m * k for ( m , k ) in ks ) <EOL> assert_equal ( lhs , n ) <EOL> def _norm_moment ( n ) : <EOL> return ( <NUM_LIT:1> - n % <NUM_LIT:2> ) * factorial2 ( n - <NUM_LIT:1> ) <EOL> def _norm_cumulant ( n ) : <EOL> try : <EOL> return { <NUM_LIT:1> : <NUM_LIT:0> , <NUM_LIT:2> : <NUM_LIT:1> } [ n ] <EOL> except KeyError : <EOL> return <NUM_LIT:0> <EOL> def _chi2_moment ( n , df ) : <EOL> return ( <NUM_LIT:2> ** n ) * gamma ( n + df / <NUM_LIT> ) / gamma ( df / <NUM_LIT> ) <EOL> def _chi2_cumulant ( n , df ) : <EOL> assert n > <NUM_LIT:0> <EOL> return <NUM_LIT:2> ** ( n - <NUM_LIT:1> ) * factorial ( n - <NUM_LIT:1> ) * df <EOL> class TestCumulants ( TestCase ) : <EOL> def test_badvalues ( self ) : <EOL> assert_raises ( ValueError , cumulant_from_moments , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT:0> ) <EOL> assert_raises ( ValueError , cumulant_from_moments , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT:4> ) <EOL> def test_norm ( self ) : <EOL> N = <NUM_LIT:4> <EOL> momt = [ _norm_moment ( j + <NUM_LIT:1> ) for j in range ( N ) ] <EOL> for n in range ( <NUM_LIT:1> , N + <NUM_LIT:1> ) : <EOL> kappa = cumulant_from_moments ( momt , n ) <EOL> assert_allclose ( kappa , _norm_cumulant ( n ) , <EOL> atol = <NUM_LIT> ) <EOL> def test_chi2 ( self ) : <EOL> N = <NUM_LIT:4> <EOL> df = <NUM_LIT:8> <EOL> momt = [ _chi2_moment ( j + <NUM_LIT:1> , df ) for j in range ( N ) ] <EOL> for n in range ( <NUM_LIT:1> , N + <NUM_LIT:1> ) : <EOL> kappa = cumulant_from_moments ( momt , n ) <EOL> assert_allclose ( kappa , _chi2_cumulant ( n , df ) ) <EOL> class TestExpandedNormal ( TestCase ) : <EOL> def test_too_few_cumulants ( self ) : <EOL> assert_raises ( ValueError , ExpandedNormal , [ <NUM_LIT:1> ] ) <EOL> def test_coefficients ( self ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , UserWarning ) <EOL> ne3 = ExpandedNormal ( [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) <EOL> assert_allclose ( ne3 . _coef , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT:6> ] ) <EOL> ne4 = ExpandedNormal ( [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) <EOL> assert_allclose ( ne4 . _coef , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT:6> , <NUM_LIT:1.> / <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT> ] ) <EOL> ne5 = ExpandedNormal ( [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) <EOL> assert_allclose ( ne5 . _coef , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT:6> , <NUM_LIT:1.> / <NUM_LIT> , <NUM_LIT:1.> / <NUM_LIT> , <EOL> <NUM_LIT:1.> / <NUM_LIT> , <NUM_LIT:1.> / <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT> ] ) <EOL> ne33 = ExpandedNormal ( [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] ) <EOL> assert_allclose ( ne33 . _coef , [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT:6> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> / <NUM_LIT> ] ) <EOL> def test_normal ( self ) : <EOL> ne2 = ExpandedNormal ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> x = np . linspace ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ) <EOL> assert_allclose ( ne2 . pdf ( x ) , stats . norm . pdf ( x , loc = <NUM_LIT:3> , scale = <NUM_LIT:2> ) ) <EOL> def test_chi2_moments ( self ) : <EOL> N , df = <NUM_LIT:6> , <NUM_LIT:15> <EOL> cum = [ _chi2_cumulant ( n + <NUM_LIT:1> , df ) for n in range ( N ) ] <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" , UserWarning ) <EOL> ne = ExpandedNormal ( cum , name = '<STR_LIT>' ) <EOL> assert_allclose ( [ _chi2_moment ( n , df ) for n in range ( N ) ] , <EOL> [ ne . moment ( n ) for n in range ( N ) ] ) <EOL> check_pdf ( ne , arg = ( ) , msg = '<STR_LIT>' ) <EOL> check_cdf_ppf ( ne , arg = ( ) , msg = '<STR_LIT>' ) <EOL> check_cdf_sf ( ne , arg = ( ) , msg = '<STR_LIT>' ) <EOL> np . random . seed ( <NUM_LIT> ) <EOL> rvs = ne . rvs ( size = <NUM_LIT> ) <EOL> check_distribution_rvs ( ne , args = ( ) , alpha = <NUM_LIT> , rvs = rvs ) <EOL> def test_pdf_no_roots ( self ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:error>" , UserWarning ) <EOL> ne = ExpandedNormal ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> ne = ExpandedNormal ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> ] ) <EOL> def test_pdf_has_roots ( self ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:error>" , UserWarning ) <EOL> assert_raises ( UserWarning , ExpandedNormal , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ] ) <EOL> DECIMAL = <NUM_LIT:8> <EOL> def check_pdf ( distfn , arg , msg ) : <EOL> median = distfn . ppf ( <NUM_LIT:0.5> , * arg ) <EOL> eps = <NUM_LIT> <EOL> pdfv = distfn . pdf ( median , * arg ) <EOL> if ( pdfv < <NUM_LIT> ) or ( pdfv > <NUM_LIT> ) : <EOL> median = median + <NUM_LIT:0.1> <EOL> pdfv = distfn . pdf ( median , * arg ) <EOL> cdfdiff = ( distfn . cdf ( median + eps , * arg ) - <EOL> distfn . cdf ( median - eps , * arg ) ) / eps / <NUM_LIT> <EOL> npt . assert_almost_equal ( pdfv , cdfdiff , <EOL> decimal = DECIMAL , err_msg = msg + '<STR_LIT>' ) <EOL> def check_cdf_ppf ( distfn , arg , msg ) : <EOL> values = [ <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> ] <EOL> npt . assert_almost_equal ( distfn . cdf ( distfn . ppf ( values , * arg ) , * arg ) , <EOL> values , decimal = DECIMAL , err_msg = msg + '<STR_LIT>' ) <EOL> def check_cdf_sf ( distfn , arg , msg ) : <EOL> values = [ <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> ] <EOL> npt . assert_almost_equal ( distfn . cdf ( values , * arg ) , <EOL> <NUM_LIT:1.> - distfn . sf ( values , * arg ) , <EOL> decimal = DECIMAL , err_msg = msg + '<STR_LIT>' ) <EOL> def check_distribution_rvs ( distfn , args , alpha , rvs ) : <EOL> D , pval = stats . kstest ( rvs , distfn . cdf , args = args , N = <NUM_LIT:1000> ) <EOL> if ( pval < alpha ) : <EOL> D , pval = stats . kstest ( distfn . rvs , distfn . cdf , args = args , N = <NUM_LIT:1000> ) <EOL> npt . assert_ ( pval > alpha , "<STR_LIT>" + str ( D ) + "<STR_LIT>" + str ( pval ) + <EOL> "<STR_LIT>" + str ( alpha ) + "<STR_LIT>" + str ( args ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_module_suite ( ) </s>
