<s> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from sklearn . metrics import roc_auc_score <EOL> import pyhawkes . models <EOL> reload ( pyhawkes . models ) <EOL> from pyhawkes . models import DiscreteTimeNetworkHawkesModelSpikeAndSlab <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> def demo ( K = <NUM_LIT:3> , T = <NUM_LIT:1000> , dt_max = <NUM_LIT:20> , p = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> network_hypers = { "<STR_LIT:p>" : p , "<STR_LIT>" : False } <EOL> true_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab ( <EOL> K = K , dt_max = dt_max , <EOL> network_hypers = network_hypers ) <EOL> assert true_model . check_stability ( ) <EOL> S , R = true_model . generate ( T = T , keep = True , print_interval = <NUM_LIT:50> ) <EOL> plt . ion ( ) <EOL> true_figure , _ = true_model . plot ( color = "<STR_LIT>" , T_slice = ( <NUM_LIT:0> , <NUM_LIT:100> ) ) <EOL> test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab ( <EOL> K = K , dt_max = dt_max , <EOL> network_hypers = network_hypers ) <EOL> test_model . add_data ( S ) <EOL> test_figure , test_handles = test_model . plot ( color = "<STR_LIT>" , T_slice = ( <NUM_LIT:0> , <NUM_LIT:100> ) ) <EOL> N_samples = <NUM_LIT:100> <EOL> samples = [ ] <EOL> lps = [ ] <EOL> for itr in xrange ( N_samples ) : <EOL> print "<STR_LIT>" , itr <EOL> test_model . resample_model ( ) <EOL> lps . append ( test_model . log_probability ( ) ) <EOL> samples . append ( test_model . copy_sample ( ) ) <EOL> test_model . plot ( handles = test_handles ) <EOL> analyze_samples ( true_model , samples , lps ) <EOL> def analyze_samples ( true_model , samples , lps ) : <EOL> N_samples = len ( samples ) <EOL> A_samples = np . array ( [ s . weight_model . A for s in samples ] ) <EOL> W_samples = np . array ( [ s . weight_model . W for s in samples ] ) <EOL> lps = np . array ( lps ) <EOL> offset = N_samples // <NUM_LIT:2> <EOL> A_mean = A_samples [ offset : , ... ] . mean ( axis = <NUM_LIT:0> ) <EOL> W_mean = W_samples [ offset : , ... ] . mean ( axis = <NUM_LIT:0> ) <EOL> plt . figure ( ) <EOL> plt . plot ( np . arange ( N_samples ) , lps , '<STR_LIT:k>' ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . show ( ) <EOL> auc_A_mean = roc_auc_score ( true_model . weight_model . A . ravel ( ) , <EOL> A_mean . ravel ( ) ) <EOL> auc_W_mean = roc_auc_score ( true_model . weight_model . A . ravel ( ) , <EOL> W_mean . ravel ( ) ) <EOL> aucs = [ ] <EOL> for A in A_samples : <EOL> aucs . append ( roc_auc_score ( true_model . weight_model . A . ravel ( ) , A . ravel ( ) ) ) <EOL> plt . figure ( ) <EOL> plt . plot ( aucs , '<STR_LIT>' ) <EOL> plt . plot ( auc_A_mean * np . ones_like ( aucs ) , '<STR_LIT>' ) <EOL> plt . plot ( auc_W_mean * np . ones_like ( aucs ) , '<STR_LIT>' ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . show ( ) <EOL> plt . ioff ( ) <EOL> plt . show ( ) <EOL> demo ( ) </s>
