Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Steps with neg sigma #970

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lmfit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,8 +1525,9 @@ class StepModel(Model):
https://en.wikipedia.org/wiki/Logistic_function)

The step function starts with a value 0 and ends with a value of
:math:`A` rising to :math:`A/2` at :math:`\mu`, with :math:`\sigma`
setting the characteristic width. The functional forms are defined as:
:math:`\tt{sign}(\sigma)A` rising or falling to :math:`A/2` at :math:`\mu`,
with :math:`\sigma` setting the characteristic width and the sign up the step.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"sign up" is that what you meant?

The functional forms are defined as:

.. math::
:nowrap:
Expand All @@ -1540,6 +1541,8 @@ class StepModel(Model):

where :math:`\alpha = (x - \mu)/{\sigma}`.

Note that :math:`\sigma \gt 0` gives a rising step, while :math:`\sigma \lt 0` gives
a falling step.
"""

valid_forms = ('linear', 'atan', 'arctan', 'erf', 'logistic')
Expand All @@ -1556,7 +1559,11 @@ def guess(self, data, x, **kwargs):
xmin, xmax = min(x), max(x)
pars = self.make_params(amplitude=(ymax-ymin),
center=(xmax+xmin)/2.0)
pars[f'{self.prefix}sigma'].set(value=(xmax-xmin)/7.0, min=0.0)
n = len(data)
sigma = 0.1*(xmax - xmin)
if data[:n//5].mean() > data[-n//5:].mean():
sigma = -sigma
pars[f'{self.prefix}sigma'].set(value=sigma)
return update_param_vals(pars, self.prefix, **kwargs)

__init__.__doc__ = COMMON_INIT_DOC
Expand Down Expand Up @@ -1600,6 +1607,8 @@ class RectangleModel(Model):
where :math:`\alpha_1 = (x - \mu_1)/{\sigma_1}` and
:math:`\alpha_2 = -(x - \mu_2)/{\sigma_2}`.

Note that, unlike a StepModel, :math:`\sigma_1 \gt 0` is enforced, giving a
rising initial step, and :math:`\sigma_2 \gt 0` gives a falling final step.
"""

valid_forms = ('linear', 'atan', 'arctan', 'erf', 'logistic')
Expand All @@ -1624,9 +1633,10 @@ def guess(self, data, x, **kwargs):
xmin, xmax = min(x), max(x)
pars = self.make_params(amplitude=(ymax-ymin),
center1=(xmax+xmin)/4.0,
center2=3*(xmax+xmin)/4.0)
pars[f'{self.prefix}sigma1'].set(value=(xmax-xmin)/7.0, min=0.0)
pars[f'{self.prefix}sigma2'].set(value=(xmax-xmin)/7.0, min=0.0)
center2=3*(xmax+xmin)/4.0,
sigma1={'value': (xmax-xmin)/10.0, 'min': 0},
sigma2={'value': (xmax-xmin)/10.0, 'min': 0})

return update_param_vals(pars, self.prefix, **kwargs)

__init__.__doc__ = COMMON_INIT_DOC
Expand Down