<s> import crosscat . tests . component_model_extensions . ContinuousComponentModel as ccmext <EOL> import random <EOL> import math <EOL> import numpy <EOL> import six <EOL> import unittest <EOL> def main ( ) : <EOL> unittest . main ( ) <EOL> class TestContinuousComponentModelExtensions_Constructors ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> N = <NUM_LIT:10> <EOL> self . N = N <EOL> random . seed ( <NUM_LIT:0> ) <EOL> self . X = numpy . array ( [ [ random . normalvariate ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) ] for i in range ( N ) ] ) <EOL> self . params_good = dict ( rho = <NUM_LIT:1.0> , mu = <NUM_LIT:0.0> ) <EOL> self . params_empty = dict ( ) <EOL> self . params_missing_rho = dict ( mu = <NUM_LIT:0.0> ) <EOL> self . params_missing_mu = dict ( mu = <NUM_LIT:0.0> ) <EOL> self . params_not_dict = [ <NUM_LIT:0.0> , <NUM_LIT:1.0> ] <EOL> self . params_negative_rho = dict ( rho = - <NUM_LIT:1.0> , mu = <NUM_LIT:0.0> ) <EOL> self . params_zero_rho = dict ( rho = <NUM_LIT:0.0> , mu = <NUM_LIT:0.0> ) <EOL> self . hypers_good = dict ( mu = <NUM_LIT:0.0> , nu = <NUM_LIT:1.0> , r = <NUM_LIT:1.0> , s = <NUM_LIT:1.0> ) <EOL> self . hypers_missing_mu = dict ( nu = <NUM_LIT:1.0> , r = <NUM_LIT:1.0> , s = <NUM_LIT:1.0> ) <EOL> self . hypers_missing_nu = dict ( mu = <NUM_LIT:0.0> , r = <NUM_LIT:1.0> , s = <NUM_LIT:1.0> ) <EOL> self . hypers_missing_r = dict ( mu = <NUM_LIT:0.0> , nu = <NUM_LIT:1.0> , s = <NUM_LIT:1.0> ) <EOL> self . hypers_missing_s = dict ( mu = <NUM_LIT:0.0> , nu = <NUM_LIT:1.0> , r = <NUM_LIT:1.0> ) <EOL> self . hypers_low_nu = dict ( mu = <NUM_LIT:0.0> , nu = - <NUM_LIT:1.0> , r = <NUM_LIT:1.0> , s = <NUM_LIT:1.0> ) <EOL> self . hypers_low_r = dict ( mu = <NUM_LIT:0.0> , nu = <NUM_LIT:1.0> , r = - <NUM_LIT:1.0> , s = <NUM_LIT:1.0> ) <EOL> self . hypers_low_s = dict ( mu = <NUM_LIT:0.0> , nu = <NUM_LIT:1.0> , r = <NUM_LIT:1.0> , s = - <NUM_LIT:1.0> ) <EOL> self . hypers_not_dict = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> def test_from_parameters_contructor_with_good_complete_params_and_hypers ( self ) : <EOL> m = ccmext . p_ContinuousComponentModel . from_parameters ( self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> assert m is not None <EOL> def test_from_parameters_contructor_with_no_params_and_hypers ( self ) : <EOL> m = ccmext . p_ContinuousComponentModel . from_parameters ( self . N , gen_seed = <NUM_LIT:0> ) <EOL> assert m is not None <EOL> def test_from_parameters_contructor_with_bad_params_and_good_hypers ( self ) : <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_empty , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( TypeError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_not_dict , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_missing_mu , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_missing_rho , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_negative_rho , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_zero_rho , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> def test_from_parameters_contructor_with_good_params_and_bad_hypers ( self ) : <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_missing_mu , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_missing_nu , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_missing_r , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_missing_s , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_low_nu , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_low_r , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_low_s , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( TypeError , ccmext . p_ContinuousComponentModel . from_parameters , self . N , <EOL> data_params = self . params_good , <EOL> hypers = self . hypers_not_dict , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> def test_from_data_contructor_with_good_complete_hypers ( self ) : <EOL> m = ccmext . p_ContinuousComponentModel . from_data ( self . X , <EOL> hypers = self . hypers_good , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> assert m is not None <EOL> def test_from_data_contructor_with_no_params_and_hypers ( self ) : <EOL> m = ccmext . p_ContinuousComponentModel . from_data ( self . X , gen_seed = <NUM_LIT:0> ) <EOL> assert m is not None <EOL> def test_from_data_contructor_with_bad_hypers ( self ) : <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_missing_mu , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_missing_nu , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_missing_r , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_missing_s , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_low_nu , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_low_r , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_low_s , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> self . assertRaises ( TypeError , ccmext . p_ContinuousComponentModel . from_data , self . X , <EOL> hypers = self . hypers_not_dict , <EOL> gen_seed = <NUM_LIT:0> ) <EOL> class TestContinuousComponentModelExtensions_FromParametersConstructor ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> N = <NUM_LIT:10> <EOL> random . seed ( <NUM_LIT:0> ) <EOL> self . X = numpy . array ( [ [ random . normalvariate ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) ] for i in range ( N ) ] ) <EOL> self . component_model = ccmext . p_ContinuousComponentModel . from_parameters ( N , gen_seed = <NUM_LIT:0> ) <EOL> def test_all_hyperparameters_intialized ( self ) : <EOL> these_hyperparameters = self . component_model . get_hypers ( ) <EOL> for hyperparameter in [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT:r>' , b'<STR_LIT:s>' ] : <EOL> assert hyperparameter in these_hyperparameters <EOL> def test_all_suffstats_intialized ( self ) : <EOL> these_suffstats = self . component_model . get_suffstats ( ) <EOL> for suffstat in [ b'<STR_LIT>' , b'<STR_LIT>' ] : <EOL> assert suffstat in these_suffstats <EOL> def test_draw_component_model_params ( self ) : <EOL> draw = self . component_model . sample_parameters_given_hyper ( ) <EOL> assert type ( draw ) is dict <EOL> model_parameter_bounds = self . component_model . get_model_parameter_bounds ( ) <EOL> for key , value in six . iteritems ( draw ) : <EOL> assert ( key in [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert ( type ( value ) is float or type ( value ) is numpy . float64 ) <EOL> assert ( not math . isnan ( value ) ) <EOL> assert ( not math . isinf ( value ) ) <EOL> if key == '<STR_LIT>' : <EOL> assert ( value > <NUM_LIT:0.0> ) <EOL> def test_uncollapsed_likelihood ( self ) : <EOL> ans = - <NUM_LIT> <EOL> log_likelihood = self . component_model . uncollapsed_likelihood ( self . X , { '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT>' : <NUM_LIT:1.0> } ) <EOL> assert log_likelihood < <NUM_LIT:0.0> <EOL> assert math . fabs ( ans - log_likelihood ) < <NUM_LIT> <EOL> class TestContinuousComponentModelExtensions_FromDataConstructor ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> N = <NUM_LIT:10> <EOL> random . seed ( <NUM_LIT:0> ) <EOL> self . X = numpy . array ( [ [ random . normalvariate ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) ] for i in range ( N ) ] ) <EOL> self . component_model = ccmext . p_ContinuousComponentModel . from_data ( self . X , gen_seed = <NUM_LIT:0> ) <EOL> def test_all_hyperparameters_intialized ( self ) : <EOL> these_hyperparameters = self . component_model . get_hypers ( ) <EOL> for hyperparameter in [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT:r>' , b'<STR_LIT:s>' ] : <EOL> assert hyperparameter in these_hyperparameters <EOL> def test_all_suffstats_intialized ( self ) : <EOL> these_suffstats = self . component_model . get_suffstats ( ) <EOL> for suffstat in [ b'<STR_LIT>' , b'<STR_LIT>' ] : <EOL> assert suffstat in these_suffstats <EOL> def test_draw_component_model_params ( self ) : <EOL> draw = self . component_model . sample_parameters_given_hyper ( ) <EOL> assert type ( draw ) is dict <EOL> for key , value in six . iteritems ( draw ) : <EOL> assert ( key in [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert ( type ( value ) is float or type ( value ) is numpy . float64 ) <EOL> assert ( not math . isnan ( value ) ) <EOL> assert ( not math . isinf ( value ) ) <EOL> if key == '<STR_LIT>' : <EOL> assert ( value > <NUM_LIT:0.0> ) <EOL> def test_uncollapsed_likelihood ( self ) : <EOL> ans = - <NUM_LIT> <EOL> log_likelihood = self . component_model . uncollapsed_likelihood ( self . X , { '<STR_LIT>' : <NUM_LIT:0.0> , '<STR_LIT>' : <NUM_LIT:1.0> } ) <EOL> assert log_likelihood < <NUM_LIT:0.0> <EOL> assert math . fabs ( ans - log_likelihood ) < <NUM_LIT> <EOL> class TestContinuousComponentModelExtensions_static ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> N = <NUM_LIT:10> <EOL> random . seed ( <NUM_LIT:0> ) <EOL> self . X = numpy . array ( [ [ random . normalvariate ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) ] for i in range ( N ) ] ) <EOL> self . component_class = ccmext . p_ContinuousComponentModel <EOL> def test_log_likelihood ( self ) : <EOL> X_1 = numpy . array ( [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] ] ) <EOL> parameters = dict ( mu = <NUM_LIT:0.0> , rho = <NUM_LIT:1.0> ) <EOL> log_likelihood = self . component_class . log_likelihood ( X_1 , parameters ) <EOL> assert log_likelihood < <NUM_LIT:0.0> <EOL> assert math . fabs ( - <NUM_LIT> - log_likelihood ) < <NUM_LIT> <EOL> parameters = dict ( mu = <NUM_LIT> , rho = <NUM_LIT> ) <EOL> log_likelihood = self . component_class . log_likelihood ( X_1 , parameters ) <EOL> assert log_likelihood < <NUM_LIT:0.0> <EOL> assert math . fabs ( - <NUM_LIT> - log_likelihood ) < <NUM_LIT> <EOL> def test_log_pdf ( self ) : <EOL> X_1 = numpy . array ( [ [ <NUM_LIT:1> ] , [ <NUM_LIT:0> ] ] ) <EOL> parameters = dict ( mu = <NUM_LIT:0.0> , rho = <NUM_LIT:1.0> ) <EOL> log_pdf = self . component_class . log_pdf ( X_1 , parameters ) <EOL> assert len ( log_pdf ) == <NUM_LIT:2> <EOL> assert math . fabs ( - <NUM_LIT> - log_pdf [ <NUM_LIT:0> , <NUM_LIT:0> ] ) < <NUM_LIT> <EOL> assert math . fabs ( - <NUM_LIT> - log_pdf [ <NUM_LIT:1> , <NUM_LIT:0> ] ) < <NUM_LIT> <EOL> parameters = dict ( mu = <NUM_LIT> , rho = <NUM_LIT> ) <EOL> log_pdf = self . component_class . log_pdf ( X_1 , parameters ) <EOL> assert len ( log_pdf ) == <NUM_LIT:2> <EOL> assert math . fabs ( - <NUM_LIT> - log_pdf [ <NUM_LIT:0> , <NUM_LIT:0> ] ) < <NUM_LIT> <EOL> assert math . fabs ( - <NUM_LIT> - log_pdf [ <NUM_LIT:1> , <NUM_LIT:0> ] ) < <NUM_LIT> <EOL> parameters = dict ( mu = <NUM_LIT:0.0> , rho = <NUM_LIT:1.0> ) <EOL> lspc = numpy . linspace ( <NUM_LIT:0> , <NUM_LIT:10> , num = <NUM_LIT:20> ) <EOL> X_2 = numpy . array ( [ [ x ] for x in lspc ] ) <EOL> log_pdf = self . component_class . log_pdf ( X_2 , parameters ) <EOL> assert len ( log_pdf ) == <NUM_LIT:20> <EOL> for n in range ( <NUM_LIT:1> , <NUM_LIT:20> ) : <EOL> assert log_pdf [ n - <NUM_LIT:1> , <NUM_LIT:0> ] > log_pdf [ n , <NUM_LIT:0> ] <EOL> def test_generate_discrete_support ( self ) : <EOL> parameters = dict ( mu = <NUM_LIT:0.0> , rho = <NUM_LIT:1.0> ) <EOL> support = self . component_class . generate_discrete_support ( parameters , support = <NUM_LIT> , nbins = <NUM_LIT:100> ) <EOL> assert type ( support ) is list <EOL> assert len ( support ) == <NUM_LIT:100> <EOL> assert support [ <NUM_LIT:0> ] == - support [ - <NUM_LIT:1> ] <EOL> assert support [ <NUM_LIT> ] == - support [ <NUM_LIT:50> ] <EOL> assert math . fabs ( support [ <NUM_LIT:0> ] + <NUM_LIT> ) < <NUM_LIT> <EOL> assert math . fabs ( support [ - <NUM_LIT:1> ] - <NUM_LIT> ) < <NUM_LIT> <EOL> def test_draw_component_model_hyperparameters_single ( self ) : <EOL> draw_list = self . component_class . draw_hyperparameters ( self . X ) <EOL> assert type ( draw_list ) is list <EOL> assert type ( draw_list [ <NUM_LIT:0> ] ) is dict <EOL> draw = draw_list [ <NUM_LIT:0> ] <EOL> assert type ( draw ) is dict <EOL> for key , value in six . iteritems ( draw ) : <EOL> assert key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:r>' , '<STR_LIT:s>' ] <EOL> assert type ( value ) is float or type ( value ) is numpy . float64 <EOL> assert ( not math . isnan ( value ) ) <EOL> assert ( not math . isinf ( value ) ) <EOL> if key in [ '<STR_LIT>' , '<STR_LIT:s>' , '<STR_LIT:r>' ] : <EOL> assert value > <NUM_LIT:0.0> <EOL> def test_draw_component_model_hyperparameters_multiple ( self ) : <EOL> n_draws = <NUM_LIT:3> <EOL> draw_list = self . component_class . draw_hyperparameters ( self . X , n_draws = n_draws ) <EOL> assert type ( draw_list ) is list <EOL> assert len ( draw_list ) == <NUM_LIT:3> <EOL> for draw in draw_list : <EOL> assert type ( draw ) is dict <EOL> for key , value in six . iteritems ( draw ) : <EOL> assert key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:r>' , '<STR_LIT:s>' ] <EOL> assert type ( value ) is float or type ( value ) is numpy . float64 <EOL> assert ( not math . isnan ( value ) ) <EOL> assert ( not math . isinf ( value ) ) <EOL> if key in [ '<STR_LIT>' , '<STR_LIT:s>' , '<STR_LIT:r>' ] : <EOL> assert value > <NUM_LIT:0.0> <EOL> def test_generate_data_from_parameters ( self ) : <EOL> N = <NUM_LIT:10> <EOL> parameters = dict ( mu = <NUM_LIT:0.0> , rho = <NUM_LIT:1.0> ) <EOL> X = self . component_class . generate_data_from_parameters ( parameters , N ) <EOL> assert type ( X ) == numpy . ndarray <EOL> assert len ( X ) == N <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
