TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcmodel
aic.c
Go to the documentation of this file.
1
8
/****************************************************************************/
9
#include "
libtpcmodel.h
"
10
/****************************************************************************/
11
12
/****************************************************************************/
20
double
aicSS
(
22
double
ss,
24
const
int
n,
26
const
int
k
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
}
39
/****************************************************************************/
40
41
/****************************************************************************/
50
int
parFreeNr
(
52
const
int
n,
54
double
*pLower,
56
double
*pUpper
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
}
66
/*****************************************************************************/
67
68
/*****************************************************************************/
74
int
aicWeights
(
76
double
*aic,
78
double
*w,
80
int
n
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
}
100
/****************************************************************************/
101
102
/****************************************************************************/
108
double
aicWeightedAvg
(
110
double
*w,
112
double
*p,
114
int
n
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
}
124
/****************************************************************************/
125
126
/****************************************************************************/
132
double
aicModel
(
134
double
*w,
136
int
n
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
}
145
/****************************************************************************/
146
147
/****************************************************************************/
148
aicWeightedAvg
double aicWeightedAvg(double *w, double *p, int n)
Definition
aic.c:108
aicWeights
int aicWeights(double *aic, double *w, int n)
Definition
aic.c:74
parFreeNr
int parFreeNr(const int n, double *pLower, double *pUpper)
Calculate the number of free parameters.
Definition
aic.c:50
aicModel
double aicModel(double *w, int n)
Definition
aic.c:132
aicSS
double aicSS(double ss, const int n, const int k)
Definition
aic.c:20
libtpcmodel.h
Header file for libtpcmodel.
Generated on
for TPCCLIB by
1.18.0