本文整理汇总了Python中symfit.parameters函数的典型用法代码示例。如果您正苦于以下问题:Python parameters函数的具体用法?Python parameters怎么用?Python parameters使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parameters函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fourier_series
def fourier_series(x, f, n=0):
"""
Returns a symbolic fourier series of order `n`.
:param n: Order of the fourier series.
:param x: Independent variable
:param f: Frequency of the fourier series
"""
# Make the parameter objects for all the terms
a0, *cos_a = parameters(','.join(['a{}'.format(i) for i in range(0, n + 1)]))
sin_b = parameters(','.join(['b{}'.format(i) for i in range(1, n + 1)]))
# Construct the series
series = a0 + sum(ai * cos(i * f * x) + bi * sin(i * f * x)
for i, (ai, bi) in enumerate(zip(cos_a, sin_b), start=1))
return series
示例2: test_known_solution
def test_known_solution(self):
p, c1 = parameters('p, c1')
y, t = variables('y, t')
p.value = 3.0
model_dict = {
D(y, t): - p * y,
}
# Lets say we know the exact solution to this problem
sol = Model({y: exp(- p * t)})
# Generate some data
tdata = np.linspace(0, 3, 10001)
ydata = sol(t=tdata, p=3.22)[0]
ydata += np.random.normal(0, 0.005, ydata.shape)
ode_model = ODEModel(model_dict, initial={t: 0.0, y: ydata[0]})
fit = Fit(ode_model, t=tdata, y=ydata)
ode_result = fit.execute()
c1.value = ydata[0]
fit = Fit(sol, t=tdata, y=ydata)
fit_result = fit.execute()
self.assertAlmostEqual(ode_result.value(p) / fit_result.value(p), 1, 2)
self.assertAlmostEqual(ode_result.r_squared / fit_result.r_squared, 1, 4)
self.assertAlmostEqual(ode_result.stdev(p) / fit_result.stdev(p), 1, 3)
示例3: test_simple_kinetics
def test_simple_kinetics(self):
"""
Simple kinetics data to test fitting
"""
tdata = np.array([10, 26, 44, 70, 120])
adata = 10e-4 * np.array([44, 34, 27, 20, 14])
a, b, t = variables('a, b, t')
k, a0 = parameters('k, a0')
k.value = 0.01
# a0.value, a0.min, a0.max = 54 * 10e-4, 40e-4, 60e-4
a0 = 54 * 10e-4
model_dict = {
D(a, t): - k * a**2,
D(b, t): k * a**2,
}
ode_model = ODEModel(model_dict, initial={t: 0.0, a: a0, b: 0.0})
# Analytical solution
model = GradientModel({a: 1 / (k * t + 1 / a0)})
fit = Fit(model, t=tdata, a=adata)
fit_result = fit.execute()
fit = Fit(ode_model, t=tdata, a=adata, b=None, minimizer=MINPACK)
ode_result = fit.execute()
self.assertAlmostEqual(ode_result.value(k) / fit_result.value(k), 1.0, 4)
self.assertAlmostEqual(ode_result.stdev(k) / fit_result.stdev(k), 1.0, 4)
self.assertAlmostEqual(ode_result.r_squared / fit_result.r_squared, 1, 4)
fit = Fit(ode_model, t=tdata, a=adata, b=None)
ode_result = fit.execute()
self.assertAlmostEqual(ode_result.value(k) / fit_result.value(k), 1.0, 4)
self.assertAlmostEqual(ode_result.stdev(k) / fit_result.stdev(k), 1.0, 4)
self.assertAlmostEqual(ode_result.r_squared / fit_result.r_squared, 1, 4)
示例4: test_taylor_model
def test_taylor_model(self):
a, b = parameters('a, b')
x, y, z = variables('x, y, z')
model = Model({y: a * x + b})
appr = TaylorModel(model)
self.assertEqual(set([a, b]), set(appr.params))
appr.p0 = {a: 2.0, b: 5.0}
self.assertEqual(set(appr.p0.keys()), set(appr.params_0[p] for p in appr.params))
self.assertTrue(LinearLeastSquares.is_linear(appr))
model = Model({z: a * x**2 + b * y**2})
appr = TaylorModel(model)
appr.p0 = {a: 2, b: 5}
model = Model({z: a * x**2 + b * y**2})
appr_2 = TaylorModel(model)
appr_2.p0 = {a: 1, b: 1}
self.assertTrue(appr == appr_2)
model = Model({y: a * sympy.exp(x * b)})
appr = TaylorModel(model)
appr.p0 = {a: 2.0, b: 5.0}
self.assertTrue(LinearLeastSquares.is_linear(appr))
model = Model({y: sympy.sin(a * x)})
appr = TaylorModel(model)
appr.p0 = {a: 0.0}
self.assertTrue(LinearLeastSquares.is_linear(appr))
示例5: test_data_for_constraint
def test_data_for_constraint(self):
"""
Test the signature handling when constraints are at play. Constraints
should take seperate data, but still kwargs that are not found in either
the model nor the constraints should raise an error.
"""
A, mu, sig = parameters('A, mu, sig')
x, y, Y = variables('x, y, Y')
model = Model({y: A * Gaussian(x, mu=mu, sig=sig)})
constraint = Model.as_constraint(Y, model, constraint_type=Eq)
np.random.seed(2)
xdata = np.random.normal(1.2, 2, 10)
ydata, xedges = np.histogram(xdata, bins=int(np.sqrt(len(xdata))),
density=True)
# Allowed
fit = Fit(model, x=xdata, y=ydata, Y=2, constraints=[constraint])
fit = Fit(model, x=xdata, y=ydata)
fit = Fit(model, x=xdata, objective=LogLikelihood)
# Not allowed
with self.assertRaises(TypeError):
fit = Fit(model, x=xdata, y=ydata, Y=2)
with self.assertRaises(TypeError):
fit = Fit(model, x=xdata, y=ydata, Y=2, Z=3, constraints=[constraint])
示例6: test_straight_line_analytical
def test_straight_line_analytical(self):
"""
Test symfit against a straight line, for which the parameters and their
uncertainties are known analytically. Assuming equal weights.
"""
data = [[0, 1], [1, 0], [3, 2], [5, 4]]
xdata, ydata = (np.array(i, dtype='float64') for i in zip(*data))
# x = np.arange(0, 100, 0.1)
# np.random.seed(10)
# y = 3.0*x + 105.0 + np.random.normal(size=x.shape)
dx = xdata - xdata.mean()
dy = ydata - ydata.mean()
mean_squared_x = np.mean(xdata**2) - np.mean(xdata)**2
mean_xy = np.mean(xdata * ydata) - np.mean(xdata)*np.mean(ydata)
a = mean_xy/mean_squared_x
b = ydata.mean() - a * xdata.mean()
self.assertAlmostEqual(a, 0.694915, 6) # values from Mathematica
self.assertAlmostEqual(b, 0.186441, 6)
S = np.sum((ydata - (a*xdata + b))**2)
var_a_exact = S/(len(xdata) * (len(xdata) - 2) * mean_squared_x)
var_b_exact = var_a_exact*np.mean(xdata**2)
a_exact = a
b_exact = b
# We will now compare these exact results with values from symfit, numerically
a, b = parameters('a, b')
x, y = variables('x, y')
model = {y: a*x + b}
fit = NumericalLeastSquares(model, x=xdata, y=ydata)#, absolute_sigma=False)
fit_result = fit.execute()
popt, pcov = curve_fit(lambda z, c, d: c * z + d, xdata, ydata,
jac=lambda z, c, d: np.transpose([xdata, np.ones_like(xdata)]))
# jac=lambda p, x, y, func: np.transpose([x, np.ones_like(x)]))
# Dfun=lambda p, x, y, func: print(p, func, x, y))
# curve_fit
self.assertAlmostEqual(a_exact, popt[0], 4)
self.assertAlmostEqual(b_exact, popt[1], 4)
self.assertAlmostEqual(var_a_exact, pcov[0][0], 6)
self.assertAlmostEqual(var_b_exact, pcov[1][1], 6)
self.assertAlmostEqual(a_exact, fit_result.value(a), 4)
self.assertAlmostEqual(b_exact, fit_result.value(b), 4)
self.assertAlmostEqual(var_a_exact, fit_result.variance(a), 6)
self.assertAlmostEqual(var_b_exact, fit_result.variance(b), 6)
# Do the fit with the LinearLeastSquares object
fit = LinearLeastSquares(model, x=xdata, y=ydata)
fit_result = fit.execute()
self.assertAlmostEqual(a_exact, fit_result.value(a), 4)
self.assertAlmostEqual(b_exact, fit_result.value(b), 4)
self.assertAlmostEqual(var_a_exact, fit_result.variance(a), 6)
self.assertAlmostEqual(var_b_exact, fit_result.variance(b), 6)
# Lets also make sure the entire covariance matrix is the same
for cov1, cov2 in zip(fit_result.params.covariance_matrix.flatten(), pcov.flatten()):
self.assertAlmostEqual(cov1, cov2)
示例7: test_likelihood_fitting_gaussian
def test_likelihood_fitting_gaussian(self):
"""
Fit using the likelihood method.
"""
mu, sig = parameters('mu, sig')
sig.min = 0.01
sig.value = 3.0
mu.value = 50.
x = Variable()
pdf = Gaussian(x, mu, sig)
np.random.seed(10)
xdata = np.random.normal(51., 3.5, 10000)
# Expected parameter values
mean = np.mean(xdata)
stdev = np.std(xdata)
mean_stdev = stdev/np.sqrt(len(xdata))
fit = Fit(pdf, xdata, objective=LogLikelihood)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(mu) / mean, 1, 6)
self.assertAlmostEqual(fit_result.stdev(mu) / mean_stdev, 1, 3)
self.assertAlmostEqual(fit_result.value(sig) / np.std(xdata), 1, 6)
示例8: test_vector_fitting
def test_vector_fitting(self):
"""
Tests fitting to a 3 component vector valued function, without bounds
or guesses.
"""
a, b, c = parameters('a, b, c')
a_i, b_i, c_i = variables('a_i, b_i, c_i')
model = {a_i: a, b_i: b, c_i: c}
xdata = np.array([
[10.1, 9., 10.5, 11.2, 9.5, 9.6, 10.],
[102.1, 101., 100.4, 100.8, 99.2, 100., 100.8],
[71.6, 73.2, 69.5, 70.2, 70.8, 70.6, 70.1],
])
fit = NumericalLeastSquares(
model=model,
a_i=xdata[0],
b_i=xdata[1],
c_i=xdata[2],
)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(a), 9.985691, 6)
self.assertAlmostEqual(fit_result.value(b), 1.006143e+02, 4)
self.assertAlmostEqual(fit_result.value(c), 7.085713e+01, 5)
示例9: test_pickle
def test_pickle(self):
"""
Make sure models can be pickled are preserved when pickling
"""
a, b = parameters('a, b')
x, y = variables('x, y')
exact_model = Model({y: a * x ** b})
constraint = Model.as_constraint(Eq(a, b), exact_model)
num_model = CallableNumericalModel(
{y: a * x ** b}, independent_vars=[x], params=[a, b]
)
connected_num_model = CallableNumericalModel(
{y: a * x ** b}, connectivity_mapping={y: {x, a, b}}
)
# Test if lsoda args and kwargs are pickled too
ode_model = ODEModel({D(y, x): a * x + b}, {x: 0.0}, 3, 4, some_kwarg=True)
models = [exact_model, constraint, num_model, ode_model,
connected_num_model]
for model in models:
new_model = pickle.loads(pickle.dumps(model))
# Compare signatures
self.assertEqual(model.__signature__, new_model.__signature__)
# Trigger the cached vars because we compare `__dict__` s
model.vars
new_model.vars
# Explicitly make sure the connectivity mapping is identical.
self.assertEqual(model.connectivity_mapping,
new_model.connectivity_mapping)
if not isinstance(model, ODEModel):
model.function_dict
model.vars_as_functions
new_model.function_dict
new_model.vars_as_functions
self.assertEqual(model.__dict__, new_model.__dict__)
示例10: test_vector_none_fitting
def test_vector_none_fitting(self):
"""
Fit to a 3 component vector valued function with one variables data set
to None, without bounds or guesses.
"""
a, b, c = parameters('a, b, c')
a_i, b_i, c_i = variables('a_i, b_i, c_i')
model = {a_i: a, b_i: b, c_i: c}
xdata = np.array([
[10.1, 9., 10.5, 11.2, 9.5, 9.6, 10.],
[102.1, 101., 100.4, 100.8, 99.2, 100., 100.8],
[71.6, 73.2, 69.5, 70.2, 70.8, 70.6, 70.1],
])
fit_none = NumericalLeastSquares(
model=model,
a_i=xdata[0],
b_i=xdata[1],
c_i=None,
)
fit = NumericalLeastSquares(
model=model,
a_i=xdata[0],
b_i=xdata[1],
c_i=xdata[2],
)
fit_none_result = fit_none.execute()
fit_result = fit.execute()
self.assertAlmostEqual(fit_none_result.value(a), fit_result.value(a), 4)
self.assertAlmostEqual(fit_none_result.value(b), fit_result.value(b), 4)
# the parameter without data should be unchanged.
self.assertAlmostEqual(fit_none_result.value(c), 1.0)
示例11: test_global_fitting
def test_global_fitting(self):
"""
In case of shared parameters between the components of the model, `Fit`
should automatically use `ConstrainedLeastSquares`.
:return:
"""
x_1, x_2, y_1, y_2 = variables('x_1, x_2, y_1, y_2')
y0, a_1, a_2, b_1, b_2 = parameters('y0, a_1, a_2, b_1, b_2')
# The following vector valued function links all the equations together
# as stated in the intro.
model = Model({
y_1: a_1 * x_1**2 + b_1 * x_1 + y0,
y_2: a_2 * x_2**2 + b_2 * x_2 + y0,
})
self.assertTrue(model.shared_parameters)
# Generate data from this model
xdata1 = np.linspace(0, 10)
xdata2 = xdata1[::2] # Only every other point.
ydata1, ydata2 = model(x_1=xdata1, x_2=xdata2, a_1=101.3, b_1=0.5, a_2=56.3, b_2=1.1111, y0=10.8)
# Add some noise to make it appear like real data
np.random.seed(1)
ydata1 += np.random.normal(0, 2, size=ydata1.shape)
ydata2 += np.random.normal(0, 2, size=ydata2.shape)
xdata = [xdata1, xdata2]
ydata = [ydata1, ydata2]
# Guesses
a_1.value = 100
a_2.value = 50
b_1.value = 1
b_2.value = 1
y0.value = 10
fit = Fit(
model, x_1=xdata[0], x_2=xdata[1], y_1=ydata[0], y_2=ydata[1]
)
self.assertIsInstance(fit.fit, ConstrainedNumericalLeastSquares)
# The next model does not share parameters, but is still a vector
model = Model({
y_1: a_1 * x_1**2 + b_1 * x_1,
y_2: a_2 * x_2**2 + b_2 * x_2,
})
fit = Fit(
model, x_1=xdata[0], x_2=xdata[1], y_1=ydata[0], y_2=ydata[1]
)
self.assertFalse(model.shared_parameters)
self.assertIsInstance(fit.fit, NumericalLeastSquares)
# Scalar model, so it should use NumericalLeastSquares.
model = Model({
y_1: a_1 * x_1**2 + b_1 * x_1,
})
fit = Fit(model, x_1=xdata[0], y_1=ydata[0])
self.assertFalse(model.shared_parameters)
self.assertIsInstance(fit.fit, NumericalLeastSquares)
示例12: test_single_eval
def test_single_eval(self):
"""
Eval an ODEModel at a single value rather than a vector.
"""
x, y, t = variables('x, y, t')
k, = parameters('k') # C is the integration constant.
# The harmonic oscillator as a system, >1st order is not supported yet.
harmonic_dict = {
D(x, t): - k * y,
D(y, t): k * x,
}
# Make a second model to prevent caching of integration results.
# This also means harmonic_dict should NOT be a Model object.
harmonic_model_array = ODEModel(harmonic_dict, initial={t: 0.0, x: 1.0, y: 0.0})
harmonic_model_points = ODEModel(harmonic_dict, initial={t: 0.0, x: 1.0, y: 0.0})
tdata = np.linspace(0, 100, 101)
X, Y = harmonic_model_array(t=tdata, k=0.1)
# Shuffle the data to prevent using the result at time t to calculate
# t+dt
random_order = np.random.permutation(len(tdata))
for idx in random_order:
t = tdata[idx]
X_val = X[idx]
Y_val = Y[idx]
X_point, Y_point = harmonic_model_points(t=t, k=0.1)
self.assertAlmostEqual(X_point[0], X_val)
self.assertAlmostEqual(Y_point[0], Y_val)
示例13: test_simple_kinetics
def test_simple_kinetics(self):
"""
Simple kinetics data to test fitting
"""
tdata = np.array([10, 26, 44, 70, 120])
adata = 10e-4 * np.array([44, 34, 27, 20, 14])
a, b, t = variables('a, b, t')
k, a0 = parameters('k, a0')
k.value = 0.01
# a0.value, a0.min, a0.max = 54 * 10e-4, 40e-4, 60e-4
a0 = 54 * 10e-4
model_dict = {
D(a, t): - k * a**2,
D(b, t): k * a**2,
}
ode_model = ODEModel(model_dict, initial={t: 0.0, a: a0, b: 0.0})
# Generate some data
tvec = np.linspace(0, 500, 1000)
fit = NumericalLeastSquares(ode_model, t=tdata, a=adata, b=None)
fit_result = fit.execute()
# print(fit_result)
self.assertAlmostEqual(fit_result.value(k), 4.302875e-01, 4)
self.assertAlmostEqual(fit_result.stdev(k), 6.447068e-03, 4)
fit = Fit(ode_model, t=tdata, a=adata, b=None)
fit_result = fit.execute()
# print(fit_result)
self.assertAlmostEqual(fit_result.value(k), 4.302875e-01, 4)
self.assertTrue(np.isnan(fit_result.stdev(k)))
示例14: test_full_eval_range
def test_full_eval_range(self):
"""
Test if ODEModels can be evaluated at t < t_initial.
A bit of a no news is good news test.
"""
tdata = np.array([0, 10, 26, 44, 70, 120])
adata = 10e-4 * np.array([54, 44, 34, 27, 20, 14])
a, b, t = variables('a, b, t')
k, a0 = parameters('k, a0')
k.value = 0.01
t0 = tdata[2]
a0 = adata[2]
b0 = 0.02729855 # Obtained from evaluating from t=0.
model_dict = {
D(a, t): - k * a**2,
D(b, t): k * a**2,
}
ode_model = ODEModel(model_dict, initial={t: t0, a: a0, b: b0})
fit = Fit(ode_model, t=tdata, a=adata, b=None)
ode_result = fit.execute()
self.assertGreater(ode_result.r_squared, 0.95, 4)
# Now start from a timepoint that is not in the t-array such that it
# triggers another pathway to be taken in integrating it.
# Again, no news is good news.
ode_model = ODEModel(model_dict, initial={t: t0 + 1e-5, a: a0, b: b0})
fit = Fit(ode_model, t=tdata, a=adata, b=None)
ode_result = fit.execute()
self.assertGreater(ode_result.r_squared, 0.95, 4)
示例15: test_vector_fitting_guess
def test_vector_fitting_guess(self):
"""
Tests fitting to a 3 component vector valued function, with guesses.
"""
a, b, c = parameters('a, b, c')
a.value = 10
b.value = 100
a_i, b_i, c_i = variables('a_i, b_i, c_i')
model = {a_i: a, b_i: b, c_i: c}
xdata = np.array([
[10.1, 9., 10.5, 11.2, 9.5, 9.6, 10.],
[102.1, 101., 100.4, 100.8, 99.2, 100., 100.8],
[71.6, 73.2, 69.5, 70.2, 70.8, 70.6, 70.1],
])
fit = NumericalLeastSquares(
model=model,
a_i=xdata[0],
b_i=xdata[1],
c_i=xdata[2],
)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(a), np.mean(xdata[0]), 4)
self.assertAlmostEqual(fit_result.value(b), np.mean(xdata[1]), 4)
self.assertAlmostEqual(fit_result.value(c), np.mean(xdata[2]), 4)