lamatrix.models.simple#

Generator objects for simple types of models.

Classes

Constant([priors, posteriors])

lamatrix.Model object to model constants or offsets.

Fixed([x_name, width, priors, posteriors])

A model that has fixed input vectors

Polynomial([x_name, order, priors, posteriors])

lamatrix.Model object to model polynomial trends.

Sinusoid([x_name, nterms, priors, posteriors])

lamatrix.Model object to model sinusoidal trends.

Step([x_name, breakpoints, priors, posteriors])

dPolynomial(weights[, x_name, order, ...])

lamatrix.Model object for capturing derivatives of polynomial models.

dSinusoid(weights[, x_name, nterms, priors, ...])

lamatrix.Model object to model the gradient of sinusoid trends.

class lamatrix.models.simple.Constant(priors=None, posteriors=None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

lamatrix.Model object to model constants or offsets. This model has no input variable.

Initialize a Constant model.

Note this model has no variable.

Parameters:
  • priors (optional) – Prior distributions for model weights (default is None, i.e. no priors).

  • posteriors (optional) – Posterior distributions for model parameters (default is None, i.e. no posteriors). Posterior keyword is provided so that models can be loaded.

Examples

Create a constant model

>>> model = Constant()
property arg_names#

Argument names that must be input into the model. This is an empty dictionary for a constant model.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build a design matrix consisting of a single column of ones.

You must pass a keyword argument to this design matrix to detect the required shape.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Number of vectors required to be input to create the model. This is zero for a constant model.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_latex()#
property width#

Width of the model. This is always one for a constant model.

class lamatrix.models.simple.Fixed(x_name: str = 'x', width: int = 1, priors=None, posteriors=None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

A model that has fixed input vectors

A model with fixed vector inputs

property arg_names#

Returns a set of the user defined strings for all the arguments that the design matrix requires.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build a 1D polynomial in x_name.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Returns the number of vectors required to build the object.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_latex()#
property width#

Returns the width of the design matrix once built.

class lamatrix.models.simple.Polynomial(x_name: str = 'x', order: int = 3, priors: DistributionsContainer | None = None, posteriors: DistributionsContainer | None = None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

lamatrix.Model object to model polynomial trends.

Initialize a Polynomial model.

Note that this model does not include a constant term. You can add one using the “Constant” model.

Parameters:
  • x_name (str, optional) – The name of the independent variable (default is “x”).

  • order (int, optional) – The order of the polynomial. Must be at least 1 (default is 3).

  • priors (optional) – Prior distributions for model weights (default is None, i.e. no priors).

  • posteriors (optional) – Posterior distributions for model parameters (default is None, i.e. no posteriors). Posterior keyword is provided so that models can be loaded.

Raises:

Examples

Create a cubic polynomial model:

>>> model = Polynomial(x_name="t", order=3)
property arg_names#

Argument names that must be input into the model.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build a 1D polynomial in self.x_name.

You must pass the keyword argument self.x_name to use this function.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Number of vectors required to be input to create the model.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_gradient(weights: ndarray[Any, dtype[_ScalarType_co]] | None = None, priors: DistributionsContainer | None = None)[source]#

Converts this model to the gradient of that model, assigning the posteriors of the fit as weights.

If the posteriors are None, will assign the priors as weights.

Parameters:
  • weights (np.ndarray) – The weights applied to the Polynomial model. These update the commponents for the design matrix of the gradient.

  • priors (DistributionsContainer) – Priors to apply to the gradient model.

Returns:

model – A model for the gradient of a polynomial

Return type:

lamatrix.models.simple.dPolynomial

Example

To obtain the gradient of a fit polynomial model

>>> model = Polynomial(x_name="t", order=3)
>>> model.fit(t=t, data=data, errors=errors)
>>> dmodel = model.to_gradient()
to_latex()#
property width#

Width of the model. This is equivalent to the order.

class lamatrix.models.simple.Sinusoid(x_name: str = 'x', nterms: int = 1, priors=None, posteriors=None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

lamatrix.Model object to model sinusoidal trends.

Initialize a Sinusoid model.

Parameters:
  • x_name (str, optional) – The name of the independent variable (default is “x”).

  • nterms (int, optional) – The number of terms in the sinusoid.

  • priors (optional) – Prior distributions for model weights (default is None, i.e. no priors).

  • posteriors (optional) – Posterior distributions for model parameters (default is None, i.e. no posteriors). Posterior keyword is provided so that models can be loaded.

Raises:

Examples

Create a sinusoid model with nterms=3:

>>> model = Sinusoid(x_name="phi", nterms=3)
property arg_names#

Argument names that must be input into the model.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build a 1D Sinusoid in self.x_name.

You must pass the keyword argument self.x_name to use this function.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Number of vectors required to be input to create the model.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_gradient(weights=None, priors=None)[source]#

Converts this model to the gradient of that model, assigning the posteriors of the fit as weights.

If the posteriors are None, will assign the priors as weights.

Parameters:
  • weights (np.ndarray) – The weights applied to the Sinusoid model. These update the commponents for the design matrix of the gradient.

  • priors (DistributionsContainer) – Priors to apply to the gradient model.

Returns:

model – A model for the gradient of a sinusoid

Return type:

lamatrix.models.simple.dSinusoid

Example

To obtain the gradient of a fit sinusoid model

>>> model = Sinusoid(x_name="phi", nterms=3)
>>> model.fit(phi=phi, data=data, errors=errors)
>>> dmodel = model.to_gradient()
to_latex()#
property width#

Width of the model. This is equivalent to the nterms * 2 for Sinusoid models.

class lamatrix.models.simple.Step(x_name: str = 'x', breakpoints: list[float] = [0], priors=None, posteriors=None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

property arg_names#

Returns a set of the user defined strings for all the arguments that the design matrix requires.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build a 1D polynomial in x_name.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Returns the number of vectors required to build the object.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_latex()#
property width#

Returns the width of the design matrix once built.

class lamatrix.models.simple.dPolynomial(weights: ndarray[Any, dtype[_ScalarType_co]], x_name: str = 'x', order: int = 3, priors=None, posteriors=None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

lamatrix.Model object for capturing derivatives of polynomial models.

In this case, this is a special variant on a Polynomial model.

Initialize a dPolynomial model.

Note that this model does not include a constant term. You can add one using the “Constant” model.

Parameters:
  • weights (npt.NDArray,) – The input weights to be applied to the input model.

  • x_name (str, optional) – The name of the independent variable (default is “x”).

  • order (int, optional) – The order of the polynomial. Must be at least 1 (default is 3).

  • priors (optional) – Prior distributions for model weights (default is None, i.e. no priors).

  • posteriors (optional) – Posterior distributions for model parameters (default is None, i.e. no posteriors). Posterior keyword is provided so that models can be loaded.

Raises:
property arg_names#

Argument names that must be input into the model.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build the derivative of a 1D Polynomial in self.x_name.

You must pass the keyword argument self.x_name to use this function.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Number of vectors required to be input to create the model.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_latex()#
property width#

Width of the model. This is 1 for 1D gradients.

class lamatrix.models.simple.dSinusoid(weights: List, x_name: str = 'x', nterms: int = 1, priors=None, posteriors=None)[source]#

Bases: MathMixins, LatexMixins, IOMixins, Model

lamatrix.Model object to model the gradient of sinusoid trends.

Initialize a dSinusoid model.

Parameters:
  • weights (npt.NDArray,) – The input weights to be applied to the input model.

  • x_name (str, optional) – The name of the independent variable (default is “x”).

  • nterms (int, optional) – The number of terms in the sinusoid.

  • priors (optional) – Prior distributions for model weights (default is None, i.e. no priors).

  • posteriors (optional) – Posterior distributions for model parameters (default is None, i.e. no posteriors). Posterior keyword is provided so that models can be loaded.

Raises:
property arg_names#

Argument names that must be input into the model.

copy()#

Returns a deep copy of self.

design_matrix(**kwargs)[source]#

Build a gradient of a 1D sinusoid in self.x_name.

You must pass the keyword argument self.x_name to use this function.

Returns:

X – Design matrix with shape (len(x), self.nvectors)

Return type:

np.ndarray

property equation#

Provides the equation for the model in latex.

If accessed within a jupyter instance will return the equation in displayed latex, otherwise will return the equation in raw latex.

evaluate(**kwargs)#

Given an input set of arguments, will evaluate the model with the current best fit weights.

fit(data: ndarray[Any, dtype[_ScalarType_co]], errors: ndarray[Any, dtype[_ScalarType_co]] | None = None, mask: ndarray[Any, dtype[_ScalarType_co]] | None = None, **kwargs)#

Fit the design matrix of this model object.

Executing this function will update the posteriors argument to the best fit posteriors.

Parameters:
  • data (np.ndarray) – Input data to fit

  • errors (np.ndarray, optional) – Errors on the input data

  • mask (np.ndarray, optional) – Mask to apply when fitting. Values where mask is False will not be used during the fit.

property nvectors#

Number of vectors required to be input to create the model.

sample(**kwargs)#

Given an input set of arguments, will evaluate the model with a sample of the best fit weights drawn from the posteriors.

save(filename: str)#
to_latex()#
property width#

Width of the model. This is 1 for 1D gradients.