<s> import numpy as np <EOL> import tensorflow as tf <EOL> from nose . tools import raises <EOL> from numpy . testing import assert_almost_equal <EOL> import scipy . stats as st <EOL> import tensorprob as tp <EOL> def test_creation ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> pass <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_scalar_creation_outside_with ( ) : <EOL> tp . Parameter ( name = '<STR_LIT>' ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_observed_in_model_block ( ) : <EOL> with tp . Model ( ) : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> tp . observed ( X ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_initalize_in_model_block ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> tp . Normal ( mu , sigma ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:5> , <EOL> sigma : <NUM_LIT:2> <EOL> } ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_observed_in_model_block ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:5> , <EOL> sigma : <NUM_LIT:2> <EOL> } ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:5> , <EOL> sigma : <NUM_LIT:2> <EOL> } ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_initialise_before_observed ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> tp . Normal ( mu , sigma ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:5> , <EOL> sigma : <NUM_LIT:2> <EOL> } ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_initialise_with_too_few_variables ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:5> <EOL> } ) <EOL> @ raises ( ValueError ) <EOL> def test_initialise_with_list ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . initialize ( [ <NUM_LIT:5> , <NUM_LIT:2> ] ) <EOL> @ raises ( ValueError ) <EOL> def test_initialise_with_invalid_keys ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . initialize ( { <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } ) <EOL> def test_distribution_creation_global_graph ( ) : <EOL> before = tf . get_default_graph ( ) . as_graph_def ( ) <EOL> with tp . Model ( ) : <EOL> tp . Parameter ( ) <EOL> after = tf . get_default_graph ( ) . as_graph_def ( ) <EOL> assert before == after <EOL> def test_internal_graph_no_growth ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:1> , <EOL> sigma : <NUM_LIT:1> , <EOL> } ) <EOL> before = model . session . graph_def <EOL> model . assign ( { <EOL> mu : <NUM_LIT:0> , <EOL> sigma : <NUM_LIT:2> , <EOL> } ) <EOL> model . fit ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> after = model . session . graph_def <EOL> assert before == after <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_distribution_creation_outside_with ( ) : <EOL> with tp . Model ( ) : <EOL> mu = tp . Parameter ( name = '<STR_LIT>' ) <EOL> sigma = tp . Parameter ( name = '<STR_LIT>' , lower = <NUM_LIT:0> ) <EOL> tp . Normal ( mu , sigma ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_nesting_models ( ) : <EOL> with tp . Model ( ) : <EOL> with tp . Model ( ) : <EOL> raise NotImplementedError <EOL> def test_observed ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> assert ( X in model . _observed ) <EOL> @ raises ( ValueError ) <EOL> def test_observed_erorr_on_non_distribution ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X , int ( <NUM_LIT> ) ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_prepare_without_observed ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> model . initialize ( { mu : <NUM_LIT> } ) <EOL> def test_nll ( ) : <EOL> with tp . Model ( ) as model : <EOL> mu = tp . Parameter ( ) <EOL> sigma = tp . Parameter ( ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> xs = np . linspace ( - <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:100> ) <EOL> model . initialize ( { <EOL> mu : <NUM_LIT:0> , <EOL> sigma : <NUM_LIT:1> <EOL> } ) <EOL> out1 = model . nll ( xs ) <EOL> out2 = - sum ( st . norm . logpdf ( xs , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> assert_almost_equal ( out1 , out2 , <NUM_LIT:10> ) <EOL> def test_fit ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . initialize ( { mu : <NUM_LIT:2> , sigma : <NUM_LIT:2> } ) <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> data = np . random . normal ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:100> ) <EOL> results = model . fit ( data ) <EOL> assert results . success <EOL> def test_assign ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> feed = { mu : <NUM_LIT> , sigma : <NUM_LIT:1> } <EOL> model . initialize ( feed ) <EOL> model . assign ( { mu : <NUM_LIT:1> , sigma : <NUM_LIT:0> } ) <EOL> model . assign ( { mu : - <NUM_LIT:100> , sigma : <NUM_LIT:0> } ) <EOL> assert model . state == { mu : - <NUM_LIT:100> , sigma : <NUM_LIT:0> } <EOL> model . assign ( feed ) <EOL> assert model . state == feed <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_assign_in_model_block ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . assign ( { mu : <NUM_LIT:1> , sigma : <NUM_LIT:0> } ) <EOL> model . observed ( X ) <EOL> model . initialize ( { mu : <NUM_LIT> , sigma : <NUM_LIT:1> } ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_assign_in_before_observed ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> tp . Normal ( mu , sigma ) <EOL> model . assign ( { mu : <NUM_LIT:1> , sigma : <NUM_LIT:0> } ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_assign_in_before_initalize ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . assign ( { mu : <NUM_LIT:1> , sigma : <NUM_LIT:0> } ) <EOL> model . observed ( X ) <EOL> model . assign ( { mu : <NUM_LIT:1> , sigma : <NUM_LIT:0> } ) <EOL> @ raises ( ValueError ) <EOL> def test_assign_empty_dict ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . assign ( { } ) <EOL> @ raises ( ValueError ) <EOL> def test_assign_wrong_container ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> model . assign ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> @ raises ( tp . model . ModelError ) <EOL> def test_observed_in_model ( ) : <EOL> model = tp . Model ( ) <EOL> with model : <EOL> mu = tp . Parameter ( lower = - <NUM_LIT:5> , upper = <NUM_LIT:5> ) <EOL> sigma = tp . Parameter ( lower = <NUM_LIT:0> ) <EOL> X = tp . Normal ( mu , sigma ) <EOL> model . observed ( X ) <EOL> def test_variable_independent ( ) : <EOL> with tp . Model ( ) as model : <EOL> a = tp . Parameter ( ) <EOL> b = tp . Parameter ( ) <EOL> model . observed ( b ) <EOL> model . initialize ( { <EOL> a : <NUM_LIT:1> , <EOL> } ) <EOL> model . fit ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) </s>
