<s> """<STR_LIT>""" <EOL> __all__ = [ "<STR_LIT>" ] <EOL> from numpy import mean , std , sqrt , where , argmin , arange , array <EOL> from numpy import sort <EOL> from scipy . stats import t as student_t <EOL> from scipy . stats import scoreatpercentile <EOL> from . mahal import mahalanobis <EOL> from . acr import ACR <EOL> tinv = student_t . ppf <EOL> def prctile ( v , Q ) : <EOL> v = sort ( v ) <EOL> return [ scoreatpercentile ( v , Qi ) for Qi in Q ] <EOL> def identify_outliers ( test , chains , x ) : <EOL> """<STR_LIT>""" <EOL> v = mean ( chains , axis = <NUM_LIT:0> ) <EOL> test = test . lower ( ) <EOL> if test == '<STR_LIT>' : <EOL> q1 , q3 = prctile ( v , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> iqr = q3 - q1 <EOL> outliers = where ( v < q1 - <NUM_LIT:2> * iqr ) [ <NUM_LIT:0> ] <EOL> elif test == '<STR_LIT>' : <EOL> zscore = ( mean ( v ) - v ) / std ( v , ddof = <NUM_LIT:1> ) <EOL> n = len ( v ) <EOL> t2 = tinv ( <NUM_LIT:1> - <NUM_LIT> / n , n - <NUM_LIT:2> ) ** <NUM_LIT:2> <EOL> gcrit = ( ( n - <NUM_LIT:1> ) / sqrt ( n ) ) * sqrt ( t2 / ( n - <NUM_LIT:2> + t2 ) ) <EOL> outliers = where ( zscore > gcrit ) [ <NUM_LIT:0> ] <EOL> elif test == '<STR_LIT>' : <EOL> alpha = <NUM_LIT> <EOL> npop , nvar = x . shape <EOL> gcrit = ACR ( nvar , npop - <NUM_LIT:1> , alpha ) <EOL> minidx = argmin ( v ) <EOL> d1 = mahalanobis ( x [ minidx , : ] , x [ minidx != arange ( npop ) , : ] ) <EOL> outliers = array ( [ minidx ] ) if d1 > gcrit else array ( [ ] ) <EOL> elif test == '<STR_LIT:none>' : <EOL> outliers = array ( [ ] ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + test ) <EOL> return outliers <EOL> def test_outliers ( ) : <EOL> from . walk import walk <EOL> from numpy . random import multivariate_normal , seed <EOL> from numpy import vstack , ones , eye <EOL> seed ( <NUM_LIT:2> ) <EOL> ngood , nbad = <NUM_LIT> , <NUM_LIT:2> <EOL> chains = walk ( <NUM_LIT:1000> , mu = [ <NUM_LIT:1> ] * nbad + [ <NUM_LIT:5> ] * ngood , sigma = <NUM_LIT> , alpha = <NUM_LIT:0.1> ) <EOL> assert ( identify_outliers ( '<STR_LIT>' , chains , None ) == arange ( nbad ) ) . all ( ) <EOL> assert ( identify_outliers ( '<STR_LIT>' , chains , None ) == arange ( nbad ) ) . all ( ) <EOL> x = vstack ( ( multivariate_normal ( - ones ( <NUM_LIT:4> ) , <NUM_LIT:0.1> * eye ( <NUM_LIT:4> ) , size = nbad ) , <EOL> multivariate_normal ( ones ( <NUM_LIT:4> ) , <NUM_LIT:0.1> * eye ( <NUM_LIT:4> ) , size = ngood ) ) ) <EOL> assert identify_outliers ( '<STR_LIT>' , chains , x ) [ <NUM_LIT:0> ] in range ( nbad ) <EOL> xsame = multivariate_normal ( ones ( <NUM_LIT:4> ) , <NUM_LIT> * eye ( <NUM_LIT:4> ) , size = ngood + nbad ) <EOL> assert len ( identify_outliers ( '<STR_LIT>' , chains , xsame ) ) == <NUM_LIT:0> <EOL> x = vstack ( ( multivariate_normal ( - <NUM_LIT:3> * ones ( <NUM_LIT:4> ) , eye ( <NUM_LIT:4> ) , size = nbad ) , <EOL> multivariate_normal ( ones ( <NUM_LIT:4> ) , <NUM_LIT:10> * eye ( <NUM_LIT:4> ) , size = ngood ) ) ) <EOL> assert len ( identify_outliers ( '<STR_LIT>' , chains , x ) ) == <NUM_LIT:0> <EOL> from numpy . linalg import norm <EOL> from . state import MCMCDraw <EOL> ngen , npop = chains . shape <EOL> npop , nvar = x . shape <EOL> state = MCMCDraw ( Ngen = ngen , Nthin = ngen , Nupdate = <NUM_LIT:0> , <EOL> Nvar = nvar , Npop = npop , Ncr = <NUM_LIT:0> , thinning = <NUM_LIT:0> ) <EOL> for i in range ( ngen ) : <EOL> state . _generation ( new_draws = npop , x = x , logp = chains [ i ] , accept = npop ) <EOL> nx , nlogp = x + <NUM_LIT:0> , chains [ - <NUM_LIT:1> ] + <NUM_LIT:0> <EOL> state . remove_outliers ( nx , nlogp , test = '<STR_LIT>' , portion = <NUM_LIT:0.5> ) <EOL> outliers = state . outliers ( ) <EOL> assert outliers . shape [ <NUM_LIT:0> ] == nbad <EOL> for i in range ( nbad ) : <EOL> assert nlogp [ outliers [ i , <NUM_LIT:1> ] ] == chains [ - <NUM_LIT:1> ] [ outliers [ i , <NUM_LIT:2> ] ] <EOL> assert norm ( nx [ outliers [ i , <NUM_LIT:1> ] , : ] - x [ outliers [ i , <NUM_LIT:2> ] , : ] ) == <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_outliers ( ) </s>
