File: adap.c

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (195 lines) | stat: -rw-r--r-- 4,870 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 *   Copyright (c) 1996-2001 Lucent Technologies.
 *   See README file for details.
 */

/*
  Functions implementing the adaptive bandwidth selection.
  Will make the final call to nbhd() to set smoothing weights
  for selected bandwidth, But will **not** make the
  final call to locfit().
*/

#include "local.h"

static double hmin;

double acri(lk,t0,t2,pen)
double lk, t0, t2, pen;
{ double y;
/* return(-2*lk/(t0*exp(pen*log(1-t2/t0)))); */
  /* return((-2*lk+pen*t2)/t0); */
  y = (MAX(-2*lk,t0-t2)+pen*t2)/t0;
  return(y);
}

double mmse(lf,des)
lfit *lf;
design *des;
{ int i, ii, j, p, p1;
  double sv, sb, *l, dp;
  l = des->wd;
  wdiag(lf,des,l,(INT)0,(INT)1,(INT)0);
  sv = sb = 0;
  p = lf->mi[MP];
  for (i=0; i<des->n; i++)
  { sv += l[i]*l[i];
    ii = des->ind[i];
    dp = des->di[ii];
    for (j=0; j<lf->mi[MDEG]; j++) dp *= des->di[ii];
    sb += fabs(l[i])*dp;
  }
  p1 = factorial((int)lf->mi[MDEG]+1);
  return(sv+sb*sb*lf->dp[DADP]*lf->dp[DADP]/(p1*p1));
}

static double mcp, clo, cup;

/*
  Initial bandwidth will be (by default)
  k-nearest neighbors for k small, just lage enough to
  get defined estimate (unless user provided nonzero DALP
  or DFXH components)
*/

INT ainitband(des,lf)
design *des;
lfit   *lf;
{ INT lf_status = LF_OK, p, z, cri, noit, redo;
  double h, ho, t[6];
  p = des->p;
  cri = lf->mi[MACRI];
  noit = !((cri==AOK) | (cri==ANONE));
  z = (INT)(lf->mi[MN]*lf->dp[DALP]);
  if ((noit) && (z<p+2)) z = p+2;
  redo = 0; ho = -1;
  do
  { h = nbhd(lf,des,z,lf->dp[DFXH],redo);
    if (z<des->n) z = des->n;
    if (h>ho) lf_status = locfit(lf,des,h,noit);
    if (cri==ANONE) return(lf_status);
    z++;
    redo = 1;
  } while ((z<=lf->mi[MN]) && ((h==0)||(lf_status!=LF_OK)));
  hmin = h;

  switch(lf->mi[MACRI])
  { case ACP:
      local_df(lf,des,t);
      mcp = acri(des->llk,t[0],t[2],lf->dp[DADP]);
      return(lf_status);
    case AKAT:
      local_df(lf,des,t);
      clo = des->cf[0]-lf->dp[DADP]*t[5];
      cup = des->cf[0]+lf->dp[DADP]*t[5];
      return(lf_status);
    case AMDI:
      mcp = mmse(lf,des);
      return(lf_status);
    case AOK: return(lf_status);
  }
  ERROR(("aband1: unknown criterion"));
  return(LF_ERR);
}

/*
  aband2 increases the initial bandwidth until lack of fit results,
  or the fit is close to a global fit. Increase h by 1+0.3/d at
  each iteration.
*/

double aband2(des,lf,h0)
design *des;
lfit   *lf;
double h0;
{ double t[6], h, h1, nu1, cp, ncp, tlo, tup;
  INT d, inc, n, p, done;
  d = lf->mi[MDIM]; n = lf->mi[MN]; p = lf->mi[MP];
  h1 = h = h0;
  done = 0; nu1 = 0.0;
  inc = 0; ncp = 0.0;
  while ((!done) & (nu1<(n-p)*0.95))
  { h = nbhd(lf,des,0,(1+0.3/d)*h,1);
    if (locfit(lf,des,h,1)>0) WARN(("aband2: failed fit"));
    local_df(lf,des,t);
    nu1 = t[0]-t[2]; /* tr(A) */
    switch(lf->mi[MACRI])
    { case AKAT:
        tlo = des->cf[0]-lf->dp[DADP]*t[5];
        tup = des->cf[0]+lf->dp[DADP]*t[5];
/* printf("h %8.5f  tlo %8.5f  tup %8.5f\n",h,tlo,tup); */
        done = ((tlo>cup) | (tup<clo));
        if (!done)
        { clo = MAX(clo,tlo);
          cup = MIN(cup,tup);
          h1 = h;
        }
        break;
      case ACP:
        cp = acri(des->llk,t[0],t[2],lf->dp[DADP]);
/* printf("h %8.5f  lk %8.5f  t0 %8.5f  t2 %8.5f  cp %8.5f\n",h,des->llk,t[0],t[2],cp); */
        if (cp<mcp) { mcp = cp; h1 = h; }
        if (cp>=ncp) inc++; else inc = 0;
        ncp = cp;
        done = (inc>=10) | ((inc>=3) & ((t[0]-t[2])>=10) & (cp>1.5*mcp));
        break;
      case AMDI:
        cp = mmse(lf,des);
        if (cp<mcp) { mcp = cp; h1 = h; }
        if (cp>ncp) inc++; else inc = 0;
        ncp = cp;
        done = (inc>=3);
        break;
    }
  }
  return(h1);
}

/*
  aband3 does a finer search around best h so far. Try
  h*(1-0.2/d), h/(1-0.1/d), h*(1+0.1/d), h*(1+0.2/d)
*/
double aband3(des,lf,h0)
design *des;
lfit   *lf;
double h0;
{ double t[6], h, h1, cp, tlo, tup;
  INT i, i0, d, n;
  d = lf->mi[MDIM]; n = lf->mi[MN];

  h1 = h0;
  i0 = (lf->mi[MACRI]==AKAT) ? 1 : -2;
  if (h0==hmin) i0 = 1;
  for (i=i0; i<=2; i++)
  { if (i==0) i++;
    h = h0*(1+0.1*i/lf->mi[MDIM]);
    h = nbhd(lf,des,0,h,1);
    if (locfit(lf,des,h,1)>0) WARN(("aband3: failed fit"));
    local_df(lf,des,t);
    switch (lf->mi[MACRI])
    { case AKAT:
        tlo = des->cf[0]-lf->dp[DADP]*t[5];
        tup = des->cf[0]+lf->dp[DADP]*t[5];
        if ((tlo>cup) | (tup<clo)) /* done */
          i = 2;
        else
        { h1 = h;
          clo = MAX(clo,tlo);
          cup = MIN(cup,tup);
        }
        break;
      case ACP:
        cp = acri(des->llk,t[0],t[2],lf->dp[DADP]);
        if (cp<mcp) { mcp = cp; h1 = h; }
        else
        { if (i>0) i = 2; }
        break;
      case AMDI:
        cp = mmse(lf,des);
        if (cp<mcp) { mcp = cp; h1 = h; }
        else
        { if (i>0) i = 2; }
    }
  }
  return(h1);
}