<s> '''<STR_LIT:U+0020>''' <EOL> from bnpy . distr import WishartDistr <EOL> from bnpy . suffstats import SuffStatBag <EOL> import numpy as np <EOL> import copy <EOL> class TestWishart ( object ) : <EOL> def setUp ( self ) : <EOL> self . v = <NUM_LIT:4> <EOL> self . invW = np . eye ( <NUM_LIT:2> ) <EOL> self . distr = WishartDistr ( v = self . v , invW = self . invW ) <EOL> def test_dimension ( self ) : <EOL> assert self . distr . D == self . invW . shape [ <NUM_LIT:0> ] <EOL> def test_cholinvW ( self ) : <EOL> cholinvW = self . distr . cholinvW ( ) <EOL> assert np . allclose ( np . dot ( cholinvW , cholinvW . T ) , self . distr . invW ) <EOL> def test_expected_covariance_matrix ( self ) : <EOL> CovMat = self . distr . ECovMat ( ) <EOL> MyCovMat = self . invW / ( self . v - self . distr . D - <NUM_LIT:1> ) <EOL> print MyCovMat , CovMat <EOL> assert np . allclose ( MyCovMat , CovMat ) <EOL> def test_post_update_soVB ( self , rho = <NUM_LIT> ) : <EOL> distrA = copy . deepcopy ( self . distr ) <EOL> distrB = WishartDistr ( distrA . v , invW = np . eye ( distrA . D ) ) <EOL> self . distr . post_update_soVB ( rho , distrB ) <EOL> assert self . distr . v == rho * distrA . v + ( <NUM_LIT:1> - rho ) * distrB . v <EOL> assert np . allclose ( self . distr . invW , rho * distrA . invW + ( <NUM_LIT:1> - rho ) * distrB . invW ) <EOL> def test_entropy_posterior_gets_smaller ( self , N = <NUM_LIT:1> ) : <EOL> PRNG = np . random . RandomState ( seed = <NUM_LIT> ) <EOL> for trial in range ( <NUM_LIT:3> ) : <EOL> X = PRNG . randn ( N , self . distr . D ) <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>' , [ 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 . get_entropy ( ) <EOL> Hprior = self . distr . get_entropy ( ) <EOL> print '<STR_LIT>' % ( Hprior , Hpost ) <EOL> print self . distr . invW <EOL> print postD . invW <EOL> assert Hpost < Hprior </s>
