Skip to content

Commit ed27245

Browse files
committed
New parametrization features, and true fit in non-0 parametrizations.
1 parent f4edd9e commit ed27245

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
author = 'Jose Maria Miotto'
2525

2626
# The short X.Y version
27-
version = '0.8'
27+
version = '1.0'
2828
# The full version, including alpha/beta/rc tags
29-
release = '0.8'
29+
release = '1.0'
3030

3131

3232
# -- General configuration ---------------------------------------------------

levy/__init__.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
from scipy.special import gamma
5757
from scipy import optimize
5858

59-
__version__ = "0.9"
59+
__version__ = "1.0"
6060

6161
# Some constants of the program.
6262
# Dimensions: 0 - x, 1 - alpha, 2 - beta
@@ -74,7 +74,6 @@
7474
}
7575
default = [1.5, 0.0, 0.0, 1.0] # default values of the parameters for fit.
7676
default = {k: {par_names[k][i]: default[i] for i in range(4)} for k in par_names.keys()}
77-
7877
f_bounds = [
7978
lambda x: _reflect(x, *par_bounds[0]),
8079
lambda x: _reflect(x, *par_bounds[1]),
@@ -159,6 +158,15 @@ def _interpolate(points, grid, lower, upper):
159158
return np.reshape(result, point_shape)
160159

161160

161+
def _psi(alpha):
162+
return np.pi / 2 * (alpha - 1 - np.sign(alpha - 1))
163+
164+
165+
def _phi(alpha, beta):
166+
""" Common function. """
167+
return beta * np.tan(np.pi * alpha / 2.0)
168+
169+
162170
convert_to_par0 = {
163171
'0': lambda x: x,
164172
'1': lambda x: np.array([
@@ -209,7 +217,7 @@ def _interpolate(points, grid, lower, upper):
209217
]),
210218
'B': lambda x: np.array([
211219
x[0],
212-
np.arctan(_phi(x[0], x[1]) / ((x[0] - 1 - np.sign(x[0] - 1)) * np.pi / 2)),
220+
np.arctan(_phi(x[0], x[1])) / _psi(x[0]),
213221
(x[2] / (x[3] ** x[0]) - _phi(x[0], x[1])) * np.cos(np.arctan(_phi(x[0], x[1]))),
214222
x[3] ** x[0] / np.cos(np.arctan(_phi(x[0], x[1])))
215223
])
@@ -233,9 +241,14 @@ def convert(cls, pars, par_in, par_out):
233241
Use to convert a parameter array from one parametrization to another.
234242
235243
Examples:
236-
>>> a = np.array([1.5, 0.5, 0, 1.2])
237-
>>> Parameters.convert(a, '1', 'B')
238-
array([1.5 , 0.5669115 , 0.03896531, 1.46969385])
244+
>>> a = np.array([1.6, 0.5, 0.3, 1.2])
245+
>>> b = Parameters.convert(a, '1', 'B')
246+
>>> b
247+
array([1.6 , 0.55457302, 0.2460079 , 1.4243171 ])
248+
>>> c = Parameters.convert(b, 'B', '1')
249+
>>> c
250+
array([1.6, 0.5, 0.3, 1.2])
251+
>>> np.testing.assert_allclose(a, c)
239252
240253
:param pars: array of parameters to be converted
241254
:type x: :class:`~numpy.ndarray`
@@ -268,14 +281,13 @@ def get(self, par_out=None):
268281
Examples:
269282
>>> p = Parameters(par='1', alpha=1.5, beta=0.5, mu=0, sigma=1.2) # to convert
270283
>>> p.get('B') # returns the parameters in the parametrization B
271-
array([1.5 , 0.5669115 , 0.03896531, 1.46969385])
284+
array([1.5 , 0.59033447, 0.03896531, 1.46969385])
272285
273286
"""
274287
if par_out is None:
275288
par_out = self.par
276289
return Parameters.convert(self._x, self.par, par_out)
277290

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

301313

302-
def _phi(alpha, beta):
303-
""" Common function. """
304-
return beta * np.tan(np.pi * alpha / 2.0)
305-
306-
307-
def _psi(alpha):
308-
return np.pi / 2 * (alpha - 1 - np.sign(alpha - 1))
309-
310-
311314
def _calculate_levy(x, alpha, beta, cdf=False):
312315
"""
313316
Calculation of Levy stable distribution via numerical integration.
@@ -587,7 +590,7 @@ def random(alpha, beta, mu=0.0, sigma=1.0, shape=()):
587590
588591
Example:
589592
>>> x = random(1.5, 0, shape=100) # parametrization 0 is implicit
590-
>>> x = random(*Parameters.convert([1.5, 1.2, 0.1, 1.2] , 'B' ,'0')) # example with conversion
593+
>>> x = random(*Parameters.convert([1.5, 0.905, 0.707, 1.414] ,'B' ,'0'), shape=100) # example with conversion
591594
592595
:param alpha: alpha
593596
:type alpha: float

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

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

0 commit comments

Comments
 (0)