Optimization (Module)
This module stores classes and functions which are used for performing mathematical optimizations, a critical component of a Lomb-Scargle-based pre-whitening routine.
models
pywhiten.optimization.models stores all the functions used for optimizations. These are listed below.
chisq(data:numpy.array, model:numpy.array, err:numpy.array)->float| Returns the chi squared of the provided model.sin_model(x:numpy.array, f:float, a:float, p:float) -> numpy.array| A sinusoidal model of the formf(x) = a*sin(2*pi*(fx+p))cos_model(x:numpy.array, f:float, a:float, p:float) -> numpy.array| A (co-)sinusoidal model of the formf(x) = a*cos(2*pi*(fx+p))-
n_sin_model(x:numpy.array, *params:float) -> numpy.array| A model consisting of n superimposed sinusoidal models fromsin_model.*paramsmust be of the form:*frequencies, *amplitudes, *phases, zero point, and therefore will be of length3n+1. -
n_cos_model(x:numpy.array, *params:float) -> numpy.array| A model consisting of n superimposed sinusoidal models fromcos_model.*paramsmust be of the form:*frequencies, *amplitudes, *phases, zero point, and therefore will be of length3n+1. -
n_model_poly(x:np.array, *params:float) -> np.array| A polynomial model of ordern.*paramsmust contain the polynomial coefficients and must of lengthn+1. The model is evaluated asf(x) = params[0] + x * params[1] + x^2 * params[2] ... x^n * params[n]. -
slf_noise(x:np.array, *params) -> np.array| The Bowman et al. (2019) SLF variability model.*paramsmust be of the form[x0, alpha_0, gamma, Cw].
class Optimizer
A class which handles chi-squared minimization of single-frequency and multi-frequency sinusoidal models.
Attributes:
-
function sf_func| A sinusoidal function taking arguments of the formatf(x:numpy.array, f:float, a:float, p:float)used for single-frequency optimizations and summed for multi-frequency optimizations (when using LMFit as the optimization engine, which is the only option at present). -
function mf_func| A function taking arguments of the format x:ndarray, pars, where pars is an arbitrarily large set of floats arranged such that it contains a group of frequency guesses followed by a group of amplitude guesses followed by a group of phase guesses followed by a guess for the zero point. The frequency, amplitude, and phase guesses must be of equal size, meaning *pars should be of length 3n+1 where n is some positive integer. Not currently used for optimization, but left in case the scipy option for minimization is re-implemented. dict cfg| A configuration dictionary.c_zp| The zero point value of the last fit performed with this object.
Methods
__init__(cfg : dict)
Constructor for this object. Requires a configuration dictionary, and sets up the fitting functions.
Args:
dict cfg| A configuration dictionary.
single_frequency_optimization(x: numpy.array, data: numpy.array, err: numpy.array, f0: float, a0: float, p0: float)
Determines optimized parameters for a fit of a sinusoidal model to an x-y dataset.
Args:
numpy.array x| x-axis of data to be fitnumpy.array data| y-axis of data to be fitnumpy.array err| y-axis weights of data to be fitfloat f0| An initial guess for the model frequencyfloat a0| An initial guess for the model amplitudefloat p0| An initial guess for the model phase
Returns:
float| The optimized model frequencyfloat| The optimized model amplitudefloat| The optimized model phasenumpy.array| The optimized single-frequency model evaluated at all values ofx
multi_frequency_optimization(x: numpy.array, data: numpy.array, err: numpy.array, freqs: list)
Determines the optimized parameters for a fit of a composite sinusoidal model to an x-y dataset, and updates parameters in place. Sets the c_zp attribute to the optimized value when the fit is complete as well.
Args:
numpy.array x| x-axis of data to be fitnumpy.array data| y-axis of data to be fitnumpy.array err| y-axis weights of data to be fitlist freqs| A list ofpywhiten.data.Frequencyobjects. The values off,a, andpfor these objects are used as the initial guesses for the fit. When the optimization is complete, theFrequencyobject parameters are updated in-place.
Returns:
numpy.array| The optimized multi-frequency model evaluated at all values ofx