Chi2fit.Fit (Chi-SquaredFit v2.0.0) View Source

Implements fitting a distribution function to sample data. It minimizes the liklihood function.

Asymmetric Errors

To handle asymmetric errors the module provides three ways of determining the contribution to the likelihood function:

`simple` - value difference of the observable and model divided by the averaged error lower and upper bounds;
`asimple` - value difference of the observable and model divided by the difference between upper/lower bound and the observed
  value depending on whether the model is larger or smaller than the observed value;
`linear` - value difference of the observable and model divided by a linear tranformation (See below).

'linear': Linear transformation

Linear transformation that:

- is continuous in u=0,
- passes through the point sigma+ at u=1,
- asymptotically reaches 1-y at u->infinity
- pass through the point -sigma- at u=-1,
- asymptotically reaches -y at u->-infinity

References

[1] See https://arxiv.org/pdf/physics/0401042v1.pdf

Link to this section Summary

Types

Chi-squared statistic

Tuple holding chi-squared value, covariance matrix, parameter values, and parameter errors at the minimum chi2fit(see chi2fit/4)

Result of chi-squared probe (see &chi2probe/4)

Tuple with chi-squared, parameter values, parameter errors, and list of intermediate fit results (see chi2probe/4)

Tuple with chi-squared, parameter values, and parameter errors at the found minimum (see chi2probe/4)

Covariance matrix

Cumulative distribution mapping 'x' and parameters to a float in the range [0,1].

Tuple describing the parameter values and the distribution function.

Observation with asymmetric bounds 'y1 < y < y2'.

Observation with symmetric errors 'dy'.

List of parameter ranges

Functions

Calculates the Chi-squared function for a list of observables.

Fits observables to a known model.

Probes the chi-squared surface within a certain range of the parameters.

Partitions the data list in segments with similar chi-squared values when fitting the model

Finds the point in the data where the chi-squared has a jump when fitting the model

Link to this section Types

Specs

chi2() :: float()

Chi-squared statistic

Specs

chi2fit() :: {chi2(), cov(), parameters :: [float()], errors :: [float()]}

Tuple holding chi-squared value, covariance matrix, parameter values, and parameter errors at the minimum chi2fit(see chi2fit/4)

Specs

chi2probe() :: chi2probe_simple() | chi2probe_saved()

Result of chi-squared probe (see &chi2probe/4)

Specs

chi2probe_saved() ::
  {chi2(), [parameters :: float()], {[float()], [float()]},
   [{float(), [float()]}]}

Tuple with chi-squared, parameter values, parameter errors, and list of intermediate fit results (see chi2probe/4)

Specs

chi2probe_simple() :: {chi2(), [parameters :: float()], {[float()], [float()]}}

Tuple with chi-squared, parameter values, and parameter errors at the found minimum (see chi2probe/4)

Specs

Covariance matrix

Specs

distribution() :: (x :: float(), [parameter :: float()] -> float())

Cumulative distribution mapping 'x' and parameters to a float in the range [0,1].

Specs

model() :: {[float()], distribution()}

Tuple describing the parameter values and the distribution function.

Specs

observable() :: observable_symm() | observable_asym()

Specs

observable_asym() :: {x :: float(), y :: float(), y1 :: float(), y2 :: float()}

Observation with asymmetric bounds 'y1 < y < y2'.

Specs

observable_symm() :: {x :: float(), y :: float(), dy :: float()}

Observation with symmetric errors 'dy'.

Specs

observables() :: [observable()]

Specs

params() :: [{float(), float()}]

List of parameter ranges

Link to this section Functions

Link to this function

chi2(observables, fun, penalties \\ fn _ -> 0.0 end, options \\ [])

View Source

Specs

chi2(observables(), (float() -> float()), (float() -> float()), Keyword.t()) ::
  float()

Calculates the Chi-squared function for a list of observables.

The observables are given as a list. Each observation has an error associated with it. The errors can be either symmetric or asymmetric.

A 'penalties'-function is used to assign penalties and these contribute to the chi-squared function. It may be used to 'forbid' certain parameter, x combinations.

Options

`model` - Required. Determines the contribution to chi-squared taking the asymmetric errors into account.
        Vaid values are `:linear`, `:simple`, and `:asimple`. See Errors below

Errors

`simple` - Use for asymmetric errors when the sigma+ and sigma- are close to each other
`asimple` - Use for asymmetric errors when y-values are not bound.
`linear` - Use this model when the y-values ar bound between 0 and 1. Linear transformation that:
    - is continuous in u=0,
    - passes through the point sigma+ at u=1,
    - asymptotically reaches 1-y at u->infinity
    - pass through the point -sigma- at u=-1,
    - asymptotically reaches -y at u->-infinity

Examples

iex> fun = &(&1)
...> chi2 [{0,3,1}], fun
9.0

iex> fun = &(&1)
...> chi2 [{0,3,1},{1,7,1},{2,3,1}], fun
46.0

iex> fun = &(&1)
...> chi2 [{0,3,3},{1,7,1},{2,3,1}], fun
38.0

iex> fun = &(&1-2)
...> chi2 [{0,3,1}], fun
25.0

end

Link to this function

chi2fit(observables, model, max \\ 100, options \\ [])

View Source

Specs

chi2fit(
  observables(),
  model(),
  iterations :: pos_integer(),
  options :: Keyword.t()
) :: chi2fit()

Fits observables to a known model.

Returns the found minimum chi-squared value, covariance matrix, gradient at the minimum, and the corresponding parameter values including error estimates. For a good fit check the following:

`chi2 per degree of freedom` - this should be about 1 or less,
`gradient` - at the minimum the gradient should be zero at all directions.

For asymmetric errors use the option model equal to linear. Rough chi-squared surfaces or if numerically unstable, use the option smoothing set to true.

Arguments

`observables` - list of measurements including errors,
`model` - `{parameters, fun}`: set of initial parameter values and a function to fit against the measurements

Options

`onstep` - call back function; it is called with a map with keys `delta`, `chi2`, and `params`,
`smoothing` - boolean value indicating whether the chi-squared is smoothened using a Gauss distribution. This
is used in case the surface is rough because of numerical instabilities to smoothen the surface,
`model` - The same values as in `chi2/3` and `chi2/4`
`grid?` - Performs a grid search: per step, tries to fit only one parameter and keeps the others fixed; selects the parameter in
a round-robin fashion
`probes` -- a list of tuples containing the result of the `chi2probe/4` function. Each tuple contains the chi2 value and parameter list.
Defaults to the empty list.
Link to this function

chi2probe(observables, parranges, fun_penalties, options)

View Source

Specs

chi2probe(observables(), [float()], (... -> any()), Keyword.t()) :: chi2probe()

Probes the chi-squared surface within a certain range of the parameters.

It does so by randomly selecting parameter value combinations and calculate the chi-squared for the list of observations based on the selected parameter values. This routine is used to roughly probe the chi-squared surface and perform more detailed and expensive calculations to precisely determine the minimum by chi2fit/4.

Returns the minimum chi-squared found, the parameter values, and all probes that resulted in chi-squared difference less than 1 with the minimum. The parameter values found in this set correspond with the errors in determining the parameters.

Options

`num` or `probes` - the number of points to calculate,
`mark` - progress indicator: a keyword list with keys `m`, `c`, `x`, and `*`; the value must be a call back
function taking zero arguments. These are called when 1000, 100, 10, probes have been done. The value of
key `*` is called when a new chi-squared minimum has been found,
`smoothing` - boolean value indicating whether the chi-squared is smoothened using a Gauss distribution. This
is used in case the surface is rough because of numerical instabilities to smoothen the surface,
`model` - See `chi2/3` and `chi2/4`

Specs

find_all(nil | [number()], options :: Keyword.t()) :: [[float()]]

Partitions the data list in segments with similar chi-squared values when fitting the model

Link to this function

find_change(list, options)

View Source

Specs

find_change(list :: [number()], options :: Keyword.t()) :: [
  {chi :: float(), [float()]}
]

Finds the point in the data where the chi-squared has a jump when fitting the model