<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import numpy as np <EOL> from scipy import stats <EOL> from statsmodels . stats . gof import ( chisquare , chisquare_power , <EOL> chisquare_effectsize ) <EOL> from numpy . testing import assert_almost_equal <EOL> nobs = <NUM_LIT> <EOL> n_bins = <NUM_LIT:5> <EOL> probs = <NUM_LIT:1.> / np . arange ( <NUM_LIT:2> , n_bins + <NUM_LIT:2> ) <EOL> probs /= probs . sum ( ) <EOL> probs = np . round ( probs , <NUM_LIT:2> ) <EOL> probs [ - <NUM_LIT:1> ] = <NUM_LIT:1> - probs [ : - <NUM_LIT:1> ] . sum ( ) <EOL> print ( "<STR_LIT>" , probs ) <EOL> probs_d = probs . copy ( ) <EOL> delta = <NUM_LIT> <EOL> probs_d [ <NUM_LIT:0> ] += delta <EOL> probs_d [ <NUM_LIT:1> ] -= delta <EOL> probs_cs = probs . cumsum ( ) <EOL> rvs = np . argmax ( np . random . rand ( nobs , <NUM_LIT:1> ) < probs_cs , <NUM_LIT:1> ) <EOL> print ( probs ) <EOL> print ( np . bincount ( rvs ) * ( <NUM_LIT:1.> / nobs ) ) <EOL> freq = np . bincount ( rvs ) <EOL> print ( stats . chisquare ( freq , nobs * probs ) ) <EOL> print ( '<STR_LIT:null>' , chisquare ( freq , nobs * probs ) ) <EOL> print ( '<STR_LIT>' , chisquare ( freq , nobs * probs_d ) ) <EOL> chisq_null , pval_null = chisquare ( freq , nobs * probs ) <EOL> d_null = ( ( freq / float ( nobs ) - probs ) ** <NUM_LIT:2> / probs ) . sum ( ) <EOL> print ( d_null ) <EOL> d_delta = ( ( freq / float ( nobs ) - probs_d ) ** <NUM_LIT:2> / probs_d ) . sum ( ) <EOL> print ( d_delta ) <EOL> d_null_alt = ( ( probs - probs_d ) ** <NUM_LIT:2> / probs_d ) . sum ( ) <EOL> print ( d_null_alt ) <EOL> print ( '<STR_LIT>' ) <EOL> chisq , pval = chisquare ( freq , nobs * probs_d ) <EOL> print ( stats . ncx2 . sf ( chisq_null , n_bins , <NUM_LIT> * nobs ) ) <EOL> print ( stats . ncx2 . sf ( chisq , n_bins , <NUM_LIT> * nobs ) ) <EOL> print ( stats . ncx2 . sf ( chisq , n_bins , d_delta * nobs ) ) <EOL> print ( chisquare ( freq , nobs * probs_d , value = np . sqrt ( d_delta ) ) ) <EOL> print ( chisquare ( freq , nobs * probs_d , value = np . sqrt ( chisq / nobs ) ) ) <EOL> print ( ) <EOL> assert_almost_equal ( stats . chi2 . sf ( d_delta * nobs , n_bins - <NUM_LIT:1> ) , <EOL> chisquare ( freq , nobs * probs_d ) [ <NUM_LIT:1> ] , decimal = <NUM_LIT> ) <EOL> crit = stats . chi2 . isf ( <NUM_LIT> , n_bins - <NUM_LIT:1> ) <EOL> power = stats . ncx2 . sf ( crit , n_bins - <NUM_LIT:1> , <NUM_LIT> ** <NUM_LIT:2> * nobs ) <EOL> assert_almost_equal ( power , <NUM_LIT> , decimal = <NUM_LIT:7> ) <EOL> effect_size = <NUM_LIT> <EOL> power = chisquare_power ( effect_size , nobs , n_bins , alpha = <NUM_LIT> ) <EOL> assert_almost_equal ( power , <NUM_LIT> , decimal = <NUM_LIT:7> ) <EOL> print ( chisquare ( freq , nobs * probs , value = <NUM_LIT:0> , ddof = <NUM_LIT:0> ) ) <EOL> d_null_alt = ( ( probs - probs_d ) ** <NUM_LIT:2> / probs ) . sum ( ) <EOL> print ( chisquare ( freq , nobs * probs , value = np . sqrt ( d_null_alt ) , ddof = <NUM_LIT:0> ) ) <EOL> d_delta_r = chisquare_effectsize ( probs , probs_d ) <EOL> n_rep = <NUM_LIT> <EOL> nobs = <NUM_LIT> <EOL> res_boots = np . zeros ( ( n_rep , <NUM_LIT:6> ) ) <EOL> for i in range ( n_rep ) : <EOL> rvs = np . argmax ( np . random . rand ( nobs , <NUM_LIT:1> ) < probs_cs , <NUM_LIT:1> ) <EOL> freq = np . bincount ( rvs ) <EOL> res1 = chisquare ( freq , nobs * probs ) <EOL> res2 = chisquare ( freq , nobs * probs_d ) <EOL> res3 = chisquare ( freq , nobs * probs_d , value = d_delta_r ) <EOL> res_boots [ i ] = [ res1 [ <NUM_LIT:0> ] , res2 [ <NUM_LIT:0> ] , res3 [ <NUM_LIT:0> ] , res1 [ <NUM_LIT:1> ] , res2 [ <NUM_LIT:1> ] , res3 [ <NUM_LIT:1> ] ] <EOL> alpha = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> ] ) <EOL> chi2_power = chisquare_power ( chisquare_effectsize ( probs , probs_d ) , <NUM_LIT> , n_bins , <EOL> alpha = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> ] ) <EOL> print ( ( res_boots [ : , <NUM_LIT:3> : ] < <NUM_LIT> ) . mean ( <NUM_LIT:0> ) ) <EOL> reject_freq = ( res_boots [ : , <NUM_LIT:3> : , None ] < alpha ) . mean ( <NUM_LIT:0> ) <EOL> reject = ( res_boots [ : , <NUM_LIT:3> : , None ] < alpha ) . sum ( <NUM_LIT:0> ) <EOL> desired = np . column_stack ( ( alpha , chi2_power , alpha ) ) . T <EOL> print ( '<STR_LIT>' ) <EOL> print ( ( reject_freq / desired - <NUM_LIT:1> ) * <NUM_LIT:100> ) </s>
