irfpy.util.bipower

A module for functions of two component power law.

Code author: Yoshifumi Futaana

The function form is

\[\begin{split}J(E) = \left\{ \begin{array}{ll} k_0 E^{\gamma_0} & (0<E<E_0)\\ k_1 E^{\gamma_1} & (E>E_0)\\ \end{array} \right.\end{split}\]

where

\[E_0 = \left(\frac{k_1}{k_0}\right)^{\frac{1}{\gamma_0-\gamma_1}}\]

The parameter should satisfy

\[\begin{split}k_0 > 0 \\ k_1 > 0 \\ \gamma_1 < 0\end{split}\]
>>> cls = BiPower(100, -0.5, 1000, -1)
>>> func = cls.mkfunc()
>>> func(1)
100.0
>>> func(10) == 100 / math.sqrt(10)
True
>>> func(100)
10.0
>>> func(1000)
1.0
class irfpy.util.bipower.BiPower(k0, r0, k1, r1)[source]

Bases: object

mkfunc()[source]

Return the function: J(E)

get_knee()[source]

Return the knee (E0).

Return the knee point. For example,

>>> k0 = 1000
>>> r0 = -1
>>> k1 = 10000
>>> r1 = -2

will make the knee at 10.

>>> bip = BiPower(k0, r0, k1, r1)
>>> print('%.1f' % bip.get_knee())
10.0
getRandom(n=10000)[source]
integral(x0, x1)[source]

Return the integral over [x0, x1].

Returned is a tuple, with the first element holding the estimated value of the integral and the second element holding an upper bound on the error. This method just calls scipy.integrate.quad. Use scipy.integrate.Inf and -scipy.integrate.Inf for infinity.

irfpy.util.bipower.mkfunc(k0, r0, k1, r1)[source]
irfpy.util.bipower.mklnfunc(log10k0, r0, log10k1, r1)[source]

Creating a function from with parameters in log space.

Parameter is log10(k0), r0, log10(k1) and r1. Input/Output for the generated functions is in linear space.

A bipwer function, f0, is made with linear parameters.

>>> f0 = mkfunc(100, -1, 1000, -2)
>>> y0 = np.array([f0(x) for x in (0.1, 1, 10, 100)])

A bipwer function, f1, is made with log parameres. Parameter k0 (=100) and k1 (=1000) is in log space, i.e. 2 and 3, respectively.

>>> f1 = mklnfunc(2, -1, 3, -2)

Note that the generated function, f1, will take the argument (x) in the linear space, thus, the argument should be the same for f0 and f1.

>>> y1 = np.array([f1(x) for x in (0.1, 1, 10, 100)])

f0 and f1 should be the same.

>>> (y0 == y1).all()
True
irfpy.util.bipower.get_knee(k0, r0, k1, r1)[source]

Return the knee (E0).

See also BiPower.get_knee().

>>> print('%.1f' % get_knee(1000, -1, 10000, -2))
10.0
irfpy.util.bipower.integral(k0, r0, k1, r1, x0, x1)[source]

Return the integral over [x0, x1].

See also BiPower.integral().

irfpy.util.bipower.getRandom(E0, r0, E1, r1, n=10000)[source]
irfpy.util.bipower.fitting(x_array, y_array, initprm=None)[source]

Return fitted parameters of k0, k1, r0, and r1.

As a sample, we try to fit the data that follows exactly the bi-linear.

\[\begin{split}f(x) = 1000 \times x^{-1} ~~~\mathrm{for}~x < 10 \\ f(x) = 10000 \times x^{-2} ~~~\mathrm{for}~x >= 10\end{split}\]
>>> x_array = [0.1, 1, 10, 100, 1000]
>>> y_array = [10000, 1000, 100, 1, 0.01]
>>> prm = fitting(x_array, y_array, initprm=(3, -1, 4, -2))
>>> prm = fitting(x_array, y_array)
Parameters:
  • x_array (numpy.array type. Not MaskedArray. You have to use compressed() method to make new array from MaskedArray.) – Array of x.

  • y_array (numpy.array type. Not MaskedArray. You have to use compressed() method to make new array from MaskedArray.) – Array of y.

  • initprm – You can specify the initial values for iteration. The parameter is passed to scipy.optimize.leastsq function. initprm should be a tuple of (log10k0, r0, log10k1, r1) or None.

Returns:

(k0, r0, k1, r1), success_code

irfpy.util.bipower.doctests()[source]