Skip to content

Commit

Permalink
Provide wrapper for calculating chi squared prob
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondEhlers committed Aug 19, 2019
1 parent 1fedadc commit 2277444
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pachyderm/fit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
]

from .base import ( # noqa: F401
BaseFitResult, FitFailed, FitResult, FuncCode, calculate_function_errors, fit_with_minuit
BaseFitResult, FitFailed, FitResult, FuncCode, calculate_function_errors, chi_squared_probability, fit_with_minuit
)
from .cost_function import ( # noqa: F401
BinnedChiSquared, BinnedLogLikelihood, ChiSquared, CostFunctionBase, LogLikelihood, SimultaneousFit
Expand Down
16 changes: 15 additions & 1 deletion pachyderm/fit/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import logging
import time
from dataclasses import dataclass
from typing import TYPE_CHECKING, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Type, TypeVar, Union
from typing import TYPE_CHECKING, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Type, TypeVar, Union, cast

import iminuit
import numdifftools as nd
Expand Down Expand Up @@ -455,3 +455,17 @@ def call_list_of_callables(functions: Iterable[Callable[..., float]], argument_p
#logger.debug(f"describe args: {iminuit.util.describe(func)}")
value += func(*function_args)
return value

def chi_squared_probability(chi_2: float, ndf: float) -> float:
""" Calculate the probability that the
This is just a thin wrapped around ``scipy.stats``, but it's convenient.
Args:
chi_2: Chi squared value.
ndf: Number of degrees of freedom.
Returns:
Probability that the fit is consistent with the data.
"""
import scipy.stats
return cast(float, 1 - scipy.stats.chi2.cdf(chi_2, ndf))

0 comments on commit 2277444

Please sign in to comment.