Fitting Functions¶
Functions¶
-
fitting_functions.log_power_law(x, a, alpha)¶ Log power law dependence of MSD curve (for curve fitting)
Parameters: - x – independent variable values
- a – scaling coefficient
- alpha – power (< 1: subdiffusive, 1: brownian, >1: superdiffusive)
:return function evaluated at x values
-
fitting_functions.power_law(x, a, alpha)¶ Power law dependence of MSD curve
Parameters: - x – independent variable values
- a – scaling coefficient
- alpha – power (< 1: subdiffusive, 1: brownian, >1: superdiffusive)
:return function evaluated at x values
-
fitting_functions.cdf_exp(left, right, scale)¶ Calculate area under expnonential curve between two x locations
-
fitting_functions.exponential_integrated(edges, A, B)¶ Fit bins to exponential function based on integrated area under bins. The form of the exponential is: A*B*e^-Bx
Parameters: - edges (np.ndarray) – edges of bins
- A (float) – exponential function parameter. Controls scale of exponential function
- B (float) – exponential function parameter. Controls rate of decay
:return integrated area between bin edges and under exponential curve as a function of x
-
fitting_functions.cdf_power_law(left, right, scale, alpha)¶ Calculate area under power law curve between two x locations by integrating scale*e^-alpha from left to right.
Scipy does not have tabulated data for power laws with alpha < 1, so I wrote my own for that case
Parameters: - left (float) – left bound of integral
- right (float) – right bound of integral
- scale (float) – parameter of power law function that controls its scale
- alpha (float) – exponent of power law
Returns: integrated area between left and right
Return type: float
-
fitting_functions.powerlaw_integrated(edges, alpha, A)¶ Fit bins to powerlaw function of form: At^-alpha
Parameters: - edges (np.ndarray) – edges of bins to which power law will be fit
- alpha (float) – exponent of power law
- A (float) – parameter of power law function that controls its scale
Returns: integrated area between bin edges and under power law curve as function of x
-
fitting_functions.fit_power_law(x, y, cut=1, interactive=True)¶ Fit power law to MSD curves TODO: weighted fit (need to do error analysis first)
Parameters: - y (np.ndarray) – y-axis values of MSD curve (x-axis values are values from self.time_uniform
- cut (float) – fraction of trajectory to include in fit
Returns: Coefficient and exponent in power law of form [coefficient, power]
-
fitting_functions.line(m, x, b)¶ Return y values of a line given points, a slope and an intercept
y = m*x + b
Parameters: - m (float) – slope
- x (point or array of points) – points
- b (float) – y-intercept
Returns: y or array of y-values
Return type: np.ndarray()
-
fitting_functions.zeta(x, alpha)¶ Numerical estimate of Hurwitz zeta function. Used as discrete power law distribution normalization constant
sum from n=0 to infinity of (n + x)^-lpha
Parameters: - x (float) – point at which to evaluate function
- alpha (float) – exponent of power law
- upper_limit (int) – number of terms used to calculate Hurwitz zeta function (highest value of n above)
Returns: evaluation of Hurwitz zeta function at x
Return type: float
-
fitting_functions.power_law_discrete_log_likelihood(alpha, x, xmin, minimize=False)¶ Calculate log likelihood for alpha given a set of x values that might come from a power law distribution
Parameters: alpha – power law exponent. Calculates the log-likelihood of this value of alpha for the data :param x: array of values making up emperical distribution :param xmin: lower bound of power law distribution :param upper_limit: number of terms used to calculate zeta function
:return log-likelihood of input parameters :rtype float
-
fitting_functions.gaussian_log_likelihood(parameters, data, maximize=False)¶ Calculate log-likelihood given parameters and data
Parameters: - parameters – a tuple of form (mean, sigma)
- data – data that might be gaussian
- maximize – if this is true, the opposite sign of the log-likelihood is returned so it can be used in a
minimization function (as a way to calculate the maximum)
Returns: log-likelihood
-
fitting_functions.hurst_autocovariance(K, H)¶ Return the analytical autocovariance of fractional gaussian noise for a given hurst exponent as a function of step number, k
\[\gamma(k) = \dfrac{1}{2}[|k-1|^{2H} - 2|k|^{2H} + |k + 1|^{2H}]\]Parameters: - K – values of k at which to evaluate autocovariance (only integer values make sense)
- H – hurst exponent
Returns: analytical autocovariance function for fractional gaussian noise
-
fitting_functions.powerlaw_cutoff_mle(x, xmin=1.0, guess=(1.5, 0.1))¶ Given data, determine the maximum likelihood paramters, \(\alpha\) and \(\lambda\) for a power law with an exponential cutoff. Based on this answer to a mathoverflow question.
The PDF for a power law with an exponential cut-off is:
\[P(x; \alpha , \lambda , x_{min}) = \frac{\lambda^{1-\alpha}}{\Gamma (1-\alpha , \lambda x_{min})} x^{-\alpha}e^{-\lambda x}\]There is no closed form formula for the MLE parameters, so we maximize the log likelihood:
\[\mathcal{L} = n*(1 - \alpha)ln\lambda - n*ln\Gamma(1 - \alpha, x_{min}\lambda) - \alpha\sum_{i=1}^{n}ln x_i - \lambda\sum_{i=1}^n x_i\]Parameters: - x (numpy.ndarray) – vector of data points
- xmin (float) – minimum x-value where power law behavior is observed
- guess (tuple of floats) – initial guess at paramters (\(\alpha\), \(\lambda\))
Returns: MLE values of \(\alpha\) and \(\lambda\)