本文整理汇总了Python中symfit.Fit类的典型用法代码示例。如果您正苦于以下问题:Python Fit类的具体用法?Python Fit怎么用?Python Fit使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Fit类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: 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)
开发者ID:tBuLi,项目名称:symfit,代码行数:28,代码来源:test_ode.py
示例2: 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)
开发者ID:tBuLi,项目名称:symfit,代码行数:34,代码来源:test_ode.py
示例3: test_likelihood_fitting_exponential
def test_likelihood_fitting_exponential(self):
"""
Fit using the likelihood method.
"""
b = Parameter(value=4, min=3.0)
x, y = variables('x, y')
pdf = {y: Exp(x, 1/b)}
# Draw points from an Exp(5) exponential distribution.
np.random.seed(100)
xdata = np.random.exponential(5, 1000000)
# Expected parameter values
mean = np.mean(xdata)
stdev = np.std(xdata)
mean_stdev = stdev / np.sqrt(len(xdata))
with self.assertRaises(NotImplementedError):
fit = Fit(pdf, x=xdata, sigma_y=2.0, objective=LogLikelihood)
fit = Fit(pdf, xdata, objective=LogLikelihood)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(b) / mean, 1, 3)
self.assertAlmostEqual(fit_result.value(b) / stdev, 1, 3)
self.assertAlmostEqual(fit_result.stdev(b) / mean_stdev, 1, 3)
开发者ID:tBuLi,项目名称:symfit,代码行数:25,代码来源:test_general.py
示例4: test_chained_min_signature
def test_chained_min_signature(self):
"""
Test the automatic generation of the signature for ChainedMinimizer
"""
minimizers = [
BFGS, DifferentialEvolution, BFGS, DifferentialEvolution, BFGS
]
fit = Fit(self.model, self.xx, self.yy, self.ydata,
minimizer=minimizers)
names = [
'BFGS', 'DifferentialEvolution', 'BFGS_2',
'DifferentialEvolution_2', 'BFGS_3'
]
for name, param_name in zip(names, fit.minimizer.__signature__.parameters):
self.assertEqual(name, param_name)
# Check for equal lengths because zip is slippery that way
self.assertEqual(len(names), len(fit.minimizer.__signature__.parameters))
for param in fit.minimizer.__signature__.parameters.values():
self.assertEqual(param.kind, inspect_sig.Parameter.KEYWORD_ONLY)
# Make sure keywords end up at the right minimizer.
with self.assertRaises(TypeError):
# This is not a valid kwarg to DiffEvo, but it is to BFGS. Check if
# we really go by name of the Minimizer, not by order.
fit.execute(DifferentialEvolution={'return_all': False})
开发者ID:tBuLi,项目名称:symfit,代码行数:27,代码来源:test_global_opt.py
示例5: 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 = Fit(
model=model,
a_i=xdata[0],
b_i=xdata[1],
c_i=xdata[2],
minimizer = MINPACK
)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(a) / 9.985691, 1.0, 5)
self.assertAlmostEqual(fit_result.value(b) / 1.006143e+02, 1.0, 4)
self.assertAlmostEqual(fit_result.value(c) / 7.085713e+01, 1.0, 5)
开发者ID:tBuLi,项目名称:symfit,代码行数:28,代码来源:test_general.py
示例6: test_vector_fitting_bounds
def test_vector_fitting_bounds(self):
"""
Tests fitting to a 3 component vector valued function, with bounds.
"""
a, b, c = parameters('a, b, c')
a.min = 0
a.max = 25
b.min = 0
b.max = 500
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 = Fit(
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)
开发者ID:Pitje06,项目名称:symfit,代码行数:30,代码来源:test_auto_fit.py
示例7: test_fitting
def test_fitting(self):
"""
Tests fitting with NumericalLeastSquares. Makes sure that the resulting
objects and values are of the right type, and that the fit_result does
not have unexpected members.
"""
xdata = np.linspace(1, 10, 10)
ydata = 3*xdata**2
a = Parameter() # 3.1, min=2.5, max=3.5
b = Parameter()
x = Variable()
new = a*x**b
fit = Fit(new, xdata, ydata, minimizer=MINPACK)
fit_result = fit.execute()
self.assertIsInstance(fit_result, FitResults)
self.assertAlmostEqual(fit_result.value(a), 3.0)
self.assertAlmostEqual(fit_result.value(b), 2.0)
self.assertIsInstance(fit_result.stdev(a), float)
self.assertIsInstance(fit_result.stdev(b), float)
self.assertIsInstance(fit_result.r_squared, float)
self.assertEqual(fit_result.r_squared, 1.0) # by definition since there's no fuzzyness
开发者ID:tBuLi,项目名称:symfit,代码行数:26,代码来源:test_general.py
示例8: 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)
开发者ID:tBuLi,项目名称:symfit,代码行数:25,代码来源:test_general.py
示例9: test_gaussian_fitting
def test_gaussian_fitting(self):
"""
Tests fitting to a gaussian function and fit_result.params unpacking.
"""
xdata = 2*np.random.rand(10000) - 1 # random betwen [-1, 1]
ydata = 5.0 * scipy.stats.norm.pdf(xdata, loc=0.0, scale=1.0)
x0 = Parameter()
sig = Parameter()
A = Parameter()
x = Variable()
g = A * Gaussian(x, x0, sig)
fit = Fit(g, xdata, ydata)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(A), 5.0)
self.assertAlmostEqual(np.abs(fit_result.value(sig)), 1.0)
self.assertAlmostEqual(fit_result.value(x0), 0.0)
# raise Exception([i for i in fit_result.params])
sexy = g(x=2.0, **fit_result.params)
ugly = g(
x=2.0,
x0=fit_result.value(x0),
A=fit_result.value(A),
sig=fit_result.value(sig),
)
self.assertEqual(sexy, ugly)
开发者ID:Pitje06,项目名称:symfit,代码行数:28,代码来源:test_general.py
示例10: test_fitting
def test_fitting(self):
xdata = np.linspace(1,10,10)
ydata = 3*xdata**2
a = Parameter() #3.1, min=2.5, max=3.5
b = Parameter()
x = Variable()
new = a*x**b
fit = Fit(new, xdata, ydata, minimizer=MINPACK)
fit_result = fit.execute()
self.assertIsInstance(fit_result, FitResults)
self.assertAlmostEqual(fit_result.value(a), 3.0)
self.assertAlmostEqual(fit_result.value(b), 2.0)
self.assertIsInstance(fit_result.stdev(a), float)
self.assertIsInstance(fit_result.stdev(b), float)
self.assertIsInstance(fit_result.r_squared, float)
self.assertEqual(fit_result.r_squared, 1.0) # by definition since there's no fuzzyness
# Test several illegal ways to access the data.
self.assertRaises(AttributeError, getattr, *[fit_result.params, 'a_fdska'])
self.assertRaises(AttributeError, getattr, *[fit_result.params, 'c'])
self.assertRaises(AttributeError, getattr, *[fit_result.params, 'a_stdev_stdev'])
self.assertRaises(AttributeError, getattr, *[fit_result.params, 'a_stdev_'])
self.assertRaises(AttributeError, getattr, *[fit_result.params, 'a__stdev'])
开发者ID:tBuLi,项目名称:symfit,代码行数:27,代码来源:test_fit_result.py
示例11: test_minimize
def test_minimize(self):
"""
Tests maximizing a function with and without constraints, taken from the
scipy `minimize` tutorial. Compare the symfit result with the scipy
result.
https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/optimize.html#constrained-minimization-of-multivariate-scalar-functions-minimize
"""
x = Parameter(value=-1.0)
y = Parameter(value=1.0)
# Use an unnamed Variable on purpose to test the auto-generation of names.
model = Model(2 * x * y + 2 * x - x ** 2 - 2 * y ** 2)
constraints = [
Ge(y - 1, 0), # y - 1 >= 0,
Eq(x**3 - y, 0), # x**3 - y == 0,
]
def func(x, sign=1.0):
""" Objective function """
return sign*(2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2)
def func_deriv(x, sign=1.0):
""" Derivative of objective function """
dfdx0 = sign*(-2*x[0] + 2*x[1] + 2)
dfdx1 = sign*(2*x[0] - 4*x[1])
return np.array([ dfdx0, dfdx1 ])
cons = (
{'type': 'eq',
'fun' : lambda x: np.array([x[0]**3 - x[1]]),
'jac' : lambda x: np.array([3.0*(x[0]**2.0), -1.0])},
{'type': 'ineq',
'fun' : lambda x: np.array([x[1] - 1]),
'jac' : lambda x: np.array([0.0, 1.0])})
# Unconstrained fit
res = minimize(func, [-1.0,1.0], args=(-1.0,), jac=func_deriv,
method='BFGS', options={'disp': False})
fit = Fit(model=- model)
self.assertIsInstance(fit.objective, MinimizeModel)
self.assertIsInstance(fit.minimizer, BFGS)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(x) / res.x[0], 1.0, 6)
self.assertAlmostEqual(fit_result.value(y) / res.x[1], 1.0, 6)
# Same test, but with constraints in place.
res = minimize(func, [-1.0,1.0], args=(-1.0,), jac=func_deriv,
constraints=cons, method='SLSQP', options={'disp': False})
from symfit.core.minimizers import SLSQP
fit = Fit(- model, constraints=constraints)
self.assertEqual(fit.constraints[0].constraint_type, Ge)
self.assertEqual(fit.constraints[1].constraint_type, Eq)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(x), res.x[0], 6)
self.assertAlmostEqual(fit_result.value(y), res.x[1], 6)
开发者ID:tBuLi,项目名称:symfit,代码行数:58,代码来源:test_minimize.py
示例12: test_interdependency_constrained
def test_interdependency_constrained(self):
"""
Test a model with interdependent components, and with constraints which
depend on the Model's output.
This is done in the MatrixSymbol formalism, using a Tikhonov
regularization as an example. In this, a matrix inverse has to be
calculated and is used multiple times. Therefore we split that term of
into a seperate component, so the inverse only has to be computed once
per model call.
See https://arxiv.org/abs/1901.05348 for a more detailed background.
"""
N = Symbol('N', integer=True)
M = MatrixSymbol('M', N, N)
W = MatrixSymbol('W', N, N)
I = MatrixSymbol('I', N, N)
y = MatrixSymbol('y', N, 1)
c = MatrixSymbol('c', N, 1)
a, = parameters('a')
z, = variables('z')
i = Idx('i')
model_dict = {
W: Inverse(I + M / a ** 2),
c: - W * y,
z: sqrt(c.T * c)
}
# Sympy currently does not support derivatives of matrix expressions,
# so we use CallableModel instead of Model.
model = CallableModel(model_dict)
# Generate data
iden = np.eye(2)
M_mat = np.array([[2, 1], [3, 4]])
y_vec = np.array([[3], [5]])
eval_model = model(I=iden, M=M_mat, y=y_vec, a=0.1)
# Calculate the answers 'manually' so I know it was done properly
W_manual = np.linalg.inv(iden + M_mat / 0.1 ** 2)
c_manual = - np.atleast_2d(W_manual.dot(y_vec))
z_manual = np.atleast_1d(np.sqrt(c_manual.T.dot(c_manual)))
self.assertEqual(y_vec.shape, (2, 1))
self.assertEqual(M_mat.shape, (2, 2))
self.assertEqual(iden.shape, (2, 2))
self.assertEqual(W_manual.shape, (2, 2))
self.assertEqual(c_manual.shape, (2, 1))
self.assertEqual(z_manual.shape, (1, 1))
np.testing.assert_almost_equal(W_manual, eval_model.W)
np.testing.assert_almost_equal(c_manual, eval_model.c)
np.testing.assert_almost_equal(z_manual, eval_model.z)
fit = Fit(model, z=z_manual, I=iden, M=M_mat, y=y_vec)
fit_result = fit.execute()
# See if a == 0.1 was reconstructed properly. Since only a**2 features
# in the equations, we check for the absolute value. Setting a.min = 0.0
# is not appreciated by the Minimizer, it seems.
self.assertAlmostEqual(np.abs(fit_result.value(a)), 0.1)
开发者ID:tBuLi,项目名称:symfit,代码行数:57,代码来源:test_constrained.py
示例13: test_2_gaussian_2d_fitting
def test_2_gaussian_2d_fitting(self):
"""
Tests fitting to a scalar gaussian with 2 independent variables with
tight bounds.
"""
mean = (0.3, 0.4) # x, y mean 0.6, 0.4
cov = [[0.01**2, 0], [0, 0.01**2]]
data = np.random.multivariate_normal(mean, cov, 3000000)
mean = (0.7, 0.8) # x, y mean 0.6, 0.4
cov = [[0.01**2, 0], [0, 0.01**2]]
data_2 = np.random.multivariate_normal(mean, cov, 3000000)
data = np.vstack((data, data_2))
# Insert them as y,x here as np fucks up cartesian conventions.
ydata, xedges, yedges = np.histogram2d(data[:, 1], data[:, 0], bins=100,
range=[[0.0, 1.0], [0.0, 1.0]])
xcentres = (xedges[:-1] + xedges[1:]) / 2
ycentres = (yedges[:-1] + yedges[1:]) / 2
# Make a valid grid to match ydata
xx, yy = np.meshgrid(xcentres, ycentres, sparse=False)
# xdata = np.dstack((xx, yy)).T
x = Variable()
y = Variable()
x0_1 = Parameter(0.7, min=0.6, max=0.9)
sig_x_1 = Parameter(0.1, min=0.0, max=0.2)
y0_1 = Parameter(0.8, min=0.6, max=0.9)
sig_y_1 = Parameter(0.1, min=0.0, max=0.2)
A_1 = Parameter()
g_1 = A_1 * Gaussian(x, x0_1, sig_x_1) * Gaussian(y, y0_1, sig_y_1)
x0_2 = Parameter(0.3, min=0.2, max=0.5)
sig_x_2 = Parameter(0.1, min=0.0, max=0.2)
y0_2 = Parameter(0.4, min=0.2, max=0.5)
sig_y_2 = Parameter(0.1, min=0.0, max=0.2)
A_2 = Parameter()
g_2 = A_2 * Gaussian(x, x0_2, sig_x_2) * Gaussian(y, y0_2, sig_y_2)
model = g_1 + g_2
fit = Fit(model, xx, yy, ydata)
fit_result = fit.execute()
self.assertIsInstance(fit.fit, ConstrainedNumericalLeastSquares)
img = model(x=xx, y=yy, **fit_result.params)
img_g_1 = g_1(x=xx, y=yy, **fit_result.params)
img_g_2 = g_2(x=xx, y=yy, **fit_result.params)
np.testing.assert_array_equal(img, img_g_1 + img_g_2)
# Equal up to some precision. Not much obviously.
self.assertAlmostEqual(fit_result.value(x0_1), 0.7, 3)
self.assertAlmostEqual(fit_result.value(y0_1), 0.8, 3)
self.assertAlmostEqual(fit_result.value(x0_2), 0.3, 3)
self.assertAlmostEqual(fit_result.value(y0_2), 0.4, 3)
开发者ID:Pitje06,项目名称:symfit,代码行数:56,代码来源:test_general.py
示例14: test_global_fitting
def test_global_fitting(self):
"""
Test a global fitting scenario with datasets of unequal length. In this
scenario, a quartic equation is fitted where the constant term is shared
between the datasets. (e.g. identical background noise)
"""
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,
})
# Generate data from this model
# xdata = np.linspace(0, 10)
xdata1 = np.linspace(0, 10)
xdata2 = xdata1[::2] # Make the sets of unequal size
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
eval_jac = model.eval_jacobian(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)
self.assertEqual(len(eval_jac), 2)
for comp in eval_jac:
self.assertEqual(len(comp), len(model.params))
sigma_y = np.concatenate((np.ones(20), [2., 4., 5, 7, 3]))
fit = Fit(model, x_1=xdata[0], x_2=xdata[1],
y_1=ydata[0], y_2=ydata[1], sigma_y_2=sigma_y)
fit_result = fit.execute()
# fit_curves = model(x_1=xdata[0], x_2=xdata[1], **fit_result.params)
self.assertAlmostEqual(fit_result.value(y0), 1.061892e+01, 3)
self.assertAlmostEqual(fit_result.value(a_1), 1.013269e+02, 3)
self.assertAlmostEqual(fit_result.value(a_2), 5.625694e+01, 3)
self.assertAlmostEqual(fit_result.value(b_1), 3.362240e-01, 3)
self.assertAlmostEqual(fit_result.value(b_2), 1.565253e+00, 3)
开发者ID:tBuLi,项目名称:symfit,代码行数:55,代码来源:test_constrained.py
示例15: test_chained_min
def test_chained_min(self):
"""Test fitting with a chained minimizer"""
curvals = [p.value for p in self.model.params]
fit = Fit(self.model, self.xx, self.yy, self.ydata,
minimizer=[DifferentialEvolution, BFGS])
fit_result = fit.execute(
DifferentialEvolution={'seed': 0, 'tol': 1e-4, 'maxiter': 10}
)
self.assertAlmostEqual(fit_result.value(self.x0_1), 0.4, 4)
self.assertAlmostEqual(fit_result.value(self.y0_1), 0.4, 4)
self.assertEqual(curvals, [p.value for p in self.model.params])
开发者ID:tBuLi,项目名称:symfit,代码行数:11,代码来源:test_global_opt.py
示例16: test_named_fitting
def test_named_fitting(self):
xdata = np.linspace(1, 10, 10)
ydata = 3*xdata**2
a = Parameter('a', 1.0)
b = Parameter('b', 2.5)
x, y = variables('x, y')
model = {y: a*x**b}
fit = Fit(model, x=xdata, y=ydata)
fit_result = fit.execute()
self.assertIsInstance(fit_result, FitResults)
self.assertAlmostEqual(fit_result.value(a), 3.0, 3)
self.assertAlmostEqual(fit_result.value(b), 2.0, 4)
开发者ID:tBuLi,项目名称:symfit,代码行数:14,代码来源:test_constrained.py
示例17: test_pickle
def test_pickle(self):
xdata = np.linspace(1, 10, 10)
ydata = 3 * xdata ** 2
a = Parameter('a') # 3.1, min=2.5, max=3.5
b = Parameter('b')
x = Variable('x')
y = Variable('y')
new = {y: a * x ** b}
fit = Fit(new, x=xdata, y=ydata)
fit_result = fit.execute()
new_result = pickle.loads(pickle.dumps(fit_result))
self.assertEqual(fit_result.__dict__.keys(), new_result.__dict__.keys())
开发者ID:tBuLi,项目名称:symfit,代码行数:14,代码来源:test_fit_result.py
示例18: test_MatrixSymbolModel
def test_MatrixSymbolModel(self):
"""
Test a model which is defined by ModelSymbols, see #194
"""
N = Symbol('N', integer=True)
M = MatrixSymbol('M', N, N)
W = MatrixSymbol('W', N, N)
I = MatrixSymbol('I', N, N)
y = MatrixSymbol('y', N, 1)
c = MatrixSymbol('c', N, 1)
a, b = parameters('a, b')
z, x = variables('z, x')
model_dict = {
W: Inverse(I + M / a ** 2),
c: - W * y,
z: sqrt(c.T * c)
}
# TODO: This should be a Model in the future, but sympy is not yet
# capable of computing Matrix derivatives at the time of writing.
model = CallableModel(model_dict)
self.assertEqual(model.params, [a])
self.assertEqual(model.independent_vars, [I, M, y])
self.assertEqual(model.dependent_vars, [z])
self.assertEqual(model.interdependent_vars, [W, c])
self.assertEqual(model.connectivity_mapping,
{W: {I, M, a}, c: {W, y}, z: {c}})
# Generate data
iden = np.eye(2)
M_mat = np.array([[2, 1], [3, 4]])
y_vec = np.array([3, 5])
eval_model = model(I=iden, M=M_mat, y=y_vec, a=0.1)
W_manual = np.linalg.inv(iden + M_mat / 0.1 ** 2)
c_manual = - W_manual.dot(y_vec)
z_manual = np.atleast_1d(np.sqrt(c_manual.T.dot(c_manual)))
np.testing.assert_allclose(eval_model.W, W_manual)
np.testing.assert_allclose(eval_model.c, c_manual)
np.testing.assert_allclose(eval_model.z, z_manual)
# Now try to retrieve the value of `a` from a fit
a.value = 0.2
fit = Fit(model, z=z_manual, I=iden, M=M_mat, y=y_vec)
fit_result = fit.execute()
eval_model = model(I=iden, M=M_mat, y=y_vec, **fit_result.params)
self.assertAlmostEqual(0.1, np.abs(fit_result.value(a)))
np.testing.assert_allclose(eval_model.W, W_manual, rtol=1e-5)
np.testing.assert_allclose(eval_model.c, c_manual, rtol=1e-5)
np.testing.assert_allclose(eval_model.z, z_manual, rtol=1e-5)
开发者ID:tBuLi,项目名称:symfit,代码行数:50,代码来源:test_model.py
示例19: test_gaussian_2d_fitting
def test_gaussian_2d_fitting(self):
"""
Tests fitting to a scalar gaussian function with 2 independent
variables. Very sensitive to initial guesses, and if they are chosen too
restrictive Fit actually throws a tantrum.
It therefore appears to be more sensitive than NumericalLeastSquares.
"""
mean = (0.6, 0.4) # x, y mean 0.6, 0.4
cov = [[0.2**2, 0], [0, 0.1**2]]
np.random.seed(0)
data = np.random.multivariate_normal(mean, cov, 100000)
# Insert them as y,x here as np fucks up cartesian conventions.
ydata, xedges, yedges = np.histogram2d(data[:, 0], data[:, 1], bins=100,
range=[[0.0, 1.0], [0.0, 1.0]])
xcentres = (xedges[:-1] + xedges[1:]) / 2
ycentres = (yedges[:-1] + yedges[1:]) / 2
# Make a valid grid to match ydata
xx, yy = np.meshgrid(xcentres, ycentres, sparse=False, indexing='ij')
x0 = Parameter(value=mean[0], min=0.0, max=1.0)
sig_x = Parameter(value=0.2, min=0.0, max=0.3)
y0 = Parameter(value=mean[1], min=0.0, max=1.0)
sig_y = Parameter(value=0.1, min=0.0, max=0.3)
A = Parameter(value=np.mean(ydata), min=0.0)
x = Variable('x')
y = Variable('y')
g = Variable('g')
model = Model({g: A * Gaussian(x, x0, sig_x) * Gaussian(y, y0, sig_y)})
fit = Fit(model, x=xx, y=yy, g=ydata)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(x0), np.mean(data[:, 0]), 3)
self.assertAlmostEqual(fit_result.value(y0), np.mean(data[:, 1]), 3)
self.assertAlmostEqual(np.abs(fit_result.value(sig_x)), np.std(data[:, 0]), 2)
self.assertAlmostEqual(np.abs(fit_result.value(sig_y)), np.std(data[:, 1]), 2)
self.assertGreaterEqual(fit_result.r_squared, 0.96)
# Compare with industry standard MINPACK
fit_std = Fit(model, x=xx, y=yy, g=ydata, minimizer=MINPACK)
fit_std_result = fit_std.execute()
self.assertAlmostEqual(fit_std_result.value(x0), fit_result.value(x0), 4)
self.assertAlmostEqual(fit_std_result.value(y0), fit_result.value(y0), 4)
self.assertAlmostEqual(fit_std_result.value(sig_x), fit_result.value(sig_x), 4)
self.assertAlmostEqual(fit_std_result.value(sig_y), fit_result.value(sig_y), 4)
self.assertAlmostEqual(fit_std_result.r_squared, fit_result.r_squared, 4)
开发者ID:tBuLi,项目名称:symfit,代码行数:49,代码来源:test_constrained.py
示例20: test_powell
def test_powell(self):
"""
Powell with a single parameter gave an error because a 0-d array was
returned by scipy. So no error here is winning.
"""
x, y = variables('x, y')
a, b = parameters('a, b')
b.fixed = True
model = Model({y: a * x + b})
xdata = np.linspace(0, 10)
ydata = model(x=xdata, a=5.5, b=15.0).y + np.random.normal(0, 1)
fit = Fit({y: a * x + b}, x=xdata, y=ydata, minimizer=Powell)
fit_result = fit.execute()
self.assertAlmostEqual(fit_result.value(b), 1.0)
开发者ID:tBuLi,项目名称:symfit,代码行数:15,代码来源:test_minimizers.py
注:本文中的symfit.Fit类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论