本文整理汇总了Python中lmfit.Minimizer.penalty方法的典型用法代码示例。如果您正苦于以下问题:Python Minimizer.penalty方法的具体用法?Python Minimizer.penalty怎么用?Python Minimizer.penalty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lmfit.Minimizer
的用法示例。
在下文中一共展示了Minimizer.penalty方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CommonMinimizerTest
# 需要导入模块: from lmfit import Minimizer [as 别名]
# 或者: from lmfit.Minimizer import penalty [as 别名]
#.........这里部分代码省略.........
if not HAS_EMCEE:
return True
self.mini.emcee(nwalkers=100, steps=5)
# if you've run the sampler the Minimizer object should have a _lastpos
# attribute
assert_(hasattr(self.mini, '_lastpos'))
# now try and re-use sampler
out2 = self.mini.emcee(steps=10, reuse_sampler=True)
assert_(out2.chain.shape[1] == 15)
# you shouldn't be able to reuse the sampler if nvarys has changed.
self.mini.params['amp'].vary = False
pytest.raises(ValueError, self.mini.emcee, reuse_sampler=True)
def test_emcee_lnpost(self):
# check ln likelihood is calculated correctly. It should be
# -0.5 * chi**2.
result = self.mini.minimize()
# obtain the numeric values
# note - in this example all the parameters are varied
fvars = np.array([par.value for par in result.params.values()])
# calculate the cost function with scaled values (parameters all have
# lower and upper bounds.
scaled_fvars = []
for par, fvar in zip(result.params.values(), fvars):
par.value = fvar
scaled_fvars.append(par.setup_bounds())
val = self.mini.penalty(np.array(scaled_fvars))
# calculate the log-likelihood value
bounds = np.array([(par.min, par.max)
for par in result.params.values()])
val2 = _lnpost(fvars,
self.residual,
result.params,
result.var_names,
bounds,
userargs=(self.x, self.data))
assert_almost_equal(-0.5 * val, val2)
def test_emcee_output(self):
# test mcmc output
if not HAS_EMCEE:
return True
try:
from pandas import DataFrame
except ImportError:
return True
out = self.mini.emcee(nwalkers=10, steps=20, burn=5, thin=2)
assert_(isinstance(out, MinimizerResult))
assert_(isinstance(out.flatchain, DataFrame))
# check that we can access the chains via parameter name
assert_(out.flatchain['amp'].shape[0] == 80)
assert out.errorbars
assert_(np.isfinite(out.params['amp'].correl['period']))
# the lnprob array should be the same as the chain size
assert_(np.size(out.chain)//out.nvarys == np.size(out.lnprob))
示例2: CommonMinimizerTest
# 需要导入模块: from lmfit import Minimizer [as 别名]
# 或者: from lmfit.Minimizer import penalty [as 别名]
#.........这里部分代码省略.........
# but you can't initialise if the shape is wrong.
assert_raises(ValueError,
self.mini.emcee,
nwalkers=100,
steps=1,
pos=out.chain[..., -1, :-1])
def test_emcee_reuse_sampler(self):
if not HAS_EMCEE:
return True
self.mini.emcee(nwalkers=100, steps=5)
# if you've run the sampler the Minimizer object should have a _lastpos
# attribute
assert_(hasattr(self.mini, '_lastpos'))
# now try and re-use sampler
out2 = self.mini.emcee(steps=10, reuse_sampler=True)
assert_(out2.chain.shape[1] == 15)
# you shouldn't be able to reuse the sampler if nvarys has changed.
self.mini.params['amp'].vary = False
assert_raises(ValueError, self.mini.emcee, reuse_sampler=True)
def test_emcee_lnpost(self):
# check ln likelihood is calculated correctly. It should be
# -0.5 * chi**2.
result = self.mini.minimize()
# obtain the numeric values
# note - in this example all the parameters are varied
fvars = np.array([par.value for par in result.params.values()])
# calculate the cost function with scaled values (parameters all have
# lower and upper bounds.
scaled_fvars = []
for par, fvar in zip(result.params.values(), fvars):
par.value = fvar
scaled_fvars.append(par.setup_bounds())
val = self.mini.penalty(np.array(scaled_fvars))
# calculate the log-likelihood value
bounds = np.array([(par.min, par.max)
for par in result.params.values()])
val2 = _lnpost(fvars,
self.residual,
result.params,
result.var_names,
bounds,
userargs=(self.x, self.data))
assert_almost_equal(-0.5 * val, val2)
def test_emcee_output(self):
# test mcmc output
if not HAS_EMCEE:
return True
try:
from pandas import DataFrame
except ImportError:
return True
out = self.mini.emcee(nwalkers=10, steps=20, burn=5, thin=2)
assert_(isinstance(out, MinimizerResult))
assert_(isinstance(out.flatchain, DataFrame))
# check that we can access the chains via parameter name
assert_(out.flatchain['amp'].shape[0] == 80)
assert_(out.errorbars is True)
assert_(np.isfinite(out.params['amp'].correl['period']))
# the lnprob array should be the same as the chain size
assert_(np.size(out.chain)//4 == np.size(out.lnprob))
@decorators.slow
def test_emcee_float(self):
# test that it works if the residuals returns a float, not a vector
if not HAS_EMCEE:
return True
def resid(pars, x, data=None):
return -0.5 * np.sum(self.residual(pars, x, data=data)**2)
# just return chi2
def resid2(pars, x, data=None):
return np.sum(self.residual(pars, x, data=data)**2)
self.mini.userfcn = resid
np.random.seed(123456)
out = self.mini.emcee(nwalkers=100, steps=200,
burn=50, thin=10)
check_paras(out.params, self.p_true, sig=3)
self.mini.userfcn = resid2
np.random.seed(123456)
out = self.mini.emcee(nwalkers=100, steps=200,
burn=50, thin=10, float_behavior='chi2')
check_paras(out.params, self.p_true, sig=3)