def simulate_params(n_groups): var1 = Variable('constant_one', lambda x: x, 0.0, 0.0, fe_bounds=[-np.inf, 0.0]) var2 = Variable('constant_one', np.exp, 0.0, 0.0, fe_bounds=[-np.inf, 0.0]) if n_groups == 1: var1.re_bounds = [0.0, 0.0] var2.re_bounds = [0.0, 0.0] else: var1.re_bounds = [-1.0, 1.0] var2.re_bounds = [-1.0, 1.0] param1 = Parameter('p1', np.exp, [var1]) param2 = Parameter('p2', lambda x: x, [var1, var2]) param3 = Parameter('p3', np.exp, [var1]) n_var = 4 fe_true = -np.random.rand(n_var) * 3 if n_groups == 1: re_true = np.zeros((n_groups, n_var)) else: re_true = np.random.randn(n_groups, n_var) x_true = re_true + fe_true param_true = np.zeros((n_groups, 3)) param_true[:, 0] = np.exp(x_true[:, 0]) param_true[:, 1] = x_true[:, 1] + np.exp(x_true[:, 2]) param_true[:, 2] = np.exp(x_true[:, 3]) return ParameterSet([param1, param2, param3]), param_true, x_true
def variable(): return Variable( covariate='covariate1', var_link_fun=lambda x: x, fe_init=0., re_init=0. )
def test_variable_bounds(): variable = Variable( var_link_fun=lambda x: x, covariate='covariate1', fe_init=0., re_init=0., fe_bounds=[-10., 10.], re_bounds=[-3., 1.] ) assert variable.fe_bounds == [-10., 10.] assert variable.re_bounds == [-3., 1.]
def test_variable_gprior(): variable = Variable( var_link_fun=lambda x: x, covariate='covariate1', fe_init=0., re_init=0., fe_gprior=[0., 0.001], re_gprior=[0., 0.0001] ) assert variable.fe_gprior == [0., 1e-3] assert variable.re_gprior == [0., 1e-4]
def parameter_set(): var1 = Variable(covariate='intercept', var_link_fun=lambda x: x, fe_init=0., re_init=0.) var2 = Variable(covariate='intercept', var_link_fun=lambda x: x, fe_init=0., re_init=0.) alpha = Parameter(param_name='alpha', link_fun=lambda x: x, variables=[var1]) beta = Parameter(param_name='beta', link_fun=lambda x: x, variables=[var2]) ln_alpha_beta = ParameterFunction( param_function_name='ln-alpha-beta', param_function=lambda params: np.log(params[0] * params[1])) param_set = ParameterSet(parameters=[alpha, beta], parameter_functions=[ln_alpha_beta]) return param_set
def param2(): var2 = Variable( covariate='covariate2', var_link_fun=lambda x: x, fe_init=1., re_init=1. ) parameter2 = Parameter( param_name='beta', variables=[var2], link_fun=lambda x: x, ) return parameter2
def param1(): var1 = Variable( covariate='covariate1', var_link_fun=lambda x: x, fe_init=1., re_init=1. ) parameter1 = Parameter( param_name='alpha', variables=[var1], link_fun=lambda x: x, ) return parameter1
def param_set(): variable1 = Variable('intercept', lambda x: x, 0.0, 0.3, re_bounds=[0.0, 1.0]) variable2 = Variable('intercept', lambda x: x, 0.1, 0.4, re_bounds=[0.0, 2.0]) variable3 = Variable('intercept', lambda x: x, 0.2, 0.5, re_bounds=[0.0, 3.0]) parameter1 = Parameter('p1', np.exp, [variable1]) parameter2 = Parameter('p2', np.exp, [variable2]) parameter3 = Parameter('p3', np.exp, [variable3] * 2) parameter_set = ParameterSet([parameter1, parameter2, parameter3]) assert parameter_set.num_fe == 4 return parameter_set
def test_parameter(): var1 = Variable( covariate='covariate1', var_link_fun=lambda x: x, fe_init=1., re_init=1. ) var2 = Variable( covariate='covariate2', var_link_fun=lambda x: x, fe_init=1., re_init=1. ) parameter = Parameter( param_name='alpha', variables=[var1, var2], link_fun=lambda x: x, ) assert parameter.num_fe == 2 assert callable(parameter.link_fun) assert len(parameter.fe_init) == 2 assert len(parameter.re_init) == 2 assert len(parameter.fe_gprior) == 2 assert len(parameter.re_gprior) == 2 assert len(parameter.fe_bounds) == 2 assert len(parameter.re_bounds) == 2
data_frame = pandas.DataFrame(data_dict) # ------------------------------------------------------------------------ # curve_model data = Data(df=data_frame, col_t='independent_var', col_obs='measurement_value', col_covs=['cov_one', 'social_distance'], col_group='data_group', obs_space=gaussian_cdf, col_obs_se='measurement_std') a_intercept = Variable(covariate='cov_one', var_link_fun=identity_fun, fe_init=a_true / 3, re_init=0.0, fe_bounds=[-numpy.inf, numpy.inf], re_bounds=[0.0, 0.0]) b_intercept = Variable(covariate='cov_one', var_link_fun=identity_fun, fe_init=b_true / 3, re_init=0.0, fe_bounds=[-numpy.inf, numpy.inf], re_bounds=[0.0, 0.0]) b_social_distance = Variable(covariate='social_distance', var_link_fun=identity_fun, fe_init=b_true / 3, re_init=0.0, fe_bounds=[-numpy.inf, numpy.inf],
col_obs='obs', col_covs=['intercept'], obs_space=ln_gaussian_pdf, col_group='group_name', obs_se_func=lambda x: 1) """ ``` ### Create a Parameter Set We need to define variables, parameters that use those variables (in this case there are only intercepts -- and one covariate per parameter -- so the variables are effectively the same as the parameters), and a parameter set that collects all of that information into one object. ```python """ alpha_fe = Variable(covariate='intercept', var_link_fun=lambda x: x, fe_init=fe_mean[0], re_init=0., fe_gprior=fe_gprior, re_gprior=re_gprior) beta_fe = Variable(covariate='intercept', var_link_fun=lambda x: x, fe_init=fe_mean[1], re_init=0., fe_gprior=fe_gprior, re_gprior=re_gprior) p_fe = Variable(covariate='intercept', var_link_fun=lambda x: x, fe_init=fe_mean[2], re_init=0., fe_gprior=fe_gprior, re_gprior=re_gprior) alpha = Parameter('alpha', link_fun=np.exp, variables=[alpha_fe])
# ------------------------------------------------------------------------ # curve_model data = Data(df=data_frame, col_t='independent_var', col_obs='measurement_value', col_covs=num_params * ['constant_one'], col_group='data_group', obs_space=expit, col_obs_se='measurement_std') a_intercept = Variable(covariate='constant_one', var_link_fun=lambda x: x, fe_init=a_true[0] / 3.0, re_init=0.0, re_zero_sum_std=abs(a_true[0]) / 100.0, fe_gprior=[a_true[0], abs(a_true[0]) / 100.0], fe_bounds=[0.0, numpy.inf], re_bounds=[-2.0, 2.0]) b_intercept = Variable(covariate='constant_one', var_link_fun=lambda x: x, fe_init=b_true[0] / 3.0, re_init=0.0, re_zero_sum_std=abs(b_true[0]) / 100.0, fe_gprior=[b_true[0], abs(b_true[0]) / 100.0], fe_bounds=[-numpy.inf, numpy.inf], re_bounds=[-numpy.inf, numpy.inf]) phi_intercept = Variable(covariate='constant_one', var_link_fun=lambda x: x,