TPCCLIB
Loading...
Searching...
No Matches
aic.c File Reference

Routines for model selection and weighting using Akaike's information criteria. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

double aicSS (double ss, const int n, const int k)
 
int parFreeNr (const int n, double *pLower, double *pUpper)
 Calculate the number of free parameters.
 
int aicWeights (double *aic, double *w, int n)
 
double aicWeightedAvg (double *w, double *p, int n)
 
double aicModel (double *w, int n)
 

Detailed Description

Routines for model selection and weighting using Akaike's information criteria.

https://www.turkupetcentre.net/reports/tpcmod0016.pdf

Author
Kaisa Sederholm, Vesa Oikonen

Definition in file aic.c.

Function Documentation

◆ aicModel()

double aicModel ( double * w,
int n )

Calculates a value describing the relative goodness of models, based on an array of model weights.

See also
aicSS, aicWeightedAvg, aicWeights
Returns
Returns the weighted average of model number.
Parameters
wArray of weights
nLength of array

Definition at line 132 of file aic.c.

137 {
138 int i;
139 double avg;
140
141 if(n<1 || w==NULL) avg=0.0;
142 else for(i=0, avg=0.0; i<n; i++) avg+=w[i]*(double)(i+1);
143 return(avg);
144}

◆ aicSS()

double aicSS ( double ss,
const int n,
const int k )

Computation of AICc in the special case of sum-of-squares optimization from the SS, nr of fitted points and nr of fitted parameters.

If variance is different between the data points, weighted SS must be given.

See also
parFreeNr, dftWSampleNr, aicWeights, aicWeightedAvg
Returns
Returns the AIC value.
Parameters
ssSum-of-Squares of the fit
nSample size, i.e. nr of fitted data points
kNumber of fitted model parameters; AICc calculation is valid only when (n-k)>1.

Definition at line 20 of file aic.c.

27 {
28 if(!(ss>=0.0) || n<1 || k<0 || (n-k)<2) return(nan(""));
29
30 double aic=0.0, bias_adj, css;
31 int dr, dv;
32
33 dr=n-k-1; dv=2*k*(k+1);
34 if(dr>0) bias_adj=((double)dv)/((double)dr); else bias_adj=0.0;
35 if(ss<1.0e-50) css=1.0e-50; else css=ss; /* Because log(0) is an error */
36 if(n>0) aic= n*log(css/(double)n) + 2.0*(double)k + bias_adj;
37 return(aic);
38}

Referenced by imgNoiseTemplate().

◆ aicWeightedAvg()

double aicWeightedAvg ( double * w,
double * p,
int n )

Computation of the Akaike weighted model parameter average. Requires arrays of AIC weight values, and corresponding parameter values.

See also
aicSS, aicModel, aicWeights
Returns
Returns the weighted average.
Parameters
wArray of weights
pArray of parameters
nLengths of arrays

Definition at line 108 of file aic.c.

115 {
116 int i;
117 double avg;
118
119 /* Check data */
120 if(n<1 || w==NULL || p==NULL) return(1);
121 for(i=0, avg=0.0; i<n; i++) avg+=w[i]*p[i];
122 return(avg);
123}

◆ aicWeights()

int aicWeights ( double * aic,
double * w,
int n )

Computation of the Akaike weights for model averaging. Requires an array of AIC values, and an output array for weights.

See also
aicSS, aicWeightedAvg, aicModel
Returns
Returns 0, if OK.
Parameters
aicArray of AICs
wArray of weights (output)
nLengths of arrays

Definition at line 74 of file aic.c.

81 {
82 int i, mini;
83 double minaic, sume;
84
85 /* Check data */
86 if(n<1 || aic==NULL || w==NULL) return(1);
87 if(n==1) {w[0]=1.0; return(0);}
88 /* Find out which model gave the smallest AIC */
89 mini=0; for(i=1; i<n; i++) if(aic[i]<aic[mini]) mini=i;
90 minaic=aic[mini];
91 /* Compute relative weights for each model */
92 for(i=0, sume=0.0; i<n; i++) {
93 w[i]=exp(-0.5*(aic[i]-minaic));
94 sume+=w[i];
95 }
96 if(sume==0.0) return(2);
97 for(i=0; i<n; i++) w[i]/=sume;
98 return(0);
99}

◆ parFreeNr()

int parFreeNr ( const int n,
double * pLower,
double * pUpper )

Calculate the number of free parameters.

Model parameters can be fixed by setting lower and upper limit to equal values. This function simply checks the limits for each parameter.

Returns
Returns the number of free parameters.
See also
aicSS, dftWSampleNr
Parameters
nNr of parameters
pLowerLower limits (array of length n)
pUpperUpper limits (array of length n)

Definition at line 50 of file aic.c.

57 {
58 if(n<1 || pLower==NULL || pUpper==NULL) return(0);
59 int nf=0;
60 for(int i=0; i<n; i++) {
61 double range=pUpper[i]-pLower[i];
62 if(range>1.0E-10) nf++;
63 }
64 return(nf);
65}