<s> '''<STR_LIT>''' <EOL> from bnpy . distr import GaussWishDistr , WishartDistr <EOL> from bnpy . suffstats import SuffStatBag <EOL> import numpy as np <EOL> import copy <EOL> class TestGaussWishDistr ( object ) : <EOL> def setUp ( self ) : <EOL> self . invW = np . eye ( <NUM_LIT:2> ) <EOL> self . m = np . zeros ( <NUM_LIT:2> ) <EOL> self . distr = GaussWishDistr ( m = self . m , invW = self . invW , kappa = <NUM_LIT:1.0> , dF = <NUM_LIT:4> ) <EOL> def test_dimension ( self ) : <EOL> assert self . distr . D == self . invW . shape [ <NUM_LIT:0> ] <EOL> def test_entropyWish ( self ) : <EOL> '''<STR_LIT>''' <EOL> Hself = self . distr . entropyWish ( ) <EOL> wishDistr = WishartDistr ( v = self . distr . dF , invW = self . distr . invW ) <EOL> Hwish = wishDistr . get_entropy ( ) <EOL> assert np . allclose ( Hself , Hwish ) <EOL> def test_dist_mahalanobis ( self , N = <NUM_LIT:10> ) : <EOL> '''<STR_LIT>''' <EOL> Xlist = list ( ) <EOL> for r in [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] : <EOL> Xlist . append ( self . distr . m + r ) <EOL> X = np . asarray ( Xlist ) <EOL> Dist = self . distr . dist_mahalanobis ( X ) <EOL> print Dist <EOL> assert np . all ( Dist [ : - <NUM_LIT:1> ] < Dist [ <NUM_LIT:1> : ] ) <EOL> def test_update_soVB ( self , rho = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> distrB = copy . deepcopy ( self . distr ) <EOL> distrB . invW *= <NUM_LIT:3> <EOL> distrB . m += <NUM_LIT:2> <EOL> distrB . kappa *= <NUM_LIT:10> <EOL> distrB2 = copy . deepcopy ( distrB ) <EOL> assert not np . allclose ( distrB . invW , self . distr . invW ) <EOL> assert not np . allclose ( distrB . m , self . distr . m ) <EOL> distrB . post_update_soVB ( rho , self . distr ) <EOL> assert distrB . dF == distrB2 . dF * ( <NUM_LIT:1> - rho ) + self . distr . dF * rho <EOL> assert np . allclose ( distrB . kappa , distrB2 . kappa * ( <NUM_LIT:1> - rho ) + self . distr . kappa * rho ) <EOL> def test_entropy_posterior_gets_smaller ( self , N = <NUM_LIT:10> ) : <EOL> PRNG = np . random . RandomState ( seed = <NUM_LIT> ) <EOL> for trial in range ( <NUM_LIT:3> ) : <EOL> X = PRNG . randn ( N , self . distr . D ) + self . distr . m <EOL> x = np . sum ( X , axis = <NUM_LIT:0> ) <EOL> xxT = np . dot ( X . T , X ) <EOL> SS = SuffStatBag ( K = <NUM_LIT:1> , D = self . distr . D ) <EOL> SS . setField ( '<STR_LIT:N>' , [ N ] , dims = '<STR_LIT>' ) <EOL> SS . setField ( '<STR_LIT:x>' , [ x ] , dims = ( '<STR_LIT>' , '<STR_LIT:D>' ) ) <EOL> SS . setField ( '<STR_LIT>' , [ xxT ] , dims = ( '<STR_LIT>' , '<STR_LIT:D>' , '<STR_LIT:D>' ) ) <EOL> postD = self . distr . get_post_distr ( SS , <NUM_LIT:0> ) <EOL> assert postD . D == self . distr . D <EOL> Hpost = postD . entropyWish ( ) <EOL> Hprior = self . distr . entropyWish ( ) <EOL> print '<STR_LIT>' % ( Hprior , Hpost ) <EOL> assert Hpost < Hprior </s>
