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

Added optional weight vector to fit_levy #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
12 changes: 10 additions & 2 deletions levy/__init__.py
Original file line number Diff line number Diff line change
@@ -526,7 +526,8 @@ def neglog_levy(x, alpha, beta, mu, sigma):
return -np.log(np.maximum(1e-100, levy(x, alpha, beta, mu, sigma)))


def fit_levy(x, par='0', **kwargs):

def fit_levy(x, par='0', weights=None, **kwargs):
"""
Estimate parameters of Levy stable distribution given data x, using
Maximum Likelihood estimation.
@@ -554,6 +555,7 @@ def fit_levy(x, par='0', **kwargs):
:param x: values to be fitted
:type x: :class:`~numpy.ndarray`
:param par: parametrization
:param weights: optional weight vector for each data point
:type par: str
:return: a tuple with a `Parameters` object and the negative log likelihood of the data.
:rtype: tuple
@@ -565,10 +567,16 @@ def fit_levy(x, par='0', **kwargs):
parameters = Parameters(par=par, **values)
temp = Parameters(par=par, **values)

if weights is not None:
assert len(weights) == len(x), 'weight vector must be same length as data'
assert np.min(weights) >= 0, 'weights must all be nonnegative'

def neglog_density(param):
temp.x = param
alpha, beta, mu, sigma = temp.get('0')
return np.sum(neglog_levy(x, alpha, beta, mu, sigma))
if weights is None:
return np.sum(neglog_levy(x, alpha, beta, mu, sigma))
return np.sum(weights * neglog_levy(x, alpha, beta, mu, sigma))

bounds = tuple(par_bounds[i] for i in parameters.variables)
res = optimize.minimize(neglog_density, parameters.x, method='L-BFGS-B', bounds=bounds)