Skip to content

numpydoc build for aic.py #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 77 additions & 10 deletions src/diffpy/srmise/modelevaluators/aic.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ def evaluate(self, fit, count_fixed=False, kshift=0):
"""Return quality of fit for given ModelCluster using AIC (Akaike's Information Criterion).

Parameters
fit: A ModelCluster
count_fixed: Whether fixed parameters are considered.
kshift: (0) Treat the model has having this many additional
parameters. Negative values also allowed."""
----------
fit : ModelCluster instance
The ModelCluster instance to evaluate.
count_fixed : bool
Whether fixed parameters are considered. Default is False.
kshift : int
Treat the model has having this many additional
parameters. Negative values also allowed. Default is 0.

Returns
-------
quality : float
The quality of fit for given ModelCluster."""
# Number of parameters. By default, fixed parameters are ignored.
k = fit.model.npars(count_fixed=count_fixed) + kshift
if k < 0:
Expand All @@ -79,12 +88,34 @@ def evaluate(self, fit, count_fixed=False, kshift=0):
return self.stat

def minpoints(self, npars):
"""Calculates the minimum number of points required to make an estimate of a model's quality."""
"""Calculates the minimum number of points required to make an estimate of a model's quality.

Parameters
----------
npars : int
The number of parameters in the model.

Returns
-------
int
The minimum number of points required to make an estimate of a model's quality.
"""

return 1

def parpenalty(self, k, n):
"""Returns the cost for adding k parameters to the current model cluster."""
def parpenalty(self, k):
"""Returns the cost for adding k parameters to the current model cluster.

Parameters
----------
k : int
The number of added parameters in the model.

Returns
-------
float
The penalty cost for adding k parameters to the current model cluster.
"""

# Weight the penalty for additional parameters.
# If this isn't 1 there had better be a good reason.
Expand All @@ -94,11 +125,26 @@ def parpenalty(self, k, n):

def growth_justified(self, fit, k_prime):
"""Returns whether adding k_prime parameters to the given model (ModelCluster) is justified
given the current quality of the fit. The assumption is that adding k_prime parameters will
given the current quality of the fit.

The assumption is that adding k_prime parameters will
result in "effectively 0" chiSquared cost, and so adding it is justified if the cost of adding
these parameters is less than the current chiSquared cost.
The validity of this assumption (which depends on an unknown chiSquared value)
and the impact of the errors used should be examined more thoroughly in the future.

Parameters
----------
fit : ModelCluster instance
The ModelCluster instance to evaluate.

k_prime : int
The prime number of added parameters in the model.

Returns
-------
bool
Whether adding k_prime parameters to the given model is justified.
"""

if self.chisq is None:
Expand All @@ -124,15 +170,36 @@ def growth_justified(self, fit, k_prime):

@staticmethod
def akaikeweights(aics):
"""Return sequence of Akaike weights for sequence of AICs"""
"""Return sequence of Akaike weights for sequence of AICs

Parameters
----------
aics : array-like
The sequence of AIC instance.

Returns
-------
array-like
The sequence of Akaike weights
"""

aic_stats = np.array([aic.stat for aic in aics])
aic_min = min(aic_stats)
return np.exp(-(aic_stats - aic_min) / 2.0)

@staticmethod
def akaikeprobs(aics):
"""Return sequence of Akaike probabilities for sequence of AICs"""
"""Return sequence of Akaike probabilities for sequence of AICs

Parameters
----------
aics : array-like
The sequence of AIC instance.

Returns
-------
array-like
The sequence of Akaike probabilities"""
aic_weights = AIC.akaikeweights(aics)
return aic_weights / np.sum(aic_weights)

Expand Down
Loading