Skip to content

Commit

Permalink
New parametrization features, and true fit in non-0 parametrizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
josemiotto committed Sep 18, 2018
1 parent f4edd9e commit ed27245
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
author = 'Jose Maria Miotto'

# The short X.Y version
version = '0.8'
version = '1.0'
# The full version, including alpha/beta/rc tags
release = '0.8'
release = '1.0'


# -- General configuration ---------------------------------------------------
Expand Down
39 changes: 21 additions & 18 deletions levy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from scipy.special import gamma
from scipy import optimize

__version__ = "0.9"
__version__ = "1.0"

# Some constants of the program.
# Dimensions: 0 - x, 1 - alpha, 2 - beta
Expand All @@ -74,7 +74,6 @@
}
default = [1.5, 0.0, 0.0, 1.0] # default values of the parameters for fit.
default = {k: {par_names[k][i]: default[i] for i in range(4)} for k in par_names.keys()}

f_bounds = [
lambda x: _reflect(x, *par_bounds[0]),
lambda x: _reflect(x, *par_bounds[1]),
Expand Down Expand Up @@ -159,6 +158,15 @@ def _interpolate(points, grid, lower, upper):
return np.reshape(result, point_shape)


def _psi(alpha):
return np.pi / 2 * (alpha - 1 - np.sign(alpha - 1))


def _phi(alpha, beta):
""" Common function. """
return beta * np.tan(np.pi * alpha / 2.0)


convert_to_par0 = {
'0': lambda x: x,
'1': lambda x: np.array([
Expand Down Expand Up @@ -209,7 +217,7 @@ def _interpolate(points, grid, lower, upper):
]),
'B': lambda x: np.array([
x[0],
np.arctan(_phi(x[0], x[1]) / ((x[0] - 1 - np.sign(x[0] - 1)) * np.pi / 2)),
np.arctan(_phi(x[0], x[1])) / _psi(x[0]),
(x[2] / (x[3] ** x[0]) - _phi(x[0], x[1])) * np.cos(np.arctan(_phi(x[0], x[1]))),
x[3] ** x[0] / np.cos(np.arctan(_phi(x[0], x[1])))
])
Expand All @@ -233,9 +241,14 @@ def convert(cls, pars, par_in, par_out):
Use to convert a parameter array from one parametrization to another.
Examples:
>>> a = np.array([1.5, 0.5, 0, 1.2])
>>> Parameters.convert(a, '1', 'B')
array([1.5 , 0.5669115 , 0.03896531, 1.46969385])
>>> a = np.array([1.6, 0.5, 0.3, 1.2])
>>> b = Parameters.convert(a, '1', 'B')
>>> b
array([1.6 , 0.55457302, 0.2460079 , 1.4243171 ])
>>> c = Parameters.convert(b, 'B', '1')
>>> c
array([1.6, 0.5, 0.3, 1.2])
>>> np.testing.assert_allclose(a, c)
:param pars: array of parameters to be converted
:type x: :class:`~numpy.ndarray`
Expand Down Expand Up @@ -268,14 +281,13 @@ def get(self, par_out=None):
Examples:
>>> p = Parameters(par='1', alpha=1.5, beta=0.5, mu=0, sigma=1.2) # to convert
>>> p.get('B') # returns the parameters in the parametrization B
array([1.5 , 0.5669115 , 0.03896531, 1.46969385])
array([1.5 , 0.59033447, 0.03896531, 1.46969385])
"""
if par_out is None:
par_out = self.par
return Parameters.convert(self._x, self.par, par_out)


def __str__(self):
txt = ', '.join(['{{0[{0}]}}: {{1[{1}]:.2f}}'.format(i, i) for i in range(4)])
txt += '. Parametrization: {2}.'
Expand All @@ -299,15 +311,6 @@ def x(self, values):
self._x[i] = f_bounds[self.par][self.pnames[i]](vals[j])


def _phi(alpha, beta):
""" Common function. """
return beta * np.tan(np.pi * alpha / 2.0)


def _psi(alpha):
return np.pi / 2 * (alpha - 1 - np.sign(alpha - 1))


def _calculate_levy(x, alpha, beta, cdf=False):
"""
Calculation of Levy stable distribution via numerical integration.
Expand Down Expand Up @@ -587,7 +590,7 @@ def random(alpha, beta, mu=0.0, sigma=1.0, shape=()):
Example:
>>> x = random(1.5, 0, shape=100) # parametrization 0 is implicit
>>> x = random(*Parameters.convert([1.5, 1.2, 0.1, 1.2] , 'B' ,'0')) # example with conversion
>>> x = random(*Parameters.convert([1.5, 0.905, 0.707, 1.414] ,'B' ,'0'), shape=100) # example with conversion
:param alpha: alpha
:type alpha: float
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

setup(name='PyLevy',
version=levy.__version__,
author='Paul Harrison',
author_email='[email protected]',
url='http://www.logarithmic.net/pfh/pylevy',
author='Paul Harrison, Jose Miotto',
url='https://github.com/josemiotto/pylevy',
license='GPL',
description='A package for calculating and fitting Levy stable distributions.',
long_description=levy.__doc__,
Expand Down

0 comments on commit ed27245

Please sign in to comment.