Skip to content

Commit 6df75c1

Browse files
committed
update SimpleModel
1 parent ab443e1 commit 6df75c1

File tree

2 files changed

+27
-43
lines changed

2 files changed

+27
-43
lines changed

simplemc/models/SimpleModel.py

+16-26
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77

88
class SimpleModel:
99
"""
10-
This is a generic model
11-
12-
Parameters
13-
----------
14-
parameters : list
15-
List of Parameter objects
16-
model : str
17-
model or function. It should be in terms of x
18-
Example: '5*np.cos(x)' or 'x**2-5*x'
10+
This is a generic model
1911
12+
Parameters
13+
----------
14+
parameters : list
15+
List of Parameter objects
16+
function : function
17+
model or function. It should be in terms of the parameters
2018
"""
21-
def __init__(self, parameters, model):
19+
def __init__(self, parameters, function):
2220
self.parameters = parameters
23-
self.model = model
21+
self.function = function
2422
SimpleModel.updateParams(self, [])
2523

2624
# my free params (parameters/priors see ParamDefs.py)
@@ -38,23 +36,14 @@ def printParameters(self, params):
3836
l.append("{}: {} = +/- {}".format(p.name, p.value, p.error))
3937
return l
4038

39+
def updateParams(self, pars):
40+
return True
4141

4242
def genericModel(self, x):
43-
# values = []
44-
# for param in self.parameters:
45-
# values.append(param.value)
46-
# return self.function(values, x)
43+
values = []
4744
for param in self.parameters:
48-
exec("%s = %f" % (param.name, param.value))
49-
try:
50-
r = eval(self.model)
51-
except:
52-
print("Please check your model expression: {}".format(self.model))
53-
sys.exit("Your expression should be in terms of x. \\ For example: '5*np.sin(x)' or '5*x**2'")
54-
return r
55-
56-
def updateParams(self, pars):
57-
return True
45+
values.append(param.value)
46+
return self.function(values, x)
5847

5948
def prior_loglike(self):
6049
return 0
@@ -63,7 +52,8 @@ def prior_loglike(self):
6352
class SimpleCosmoModel(LCDMCosmology):
6453
def __init__(self, extra_params=None, RHSquared=None):
6554
"""
66-
This is a simple cosmological model based on LCDMCosmology class.
55+
This is a simple cosmological model based on slightly deviations of LCDMCosmology
56+
class, RHSquared must to have an analytical form.
6757
6858
Parameters
6959
----------

testModel.py

+11-17
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,28 @@
88
# (bound_inf, bound_sup), LaTeX name
99
# The name of the variables must to be the same at the name of the Parameter
1010
m = Parameter("m", 0, 0.05, (0, 0.1), "m_0")
11+
# m = Parameter("m", 3, 0.5, (0,5), "m_0")
1112
b = Parameter("b", 3, 0.05, (0, 5), "b_0")
1213

1314
# 2) Create a list with your parameters objects
1415
parameterlist = [m, b]
1516

1617
# 3) Define a method that reads a list of parameters,
18+
# unzip them and return the a function of x with the
19+
# parameters.
20+
def model(parameterlist, x):
21+
m, b = parameterlist
22+
return m*x+b
1723

18-
model = 'm*x+b'
1924
cosmo_model = 'Ocb/a**3+Omrad/a**4+NuContrib+(1.0-Om-m)'
2025

2126
# 4) Use SimpleMC as usually, but with model = custom_model
2227

23-
# analyzer = DriverMC(model='simple', datasets='dline', analyzername='mcmc',
24-
# custom_parameters=parameterlist, custom_function=model)
28+
analyzer = DriverMC(model='simple', datasets='dline', analyzername='mcmc',
29+
custom_parameters=parameterlist, custom_function=model)
2530

26-
analyzer = DriverMC(model='simple_cosmo', datasets='SN', analyzername='mcmc',
27-
custom_parameters=parameterlist, custom_function=cosmo_model)
31+
# analyzer = DriverMC(model='simple_cosmo', datasets='SN', analyzername='mcmc',
32+
# custom_parameters=parameterlist, custom_function=cosmo_model)
2833

2934

30-
analyzer.executer(nsamp=1000)
31-
32-
# import corner
33-
# import numpy as np
34-
# import matplotlib.pyplot as plt
35-
#
36-
# dir = 'simplemc/chains/'
37-
# text_chain = 'simple_phy_dline_mcmc_1.txt'
38-
# npchain = np.loadtxt(dir+text_chain)
39-
# chain = npchain[:,2:5]
40-
# figure = corner.corner(chain)
41-
# plt.show()
35+
analyzer.executer(nsamp=1000)

0 commit comments

Comments
 (0)