<s> import copy <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from sklearn . metrics import adjusted_mutual_info_score , adjusted_rand_score <EOL> from pyhawkes . models import DiscreteTimeNetworkHawkesModelSpikeAndSlab , DiscreteTimeNetworkHawkesModelGammaMixture <EOL> from pyhawkes . plotting . plotting import plot_network <EOL> def test_sbm_mf ( seed = None ) : <EOL> """<STR_LIT>""" <EOL> if seed is None : <EOL> seed = np . random . randint ( <NUM_LIT:2> ** <NUM_LIT:32> ) <EOL> print "<STR_LIT>" , seed <EOL> np . random . seed ( seed ) <EOL> C = <NUM_LIT:5> <EOL> K = <NUM_LIT:100> <EOL> T = <NUM_LIT:1000> <EOL> dt = <NUM_LIT:1.0> <EOL> B = <NUM_LIT:3> <EOL> p = <NUM_LIT> * np . eye ( C ) + ( <NUM_LIT> ) * ( <NUM_LIT:1> - np . eye ( C ) ) <EOL> network_hypers = { '<STR_LIT:C>' : C , '<STR_LIT>' : <NUM_LIT:1.0> / K , '<STR_LIT:p>' : p } <EOL> true_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab ( K , dt = dt , B = B , <EOL> network_hypers = network_hypers ) <EOL> c = true_model . network . c <EOL> perm = np . argsort ( c ) <EOL> plt . ion ( ) <EOL> plot_network ( true_model . weight_model . A [ np . ix_ ( perm , perm ) ] , <EOL> true_model . weight_model . W [ np . ix_ ( perm , perm ) ] ) <EOL> plt . pause ( <NUM_LIT> ) <EOL> test_network_hypers = { '<STR_LIT:C>' : C , '<STR_LIT>' : <NUM_LIT:1.0> / K , '<STR_LIT>' : <NUM_LIT:0.5> , '<STR_LIT>' : <NUM_LIT:0.5> } <EOL> test_model = DiscreteTimeNetworkHawkesModelGammaMixture ( K = K , dt = dt , B = B , <EOL> network_hypers = test_network_hypers ) <EOL> test_model . weight_model . initialize_from_gibbs ( true_model . weight_model . A , <EOL> true_model . weight_model . W ) <EOL> plt . figure ( ) <EOL> im = plt . imshow ( test_model . network . mf_m [ perm , : ] , <EOL> interpolation = "<STR_LIT:none>" , cmap = "<STR_LIT>" , <EOL> aspect = float ( C ) / K ) <EOL> plt . xlabel ( '<STR_LIT:C>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> plt . pause ( <NUM_LIT> ) <EOL> N_iters = <NUM_LIT:20> <EOL> c_samples = [ ] <EOL> vlbs = [ ] <EOL> for itr in xrange ( N_iters ) : <EOL> if itr % <NUM_LIT:5> == <NUM_LIT:0> : <EOL> print "<STR_LIT>" , itr <EOL> im . set_data ( test_model . network . mf_m [ perm , : ] ) <EOL> plt . pause ( <NUM_LIT> ) <EOL> test_model . network . resample_from_mf ( ) <EOL> c_samples . append ( copy . deepcopy ( test_model . network . c ) ) <EOL> vlbs . append ( test_model . network . get_vlb ( ) + test_model . weight_model . get_vlb ( ) ) <EOL> if itr > <NUM_LIT:0> : <EOL> if vlbs [ - <NUM_LIT:1> ] - vlbs [ - <NUM_LIT:2> ] < - <NUM_LIT> : <EOL> print "<STR_LIT>" <EOL> print np . array ( vlbs ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> test_model . network . meanfieldupdate ( test_model . weight_model ) <EOL> plt . ioff ( ) <EOL> c_samples = np . array ( c_samples ) <EOL> vlbs = np . array ( vlbs ) <EOL> print "<STR_LIT>" , true_model . network . c <EOL> print "<STR_LIT>" , c_samples [ - <NUM_LIT:10> : , : ] <EOL> amis = [ ] <EOL> arss = [ ] <EOL> for c in c_samples : <EOL> amis . append ( adjusted_mutual_info_score ( true_model . network . c , c ) ) <EOL> arss . append ( adjusted_rand_score ( true_model . network . c , c ) ) <EOL> plt . figure ( ) <EOL> plt . plot ( np . arange ( N_iters ) , amis , '<STR_LIT>' ) <EOL> plt . plot ( np . arange ( N_iters ) , arss , '<STR_LIT>' ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . figure ( ) <EOL> plt . plot ( np . arange ( N_iters ) , vlbs ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . show ( ) <EOL> test_sbm_mf ( ) </s>
