annotate rDiff/src/locfit/Source/liblocf.c @ 3:29a698dc5c7e default tip

Merge multiple heads.
author Dave Bouvier <dave@bx.psu.edu>
date Mon, 27 Jan 2014 14:15:36 -0500
parents 0f80a5141704
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
5 #include "mex.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
6 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
7 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
8 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
9 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
10 * Integration for hazard rate estimation. The functions in this
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
11 * file are used to evaluate
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
12 * sum int_0^{Ti} W_i(t,x) A()A()' exp( P() ) dt
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
13 * for hazard rate models.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
14 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
15 * These routines assume the weight function is supported on [-1,1].
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
16 * hasint_sph multiplies by exp(base(lf,i)), which allows estimating
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
17 * the baseline in a proportional hazards model, when the covariate
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
18 * effect base(lf,i) is known.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
19 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
20 * TODO:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
21 * hazint_sph, should be able to reduce mint in some cases with
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
22 * small integration range. onedint could be used for beta-family
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
23 * (RECT,EPAN,BISQ,TRWT) kernels.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
24 * hazint_prod, restrict terms from the sum based on x values.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
25 * I should count obs >= max, and only do that integration once.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
26 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
27
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
28 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
29
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
30 static double ilim[2*MXDIM], *ff, tmax;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
31 static lfdata *haz_lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
32 static smpar *haz_sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
33
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
34 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
35 * hrao returns 0 if integration region is empty.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
36 * 1 otherwise.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
37 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
38 int haz_sph_int(dfx,cf,h,r1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
39 double *dfx, *cf, h, *r1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
40 { double s, t0, t1, wt, th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
41 int j, dim, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
42 s = 0; p = npar(haz_sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
43 dim = haz_lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
44 for (j=1; j<dim; j++) s += SQR(dfx[j]/(h*haz_lfd->sca[j]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
45 if (s>1) return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
46
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
47 setzero(r1,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
48 t1 = sqrt(1-s)*h*haz_lfd->sca[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
49 t0 = -t1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
50 if (t0<ilim[0]) t0 = ilim[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
51 if (t1>ilim[dim]) t1 = ilim[dim];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
52 if (t1>dfx[0]) t1 = dfx[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
53 if (t1<t0) return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
54
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
55 /* Numerical integration by Simpson's rule.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
56 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
57 for (j=0; j<=de_mint; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
58 { dfx[0] = t0+(t1-t0)*j/de_mint;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
59 wt = weight(haz_lfd, haz_sp, dfx, NULL, h, 0, 0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
60 fitfun(haz_lfd, haz_sp, dfx,NULL,ff,NULL);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
61 th = innerprod(cf,ff,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
62 if (link(haz_sp)==LLOG) th = exp(th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
63 wt *= 2+2*(j&1)-(j==0)-(j==de_mint);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
64 addouter(r1,ff,ff,p,wt*th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
65 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
66 multmatscal(r1,(t1-t0)/(3*de_mint),p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
67
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
68 return(1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
69 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
70
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
71 int hazint_sph(t,resp,r1,cf,h)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
72 double *t, *resp, *r1, *cf, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
73 { int i, j, n, p, st;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
74 double dfx[MXDIM], eb, sb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
75 p = npar(haz_sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
76 setzero(resp,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
77 sb = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
78
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
79 n = haz_lfd->n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
80 for (i=0; i<=n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
81 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
82 if (i==n)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
83 { dfx[0] = tmax-t[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
84 for (j=1; j<haz_lfd->d; j++) dfx[j] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
85 eb = exp(sb/n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
86 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
87 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
88 { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
89 for (j=0; j<haz_lfd->d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
90 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
91
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
92 st = haz_sph_int(dfx,cf,h,r1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
93 if (st)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
94 for (j=0; j<p*p; j++) resp[j] += eb*r1[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
95 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
96 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
97 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
98
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
99 int hazint_prod(t,resp,x,cf,h)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
100 double *t, *resp, *x, *cf, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
101 { int d, p, i, j, k, st;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
102 double dfx[MXDIM], t_prev,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
103 hj, hs, ncf[MXDEG], ef, il1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
104 double prod_wk[MXDIM][2*MXDEG+1], eb, sb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
105
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
106 p = npar(haz_sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
107 d = haz_lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
108 setzero(resp,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
109 hj = hs = h*haz_lfd->sca[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
110
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
111 ncf[0] = cf[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
112 for (i=1; i<=deg(haz_sp); i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
113 { ncf[i] = hj*cf[(i-1)*d+1]; hj *= hs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
114 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
115
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
116 /* for i=0..n....
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
117 * First we compute prod_wk[j], j=0..d.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
118 * For j=0, this is int_0^T_i (u-t)^k W((u-t)/h) exp(b0*(u-t)) du
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
119 * For remaining j, (x(i,j)-x(j))^k Wj exp(bj*(x..-x.))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
120 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
121 * Second, we add to the integration (exp(a) incl. in integral)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
122 * with the right factorial denominators.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
123 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
124 t_prev = ilim[0]; sb = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
125 for (i=0; i<=haz_lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
126 { if (i==haz_lfd->n)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
127 { dfx[0] = tmax-t[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
128 for (j=1; j<d; j++) dfx[j] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
129 eb = exp(sb/haz_lfd->n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
130 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
131 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
132 { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
133 for (j=0; j<d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
134 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
135
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
136 if (dfx[0]>ilim[0]) /* else it doesn't contribute */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
137 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
138 /* time integral */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
139 il1 = (dfx[0]>ilim[d]) ? ilim[d] : dfx[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
140 if (il1 != t_prev) /* don't repeat! */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
141 { st = onedint(haz_sp,ncf,ilim[0]/hs,il1/hs,prod_wk[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
142 if (st>0) return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
143 hj = eb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
144 for (j=0; j<=2*deg(haz_sp); j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
145 { hj *= hs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
146 prod_wk[0][j] *= hj;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
147 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
148 t_prev = il1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
149 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
150
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
151 /* covariate terms */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
152 for (j=1; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
153 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
154 ef = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
155 for (k=deg(haz_sp); k>0; k--) ef = (ef+dfx[j])*cf[1+(k-1)*d+j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
156 ef = exp(ef);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
157 prod_wk[j][0] = ef * W(dfx[j]/(h*haz_lfd->sca[j]),ker(haz_sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
158 for (k=1; k<=2*deg(haz_sp); k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
159 prod_wk[j][k] = prod_wk[j][k-1] * dfx[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
160 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
161
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
162 /* add to the integration. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
163 prodintresp(resp,prod_wk,d,deg(haz_sp),p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
164 } /* if dfx0 > ilim0 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
165 } /* n loop */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
166
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
167 /* symmetrize */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
168 for (k=0; k<p; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
169 for (j=k; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
170 resp[j*p+k] = resp[k*p+j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
171 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
172 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
173
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
174 int hazint(t,resp,resp1,cf,h)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
175 double *t, *resp, *resp1, *cf, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
176 { if (haz_lfd->d==1) return(hazint_prod(t,resp,resp1,cf,h));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
177 if (kt(haz_sp)==KPROD) return(hazint_prod(t,resp,resp1,cf,h));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
178
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
179 return(hazint_sph(t,resp,resp1,cf,h));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
180 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
181
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
182 void haz_init(lfd,des,sp,il)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
183 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
184 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
185 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
186 double *il;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
187 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
188
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
189 haz_lfd = lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
190 haz_sp = sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
191
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
192 tmax = datum(lfd,0,0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
193 for (i=1; i<lfd->n; i++) tmax = MAX(tmax,datum(lfd,0,i));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
194 ff = des->xtwx.wk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
195 for (i=0; i<2*lfd->d; i++) ilim[i] = il[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
196 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
197 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
198 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
199 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
200 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
201 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
202 * Routines for one-dimensional numerical integration
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
203 * in density estimation. The entry point is
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
204 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
205 * onedint(cf,mi,l0,l1,resp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
206 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
207 * which evaluates int W(u)u^j exp( P(u) ), j=0..2*deg.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
208 * P(u) = cf[0] + cf[1]u + cf[2]u^2/2 + ... + cf[deg]u^deg/deg!
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
209 * l0 and l1 are the integration limits.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
210 * The results are returned through the vector resp.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
211 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
212 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
213
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
214 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
215
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
216 static int debug;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
217
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
218 int exbctay(b,c,n,z) /* n-term taylor series of e^(bx+cx^2) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
219 double b, c, *z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
220 int n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
221 { double ec[20];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
222 int i, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
223 z[0] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
224 for (i=1; i<=n; i++) z[i] = z[i-1]*b/i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
225 if (c==0.0) return(n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
226 if (n>=40)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
227 { WARN(("exbctay limit to n<40"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
228 n = 39;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
229 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
230 ec[0] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
231 for (i=1; 2*i<=n; i++) ec[i] = ec[i-1]*c/i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
232 for (i=n; i>1; i--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
233 for (j=1; 2*j<=i; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
234 z[i] += ec[j]*z[i-2*j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
235 return(n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
236 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
237
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
238 double explinjtay(l0,l1,j,cf)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
239 /* int_l0^l1 x^j e^(a+bx+cx^2); exbctay aroud l1 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
240 double l0, l1, *cf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
241 int j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
242 { double tc[40], f, s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
243 int k, n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
244 if ((l0!=0.0) | (l1!=1.0)) WARN(("explinjtay: invalid l0, l1"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
245 n = exbctay(cf[1]+2*cf[2]*l1,cf[2],20,tc);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
246 s = tc[0]/(j+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
247 f = 1/(j+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
248 for (k=1; k<=n; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
249 { f *= -k/(j+k+1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
250 s += tc[k]*f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
251 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
252 return(f);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
253 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
254
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
255 void explint1(l0,l1,cf,I,p) /* int x^j exp(a+bx); j=0..p-1 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
256 double l0, l1, *cf, *I;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
257 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
258 { double y0, y1, f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
259 int j, k, k1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
260 y0 = mut_exp(cf[0]+l0*cf[1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
261 y1 = mut_exp(cf[0]+l1*cf[1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
262 if (p<2*fabs(cf[1])) k = p; else k = (int)fabs(cf[1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
263
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
264 if (k>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
265 { I[0] = (y1-y0)/cf[1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
266 for (j=1; j<k; j++) /* forward steps for small j */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
267 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
268 I[j] = (y1-y0-j*I[j-1])/cf[1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
269 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
270 if (k==p) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
271 y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
272 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
273
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
274 f = 1; k1 = k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
275 while ((k<50) && (f>1.0e-8)) /* initially Ik = diff(x^{k+1}e^{a+bx}) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
276 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
277 I[k] = y1-y0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
278 if (k>=p) f *= fabs(cf[1])/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
279 k++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
280 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
281 if (k==50) WARN(("explint1: want k>50"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
282 I[k] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
283 for (j=k-1; j>=k1; j--) /* now do back step recursion */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
284 I[j] = (I[j]-cf[1]*I[j+1])/(j+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
285 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
286
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
287 void explintyl(l0,l1,cf,I,p) /* small c, use taylor series and explint1 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
288 double l0, l1, *cf, *I;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
289 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
290 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
291 double c;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
292 explint1(l0,l1,cf,I,p+8);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
293 c = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
294 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
295 I[i] = (((I[i+8]*c/4+I[i+6])*c/3+I[i+4])*c/2+I[i+2])*c+I[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
296 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
297
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
298 void solvetrid(X,y,m)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
299 double *X, *y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
300 int m;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
301 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
302 double s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
303 for (i=1; i<m; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
304 { s = X[3*i]/X[3*i-2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
305 X[3*i] = 0; X[3*i+1] -= s*X[3*i-1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
306 y[i] -= s*y[i-1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
307 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
308 for (i=m-2; i>=0; i--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
309 { s = X[3*i+2]/X[3*i+4];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
310 X[3*i+2] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
311 y[i] -= s*y[i+1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
312 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
313 for (i=0; i<m; i++) y[i] /= X[3*i+1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
314 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
315
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
316 void initi0i1(I,cf,y0,y1,l0,l1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
317 double *I, *cf, y0, y1, l0, l1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
318 { double a0, a1, c, d, bi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
319 d = -cf[1]/(2*cf[2]); c = sqrt(2*fabs(cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
320 a0 = c*(l0-d); a1 = c*(l1-d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
321 if (cf[2]<0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
322 { bi = mut_exp(cf[0]+cf[1]*d+cf[2]*d*d)/c;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
323 if (a0>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
324 { if (a0>6) I[0] = (y0*ptail(-a0)-y1*ptail(-a1))/c;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
325 else I[0] = S2PI*(mut_pnorm(-a0)-mut_pnorm(-a1))*bi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
326 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
327 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
328 { if (a1< -6) I[0] = (y1*ptail(a1)-y0*ptail(a0))/c;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
329 else I[0] = S2PI*(mut_pnorm(a1)-mut_pnorm(a0))*bi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
330 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
331 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
332 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
333 I[0] = (y1*mut_daws(a1)-y0*mut_daws(a0))/c;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
334 I[1] = (y1-y0)/(2*cf[2])+d*I[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
335 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
336
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
337 void explinsid(l0,l1,cf,I,p) /* large b; don't use fwd recursion */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
338 double l0, l1, *cf, *I;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
339 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
340 { int k, k0, k1, k2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
341 double y0, y1, Z[150];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
342 if (debug) mut_printf("side: %8.5f %8.5f %8.5f limt %8.5f %8.5f p %2d\n",cf[0],cf[1],cf[2],l0,l1,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
343
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
344 k0 = 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
345 k1 = (int)(fabs(cf[1])+fabs(2*cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
346 if (k1<2) k1 = 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
347 if (k1>p+20) k1 = p+20;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
348 k2 = p+20;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
349
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
350 if (k2>50) { mut_printf("onedint: k2 warning\n"); k2 = 50; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
351 if (debug) mut_printf("k0 %2d k1 %2d k2 %2d p %2d\n",k0,k1,k2,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
352
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
353 y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
354 y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
355 initi0i1(I,cf,y0,y1,l0,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
356 if (debug) mut_printf("i0 %8.5f i1 %8.5f\n",I[0],I[1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
357
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
358 y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
359 if (k0<k1) /* center steps; initially x^k*exp(...) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
360 for (k=k0; k<k1; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
361 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
362 I[k] = y1-y0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
363 Z[3*k] = k; Z[3*k+1] = cf[1]; Z[3*k+2] = 2*cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
364 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
365
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
366 y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
367 if (debug) mut_printf("k1 %2d y0 %8.5f y1 %8.5f\n",k1,y0,y1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
368 for (k=k1; k<k2; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
369 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
370 I[k] = y1-y0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
371 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
372 I[k2] = I[k2+1] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
373 for (k=k2-1; k>=k1; k--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
374 I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
375
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
376 if (k0<k1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
377 { I[k0] -= k0*I[k0-1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
378 I[k1-1] -= 2*cf[2]*I[k1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
379 Z[3*k0] = Z[3*k1-1] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
380 solvetrid(&Z[3*k0],&I[k0],k1-k0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
381 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
382 if (debug)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
383 { mut_printf("explinsid:\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
384 for (k=0; k<p; k++) mut_printf(" %8.5f\n",I[k]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
385 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
386 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
387
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
388 void explinbkr(l0,l1,cf,I,p) /* small b,c; use back recursion */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
389 double l0, l1, *cf, *I;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
390 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
391 { int k, km;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
392 double y0, y1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
393 y0 = mut_exp(cf[0]+l0*(cf[1]+cf[2]*l0));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
394 y1 = mut_exp(cf[0]+l1*(cf[1]+cf[2]*l1));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
395 km = p+10;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
396 for (k=0; k<=km; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
397 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
398 I[k] = y1-y0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
399 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
400 I[km+1] = I[km+2] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
401 for (k=km; k>=0; k--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
402 I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
403 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
404
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
405 void explinfbk0(l0,l1,cf,I,p) /* fwd and bac recur; b=0; c<0 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
406 double l0, l1, *cf, *I;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
407 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
408 { double y0, y1, f1, f2, f, ml2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
409 int k, ks;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
410
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
411 y0 = mut_exp(cf[0]+l0*l0*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
412 y1 = mut_exp(cf[0]+l1*l1*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
413 initi0i1(I,cf,y0,y1,l0,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
414
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
415 ml2 = MAX(l0*l0,l1*l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
416 ks = 1+(int)(2*fabs(cf[2])*ml2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
417 if (ks<2) ks = 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
418 if (ks>p-3) ks = p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
419
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
420 /* forward recursion for k < ks */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
421 for (k=2; k<ks; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
422 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
423 I[k] = (y1-y0-(k-1)*I[k-2])/(2*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
424 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
425 if (ks==p) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
426
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
427 y1 *= l1*l1; y0 *= l0*l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
428 for (k=ks; k<p; k++) /* set I[k] = x^{k+1}e^(a+cx^2) | {l0,l1} */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
429 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
430 I[k] = y1-y0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
431 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
432
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
433 /* initialize I[p-2] and I[p-1] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
434 f1 = 1.0/p; f2 = 1.0/(p-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
435 I[p-1] *= f1; I[p-2] *= f2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
436 k = p; f = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
437 while (f>1.0e-8)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
438 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
439 if ((k-p)%2==0) /* add to I[p-2] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
440 { f2 *= -2*cf[2]/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
441 I[p-2] += (y1-y0)*f2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
442 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
443 else /* add to I[p-1] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
444 { f1 *= -2*cf[2]/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
445 I[p-1] += (y1-y0)*f1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
446 f *= 2*fabs(cf[2])*ml2/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
447 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
448 k++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
449 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
450
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
451 /* use back recursion for I[ks..(p-3)] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
452 for (k=p-3; k>=ks; k--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
453 I[k] = (I[k]-2*cf[2]*I[k+2])/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
454 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
455
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
456 void explinfbk(l0,l1,cf,I,p) /* fwd and bac recur; b not too large */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
457 double l0, l1, *cf, *I;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
458 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
459 { double y0, y1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
460 int k, ks, km;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
461
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
462 y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
463 y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
464 initi0i1(I,cf,y0,y1,l0,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
465
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
466 ks = (int)(3*fabs(cf[2]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
467 if (ks<3) ks = 3;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
468 if (ks>0.75*p) ks = p; /* stretch the forward recurs as far as poss. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
469 /* forward recursion for k < ks */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
470 for (k=2; k<ks; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
471 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
472 I[k] = (y1-y0-cf[1]*I[k-1]-(k-1)*I[k-2])/(2*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
473 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
474 if (ks==p) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
475
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
476 km = p+15;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
477 y1 *= l1*l1; y0 *= l0*l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
478 for (k=ks; k<=km; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
479 { y1 *= l1; y0 *= l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
480 I[k] = y1-y0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
481 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
482 I[km+1] = I[km+2] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
483 for (k=km; k>=ks; k--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
484 I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
485 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
486
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
487 void recent(I,resp,wt,p,s,x)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
488 double *I, *resp, *wt, x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
489 int p, s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
490 { int i, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
491
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
492 /* first, use W taylor series I -> resp */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
493 for (i=0; i<=p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
494 { resp[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
495 for (j=0; j<s; j++) resp[i] += wt[j]*I[i+j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
496 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
497
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
498 /* now, recenter x -> 0 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
499 if (x==0) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
500 for (j=0; j<=p; j++) for (i=p; i>j; i--) resp[i] += x*resp[i-1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
501 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
502
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
503 void recurint(l0,l2,cf,resp,p,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
504 double l0, l2, *cf, *resp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
505 int p, ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
506 { int i, s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
507 double l1, d0, d1, d2, dl, z0, z1, z2, wt[20], ncf[3], I[50], r1[5], r2[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
508 if (debug) mut_printf("\nrecurint: %8.5f %8.5f %8.5f %8.5f %8.5f\n",cf[0],cf[1],cf[2],l0,l2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
509
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
510 if (cf[2]==0) /* go straight to explint1 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
511 { s = wtaylor(wt,0.0,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
512 if (debug) mut_printf("case 1\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
513 explint1(l0,l2,cf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
514 recent(I,resp,wt,p,s,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
515 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
516 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
517
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
518 dl = l2-l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
519 d0 = cf[1]+2*l0*cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
520 d2 = cf[1]+2*l2*cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
521 z0 = cf[0]+l0*(cf[1]+l0*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
522 z2 = cf[0]+l2*(cf[1]+l2*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
523
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
524 if ((fabs(cf[1]*dl)<1) && (fabs(cf[2]*dl*dl)<1))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
525 { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
526 if (debug) mut_printf("case 2\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
527 s = wtaylor(wt,l0,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
528 explinbkr(0.0,dl,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
529 recent(I,resp,wt,p,s,l0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
530 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
531 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
532
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
533 if (fabs(cf[2]*dl*dl)<0.001) /* small c, use explint1+tay.ser */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
534 { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
535 if (debug) mut_printf("case small c\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
536 s = wtaylor(wt,l0,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
537 explintyl(0.0,l2-l0,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
538 recent(I,resp,wt,p,s,l0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
539 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
540 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
541
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
542 if (d0*d2<=0) /* max/min in [l0,l2] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
543 { l1 = -cf[1]/(2*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
544 z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
545 d1 = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
546 if (cf[2]<0) /* peak, integrate around l1 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
547 { s = wtaylor(wt,l1,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
548 ncf[0] = z1; ncf[1] = 0.0; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
549 if (debug) mut_printf("case peak p %2d s %2d\n",p,s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
550 explinfbk0(l0-l1,l2-l1,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
551 recent(I,resp,wt,p,s,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
552 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
553 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
554 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
555
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
556 if ((d0-2*cf[2]*dl)*(d2+2*cf[2]*dl)<0) /* max/min is close to [l0,l2] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
557 { l1 = -cf[1]/(2*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
558 z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
559 if (l1<l0) { l1 = l0; z1 = z0; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
560 if (l1>l2) { l1 = l2; z1 = z2; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
561
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
562 if ((z1>=z0) & (z1>=z2)) /* peak; integrate around l1 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
563 { s = wtaylor(wt,l1,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
564 if (debug) mut_printf("case 4\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
565 d1 = cf[1]+2*l1*cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
566 ncf[0] = z1; ncf[1] = d1; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
567 explinfbk(l0-l1,l2-l1,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
568 recent(I,resp,wt,p,s,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
569 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
570 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
571
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
572 /* trough; integrate [l0,l1] and [l1,l2] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
573 for (i=0; i<=p; i++) r1[i] = r2[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
574 if (l0<l1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
575 { s = wtaylor(wt,l0,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
576 if (debug) mut_printf("case 5\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
577 ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
578 explinfbk(0.0,l1-l0,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
579 recent(I,r1,wt,p,s,l0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
580 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
581 if (l1<l2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
582 { s = wtaylor(wt,l2,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
583 if (debug) mut_printf("case 6\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
584 ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
585 explinfbk(l1-l2,0.0,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
586 recent(I,r2,wt,p,s,l2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
587 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
588 for (i=0; i<=p; i++) resp[i] = r1[i]+r2[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
589 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
590 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
591
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
592 /* Now, quadratic is monotone on [l0,l2]; big b; moderate c */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
593 if (z2>z0+3) /* steep increase, expand around l2 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
594 { s = wtaylor(wt,l2,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
595 if (debug) mut_printf("case 7\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
596
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
597
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
598 ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
599 explinsid(l0-l2,0.0,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
600 recent(I,resp,wt,p,s,l2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
601 if (debug) mut_printf("7 resp: %8.5f %8.5f %8.5f %8.5f\n",resp[0],resp[1],resp[2],resp[3]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
602 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
603 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
604
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
605 /* bias towards expansion around l0, because it's often 0 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
606 if (debug) mut_printf("case 8\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
607 s = wtaylor(wt,l0,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
608 ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
609 explinsid(0.0,l2-l0,ncf,I,p+s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
610 recent(I,resp,wt,p,s,l0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
611 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
612 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
613
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
614 int onedexpl(cf,deg,resp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
615 double *cf, *resp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
616 int deg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
617 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
618 double f0, fr, fl;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
619 if (deg>=2) LERR(("onedexpl only valid for deg=0,1"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
620 if (fabs(cf[1])>=EFACT) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
621
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
622 f0 = exp(cf[0]); fl = fr = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
623 for (i=0; i<=2*deg; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
624 { f0 *= i+1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
625 fl /=-(EFACT+cf[1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
626 fr /= EFACT-cf[1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
627 resp[i] = f0*(fr-fl);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
628 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
629 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
630 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
631
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
632 int onedgaus(cf,deg,resp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
633 double *cf, *resp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
634 int deg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
635 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
636 double f0, mu, s2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
637 if (deg==3)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
638 { LERR(("onedgaus only valid for deg=0,1,2"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
639 return(LF_ERR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
640 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
641 if (2*cf[2]>=GFACT*GFACT) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
642
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
643 s2 = 1/(GFACT*GFACT-2*cf[2]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
644 mu = cf[1]*s2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
645 resp[0] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
646 if (deg>=1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
647 { resp[1] = mu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
648 resp[2] = s2+mu*mu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
649 if (deg==2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
650 { resp[3] = mu*(3*s2+mu*mu);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
651 resp[4] = 3*s2*s2 + mu*mu*(6*s2+mu*mu);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
652 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
653 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
654 f0 = S2PI * exp(cf[0]+mu*mu/(2*s2))*sqrt(s2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
655 for (i=0; i<=2*deg; i++) resp[i] *= f0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
656 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
657 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
658
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
659 int onedint(sp,cf,l0,l1,resp) /* int W(u)u^j exp(..), j=0..2*deg */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
660 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
661 double *cf, l0, l1, *resp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
662 { double u, uj, y, ncf[4], rr[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
663 int i, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
664
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
665 if (debug) mut_printf("onedint: %f %f %f %f %f\n",cf[0],cf[1],cf[2],l0,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
666
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
667 if (deg(sp)<=2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
668 { for (i=0; i<3; i++) ncf[i] = (i>deg(sp)) ? 0.0 : cf[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
669 ncf[2] /= 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
670
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
671 if (ker(sp)==WEXPL) return(onedexpl(ncf,deg(sp),resp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
672 if (ker(sp)==WGAUS) return(onedgaus(ncf,deg(sp),resp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
673
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
674 if (l1>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
675 recurint(MAX(l0,0.0),l1,ncf,resp,2*deg(sp),ker(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
676 else for (i=0; i<=2*deg(sp); i++) resp[i] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
677
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
678 if (l0<0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
679 { ncf[1] = -ncf[1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
680 l0 = -l0; l1 = -l1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
681 recurint(MAX(l1,0.0),l0,ncf,rr,2*deg(sp),ker(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
682 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
683 else for (i=0; i<=2*deg(sp); i++) rr[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
684
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
685 for (i=0; i<=2*deg(sp); i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
686 resp[i] += (i%2==0) ? rr[i] : -rr[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
687
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
688 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
689 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
690
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
691 /* For degree >= 3, we use Simpson's rule. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
692 for (j=0; j<=2*deg(sp); j++) resp[j] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
693 for (i=0; i<=de_mint; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
694 { u = l0+(l1-l0)*i/de_mint;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
695 y = cf[0]; uj = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
696 for (j=1; j<=deg(sp); j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
697 { uj *= u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
698 y += cf[j]*uj/fact[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
699 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
700 y = (4-2*(i%2==0)-(i==0)-(i==de_mint)) *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
701 W(fabs(u),ker(sp))*exp(MIN(y,300.0));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
702 for (j=0; j<=2*deg(sp); j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
703 { resp[j] += y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
704 y *= u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
705 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
706 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
707 for (j=0; j<=2*deg(sp); j++) resp[j] = resp[j]*(l1-l0)/(3*de_mint);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
708 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
709 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
710 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
711 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
712 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
713 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
714
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
715 extern int lf_status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
716 static double u[MXDIM], ilim[2*MXDIM], *ff, hh, *cff;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
717 static lfdata *den_lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
718 static design *den_des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
719 static smpar *den_sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
720 int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
721 int de_mint = 20;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
722 int de_itype = IDEFA;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
723 int de_renorm= 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
724
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
725 int multint(), prodint(), gausint(), mlinint();
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
726
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
727 #define NITYPE 7
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
728 static char *itype[NITYPE] = { "default", "multi", "product", "mlinear",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
729 "hazard", "sphere", "monte" };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
730 static int ivals[NITYPE] =
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
731 { IDEFA, IMULT, IPROD, IMLIN, IHAZD, ISPHR, IMONT };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
732 int deitype(char *z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
733 { return(pmatch(z, itype, ivals, NITYPE, IDEFA));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
734 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
735
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
736 void prresp(coef,resp,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
737 double *coef, *resp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
738 int p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
739 { int i, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
740 mut_printf("Coefficients:\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
741 for (i=0; i<p; i++) mut_printf("%8.5f ",coef[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
742 mut_printf("\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
743 mut_printf("Response matrix:\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
744 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
745 { for (j=0; j<p; j++) mut_printf("%9.6f, ",resp[i+j*p]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
746 mut_printf("\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
747 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
748 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
749
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
750 int mif(u,d,resp,M)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
751 double *u, *resp, *M;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
752 int d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
753 { double wt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
754 int i, j, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
755
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
756 p = den_des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
757 wt = weight(den_lfd, den_sp, u, NULL, hh, 0, 0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
758 if (wt==0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
759 { setzero(resp,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
760 return(p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
761 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
762
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
763 fitfun(den_lfd, den_sp, u,NULL,ff,NULL);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
764 if (link(den_sp)==LLOG)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
765 wt *= mut_exp(innerprod(ff,cff,p));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
766 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
767 for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
768 resp[i*p+j] = wt*ff[i]*ff[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
769 return(p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
770 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
771
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
772 int multint(t,resp1,resp2,cf,h)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
773 double *t, *resp1, *resp2, *cf, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
774 { int d, i, mg[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
775
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
776 if (ker(den_sp)==WGAUS) return(gausint(t,resp1,resp2,cf,h,den_lfd->sca));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
777
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
778 d = den_lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
779 for (i=0; i<d; i++) mg[i] = de_mint;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
780
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
781 hh = h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
782 cff= cf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
783 simpsonm(mif,ilim,&ilim[d],d,resp1,mg,resp2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
784 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
785 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
786
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
787 int mlinint(t,resp1,resp2,cf,h)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
788 double *t, *resp1, *resp2, *cf, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
789 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
790 double hd, nb, wt, wu, g[4], w0, w1, v, *sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
791 int d, p, i, j, jmax, k, l, z, jj[2];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
792
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
793 d = den_lfd->d; p = den_des->p; sca = den_lfd->sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
794 hd = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
795 for (i=0; i<d; i++) hd *= h*sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
796
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
797 if (link(den_sp)==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
798 { setzero(resp1,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
799 resp1[0] = wint(d,NULL,0,ker(den_sp))*hd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
800 if (deg(den_sp)==0) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
801 jj[0] = 2; w0 = wint(d,jj,1,ker(den_sp))*hd*h*h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
802 for (i=0; i<d; i++) resp1[(i+1)*p+i+1] = w0*sca[i]*sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
803 if (deg(den_sp)==1) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
804 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
805 { j = p-(d-i)*(d-i+1)/2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
806 resp1[j] = resp1[p*j] = w0*sca[i]*sca[i]/2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
807 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
808 if (d>1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
809 { jj[1] = 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
810 w0 = wint(d,jj,2,ker(den_sp)) * hd*h*h*h*h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
811 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
812 jj[0] = 4;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
813 w1 = wint(d,jj,1,ker(den_sp)) * hd*h*h*h*h/4;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
814 z = d+1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
815 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
816 { k = p-(d-i)*(d-i+1)/2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
817 for (j=i; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
818 { l = p-(d-j)*(d-j+1)/2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
819 if (i==j) resp1[z*p+z] = w1*SQR(sca[i])*SQR(sca[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
820 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
821 { resp1[z*p+z] = w0*SQR(sca[i])*SQR(sca[j]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
822 resp1[k*p+l] = resp1[k+p*l] = w0/4*SQR(sca[i])*SQR(sca[j]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
823 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
824 z++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
825 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
826 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
827 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
828 switch(deg(den_sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
829 { case 0:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
830 resp1[0] = mut_exp(cf[0])*wint(d,NULL,0,ker(den_sp))*hd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
831 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
832 case 1:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
833 nb = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
834 for (i=1; i<=d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
835 { v = h*cf[i]*sca[i-1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
836 nb += v*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
837 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
838 if (ker(den_sp)==WGAUS)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
839 { w0 = 1/(GFACT*GFACT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
840 g[0] = mut_exp(cf[0]+w0*nb/2+d*log(S2PI/2.5));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
841 g[1] = g[3] = g[0]*w0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
842 g[2] = g[0]*w0*w0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
843 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
844 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
845 { wt = wu = mut_exp(cf[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
846 w0 = wint(d,NULL,0,ker(den_sp)); g[0] = wt*w0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
847 g[1] = g[2] = g[3] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
848 j = 0; jmax = (d+2)*de_mint;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
849 while ((j<jmax) && (wt*w0/g[0]>1.0e-8))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
850 { j++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
851 jj[0] = 2*j; w0 = wint(d,jj,1,ker(den_sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
852 if (d==1) g[3] += wt * w0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
853 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
854 { jj[0] = 2; jj[1] = 2*j-2; w1 = wint(d,jj,2,ker(den_sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
855 g[3] += wt*w1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
856 g[2] += wu*(w0-w1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
857 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
858 wt /= (2*j-1.0); g[1] += wt*w0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
859 wt *= nb/(2*j); g[0] += wt*w0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
860 wu /= (2*j-1.0)*(2*j);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
861 if (j>1) wu *= nb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
862 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
863 if (j==jmax) WARN(("mlinint: series not converged"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
864 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
865 g[0] *= hd; g[1] *= hd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
866 g[2] *= hd; g[3] *= hd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
867 resp1[0] = g[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
868 for (i=1; i<=d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
869 { resp1[i] = resp1[(d+1)*i] = cf[i]*SQR(h*sca[i-1])*g[1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
870 for (j=1; j<=d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
871 { resp1[(d+1)*i+j] = (i==j) ? g[3]*SQR(h*sca[i-1]) : 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
872 resp1[(d+1)*i+j] += g[2]*SQR(h*h*sca[i-1]*sca[j-1])*cf[i]*cf[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
873 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
874 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
875 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
876 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
877 LERR(("mlinint: deg=0,1 only"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
878 return(LF_ERR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
879 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
880
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
881 void prodintresp(resp,prod_wk,dim,deg,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
882 double *resp, prod_wk[MXDIM][2*MXDEG+1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
883 int dim, deg, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
884 { double prod;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
885 int i, j, k, j1, k1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
886
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
887 prod = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
888 for (i=0; i<dim; i++) prod *= prod_wk[i][0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
889 resp[0] += prod;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
890 if (deg==0) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
891
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
892 for (j1=1; j1<=deg; j1++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
893 { for (j=0; j<dim; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
894 { prod = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
895 for (i=0; i<dim; i++) prod *= prod_wk[i][j1*(j==i)];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
896 prod /= fact[j1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
897 resp[1 + (j1-1)*dim +j] += prod;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
898 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
899 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
900
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
901 for (k1=1; k1<=deg; k1++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
902 for (j1=k1; j1<=deg; j1++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
903 { for (k=0; k<dim; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
904 for (j=0; j<dim; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
905 { prod = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
906 for (i=0; i<dim; i++) prod *= prod_wk[i][k1*(k==i) + j1*(j==i)];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
907 prod /= fact[k1]*fact[j1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
908 resp[ (1+(k1-1)*dim+k)*p + 1+(j1-1)*dim+j] += prod;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
909 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
910 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
911 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
912
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
913 int prodint(t,resp,resp2,coef,h)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
914 double *t, *resp, *resp2, *coef, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
915 { int dim, p, i, j, k, st;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
916 double cf[MXDEG+1], hj, hs, prod_wk[MXDIM][2*MXDEG+1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
917
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
918 dim = den_lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
919 p = den_des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
920 for (i=0; i<p*p; i++) resp[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
921 cf[0] = coef[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
922
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
923 /* compute the one dimensional terms
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
924 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
925 for (i=0; i<dim; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
926 { hj = 1; hs = h*den_lfd->sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
927 for (j=0; j<deg(den_sp); j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
928 { hj *= hs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
929 cf[j+1] = hj*coef[ j*dim+i+1 ];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
930 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
931 st = onedint(den_sp,cf,ilim[i]/hs,ilim[i+dim]/hs,prod_wk[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
932 if (st==LF_BADP) return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
933 hj = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
934 for (j=0; j<=2*deg(den_sp); j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
935 { hj *= hs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
936 prod_wk[i][j] *= hj;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
937 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
938 cf[0] = 0.0; /* so we only include it once, when d>=2 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
939 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
940
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
941 /* transfer to the resp array
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
942 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
943 prodintresp(resp,prod_wk,dim,deg(den_sp),p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
944
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
945 /* Symmetrize.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
946 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
947 for (k=0; k<p; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
948 for (j=k; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
949 resp[j*p+k] = resp[k*p+j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
950
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
951 return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
952 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
953
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
954 int gausint(t,resp,C,cf,h,sca)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
955 double *t, *resp, *C, *cf, h, *sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
956 { double nb, det, z, *P;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
957 int d, p, i, j, k, l, m1, m2, f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
958 d = den_lfd->d; p = den_des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
959 m1 = d+1; nb = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
960 P = &C[d*d];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
961 resp[0] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
962 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
963 { C[i*d+i] = SQR(GFACT/(h*sca[i]))-cf[m1++];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
964 for (j=i+1; j<d; j++) C[i*d+j] = C[j*d+i] = -cf[m1++];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
965 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
966 eig_dec(C,P,d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
967 det = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
968 for (i=1; i<=d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
969 { det *= C[(i-1)*(d+1)];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
970 if (det <= 0) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
971 resp[i] = cf[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
972 for (j=1; j<=d; j++) resp[j+i*p] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
973 resp[i+i*p] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
974 svdsolve(&resp[i*p+1],u,P,C,P,d,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
975 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
976 svdsolve(&resp[1],u,P,C,P,d,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
977 det = sqrt(det);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
978 for (i=1; i<=d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
979 { nb += cf[i]*resp[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
980 resp[i*p] = resp[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
981 for (j=1; j<=d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
982 resp[i+p*j] += resp[i]*resp[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
983 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
984 m1 = d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
985 for (i=1; i<=d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
986 for (j=i; j<=d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
987 { m1++; f = 1+(i==j);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
988 resp[m1] = resp[m1*p] = resp[i*p+j]/f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
989 m2 = d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
990 for (k=1; k<=d; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
991 { resp[m1+k*p] = resp[k+m1*p] =
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
992 ( resp[i]*resp[j*p+k] + resp[j]*resp[i*p+k]
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
993 + resp[k]*resp[i*p+j] - 2*resp[i]*resp[j]*resp[k] )/f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
994 for (l=k; l<=d; l++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
995 { m2++; f = (1+(i==j))*(1+(k==l));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
996 resp[m1+m2*p] = resp[m2+m1*p] = ( resp[i+j*p]*resp[k+l*p]
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
997 + resp[i+k*p]*resp[j+l*p] + resp[i+l*p]*resp[j+k*p]
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
998 - 2*resp[i]*resp[j]*resp[k]*resp[l] )/f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
999 } } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1000 z = mut_exp(d*0.918938533+cf[0]+nb/2)/det;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1001 multmatscal(resp,z,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1002 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1003 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1004
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1005 int likeden(coef, lk0, f1, A)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1006 double *coef, *lk0, *f1, *A;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1007 { double lk, r;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1008 int i, j, p, rstat;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1009
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1010 lf_status = LF_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1011 p = den_des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1012 if ((link(den_sp)==LIDENT) && (coef[0] != 0.0)) return(NR_BREAK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1013 lf_status = (den_des->itype)(den_des->xev,A,den_des->xtwx.Q,coef,den_des->h);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1014 if (lf_error) lf_status = LF_ERR;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1015 if (lf_status==LF_BADP)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1016 { *lk0 = -1.0e300;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1017 return(NR_REDUCE);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1018 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1019 if (lf_status!=LF_OK) return(NR_BREAK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1020 if (lf_debug>2) prresp(coef,A,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1021
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1022 den_des->xtwx.p = p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1023 rstat = NR_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1024 switch(link(den_sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1025 { case LLOG:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1026 r = den_des->ss[0]/A[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1027 coef[0] += log(r);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1028 multmatscal(A,r,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1029 A[0] = den_des->ss[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1030 lk = -A[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1031 if (fabs(coef[0]) > 700)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1032 { lf_status = LF_OOB;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1033 rstat = NR_REDUCE;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1034 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1035 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1036 { lk += coef[i]*den_des->ss[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1037 f1[i] = den_des->ss[i]-A[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1038 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1039 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1040 case LIDENT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1041 lk = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1042 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1043 { f1[i] = den_des->ss[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1044 for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1045 den_des->res[i] -= A[i*p+j]*coef[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1046 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1047 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1048 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1049 *lk0 = den_des->llk = lk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1050
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1051 return(rstat);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1052 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1053
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1054 int inre(x,bound,d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1055 double *x, *bound;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1056 int d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1057 { int i, z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1058 z = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1059 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1060 if (bound[i]<bound[i+d])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1061 z &= (x[i]>=bound[i]) & (x[i]<=bound[i+d]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1062 return(z);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1063 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1064
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1065 int setintlimits(lfd, x, h, ang, lset)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1066 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1067 int *ang, *lset;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1068 double *x, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1069 { int d, i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1070 d = lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1071 *ang = *lset = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1072 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1073 { if (lfd->sty[i]==STANGL)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1074 { ilim[i+d] = ((h<2) ? 2*asin(h/2) : PI)*lfd->sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1075 ilim[i] = -ilim[i+d];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1076 *ang = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1077 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1078 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1079 { ilim[i+d] = h*lfd->sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1080 ilim[i] = -ilim[i+d];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1081
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1082 if (lfd->sty[i]==STLEFT) { ilim[i+d] = 0; *lset = 1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1083 if (lfd->sty[i]==STRIGH) { ilim[i] = 0; *lset = 1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1084
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1085 if (lfd->xl[i]<lfd->xl[i+d]) /* user limits for this variable */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1086 { if (lfd->xl[i]-x[i]> ilim[i])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1087 { ilim[i] = lfd->xl[i]-x[i]; *lset=1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1088 if (lfd->xl[i+d]-x[i]< ilim[i+d])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1089 { ilim[i+d] = lfd->xl[i+d]-x[i]; *lset=1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1090 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1091 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1092 if (ilim[i]==ilim[i+d]) return(LF_DEMP); /* empty integration */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1093 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1094 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1095 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1096
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1097 int selectintmeth(itype,lset,ang)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1098 int itype, lset, ang;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1099 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1100 if (itype==IDEFA) /* select the default method */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1101 { if (fam(den_sp)==THAZ)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1102 { if (ang) return(IDEFA);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1103 return( IHAZD );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1104 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1105
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1106 if (ubas(den_sp)) return(IMULT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1107
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1108 if (ang) return(IMULT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1109
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1110 if (iscompact(ker(den_sp)))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1111 { if (kt(den_sp)==KPROD) return(IPROD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1112 if (lset)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1113 return( (den_lfd->d==1) ? IPROD : IMULT );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1114 if (deg(den_sp)<=1) return(IMLIN);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1115 if (den_lfd->d==1) return(IPROD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1116 return(IMULT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1117 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1118
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1119 if (ker(den_sp)==WGAUS)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1120 { if (lset) WARN(("Integration for Gaussian weights ignores limits"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1121 if ((den_lfd->d==1)|(kt(den_sp)==KPROD)) return(IPROD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1122 if (deg(den_sp)<=1) return(IMLIN);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1123 if (deg(den_sp)==2) return(IMULT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1124 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1125
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1126 return(IDEFA);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1127 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1128
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1129 /* user provided an integration method, check it is valid */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1130
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1131 if (fam(den_sp)==THAZ)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1132 { if (ang) return(INVLD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1133 if (!iscompact(ker(den_sp))) return(INVLD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1134 return( ((kt(den_sp)==KPROD) | (kt(den_sp)==KSPH)) ? IHAZD : INVLD );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1135 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1136
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1137 if ((ang) && (itype != IMULT)) return(INVLD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1138
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1139 switch(itype)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1140 { case IMULT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1141 if (ker(den_sp)==WGAUS) return(deg(den_sp)==2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1142 return( iscompact(ker(den_sp)) ? IMULT : INVLD );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1143 case IPROD: return( ((den_lfd->d==1) | (kt(den_sp)==KPROD)) ? IPROD : INVLD );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1144 case IMLIN: return( ((kt(den_sp)==KSPH) && (!lset) &&
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1145 (deg(den_sp)<=1)) ? IMLIN : INVLD );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1146 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1147
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1148 return(INVLD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1149 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1150
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1151 extern double lf_tol;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1152
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1153 int densinit(lfd,des,sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1154 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1155 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1156 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1157 { int p, i, ii, j, nnz, rnz, ang, lset, status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1158 double w, *cf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1159
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1160 den_lfd = lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1161 den_des = des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1162 den_sp = sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1163 cf = des->cf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1164
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1165 lf_tol = (link(sp)==LLOG) ? 1.0e-6 : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1166
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1167 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1168 ff = des->xtwx.wk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1169 cf[0] = NOSLN;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1170 for (i=1; i<p; i++) cf[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1171
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1172 if (!inre(des->xev,lfd->xl,lfd->d)) return(LF_XOOR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1173
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1174 status = setintlimits(lfd,des->xev,des->h,&ang,&lset);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1175 if (status != LF_OK) return(status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1176
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1177 switch(selectintmeth(de_itype,lset,ang))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1178 { case IMULT: des->itype = multint; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1179 case IPROD: des->itype = prodint; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1180 case IMLIN: des->itype = mlinint; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1181 case IHAZD: des->itype = hazint; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1182 case INVLD: LERR(("Invalid integration method %d",de_itype));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1183 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1184 case IDEFA: LERR(("No integration type available for this model"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1185 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1186 default: LERR(("densinit: unknown integral type"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1187 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1188
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1189 switch(deg(den_sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1190 { case 0: rnz = 1; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1191 case 1: rnz = 1; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1192 case 2: rnz = lfd->d+1; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1193 case 3: rnz = lfd->d+2; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1194 default: LERR(("densinit: invalid degree %d",deg(den_sp)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1195 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1196 if (lf_error) return(LF_ERR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1197
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1198 setzero(des->ss,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1199 nnz = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1200 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1201 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1202 if (!cens(lfd,ii))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1203 { w = wght(des,ii)*prwt(lfd,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1204 for (j=0; j<p; j++) des->ss[j] += d_xij(des,ii,j)*w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1205 if (wght(des,ii)>0.00001) nnz++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1206 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1207
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1208 if (fam(den_sp)==THAZ) haz_init(lfd,des,sp,ilim);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1209 /* this should really only be done once. Not sure how to enforce that,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1210 * esp. when locfit() has been called directly.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1211 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1212 if (fam(den_sp)==TDEN)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1213 des->smwt = (lfd->w==NULL) ? lfd->n : vecsum(lfd->w,lfd->n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1214
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1215 if (lf_debug>2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1216 { mut_printf(" LHS: ");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1217 for (i=0; i<p; i++) mut_printf(" %8.5f",des->ss[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1218 mut_printf("\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1219 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1220
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1221 switch(link(den_sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1222 { case LIDENT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1223 cf[0] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1224 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1225 case LLOG:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1226 if (nnz<rnz) { cf[0] = -1000; return(LF_DNOP); }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1227 cf[0] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1228 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1229 default:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1230 LERR(("unknown link in densinit"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1231 return(LF_ERR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1232 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1233 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1234 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1235 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1236 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1237 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1238
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1239 int bino_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1240 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1241 { return((link==LLOGIT) | (link==LIDENT) | (link==LASIN));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1242 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1243
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1244 int bino_fam(y,p,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1245 double y, p, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1246 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1247 { double wp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1248 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1249 { if (y<0) y = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1250 if (y>w) y = w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1251 res[ZDLL] = y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1252 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1253 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1254 wp = w*p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1255 if (link==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1256 { if ((p<=0) && (y>0)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1257 if ((p>=1) && (y<w)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1258 res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1259 if (y>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1260 { res[ZLIK] += y*log(wp/y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1261 res[ZDLL] += y/p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1262 res[ZDDLL]+= y/(p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1263 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1264 if (y<w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1265 { res[ZLIK] += (w-y)*log((w-wp)/(w-y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1266 res[ZDLL] -= (w-y)/(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1267 res[ZDDLL]+= (w-y)/SQR(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1268 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1269 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1270 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1271 if (link==LLOGIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1272 { if ((y<0) | (y>w)) /* goon observation; delete it */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1273 { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1274 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1275 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1276 res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1277 if (y>0) res[ZLIK] -= y*log(y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1278 if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1279 res[ZDLL] = (y-wp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1280 res[ZDDLL]= wp*(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1281 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1282 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1283 if (link==LASIN)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1284 { if ((p<=0) && (y>0)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1285 if ((p>=1) && (y<w)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1286 if ((th<0) | (th>PI/2)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1287 res[ZDLL] = res[ZDDLL] = res[ZLIK] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1288 if (y>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1289 { res[ZDLL] += 2*y*sqrt((1-p)/p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1290 res[ZLIK] += y*log(wp/y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1291 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1292 if (y<w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1293 { res[ZDLL] -= 2*(w-y)*sqrt(p/(1-p));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1294 res[ZLIK] += (w-y)*log((w-wp)/(w-y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1295 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1296 res[ZDDLL] = 4*w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1297 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1298 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1299 LERR(("link %d invalid for binomial family",link));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1300 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1301 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1302
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1303 int bino_check(sp,des,lfd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1304 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1305 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1306 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1307 { int i, ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1308 double t0, t1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1309
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1310 if (fabs(des->cf[0])>700) return(LF_OOB);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1311
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1312 /* check for separation.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1313 * this won't detect separation if there's boundary points with
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1314 * both 0 and 1 responses.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1315 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1316 t0 = -1e100; t1 = 1e100;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1317 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1318 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1319 if ((resp(lfd,ii)<prwt(lfd,ii)) && (fitv(des,ii) > t0)) t0 = fitv(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1320 if ((resp(lfd,ii)>0) && (fitv(des,ii) < t1)) t1 = fitv(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1321 if (t1 <= t0) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1322 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1323 mut_printf("separated %8.5f %8.5f\n",t0,t1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1324 return(LF_NSLN);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1325 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1326
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1327 void setfbino(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1328 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1329 { fam->deflink = LLOGIT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1330 fam->canlink = LLOGIT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1331 fam->vallink = bino_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1332 fam->family = bino_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1333 fam->pcheck = bino_check;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1334 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1335
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1336 int rbin_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1337 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1338 { return(link==LLOGIT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1339 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1340
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1341 int rbin_fam(y,p,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1342 double y, p, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1343 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1344 { double s2y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1345 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1346 { res[ZDLL] = y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1347 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1348 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1349 if ((y<0) | (y>w)) /* goon observation; delete it */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1350 { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1351 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1352 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1353 res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1354 if (y>0) res[ZLIK] -= y*log(y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1355 if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1356 res[ZDLL] = (y-w*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1357 res[ZDDLL]= w*p*(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1358 if (-res[ZLIK]>HUBERC*HUBERC/2.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1359 { s2y = sqrt(-2*res[ZLIK]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1360 res[ZLIK] = HUBERC*(HUBERC/2.0-s2y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1361 res[ZDLL] *= HUBERC/s2y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1362 res[ZDDLL] = HUBERC/s2y*(res[ZDDLL]-1/(s2y*s2y)*w*p*(1-p));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1363 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1364 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1365 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1366
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1367 void setfrbino(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1368 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1369 { fam->deflink = LLOGIT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1370 fam->canlink = LLOGIT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1371 fam->vallink = rbin_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1372 fam->family = rbin_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1373 fam->pcheck = bino_check;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1374 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1375 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1376 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1377 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1378 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1379
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1380 int circ_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1381 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1382 { return(link==LIDENT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1383 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1384
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1385 int circ_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1386 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1387 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1388 { if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1389 { res[ZDLL] = w*sin(y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1390 res[ZLIK] = w*cos(y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1391 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1392 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1393 res[ZDLL] = w*sin(y-mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1394 res[ZDDLL]= w*cos(y-mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1395 res[ZLIK] = res[ZDDLL]-w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1396 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1397 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1398
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1399 extern double lf_tol;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1400 int circ_init(lfd,des,sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1401 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1402 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1403 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1404 { int i, ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1405 double s0, s1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1406 s0 = s1 = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1407 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1408 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1409 s0 += wght(des,ii)*prwt(lfd,ii)*sin(resp(lfd,ii)-base(lfd,ii));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1410 s1 += wght(des,ii)*prwt(lfd,ii)*cos(resp(lfd,ii)-base(lfd,ii));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1411 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1412 des->cf[0] = atan2(s0,s1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1413 for (i=1; i<des->p; i++) des->cf[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1414 lf_tol = 1.0e-6;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1415 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1416 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1417
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1418
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1419 void setfcirc(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1420 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1421 { fam->deflink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1422 fam->canlink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1423 fam->vallink = circ_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1424 fam->family = circ_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1425 fam->initial = circ_init;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1426 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1427 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1428 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1429 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1430 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1431
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1432 int dens_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1433 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1434 { return((link==LIDENT) | (link==LLOG));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1435 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1436
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1437 int dens_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1438 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1439 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1440 { if (cens)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1441 res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1442 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1443 { res[ZLIK] = w*th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1444 res[ZDLL] = res[ZDDLL] = w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1445 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1446 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1447 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1448
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1449 void setfdensity(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1450 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1451 { fam->deflink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1452 fam->canlink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1453 fam->vallink = dens_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1454 fam->family = dens_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1455 fam->initial = densinit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1456 fam->like = likeden;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1457 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1458 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1459 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1460 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1461 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1462
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1463 int gamma_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1464 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1465 { return((link==LIDENT) | (link==LLOG) | (link==LINVER));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1466 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1467
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1468 int gamma_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1469 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1470 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1471 { double lb, pt, dg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1472 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1473 { res[ZDLL] = MAX(y,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1474 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1475 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1476 res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1477 if (w==0.0) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1478 if ((mean<=0) & (y>0)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1479 if (link==LIDENT) lb = 1/th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1480 if (link==LINVER) lb = th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1481 if (link==LLOG) lb = mut_exp(-th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1482 if (cens)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1483 { if (y<=0) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1484 pt = 1-igamma(lb*y,w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1485 dg = dgamma(lb*y,w,1.0,0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1486 res[ZLIK] = log(pt);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1487 res[ZDLL] = -y*dg/pt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1488 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1489 * res[ZDLL] = -y*dg/pt * dlb/dth.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1490 * res[ZDDLL] = y*dg/pt * (d2lb/dth2 + ((w-1)/lb-y)*(dlb/dth)^2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1491 * + res[ZDLL]^2.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1492 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1493 if (link==LLOG) /* lambda = exp(-theta) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1494 { res[ZDLL] *= -lb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1495 res[ZDDLL] = dg*y*lb*(w-lb*y)/pt + SQR(res[ZDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1496 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1497 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1498 if (link==LINVER) /* lambda = theta */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1499 { res[ZDLL] *= 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1500 res[ZDDLL] = dg*y*((w-1)*mean-y)/pt + SQR(res[ZDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1501 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1502 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1503 if (link==LIDENT) /* lambda = 1/theta */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1504 { res[ZDLL] *= -lb*lb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1505 res[ZDDLL] = dg*y*lb*lb*lb*(1+w-lb*y)/pt + SQR(res[ZDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1506 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1507 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1508 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1509 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1510 { if (y<0) WARN(("Negative Gamma observation"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1511 if (link==LLOG)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1512 { res[ZLIK] = -lb*y+w*(1-th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1513 if (y>0) res[ZLIK] += w*log(y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1514 res[ZDLL] = lb*y-w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1515 res[ZDDLL]= lb*y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1516 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1517 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1518 if (link==LINVER)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1519 { res[ZLIK] = -lb*y+w-w*log(mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1520 if (y>0) res[ZLIK] += w*log(y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1521 res[ZDLL] = -y+w*mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1522 res[ZDDLL]= w*mean*mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1523 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1524 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1525 if (link==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1526 { res[ZLIK] = -lb*y+w-w*log(mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1527 if (y>0) res[ZLIK] += w*log(y/w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1528 res[ZDLL] = lb*lb*(y-w*mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1529 res[ZDDLL]= lb*lb*lb*(2*y-w*mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1530 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1531 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1532 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1533 LERR(("link %d invalid for Gamma family",link));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1534 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1535 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1536
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1537 void setfgamma(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1538 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1539 { fam->deflink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1540 fam->canlink = LINVER;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1541 fam->vallink = gamma_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1542 fam->family = gamma_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1543 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1544 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1545 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1546 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1547 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1548
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1549 int gaus_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1550 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1551 { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1552 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1553
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1554 int gaus_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1555 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1556 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1557 { double z, pz, dp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1558 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1559 { res[ZDLL] = w*y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1560 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1561 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1562 z = y-mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1563 if (cens)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1564 { if (link!=LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1565 { LERR(("Link invalid for censored Gaussian family"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1566 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1567 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1568 pz = mut_pnorm(-z);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1569 dp = ((z>6) ? ptail(-z) : exp(-z*z/2)/pz)/2.5066283;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1570 res[ZLIK] = w*log(pz);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1571 res[ZDLL] = w*dp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1572 res[ZDDLL]= w*dp*(dp-z);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1573 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1574 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1575 res[ZLIK] = -w*z*z/2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1576 switch(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1577 { case LIDENT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1578 res[ZDLL] = w*z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1579 res[ZDDLL]= w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1580 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1581 case LLOG:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1582 res[ZDLL] = w*z*mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1583 res[ZDDLL]= w*mean*mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1584 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1585 case LLOGIT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1586 res[ZDLL] = w*z*mean*(1-mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1587 res[ZDDLL]= w*mean*mean*(1-mean)*(1-mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1588 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1589 default:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1590 LERR(("Invalid link for Gaussian family"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1591 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1592 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1593 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1594 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1595
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1596 int gaus_check(sp,des,lfd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1597 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1598 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1599 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1600 { int i, ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1601 if (fami(sp)->robust) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1602 if (link(sp)==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1603 { for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1604 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1605 if (cens(lfd,ii)) return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1606 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1607 return(LF_DONE);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1608 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1609 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1610 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1611
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1612 void setfgauss(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1613 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1614 { fam->deflink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1615 fam->canlink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1616 fam->vallink = gaus_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1617 fam->family = gaus_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1618 fam->pcheck = gaus_check;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1619 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1620 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1621 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1622 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1623 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1624
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1625 int geom_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1626 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1627 { return((link==LIDENT) | (link==LLOG));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1628 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1629
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1630 int geom_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1631 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1632 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1633 { double p, pt, dp, p1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1634 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1635 { res[ZDLL] = MAX(y,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1636 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1637 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1638 p = 1/(1+mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1639 if (cens) /* censored observation */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1640 { if (y<=0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1641 { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1642 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1643 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1644 p1 = (link==LIDENT) ? -p*p : -p*(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1645 pt = 1-ibeta(p,w,y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1646 dp = dbeta(p,w,y,0)/pt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1647 res[ZLIK] = log(pt);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1648 res[ZDLL] = -dp*p1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1649 res[ZDDLL] = dp*dp*p1*p1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1650 if (link==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1651 res[ZDDLL] += dp*p*p*p*(1+w*(1-p)-p*y)/(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1652 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1653 res[ZDDLL] += dp*p*(1-p)*(w*(1-p)-p*y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1654 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1655 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1656 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1657 { res[ZLIK] = (y+w)*log((y/w+1)/(mean+1));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1658 if (y>0) res[ZLIK] += y*log(w*mean/y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1659 if (link==LLOG)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1660 { res[ZDLL] = (y-w*mean)*p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1661 res[ZDDLL]= (y+w)*p*(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1662 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1663 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1664 if (link==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1665 { res[ZDLL] = (y-w*mean)/(mean*(1+mean));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1666 res[ZDDLL]= w/(mean*(1+mean));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1667 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1668 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1669 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1670 LERR(("link %d invalid for geometric family",link));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1671 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1672 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1673
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1674 void setfgeom(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1675 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1676 { fam->deflink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1677 fam->canlink = LIDENT; /* this isn't correct. I haven't prog. canon */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1678 fam->vallink = geom_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1679 fam->family = geom_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1680 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1681 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1682 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1683 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1684 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1685
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1686 #define HUBERC 2.0
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1687
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1688 double links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1689 int inllmix=0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1690
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1691 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1692 * lffamily("name") converts family names into a numeric value.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1693 * typical usage is fam(&lf->sp) = lffamily("gaussian");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1694 * Note that family can be preceded by q and/or r for quasi, robust.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1695 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1696 * link(&lf->sp) = lflink("log") does the same for the link function.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1697 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1698 #define NFAMILY 18
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1699 static char *famil[NFAMILY] =
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1700 { "density", "ate", "hazard", "gaussian", "binomial",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1701 "poisson", "gamma", "geometric", "circular", "obust", "huber",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1702 "weibull", "cauchy","probab", "logistic", "nbinomial",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1703 "vonmises", "quant" };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1704 static int fvals[NFAMILY] =
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1705 { TDEN, TRAT, THAZ, TGAUS, TLOGT,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1706 TPOIS, TGAMM, TGEOM, TCIRC, TROBT, TROBT,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1707 TWEIB, TCAUC, TPROB, TLOGT, TGEOM, TCIRC, TQUANT };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1708 int lffamily(z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1709 char *z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1710 { int quasi, robu, f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1711 quasi = robu = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1712 while ((z[0]=='q') | (z[0]=='r'))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1713 { quasi |= (z[0]=='q');
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1714 robu |= (z[0]=='r');
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1715 z++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1716 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1717 z[0] = tolower(z[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1718 f = pmatch(z,famil,fvals,NFAMILY,-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1719 if ((z[0]=='o') | (z[0]=='a')) robu = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1720 if (f==-1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1721 { WARN(("unknown family %s",z));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1722 f = TGAUS;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1723 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1724 if (quasi) f += 64;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1725 if (robu) f += 128;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1726 return(f);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1727 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1728
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1729 #define NLINKS 8
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1730 static char *ltype[NLINKS] = { "default", "canonical", "identity", "log",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1731 "logi", "inverse", "sqrt", "arcsin" };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1732 static int lvals[NLINKS] = { LDEFAU, LCANON, LIDENT, LLOG,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1733 LLOGIT, LINVER, LSQRT, LASIN };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1734 int lflink(char *z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1735 { int f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1736 if (z==NULL) return(LDEFAU);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1737 z[0] = tolower(z[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1738 f = pmatch(z, ltype, lvals, NLINKS, -1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1739 if (f==-1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1740 { WARN(("unknown link %s",z));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1741 f = LDEFAU;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1742 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1743 return(f);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1744 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1745
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1746 int defaultlink(link,fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1747 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1748 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1749 { if (link==LDEFAU) return(fam->deflink);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1750 if (link==LCANON) return(fam->canlink);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1751 return(link);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1752 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1753
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1754 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1755 void robustify(res,rs)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1756 double *res, rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1757 { double sc, z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1758 sc = rs*HUBERC;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1759 if (res[ZLIK] > -sc*sc/2) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1760 z = sqrt(-2*res[ZLIK]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1761 res[ZDDLL]= -sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1762 res[ZDLL]*= sc/z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1763 res[ZLIK] = sc*sc/2-sc*z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1764 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1765 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1766 void robustify(res,rs)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1767 double *res, rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1768 { double sc, z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1769 sc = rs*HUBERC;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1770 if (res[ZLIK] > -sc*sc/2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1771 { res[ZLIK] /= sc*sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1772 res[ZDLL] /= sc*sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1773 res[ZDDLL] /= sc*sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1774 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1775 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1776 z = sqrt(-2*res[ZLIK]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1777 res[ZDDLL]= (-sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z)/(sc*sc);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1778 res[ZDLL]*= 1.0/(z*sc);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1779 res[ZLIK] = 0.5-z/sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1780 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1781
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1782 double lf_link(y,lin)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1783 double y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1784 int lin;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1785 { switch(lin)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1786 { case LIDENT: return(y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1787 case LLOG: return(log(y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1788 case LLOGIT: return(logit(y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1789 case LINVER: return(1/y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1790 case LSQRT: return(sqrt(fabs(y)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1791 case LASIN: return(asin(sqrt(y)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1792 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1793 LERR(("link: unknown link %d",lin));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1794 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1795 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1796
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1797 double invlink(th,lin)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1798 double th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1799 int lin;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1800 { switch(lin)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1801 { case LIDENT: return(th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1802 case LLOG: return(mut_exp(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1803 case LLOGIT: return(expit(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1804 case LINVER: return(1/th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1805 case LSQRT: return(th*fabs(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1806 case LASIN: return(sin(th)*sin(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1807 case LINIT: return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1808 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1809 LERR(("invlink: unknown link %d",lin));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1810 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1811 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1812
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1813 /* the link and various related functions */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1814 int links(th,y,fam,link,res,c,w,rs)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1815 double th, y, *res, w, rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1816 int link, c;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1817 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1818 { double mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1819 int st;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1820
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1821 mean = res[ZMEAN] = invlink(th,link);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1822 if (lf_error) return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1823 links_rs = rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1824 /* mut_printf("links: rs %8.5f\n",rs); */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1825
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1826 st = fam->family(y,mean,th,link,res,c,w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1827
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1828 if (st!=LF_OK) return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1829 if (link==LINIT) return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1830 if (isrobust(fam)) robustify(res,rs);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1831 return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1832 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1833
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1834 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1835 stdlinks is a version of links when family, link, response e.t.c
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1836 all come from the standard places.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1837 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1838 int stdlinks(res,lfd,sp,i,th,rs)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1839 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1840 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1841 double th, rs, *res;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1842 int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1843 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1844 return(links(th,resp(lfd,i),fami(sp),link(sp),res,cens(lfd,i),prwt(lfd,i),rs));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1845 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1846
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1847 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1848 * functions used in variance, skewness, kurtosis calculations
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1849 * in scb corrections.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1850 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1851
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1852 double b2(th,tg,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1853 double th, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1854 int tg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1855 { double y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1856 switch(tg&63)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1857 { case TGAUS: return(w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1858 case TPOIS: return(w*mut_exp(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1859 case TLOGT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1860 y = expit(th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1861 return(w*y*(1-y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1862 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1863 LERR(("b2: invalid family %d",tg));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1864 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1865 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1866
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1867 double b3(th,tg,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1868 double th, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1869 int tg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1870 { double y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1871 switch(tg&63)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1872 { case TGAUS: return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1873 case TPOIS: return(w*mut_exp(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1874 case TLOGT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1875 y = expit(th);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1876 return(w*y*(1-y)*(1-2*y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1877 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1878 LERR(("b3: invalid family %d",tg));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1879 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1880 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1881
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1882 double b4(th,tg,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1883 double th, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1884 int tg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1885 { double y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1886 switch(tg&63)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1887 { case TGAUS: return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1888 case TPOIS: return(w*mut_exp(th));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1889 case TLOGT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1890 y = expit(th); y = y*(1-y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1891 return(w*y*(1-6*y));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1892 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1893 LERR(("b4: invalid family %d",tg));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1894 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1895 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1896
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1897 int def_check(sp,des,lfd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1898 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1899 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1900 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1901 { switch(link(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1902 { case LLOG: if (des->cf[0]>700) return(LF_OOB);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1903 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1904 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1905 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1906 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1907 extern void setfdensity(), setfgauss(), setfbino(), setfpoisson();
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1908 extern void setfgamma(), setfgeom(), setfcirc(), setfweibull();
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1909 extern void setfrbino(), setfrobust(), setfcauchy(), setfquant();
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1910
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1911 void setfamily(sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1912 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1913 { int tg, lnk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1914 family *f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1915
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1916 tg = fam(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1917 f = fami(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1918 f->quasi = tg&64;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1919 f->robust = tg&128;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1920 f->initial = reginit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1921 f->like = likereg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1922 f->pcheck = def_check;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1923
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1924 switch(tg&63)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1925 { case TDEN:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1926 case THAZ:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1927 case TRAT: setfdensity(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1928 case TGAUS: setfgauss(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1929 case TLOGT: setfbino(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1930 case TRBIN: setfrbino(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1931 case TPROB:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1932 case TPOIS: setfpoisson(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1933 case TGAMM: setfgamma(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1934 case TGEOM: setfgeom(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1935 case TWEIB: setfweibull(f);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1936 case TCIRC: setfcirc(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1937 case TROBT: setfrobust(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1938 case TCAUC: setfcauchy(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1939 case TQUANT: setfquant(f); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1940 default: LERR(("setfamily: unknown family %d",tg&63));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1941 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1942 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1943
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1944 lnk = defaultlink(link(sp),f);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1945 if (!f->vallink(lnk))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1946 { WARN(("setfamily: invalid link %d - revert to default",link(sp)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1947 link(sp) = f->deflink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1948 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1949 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1950 link(sp) = lnk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1951 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1952 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1953 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1954 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1955 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1956
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1957 int pois_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1958 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1959 { return((link==LLOG) | (link==LIDENT) | (link==LSQRT));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1960 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1961
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1962 int pois_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1963 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1964 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1965 { double wmu, pt, dp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1966 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1967 { res[ZDLL] = MAX(y,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1968 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1969 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1970 wmu = w*mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1971 if (inllmix) y = w*y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1972 if (cens)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1973 { if (y<=0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1974 { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1975 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1976 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1977 pt = igamma(wmu,y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1978 dp = dgamma(wmu,y,1.0,0)/pt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1979 res[ZLIK] = log(pt);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1980 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1981 * res[ZDLL] = dp * w*dmu/dth
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1982 * res[ZDDLL]= -dp*(w*d2mu/dth2 + (y-1)/mu*(dmu/dth)^2) + res[ZDLL]^2
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1983 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1984 if (link==LLOG)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1985 { res[ZDLL] = dp*wmu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1986 res[ZDDLL]= -dp*wmu*(y-wmu) + SQR(res[ZDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1987 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1988 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1989 if (link==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1990 { res[ZDLL] = dp*w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1991 res[ZDDLL]= -dp*(y-1-wmu)*w/mean + SQR(res[ZDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1992 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1993 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1994 if (link==LSQRT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1995 { res[ZDLL] = dp*2*w*th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1996 res[ZDDLL]= -dp*w*(4*y-2-4*wmu) + SQR(res[ZDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1997 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1998 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
1999 if (link==LLOG)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2000 { if (y<0) /* goon observation - delete it */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2001 { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2002 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2003 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2004 res[ZLIK] = res[ZDLL] = y-wmu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2005 if (y>0) res[ZLIK] += y*(th-log(y/w));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2006 res[ZDDLL] = wmu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2007 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2008 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2009 if (link==LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2010 { if ((mean<=0) && (y>0)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2011 res[ZLIK] = y-wmu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2012 res[ZDLL] = -w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2013 res[ZDDLL] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2014 if (y>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2015 { res[ZLIK] += y*log(wmu/y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2016 res[ZDLL] += y/mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2017 res[ZDDLL]= y/(mean*mean);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2018 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2019 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2020 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2021 if (link==LSQRT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2022 { if ((mean<=0) && (y>0)) return(LF_BADP);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2023 res[ZLIK] = y-wmu;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2024 res[ZDLL] = -2*w*th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2025 res[ZDDLL]= 2*w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2026 if (y>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2027 { res[ZLIK] += y*log(wmu/y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2028 res[ZDLL] += 2*y/th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2029 res[ZDDLL]+= 2*y/mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2030 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2031 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2032 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2033 LERR(("link %d invalid for Poisson family",link));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2034 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2035 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2036
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2037 void setfpoisson(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2038 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2039 { fam->deflink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2040 fam->canlink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2041 fam->vallink = pois_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2042 fam->family = pois_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2043 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2044 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2045 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2046 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2047 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2048
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2049 #define QTOL 1.0e-10
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2050 extern int lf_status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2051 static double q0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2052
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2053 int quant_vallink(int link) { return(1); }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2054
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2055 int quant_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2056 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2057 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2058 { double z, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2059 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2060 { res[ZDLL] = w*y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2061 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2062 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2063 p = 0.5; /* should be pen(sp) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2064 z = y-mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2065 res[ZLIK] = (z<0) ? (w*z/p) : (-w*z/(1-p));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2066 res[ZDLL] = (z<0) ? -w/p : w/(1-p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2067 res[ZDDLL]= w/(p*(1-p));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2068 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2069 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2070
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2071 int quant_check(sp,des,lfd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2072 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2073 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2074 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2075 { return(LF_DONE);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2076 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2077
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2078 void setfquant(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2079 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2080 { fam->deflink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2081 fam->canlink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2082 fam->vallink = quant_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2083 fam->family = quant_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2084 fam->pcheck = quant_check;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2085 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2086
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2087 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2088 * cycling rule for choosing among ties.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2089 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2090 int tiecycle(ind,i0,i1,oi)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2091 int *ind, i0, i1, oi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2092 { int i, ii, im;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2093 im = ind[i0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2094 for (i=i0+1; i<=i1; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2095 { ii = ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2096 if (im<=oi)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2097 { if ((ii<im) | (ii>oi)) im = ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2098 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2099 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2100 { if ((ii<im) & (ii>oi)) im = ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2101 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2102 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2103 return(im);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2104 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2105
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2106 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2107 * move coefficient vector cf, as far as possible, in direction dc.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2108 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2109 int movecoef(lfd,des,p,cf,dc,oi)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2110 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2111 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2112 double p, *cf, *dc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2113 int oi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2114 { int i, ii, im, i0, i1, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2115 double *lb, *el, e, sp, sn, sw, sum1, sum2, tol1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2116
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2117 lb = des->th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2118 el = des->res;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2119 sum1 = sum2 = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2120
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2121 sp = sn = sw = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2122 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2123 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2124 lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2125 e = resp(lfd,ii) - innerprod(cf,d_xi(des,ii),des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2126 el[ii] = (fabs(lb[ii])<QTOL) ? 1e100 : e/lb[ii];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2127 if (lb[ii]>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2128 sp += prwt(lfd,ii)*wght(des,ii)*lb[ii];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2129 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2130 sn -= prwt(lfd,ii)*wght(des,ii)*lb[ii];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2131 sw += prwt(lfd,ii)*wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2132 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2133 printf("sp %8.5f sn %8.5f\n",sn,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2134 /* if sn, sp are both zero, should return an LF_PF.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2135 * but within numerical tolerance? what does it mean?
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2136 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2137 if (sn+sp <= QTOL*q0) { lf_status = LF_PF; return(0); }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2138
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2139 sum1 = sp/(1-p) + sn/p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2140 tol1 = QTOL*(sp+sn);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2141 mut_order(el,des->ind,0,des->n-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2142
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2143 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2144 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2145 sum2 += prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/p : -lb[ii]/(1-p) );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2146 sum1 -= prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/(1-p) : -lb[ii]/p );
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2147 if (sum1<=sum2+tol1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2148 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2149 /* determine the range of ties [i0,i1]
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2150 * el[ind[i0..i1]] = el[ind[i]].
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2151 * if sum1==sum2, el[ind[i+1]]..el[ind[i1]]] = el[ind[i1]], else i1 = i.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2152 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2153 i0 = i1 = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2154 while ((i0>0) && (el[des->ind[i0-1]]==el[ii])) i0--;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2155 while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[ii])) i1++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2156 if (sum1>=sum2-tol1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2157 while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[des->ind[i+1]])) i1++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2158
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2159 if (i0<i1) ii = tiecycle(des->ind,i0,i1,oi);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2160 for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2161 return(ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2162 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2163 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2164 mut_printf("Big finddlt problem.\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2165 ii = des->ind[des->n-1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2166 for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2167 return(ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2168 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2169
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2170 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2171 * special version of movecoef for min/max.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2172 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2173 int movemin(lfd,des,f,cf,dc,oi)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2174 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2175 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2176 double *cf, *dc, f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2177 int oi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2178 { int i, ii, im, p, s, ssum;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2179 double *lb, sum, lb0, lb1, z0, z1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2180
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2181 lb = des->th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2182 s = (f<=0.0) ? 1 : -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2183
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2184 /* first, determine whether move should be in positive or negative direction */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2185 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2186 sum = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2187 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2188 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2189 lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2190 sum += prwt(lfd,ii)*wght(des,ii)*lb[ii];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2191 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2192 if (fabs(sum) <= QTOL*q0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2193 { lf_status = LF_PF;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2194 return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2195 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2196 ssum = (sum<=0.0) ? -1 : 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2197 if (ssum != s)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2198 for (i=0; i<p; i++) dc[i] = -dc[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2199
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2200 /* now, move positively. How far can we move? */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2201 lb0 = 1.0e100; im = oi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2202 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2203 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2204 lb[ii] = innerprod(dc,d_xi(des,ii),des->p); /* must recompute - signs! */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2205 if (s*lb[ii]>QTOL) /* should have scale-free tolerance here */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2206 { z0 = innerprod(cf,d_xi(des,ii),p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2207 lb1 = (resp(lfd,ii) - z0)/lb[ii];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2208 if (lb1<lb0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2209 { if (fabs(lb1-lb0)<QTOL) /* cycle */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2210 { if (im<=oi)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2211 { if ((ii>oi) | (ii<im)) im = ii; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2212 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2213 { if ((ii>oi) & (ii<im)) im = ii; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2214 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2215 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2216 { im = ii; lb0 = lb1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2217 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2218 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2219 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2220
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2221 for (i=0; i<p; i++) cf[i] = cf[i]+lb0*dc[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2222 if (im==-1) lf_status = LF_PF;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2223 return(im);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2224 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2225
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2226 double qll(lfd,spr,des,cf)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2227 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2228 smpar *spr;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2229 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2230 double *cf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2231 { int i, ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2232 double th, sp, sn, p, e;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2233
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2234 p = pen(spr);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2235 sp = sn = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2236 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2237 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2238 th = innerprod(d_xi(des,ii),cf,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2239 e = resp(lfd,ii)-th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2240 if (e<0) sn -= prwt(lfd,ii)*wght(des,ii)*e;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2241 if (e>0) sp += prwt(lfd,ii)*wght(des,ii)*e;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2242 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2243 if (p<=0.0) return((sn<QTOL) ? -sp : -1e300);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2244 if (p>=1.0) return((sp<QTOL) ? -sn : -1e300);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2245 return(-sp/(1-p)-sn/p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2246 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2247
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2248 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2249 * running quantile smoother.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2250 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2251 void lfquantile(lfd,sp,des,maxit)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2252 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2253 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2254 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2255 int maxit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2256 { int i, ii, im, j, k, p, *ci, (*mover)();
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2257 double *cf, *db, *dc, *cm, f, q1, q2, l0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2258
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2259 printf("in lfquantile\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2260 f = pen(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2261 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2262 cf = des->cf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2263 dc = des->oc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2264 db = des->ss;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2265 setzero(cf,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2266 setzero(dc,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2267 cm = des->V;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2268 setzero(cm,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2269 ci = (int *)des->fix;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2270
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2271 q1 = -qll(lfd,sp,des,cf);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2272 if (q1==0.0) { lf_status = LF_PF; return; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2273 for (i=0; i<p; i++) cm[i*(p+1)] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2274 mover = movecoef;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2275 if ((f<=0.0) | (f>=1.0)) mover = movemin;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2276
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2277 dc[0] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2278 im = mover(lfd,des,f,cf,dc,-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2279 if (lf_status != LF_OK) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2280 ci[0] = im;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2281 printf("init const %2d\n",ci[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2282 q0 = -qll(lfd,sp,des,cf);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2283 if (q0<QTOL*q1) { lf_status = LF_PF; return; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2284
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2285 printf("loop 0\n"); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2286 for (i=1; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2287 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2288 printf("i %2d\n",i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2289 memcpy(&cm[(i-1)*p],d_xi(des,im),p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2290 setzero(db,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2291 db[i] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2292 resproj(db,cm,dc,p,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2293 printf("call mover\n"); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2294 im = mover(lfd,des,f,cf,dc,-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2295 if (lf_status != LF_OK) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2296 printf("mover %2d\n",im); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2297 ci[i] = im;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2298 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2299 printf("call qll\n"); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2300 q1 = qll(lfd,sp,des,cf);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2301
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2302 printf("loop 1 %d %d %d %d\n",ci[0],ci[1],ci[2],ci[3]); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2303 for (k=0; k<maxit; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2304 { for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2305 { for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2306 if (j!=i) memcpy(&cm[(j-(j>i))*p],d_xi(des,ci[j]),p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2307 memcpy(db,d_xi(des,ci[i]),p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2308 resproj(db,cm,dc,p,p-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2309 printf("call mover\n"); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2310 im = mover(lfd,des,f,cf,dc,ci[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2311 if (lf_status != LF_OK) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2312 printf("mover %2d\n",im); fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2313 ci[i] = im;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2314 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2315 q2 = qll(lfd,sp,des,cf);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2316 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2317 * convergence: require no change -- reasonable, since discrete?
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2318 * remember we're maximizing, and q's are negative.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2319 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2320 if (q2 <= q1) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2321 q1 = q2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2322 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2323 printf("loop 2\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2324 mut_printf("Warning: lfquantile not converged.\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2325 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2326 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2327 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2328 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2329 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2330
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2331 extern double links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2332
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2333 int robust_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2334 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2335 { return(link==LIDENT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2336 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2337
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2338 int robust_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2339 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2340 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2341 { double z, sw;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2342 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2343 { res[ZDLL] = w*y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2344 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2345 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2346 sw = (w==1.0) ? 1.0 : sqrt(w); /* don't want unnecess. sqrt! */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2347 z = sw*(y-mean)/links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2348 res[ZLIK] = (fabs(z)<HUBERC) ? -z*z/2 : HUBERC*(HUBERC/2.0-fabs(z));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2349 if (z< -HUBERC)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2350 { res[ZDLL] = -sw*HUBERC/links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2351 res[ZDDLL]= 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2352 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2353 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2354 if (z> HUBERC)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2355 { res[ZDLL] = sw*HUBERC/links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2356 res[ZDDLL]= 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2357 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2358 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2359 res[ZDLL] = sw*z/links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2360 res[ZDDLL] = w/(links_rs*links_rs);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2361 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2362 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2363
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2364 int cauchy_fam(y,p,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2365 double y, p, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2366 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2367 { double z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2368 if (link!=LIDENT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2369 { LERR(("Invalid link in famcauc"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2370 return(LF_LNK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2371 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2372 z = w*(y-th)/links_rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2373 res[ZLIK] = -log(1+z*z);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2374 res[ZDLL] = 2*w*z/(links_rs*(1+z*z));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2375 res[ZDDLL] = 2*w*w*(1-z*z)/(links_rs*links_rs*(1+z*z)*(1+z*z));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2376 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2377 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2378
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2379 extern double lf_tol;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2380 int robust_init(lfd,des,sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2381 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2382 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2383 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2384 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2385 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2386 des->res[i] = resp(lfd,(int)des->ind[i]) - base(lfd,(int)des->ind[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2387 des->cf[0] = median(des->res,des->n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2388 for (i=1; i<des->p; i++) des->cf[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2389 lf_tol = 1.0e-6;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2390 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2391 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2392
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2393 void setfrobust(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2394 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2395 { fam->deflink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2396 fam->canlink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2397 fam->vallink = robust_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2398 fam->family = robust_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2399 fam->initial = robust_init;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2400 fam->robust = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2401 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2402
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2403 void setfcauchy(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2404 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2405 { fam->deflink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2406 fam->canlink = LIDENT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2407 fam->vallink = robust_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2408 fam->family = cauchy_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2409 fam->initial = robust_init;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2410 fam->robust = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2411 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2412 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2413 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2414 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2415 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2416
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2417 int weibull_vallink(link)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2418 int link;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2419 { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2420 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2421
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2422 int weibull_fam(y,mean,th,link,res,cens,w)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2423 double y, mean, th, *res, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2424 int link, cens;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2425 { double yy;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2426 yy = pow(y,w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2427 if (link==LINIT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2428 { res[ZDLL] = MAX(yy,0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2429 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2430 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2431 if (cens)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2432 { res[ZLIK] = -yy/mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2433 res[ZDLL] = res[ZDDLL] = yy/mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2434 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2435 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2436 res[ZLIK] = 1-yy/mean-th;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2437 if (yy>0) res[ZLIK] += log(w*yy);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2438 res[ZDLL] = -1+yy/mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2439 res[ZDDLL]= yy/mean;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2440 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2441 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2442
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2443 void setfweibull(fam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2444 family *fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2445 { fam->deflink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2446 fam->canlink = LLOG;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2447 fam->vallink = weibull_vallink;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2448 fam->family = weibull_fam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2449 fam->robust = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2450 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2451 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2452 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2453 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2454 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2455 Functions implementing the adaptive bandwidth selection.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2456 Will make the final call to nbhd() to set smoothing weights
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2457 for selected bandwidth, But will **not** make the
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2458 final call to locfit().
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2459 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2460
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2461 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2462
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2463 static double hmin;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2464
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2465 #define NACRI 5
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2466 static char *atype[NACRI] = { "none", "cp", "ici", "mindex", "ok" };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2467 static int avals[NACRI] = { ANONE, ACP, AKAT, AMDI, AOK };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2468 int lfacri(char *z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2469 { return(pmatch(z, atype, avals, NACRI, ANONE));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2470 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2471
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2472 double adcri(lk,t0,t2,pen)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2473 double lk, t0, t2, pen;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2474 { double y;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2475 /* return(-2*lk/(t0*exp(pen*log(1-t2/t0)))); */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2476 /* return((-2*lk+pen*t2)/t0); */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2477 y = (MAX(-2*lk,t0-t2)+pen*t2)/t0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2478 return(y);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2479 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2480
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2481 double mmse(lfd,sp,dv,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2482 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2483 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2484 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2485 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2486 { int i, ii, j, p, p1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2487 double sv, sb, *l, dp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2488
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2489 l = des->wd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2490 wdiag(lfd, sp, des,l,dv,0,1,0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2491 sv = sb = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2492 p = npar(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2493 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2494 { sv += l[i]*l[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2495 ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2496 dp = dist(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2497 for (j=0; j<deg(sp); j++) dp *= dist(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2498 sb += fabs(l[i])*dp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2499 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2500 p1 = factorial(deg(sp)+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2501 printf("%8.5f sv %8.5f sb %8.5f %8.5f\n",des->h,sv,sb,sv+sb*sb*pen(sp)*pen(sp)/(p1*p1));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2502 return(sv+sb*sb*pen(sp)*pen(sp)/(p1*p1));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2503 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2504
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2505 static double mcp, clo, cup;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2506
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2507 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2508 Initial bandwidth will be (by default)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2509 k-nearest neighbors for k small, just large enough to
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2510 get defined estimate (unless user provided nonzero nn or fix-h components)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2511 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2512
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2513 int ainitband(lfd,sp,dv,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2514 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2515 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2516 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2517 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2518 { int lf_status, p, z, cri, noit, redo;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2519 double ho, t[6];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2520
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2521 if (lf_debug >= 2) mut_printf("ainitband:\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2522 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2523 cri = acri(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2524 noit = (cri!=AOK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2525 z = (int)(lfd->n*nn(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2526 if ((noit) && (z<p+2)) z = p+2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2527 redo = 0; ho = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2528 do
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2529 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2530 nbhd(lfd,des,z,redo,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2531 if (z<des->n) z = des->n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2532 if (des->h>ho) lf_status = locfit(lfd,des,sp,noit,0,0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2533 z++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2534 redo = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2535 } while ((z<=lfd->n) && ((des->h==0)||(lf_status!=LF_OK)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2536 hmin = des->h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2537
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2538 switch(cri)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2539 { case ACP:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2540 local_df(lfd,sp,des,t);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2541 mcp = adcri(des->llk,t[0],t[2],pen(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2542 return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2543 case AKAT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2544 local_df(lfd,sp,des,t);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2545 clo = des->cf[0]-pen(sp)*t[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2546 cup = des->cf[0]+pen(sp)*t[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2547 return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2548 case AMDI:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2549 mcp = mmse(lfd,sp,dv,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2550 return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2551 case AOK: return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2552 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2553 LERR(("aband1: unknown criterion"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2554 return(LF_ERR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2555 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2556
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2557 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2558 aband2 increases the initial bandwidth until lack of fit results,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2559 or the fit is close to a global fit. Increase h by 1+0.3/d at
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2560 each iteration.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2561 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2562
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2563 double aband2(lfd,sp,dv,des,h0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2564 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2565 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2566 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2567 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2568 double h0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2569 { double t[6], h1, nu1, cp, ncp, tlo, tup;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2570 int d, inc, n, p, done;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2571
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2572 if (lf_debug >= 2) mut_printf("aband2:\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2573 d = lfd->d; n = lfd->n; p = npar(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2574 h1 = des->h = h0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2575 done = 0; nu1 = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2576 inc = 0; ncp = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2577 while ((!done) & (nu1<(n-p)*0.95))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2578 { fixh(sp) = (1+0.3/d)*des->h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2579 nbhd(lfd,des,0,1,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2580 if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband2: failed fit"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2581 local_df(lfd,sp,des,t);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2582 nu1 = t[0]-t[2]; /* tr(A) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2583 switch(acri(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2584 { case AKAT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2585 tlo = des->cf[0]-pen(sp)*t[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2586 tup = des->cf[0]+pen(sp)*t[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2587 /* mut_printf("h %8.5f tlo %8.5f tup %8.5f\n",des->h,tlo,tup); */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2588 done = ((tlo>cup) | (tup<clo));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2589 if (!done)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2590 { clo = MAX(clo,tlo);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2591 cup = MIN(cup,tup);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2592 h1 = des->h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2593 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2594 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2595 case ACP:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2596 cp = adcri(des->llk,t[0],t[2],pen(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2597 /* mut_printf("h %8.5f lk %8.5f t0 %8.5f t2 %8.5f cp %8.5f\n",des->h,des->llk,t[0],t[2],cp); */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2598 if (cp<mcp) { mcp = cp; h1 = des->h; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2599 if (cp>=ncp) inc++; else inc = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2600 ncp = cp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2601 done = (inc>=10) | ((inc>=3) & ((t[0]-t[2])>=10) & (cp>1.5*mcp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2602 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2603 case AMDI:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2604 cp = mmse(lfd,sp,dv,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2605 if (cp<mcp) { mcp = cp; h1 = des->h; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2606 if (cp>ncp) inc++; else inc = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2607 ncp = cp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2608 done = (inc>=3);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2609 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2610 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2611 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2612 return(h1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2613 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2614
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2615 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2616 aband3 does a finer search around best h so far. Try
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2617 h*(1-0.2/d), h/(1-0.1/d), h*(1+0.1/d), h*(1+0.2/d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2618 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2619 double aband3(lfd,sp,dv,des,h0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2620 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2621 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2622 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2623 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2624 double h0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2625 { double t[6], h1, cp, tlo, tup;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2626 int i, i0, d, n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2627
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2628 if (lf_debug >= 2) mut_printf("aband3:\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2629 d = lfd->d; n = lfd->n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2630 h1 = h0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2631 i0 = (acri(sp)==AKAT) ? 1 : -2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2632 if (h0==hmin) i0 = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2633
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2634 for (i=i0; i<=2; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2635 { if (i==0) i++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2636 fixh(sp) = h0*(1+0.1*i/d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2637 nbhd(lfd,des,0,1,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2638 if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband3: failed fit"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2639 local_df(lfd,sp,des,t);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2640 switch (acri(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2641 { case AKAT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2642 tlo = des->cf[0]-pen(sp)*t[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2643 tup = des->cf[0]+pen(sp)*t[5];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2644 if ((tlo>cup) | (tup<clo)) /* done */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2645 i = 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2646 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2647 { h1 = des->h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2648 clo = MAX(clo,tlo);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2649 cup = MIN(cup,tup);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2650 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2651 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2652 case ACP:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2653 cp = adcri(des->llk,t[0],t[2],pen(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2654 if (cp<mcp) { mcp = cp; h1 = des->h; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2655 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2656 { if (i>0) i = 2; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2657 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2658 case AMDI:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2659 cp = mmse(lfd,sp,dv,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2660 if (cp<mcp) { mcp = cp; h1 = des->h; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2661 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2662 { if (i>0) i = 2; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2663 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2664 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2665 return(h1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2666 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2667
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2668 int alocfit(lfd,sp,dv,des,cv)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2669 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2670 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2671 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2672 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2673 int cv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2674 { int lf_status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2675 double h0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2676
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2677 lf_status = ainitband(lfd,sp,dv,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2678 if (lf_error) return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2679 if (acri(sp) == AOK) return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2680
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2681 h0 = fixh(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2682 fixh(sp) = aband2(lfd,sp,dv,des,des->h);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2683 fixh(sp) = aband3(lfd,sp,dv,des,fixh(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2684 nbhd(lfd,des,0,1,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2685 lf_status = locfit(lfd,des,sp,0,0,cv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2686 fixh(sp) = h0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2687
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2688 return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2689 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2690 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2691 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2692 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2693 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2694 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2695 * Evaluate the locfit fitting functions.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2696 * calcp(sp,d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2697 * calculates the number of fitting functions.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2698 * makecfn(sp,des,dv,d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2699 * makes the coef.number vector.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2700 * fitfun(lfd, sp, x,t,f,dv)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2701 * lfd is the local fit structure.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2702 * sp smoothing parameter structure.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2703 * x is the data point.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2704 * t is the fitting point.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2705 * f is a vector to return the results.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2706 * dv derivative structure.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2707 * designmatrix(lfd, sp, des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2708 * is a wrapper for fitfun to build the design matrix.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2709 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2710 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2711
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2712 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2713
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2714 int calcp(sp,d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2715 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2716 int d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2717 { int i, k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2718
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2719 if (ubas(sp)) return(npar(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2720
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2721 switch (kt(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2722 { case KSPH:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2723 case KCE:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2724 k = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2725 for (i=1; i<=deg(sp); i++) k = k*(d+i)/i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2726 return(k);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2727 case KPROD: return(d*deg(sp)+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2728 case KLM: return(d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2729 case KZEON: return(1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2730 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2731 LERR(("calcp: invalid kt %d",kt(sp)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2732 return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2733 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2734
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2735 int coefnumber(dv,kt,d,deg)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2736 int kt, d, deg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2737 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2738 { int d0, d1, t;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2739
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2740 if (d==1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2741 { if (dv->nd<=deg) return(dv->nd);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2742 return(-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2743 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2744
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2745 if (dv->nd==0) return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2746 if (deg==0) return(-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2747 if (dv->nd==1) return(1+dv->deriv[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2748 if (deg==1) return(-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2749 if (kt==KPROD) return(-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2750
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2751 if (dv->nd==2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2752 { d0 = dv->deriv[0]; d1 = dv->deriv[1];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2753 if (d0<d1) { t = d0; d0 = d1; d1 = t; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2754 return((d+1)*(d0+1)-d0*(d0+3)/2+d1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2755 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2756 if (deg==2) return(-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2757
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2758 LERR(("coefnumber not programmed for nd>=3"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2759 return(-1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2760 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2761
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2762 void makecfn(sp,des,dv,d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2763 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2764 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2765 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2766 int d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2767 { int i, nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2768
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2769 nd = dv->nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2770
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2771 des->cfn[0] = coefnumber(dv,kt(sp),d,deg(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2772 des->ncoef = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2773 if (nd >= deg(sp)) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2774 if (kt(sp)==KZEON) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2775
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2776 if (d>1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2777 { if (nd>=2) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2778 if ((nd>=1) && (kt(sp)==KPROD)) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2779 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2780
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2781 dv->nd = nd+1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2782 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2783 { dv->deriv[nd] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2784 des->cfn[i+1] = coefnumber(dv,kt(sp),d,deg(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2785 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2786 dv->nd = nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2787
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2788 des->ncoef = 1+d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2789 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2790
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2791 void fitfunangl(dx,ff,sca,cd,deg)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2792 double dx, *ff, sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2793 int deg, cd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2794 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2795 if (deg>=3) WARN(("Can't handle angular model with deg>=3"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2796
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2797 switch(cd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2798 { case 0:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2799 ff[0] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2800 ff[1] = sin(dx/sca)*sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2801 ff[2] = (1-cos(dx/sca))*sca*sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2802 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2803 case 1:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2804 ff[0] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2805 ff[1] = cos(dx/sca);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2806 ff[2] = sin(dx/sca)*sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2807 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2808 case 2:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2809 ff[0] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2810 ff[1] = -sin(dx/sca)/sca;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2811 ff[2] = cos(dx/sca);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2812 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2813 default: WARN(("Can't handle angular model with >2 derivs"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2814 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2815 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2816
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2817 void fitfun(lfd,sp,x,t,f,dv)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2818 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2819 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2820 double *x, *t, *f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2821 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2822 { int d, deg, nd, m, i, j, k, ct_deriv[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2823 double ff[MXDIM][1+MXDEG], dx[MXDIM], *xx[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2824
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2825 if (ubas(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2826 { for (i=0; i<lfd->d; i++) xx[i] = &x[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2827 i = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2828 sp->vbasis(xx,t,1,lfd->d,1,npar(sp),f);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2829 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2830 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2831
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2832 d = lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2833 deg = deg(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2834 m = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2835 nd = (dv==NULL) ? 0 : dv->nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2836
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2837 if (kt(sp)==KZEON)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2838 { f[0] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2839 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2840 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2841
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2842 if (kt(sp)==KLM)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2843 { for (i=0; i<d; i++) f[m++] = x[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2844 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2845 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2846
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2847 f[m++] = (nd==0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2848 if (deg==0) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2849
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2850 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2851 { ct_deriv[i] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2852 dx[i] = (t==NULL) ? x[i] : x[i]-t[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2853 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2854 for (i=0; i<nd; i++) ct_deriv[dv->deriv[i]]++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2855
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2856 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2857 { switch(lfd->sty[i])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2858 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2859 case STANGL:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2860 fitfunangl(dx[i],ff[i],lfd->sca[i],ct_deriv[i],deg(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2861 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2862 default:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2863 for (j=0; j<ct_deriv[i]; j++) ff[i][j] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2864 ff[i][ct_deriv[i]] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2865 for (j=ct_deriv[i]+1; j<=deg; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2866 ff[i][j] = ff[i][j-1]*dx[i]/(j-ct_deriv[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2867 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2868 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2869
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2870 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2871 * Product kernels. Note that if ct_deriv[i] != nd, that implies
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2872 * there is differentiation wrt another variable, and all components
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2873 * involving x[i] are 0.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2874 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2875 if ((d==1) || (kt(sp)==KPROD))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2876 { for (j=1; j<=deg; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2877 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2878 f[m++] = (ct_deriv[i]==nd) ? ff[i][j] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2879 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2880 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2881
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2882 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2883 * Spherical kernels with the full polynomial basis.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2884 * Presently implemented up to deg=3.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2885 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2886 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2887 f[m++] = (ct_deriv[i]==nd) ? ff[i][1] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2888 if (deg==1) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2889
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2890 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2891 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2892 /* xi^2/2 terms. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2893 f[m++] = (ct_deriv[i]==nd) ? ff[i][2] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2894
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2895 /* xi xj terms */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2896 for (j=i+1; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2897 f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][1] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2898 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2899 if (deg==2) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2900
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2901 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2902 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2903 /* xi^3/6 terms */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2904 f[m++] = (ct_deriv[i]==nd) ? ff[i][3] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2905
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2906 /* xi^2/2 xk terms */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2907 for (k=i+1; k<d; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2908 f[m++] = (ct_deriv[i]+ct_deriv[k]==nd) ? ff[i][2]*ff[k][1] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2909
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2910 /* xi xj xk terms */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2911 for (j=i+1; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2912 { f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][2] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2913 for (k=j+1; k<d; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2914 f[m++] = (ct_deriv[i]+ct_deriv[j]+ct_deriv[k]==nd) ?
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2915 ff[i][1]*ff[j][1]*ff[k][1] : 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2916 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2917 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2918 if (deg==3) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2919
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2920 LERR(("fitfun: can't handle deg=%d for spherical kernels",deg));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2921 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2922
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2923 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2924 * Build the design matrix. Assumes des->ind contains the indices of
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2925 * the required data points; des->n the number of points; des->xev
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2926 * the fitting point.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2927 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2928 void designmatrix(lfd,sp,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2929 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2930 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2931 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2932 { int i, ii, j, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2933 double *X, u[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2934
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2935 X = d_x(des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2936 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2937
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2938 if (ubas(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2939 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2940 sp->vbasis(lfd->x,des->xev,lfd->n,lfd->d,des->n,p,X);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2941 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2942 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2943
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2944 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2945 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2946 for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2947 fitfun(lfd,sp,u,des->xev,&X[ii*p],NULL);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2948 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2949 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2950 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2951 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2952 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2953 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2954 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2955 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2956 * Functions for determining bandwidth; smoothing neighborhood
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2957 * and smoothing weights.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2958 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2959
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2960 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2961
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2962 double rho(x,sc,d,kt,sty) /* ||x|| for appropriate distance metric */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2963 double *x, *sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2964 int d, kt, *sty;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2965 { double rhoi[MXDIM], s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2966 int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2967 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2968 { if (sty!=NULL)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2969 { switch(sty[i])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2970 { case STANGL: rhoi[i] = 2*sin(x[i]/(2*sc[i])); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2971 case STCPAR: rhoi[i] = 0; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2972 default: rhoi[i] = x[i]/sc[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2973 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2974 else rhoi[i] = x[i]/sc[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2975 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2976
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2977 if (d==1) return(fabs(rhoi[0]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2978
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2979 s = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2980 if (kt==KPROD)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2981 { for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2982 { rhoi[i] = fabs(rhoi[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2983 if (rhoi[i]>s) s = rhoi[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2984 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2985 return(s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2986 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2987
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2988 if (kt==KSPH)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2989 { for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2990 s += rhoi[i]*rhoi[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2991 return(sqrt(s));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2992 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2993
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2994 LERR(("rho: invalid kt"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2995 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2996 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2997
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2998 double kordstat(x,k,n,ind)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
2999 double *x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3000 int k, n, *ind;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3001 { int i, i0, i1, l, r;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3002 double piv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3003 if (k<1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3004 i0 = 0; i1 = n-1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3005 while (1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3006 { piv = x[ind[(i0+i1)/2]];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3007 l = i0; r = i1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3008 while (l<=r)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3009 { while ((l<=i1) && (x[ind[l]]<=piv)) l++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3010 while ((r>=i0) && (x[ind[r]]>piv)) r--;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3011 if (l<=r) ISWAP(ind[l],ind[r]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3012 } /* now, x[ind[i0..r]] <= piv < x[ind[l..i1]] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3013 if (r<k-1) i0 = l; /* go right */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3014 else /* put pivots in middle */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3015 { for (i=i0; i<=r; )
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3016 if (x[ind[i]]==piv) { ISWAP(ind[i],ind[r]); r--; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3017 else i++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3018 if (r<k-1) return(piv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3019 i1 = r;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3020 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3021 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3022 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3023
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3024 /* check if i'th data point is in limits */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3025 int inlim(lfd,i)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3026 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3027 int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3028 { int d, j, k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3029 double *xlim;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3030
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3031 xlim = lfd->xl;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3032 d = lfd->d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3033 k = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3034 for (j=0; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3035 { if (xlim[j]<xlim[j+d])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3036 k &= ((datum(lfd,j,i)>=xlim[j]) & (datum(lfd,j,i)<=xlim[j+d]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3037 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3038 return(k);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3039 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3040
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3041 double compbandwid(di,ind,x,n,d,nn,fxh)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3042 double *di, *x, fxh;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3043 int n, d, nn, *ind;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3044 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3045 double nnh;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3046
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3047 if (nn==0) return(fxh);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3048
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3049 if (nn<n)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3050 nnh = kordstat(di,nn,n,ind);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3051 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3052 { nnh = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3053 for (i=0; i<n; i++) nnh = MAX(nnh,di[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3054 nnh = nnh*exp(log(1.0*nn/n)/d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3055 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3056 return(MAX(fxh,nnh));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3057 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3058
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3059 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3060 fast version of nbhd for ordered 1-d data
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3061 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3062 void nbhd1(lfd,sp,des,k)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3063 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3064 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3065 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3066 int k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3067 { double x, h, *xd, sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3068 int i, l, r, m, n, z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3069
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3070 n = lfd->n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3071 x = des->xev[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3072 xd = dvari(lfd,0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3073 sc = lfd->sca[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3074
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3075 /* find closest data point to x */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3076 if (x<=xd[0]) z = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3077 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3078 if (x>=xd[n-1]) z = n-1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3079 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3080 { l = 0; r = n-1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3081 while (r-l>1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3082 { z = (r+l)/2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3083 if (xd[z]>x) r = z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3084 else l = z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3085 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3086 /* now, xd[0..l] <= x < x[r..n-1] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3087 if ((x-xd[l])>(xd[r]-x)) z = r; else z = l;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3088 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3089 /* closest point to x is xd[z] */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3090
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3091 if (nn(sp)<0) /* user bandwidth */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3092 h = sp->vb(des->xev);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3093 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3094 { if (k>0) /* set h to nearest neighbor bandwidth */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3095 { l = r = z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3096 if (l==0) r = k-1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3097 if (r==n-1) l = n-k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3098 while (r-l<k-1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3099 { if ((x-xd[l-1])<(xd[r+1]-x)) l--; else r++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3100 if (l==0) r = k-1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3101 if (r==n-1) l = n-k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3102 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3103 h = x-xd[l];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3104 if (h<xd[r]-x) h = xd[r]-x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3105 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3106 else h = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3107 h /= sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3108 if (h<fixh(sp)) h = fixh(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3109 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3110
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3111 m = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3112 if (xd[z]>x) z--; /* so xd[z]<=x */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3113 /* look left */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3114 for (i=z; i>=0; i--) if (inlim(lfd,i))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3115 { dist(des,i) = (x-xd[i])/sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3116 wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3117 if (wght(des,i)>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3118 { des->ind[m] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3119 m++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3120 } else i = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3121 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3122 /* look right */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3123 for (i=z+1; i<n; i++) if (inlim(lfd,i))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3124 { dist(des,i) = (xd[i]-x)/sc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3125 wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3126 if (wght(des,i)>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3127 { des->ind[m] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3128 m++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3129 } else i = n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3130 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3131
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3132 des->n = m;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3133 des->h = h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3134 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3135
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3136 void nbhd_zeon(lfd,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3137 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3138 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3139 { int i, j, m, eq;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3140
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3141 m = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3142 for (i=0; i<lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3143 { eq = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3144 for (j=0; j<lfd->d; j++) eq = eq && (des->xev[j] == datum(lfd,j,i));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3145 if (eq)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3146 { wght(des,i) = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3147 des->ind[m] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3148 m++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3149 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3150 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3151 des->n = m;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3152 des->h = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3153 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3154
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3155 void nbhd(lfd,des,nn,redo,sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3156 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3157 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3158 int redo, nn;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3159 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3160 { int d, i, j, m, n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3161 double h, u[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3162
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3163 if (lf_debug>1) mut_printf("nbhd: nn %d fixh %8.5f\n",nn,fixh(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3164
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3165 d = lfd->d; n = lfd->n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3166
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3167 if (ker(sp)==WPARM)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3168 { for (i=0; i<n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3169 { wght(des,i) = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3170 des->ind[i] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3171 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3172 des->n = n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3173 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3174 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3175
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3176 if (kt(sp)==KZEON)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3177 { nbhd_zeon(lfd,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3178 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3179 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3180
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3181 if (kt(sp)==KCE)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3182 { des->h = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3183 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3184 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3185
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3186 /* ordered 1-dim; use fast searches */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3187 if ((nn<=n) & (lfd->ord) & (ker(sp)!=WMINM) & (lfd->sty[0]!=STANGL))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3188 { nbhd1(lfd,sp,des,nn);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3189 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3190 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3191
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3192 if (!redo)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3193 { for (i=0; i<n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3194 { for (j=0; j<d; j++) u[j] = datum(lfd,j,i)-des->xev[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3195 dist(des,i) = rho(u,lfd->sca,d,kt(sp),lfd->sty);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3196 des->ind[i] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3197 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3198 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3199 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3200 for (i=0; i<n; i++) des->ind[i] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3201
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3202 if (ker(sp)==WMINM)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3203 { des->h = minmax(lfd,des,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3204 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3205 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3206
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3207 if (nn<0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3208 h = sp->vb(des->xev);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3209 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3210 h = compbandwid(des->di,des->ind,des->xev,n,lfd->d,nn,fixh(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3211 m = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3212 for (i=0; i<n; i++) if (inlim(lfd,i))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3213 { for (j=0; j<d; j++) u[j] = datum(lfd,j,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3214 wght(des,i) = weight(lfd, sp, u, des->xev, h, 1, dist(des,i));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3215 if (wght(des,i)>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3216 { des->ind[m] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3217 m++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3218 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3219 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3220 des->n = m;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3221 des->h = h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3222 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3223 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3224 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3225 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3226 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3227 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3228 * This file includes functions to solve for the scale estimate in
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3229 * local robust regression and likelihood. The main entry point is
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3230 * lf_robust(lfd,sp,des,mxit),
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3231 * called from the locfit() function.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3232 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3233 * The update_rs(x) accepts a residual scale x as the argument (actually,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3234 * it works on the log-scale). The function computes the local fit
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3235 * assuming this residual scale, and re-estimates the scale from this
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3236 * new fit. The final solution satisfies the fixed point equation
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3237 * update_rs(x)=x. The function lf_robust() automatically calls
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3238 * update_rs() through the fixed point iterations.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3239 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3240 * The estimation of the scale from the fit is based on the sqrt of
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3241 * the median deviance of observations with non-zero weights (in the
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3242 * gaussian case, this is the median absolute residual).
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3243 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3244 * TODO:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3245 * Should use smoothing weights in the median.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3246 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3247
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3248 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3249
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3250 extern int lf_status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3251 double robscale;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3252
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3253 static lfdata *rob_lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3254 static smpar *rob_sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3255 static design *rob_des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3256 static int rob_mxit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3257
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3258 double median(x,n)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3259 double *x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3260 int n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3261 { int i, j, lt, eq, gt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3262 double lo, hi, s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3263 lo = hi = x[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3264 for (i=0; i<n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3265 { lo = MIN(lo,x[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3266 hi = MAX(hi,x[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3267 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3268 if (lo==hi) return(lo);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3269 lo -= (hi-lo);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3270 hi += (hi-lo);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3271 for (i=0; i<n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3272 { if ((x[i]>lo) & (x[i]<hi))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3273 { s = x[i]; lt = eq = gt = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3274 for (j=0; j<n; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3275 { lt += (x[j]<s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3276 eq += (x[j]==s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3277 gt += (x[j]>s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3278 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3279 if ((2*(lt+eq)>n) && (2*(gt+eq)>n)) return(s);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3280 if (2*(lt+eq)<=n) lo = s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3281 if (2*(gt+eq)<=n) hi = s;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3282 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3283 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3284 return((hi+lo)/2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3285 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3286
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3287 double nrobustscale(lfd,sp,des,rs)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3288 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3289 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3290 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3291 double rs;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3292 { int i, ii, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3293 double link[LLEN], sc, sd, sw, e;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3294 p = des->p; sc = sd = sw = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3295 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3296 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3297 fitv(des,ii) = base(lfd,ii)+innerprod(des->cf,d_xi(des,ii),p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3298 e = resp(lfd,ii)-fitv(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3299 stdlinks(link,lfd,sp,ii,fitv(des,ii),rs);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3300 sc += wght(des,ii)*e*link[ZDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3301 sd += wght(des,ii)*e*e*link[ZDDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3302 sw += wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3303 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3304
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3305 /* newton-raphson iteration for log(s)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3306 -psi(ei/s) - log(s); s = e^{-th}
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3307 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3308 rs *= exp((sc-sw)/(sd+sc));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3309 return(rs);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3310 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3311
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3312 double robustscale(lfd,sp,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3313 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3314 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3315 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3316 { int i, ii, p, fam, lin, or;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3317 double rs, link[LLEN];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3318 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3319 fam = fam(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3320 lin = link(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3321 or = fami(sp)->robust;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3322 fami(sp)->robust = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3323
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3324 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3325 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3326 fitv(des,ii) = base(lfd,ii) + innerprod(des->cf,d_xi(des,ii),p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3327 links(fitv(des,ii),resp(lfd,ii),fami(sp),lin,link,cens(lfd,ii),prwt(lfd,ii),1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3328 des->res[i] = -2*link[ZLIK];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3329 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3330 fami(sp)->robust = or;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3331 rs = sqrt(median(des->res,des->n));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3332
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3333 if (rs==0.0) rs = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3334 return(rs);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3335 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3336
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3337 double update_rs(x)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3338 double x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3339 { double nx;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3340 if (lf_status != LF_OK) return(x);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3341 robscale = exp(x);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3342 lfiter(rob_lfd,rob_sp,rob_des,rob_mxit);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3343 if (lf_status != LF_OK) return(x);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3344
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3345 nx = log(robustscale(rob_lfd,rob_sp,rob_des));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3346 if (nx<x-0.2) nx = x-0.2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3347 return(nx);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3348 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3349
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3350 void lf_robust(lfd,sp,des,mxit)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3351 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3352 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3353 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3354 int mxit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3355 { double x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3356 rob_lfd = lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3357 rob_des = des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3358 rob_sp = sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3359 rob_mxit = mxit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3360 lf_status = LF_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3361
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3362 x = log(robustscale(lfd,sp,des));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3363
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3364 solve_fp(update_rs, x, 1.0e-6, mxit);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3365 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3366 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3367 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3368 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3369 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3370 * Post-fitting functions to compute the local variance and
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3371 * influence functions. Also the local degrees of freedom
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3372 * calculations for adaptive smoothing.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3373 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3374
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3375 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3376
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3377 extern double robscale;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3378
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3379 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3380 vmat() computes (after the local fit..) the matrix
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3381 M2 = X^T W^2 V X.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3382 M12 = (X^T W V X)^{-1} M2
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3383 Also, for convenience, tr[0] = sum(wi) tr[1] = sum(wi^2).
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3384 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3385 void vmat(lfd, sp, des, M12, M2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3386 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3387 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3388 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3389 double *M12, *M2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3390 { int i, ii, p, nk, ok;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3391 double link[LLEN], h, ww, tr0, tr1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3392 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3393 setzero(M2,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3394
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3395 nk = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3396
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3397 /* for density estimation, use integral rather than
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3398 sum form, if W^2 is programmed...
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3399 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3400 if ((fam(sp)<=THAZ) && (link(sp)==LLOG))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3401 { switch(ker(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3402 { case WGAUS: nk = WGAUS; h = des->h/SQRT2; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3403 case WRECT: nk = WRECT; h = des->h; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3404 case WEPAN: nk = WBISQ; h = des->h; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3405 case WBISQ: nk = WQUQU; h = des->h; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3406 case WTCUB: nk = W6CUB; h = des->h; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3407 case WEXPL: nk = WEXPL; h = des->h/2; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3408 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3409 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3410
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3411 tr0 = tr1 = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3412 if (nk != -1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3413 { ok = ker(sp); ker(sp) = nk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3414 /* compute M2 using integration. Use M12 as work matrix. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3415 (des->itype)(des->xev, M2, M12, des->cf, h);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3416 ker(sp) = ok;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3417 if (fam(sp)==TDEN) multmatscal(M2,des->smwt,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3418 tr0 = des->ss[0];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3419 tr1 = M2[0]; /* n int W e^<a,A> */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3420 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3421 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3422 { for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3423 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3424 stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3425 ww = SQR(wght(des,ii))*link[ZDDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3426 tr0 += wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3427 tr1 += SQR(wght(des,ii));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3428 addouter(M2,d_xi(des,ii),d_xi(des,ii),p,ww);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3429 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3430 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3431 des->tr0 = tr0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3432 des->tr1 = tr1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3433
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3434 memcpy(M12,M2,p*p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3435 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3436 jacob_solve(&des->xtwx,&M12[i*p]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3437 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3438
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3439 void lf_vcov(lfd,sp,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3440 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3441 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3442 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3443 { int i, j, k, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3444 double *M12, *M2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3445 M12 = des->V; M2 = des->P; p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3446 vmat(lfd,sp,des,M12,M2); /* M2 = X^T W^2 V X tr0=sum(W) tr1=sum(W*W) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3447 des->tr2 = m_trace(M12,p); /* tr (XTWVX)^{-1}(XTW^2VX) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3448
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3449 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3450 * Covariance matrix is M1^{-1} * M2 * M1^{-1}
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3451 * We compute this using the cholesky decomposition of
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3452 * M2; premultiplying by M1^{-1} and squaring. This
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3453 * is more stable than direct computation in near-singular cases.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3454 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3455 chol_dec(M2,p,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3456 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3457 for (j=0; j<i; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3458 { M2[j*p+i] = M2[i*p+j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3459 M2[i*p+j] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3460 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3461 for (i=0; i<p; i++) jacob_solve(&des->xtwx,&M2[i*p]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3462 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3463 { for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3464 { M12[i*p+j] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3465 for (k=0; k<p; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3466 M12[i*p+j] += M2[k*p+i]*M2[k*p+j]; /* ith column of covariance */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3467 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3468 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3469 if ((fam(sp)==TDEN) && (link(sp)==LIDENT))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3470 multmatscal(M12,1/SQR(des->smwt),p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3471
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3472 /* this computes the influence function as des->f1[0]. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3473 unitvec(des->f1,0,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3474 jacob_solve(&des->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3475 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3476
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3477 /* local_df computes:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3478 * tr[0] = trace(W)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3479 * tr[1] = trace(W*W)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3480 * tr[2] = trace( M1^{-1} M2 )
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3481 * tr[3] = trace( M1^{-1} M3 )
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3482 * tr[4] = trace( (M1^{-1} M2)^2 )
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3483 * tr[5] = var(theta-hat).
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3484 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3485 void local_df(lfd,sp,des,tr)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3486 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3487 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3488 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3489 double *tr;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3490 { int i, ii, j, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3491 double *m2, *V, ww, link[LLEN];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3492
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3493 tr[0] = tr[1] = tr[2] = tr[3] = tr[4] = tr[5] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3494 m2 = des->V; V = des->P; p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3495
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3496 vmat(lfd,sp,des,m2,V); /* M = X^T W^2 V X tr0=sum(W) tr1=sum(W*W) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3497 tr[0] = des->tr0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3498 tr[1] = des->tr1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3499 tr[2] = m_trace(m2,p); /* tr (XTWVX)^{-1}(XTW^2VX) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3500
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3501 unitvec(des->f1,0,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3502 jacob_solve(&des->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3503 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3504 for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3505 { tr[4] += m2[i*p+j]*m2[j*p+i]; /* tr(M^2) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3506 tr[5] += des->f1[i]*V[i*p+j]*des->f1[j]; /* var(thetahat) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3507 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3508 tr[5] = sqrt(tr[5]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3509
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3510 setzero(m2,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3511 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3512 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3513 stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3514 ww = wght(des,ii)*wght(des,ii)*wght(des,ii)*link[ZDDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3515 addouter(m2,d_xi(des,ii),d_xi(des,ii),p,ww);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3516 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3517 for (i=0; i<p; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3518 { jacob_solve(&des->xtwx,&m2[i*p]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3519 tr[3] += m2[i*(p+1)];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3520 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3521
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3522 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3523 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3524 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3525 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3526 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3527 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3528 * Routines for computing weight diagrams.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3529 * wdiag(lf,des,lx,deg,ty,exp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3530 * Must locfit() first, unless ker==WPARM and has par. comp.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3531 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3532 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3533
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3534 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3535
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3536 static double *wd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3537 extern double robscale;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3538 void nnresproj(lfd,sp,des,u,m,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3539 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3540 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3541 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3542 double *u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3543 int m, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3544 { int i, ii, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3545 double link[LLEN];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3546 setzero(des->f1,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3547 for (j=0; j<m; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3548 { ii = des->ind[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3549 stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3550 for (i=0; i<p; i++) des->f1[i] += link[ZDDLL]*d_xij(des,j,ii)*u[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3551 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3552 jacob_solve(&des->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3553 for (i=0; i<m; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3554 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3555 u[i] -= innerprod(des->f1,d_xi(des,ii),p)*wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3556 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3557 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3558
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3559 void wdexpand(l,n,ind,m)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3560 double *l;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3561 int *ind, n, m;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3562 { int i, j, t;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3563 double z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3564 for (j=m; j<n; j++) { l[j] = 0.0; ind[j] = -1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3565 j = m-1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3566 while (j>=0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3567 { if (ind[j]==j) j--;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3568 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3569 { i = ind[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3570 z = l[j]; l[j] = l[i]; l[i] = z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3571 t = ind[j]; ind[j] = ind[i]; ind[i] = t;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3572 if (ind[j]==-1) j--;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3573 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3574 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3575
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3576 /* for (i=n-1; i>=0; i--)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3577 { l[i] = ((j>=0) && (ind[j]==i)) ? l[j--] : 0.0; } */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3578 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3579
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3580 int wdiagp(lfd,sp,des,lx,pc,dv,deg,ty,exp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3581 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3582 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3583 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3584 paramcomp *pc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3585 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3586 double *lx;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3587 int deg, ty, exp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3588 { int i, j, p, nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3589 double *l1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3590
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3591 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3592
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3593 fitfun(lfd,sp,des->xev,pc->xbar,des->f1,dv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3594 if (exp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3595 { jacob_solve(&pc->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3596 for (i=0; i<lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3597 lx[i] = innerprod(des->f1,d_xi(des,des->ind[i]),p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3598 return(lfd->n);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3599 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3600 jacob_hsolve(&pc->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3601 for (i=0; i<p; i++) lx[i] = des->f1[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3602
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3603 nd = dv->nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3604 dv->nd = nd+1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3605 if (deg>=1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3606 for (i=0; i<lfd->d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3607 { dv->deriv[nd] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3608 l1 = &lx[(i+1)*p];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3609 fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3610 jacob_hsolve(&pc->xtwx,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3611 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3612
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3613 dv->nd = nd+2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3614 if (deg>=2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3615 for (i=0; i<lfd->d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3616 { dv->deriv[nd] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3617 for (j=0; j<lfd->d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3618 { dv->deriv[nd+1] = j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3619 l1 = &lx[(i*lfd->d+j+lfd->d+1)*p];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3620 fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3621 jacob_hsolve(&pc->xtwx,l1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3622 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3623 dv->nd = nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3624 return(p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3625 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3626
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3627 int wdiag(lfd,sp,des,lx,dv,deg,ty,exp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3628 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3629 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3630 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3631 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3632 double *lx;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3633 int deg, ty, exp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3634 /* deg=0: l(x) only.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3635 deg=1: l(x), l'(x)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3636 deg=2: l(x), l'(x), l''(x)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3637 ty = 1: e1 (X^T WVX)^{-1} X^T W -- hat matrix
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3638 ty = 2: e1 (X^T WVX)^{-1} X^T WV^{1/2} -- scb's
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3639 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3640 { double w, *X, *lxd, *lxdd, wdd, wdw, *ulx, link[LLEN], h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3641 double dfx[MXDIM], hs[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3642 int i, ii, j, k, l, m, d, p, nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3643
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3644 h = des->h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3645 nd = dv->nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3646 wd = des->wd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3647 d = lfd->d; p = des->p; X = d_x(des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3648 ulx = des->res;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3649 m = des->n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3650 for (i=0; i<d; i++) hs[i] = h*lfd->sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3651 if (deg>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3652 { lxd = &lx[m];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3653 setzero(lxd,m*d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3654 if (deg>1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3655 { lxdd = &lxd[d*m];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3656 setzero(lxdd,m*d*d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3657 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3658
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3659 if (nd>0) fitfun(lfd,sp,des->xev,des->xev,des->f1,dv); /* c(0) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3660 else unitvec(des->f1,0,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3661 jacob_solve(&des->xtwx,des->f1); /* c(0) (X^TWX)^{-1} */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3662 for (i=0; i<m; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3663 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3664 lx[i] = innerprod(des->f1,&X[ii*p],p); /* c(0)(XTWX)^{-1}X^T */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3665 if (deg>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3666 { wd[i] = Wd(dist(des,ii)/h,ker(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3667 for (j=0; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3668 { dfx[j] = datum(lfd,j,ii)-des->xev[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3669 lxd[j*m+i] = lx[i]*wght(des,ii)*weightd(dfx[j],lfd->sca[j],
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3670 d,ker(sp),kt(sp),h,lfd->sty[j],dist(des,ii));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3671 /* c(0) (XTWX)^{-1}XTW' */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3672 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3673 if (deg>1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3674 { wdd = Wdd(dist(des,ii)/h,ker(sp));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3675 for (j=0; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3676 for (k=0; k<d; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3677 { w = (dist(des,ii)==0) ? 0 : h/dist(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3678 w = wdd * (des->xev[k]-datum(lfd,k,ii)) * (des->xev[j]-datum(lfd,j,ii))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3679 * w*w / (hs[k]*hs[k]*hs[j]*hs[j]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3680 if (j==k) w += wd[i]/(hs[j]*hs[j]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3681 lxdd[(j*d+k)*m+i] = lx[i]*w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3682 /* c(0)(XTWX)^{-1}XTW'' */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3683 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3684 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3685 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3686 lx[i] *= wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3687 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3688
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3689 dv->nd = nd+1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3690 if (deg==2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3691 { for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3692 { dv->deriv[nd] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3693 fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3694 for (k=0; k<m; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3695 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3696 stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3697 for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3698 des->f1[j] -= link[ZDDLL]*lxd[i*m+k]*X[ii*p+j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3699 /* c'(x)-c(x)(XTWX)^{-1}XTW'X */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3700 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3701 jacob_solve(&des->xtwx,des->f1); /* (...)(XTWX)^{-1} */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3702 for (j=0; j<m; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3703 { ii = des->ind[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3704 ulx[j] = innerprod(des->f1,&X[ii*p],p); /* (...)XT */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3705 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3706 for (j=0; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3707 for (k=0; k<m; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3708 { ii = des->ind[k];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3709 dfx[j] = datum(lfd,j,ii)-des->xev[j];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3710 wdw = wght(des,ii)*weightd(dfx[j],lfd->sca[j],d,ker(sp),
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3711 kt(sp),h,lfd->sty[j],dist(des,ii));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3712 lxdd[(i*d+j)*m+k] += ulx[k]*wdw;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3713 lxdd[(j*d+i)*m+k] += ulx[k]*wdw;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3714 } /* + 2(c'-c(XTWX)^{-1}XTW'X)(XTWX)^{-1}XTW' */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3715 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3716 for (j=0; j<d*d; j++) nnresproj(lfd,sp,des,&lxdd[j*m],m,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3717 /* * (I-X(XTWX)^{-1} XTW */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3718 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3719 if (deg>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3720 { for (j=0; j<d; j++) nnresproj(lfd,sp,des,&lxd[j*m],m,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3721 /* c(0)(XTWX)^{-1}XTW'(I-X(XTWX)^{-1}XTW) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3722 for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3723 { dv->deriv[nd]=i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3724 fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3725 jacob_solve(&des->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3726 for (k=0; k<m; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3727 { ii = des->ind[k];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3728 for (l=0; l<p; l++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3729 lxd[i*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3730 } /* add c'(0)(XTWX)^{-1}XTW */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3731 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3732 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3733
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3734 dv->nd = nd+2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3735 if (deg==2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3736 { for (i=0; i<d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3737 { dv->deriv[nd]=i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3738 for (j=0; j<d; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3739 { dv->deriv[nd+1]=j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3740 fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3741 jacob_solve(&des->xtwx,des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3742 for (k=0; k<m; k++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3743 { ii = des->ind[k];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3744 for (l=0; l<p; l++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3745 lxdd[(i*d+j)*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3746 } /* + c''(x)(XTWX)^{-1}XTW */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3747 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3748 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3749 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3750 dv->nd = nd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3751
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3752 k = 1+d*(deg>0)+d*d*(deg==2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3753
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3754 if (exp) wdexpand(lx,lfd->n,des->ind,m);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3755
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3756 if (ty==1) return(m);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3757 for (i=0; i<m; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3758 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3759 stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3760 link[ZDDLL] = sqrt(fabs(link[ZDDLL]));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3761 for (j=0; j<k; j++) lx[j*m+i] *= link[ZDDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3762 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3763 return(m);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3764 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3765 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3766 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3767 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3768 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3769 * String matching functions. For a given argument string, find
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3770 * the best match from an array of possibilities. Is there a library
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3771 * function somewhere to do something like this?
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3772 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3773 * return values of -1 indicate failure/unknown string.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3774 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3775
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3776 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3777
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3778 int ct_match(z1, z2)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3779 char *z1, *z2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3780 { int ct = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3781 while (z1[ct]==z2[ct])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3782 { if (z1[ct]=='\0') return(ct+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3783 ct++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3784 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3785 return(ct);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3786 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3787
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3788 int pmatch(z, strings, vals, n, def)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3789 char *z, **strings;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3790 int *vals, n, def;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3791 { int i, ct, best, best_ct;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3792 best = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3793 best_ct = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3794
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3795 for (i=0; i<n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3796 { ct = ct_match(z,strings[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3797 if (ct==strlen(z)+1) return(vals[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3798 if (ct>best_ct) { best = i; best_ct = ct; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3799 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3800 if (best==-1) return(def);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3801 return(vals[best]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3802 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3803 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3804 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3805 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3806 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3807
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3808 int lf_maxit = 20;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3809 int lf_debug = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3810 int lf_error = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3811
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3812 double s0, s1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3813 static lfdata *lf_lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3814 static design *lf_des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3815 static smpar *lf_sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3816 int lf_status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3817 int ident=0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3818 double lf_tol;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3819 extern double robscale;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3820
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3821 void lfdata_init(lfd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3822 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3823 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3824 for (i=0; i<MXDIM; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3825 { lfd->sty[i] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3826 lfd->sca[i] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3827 lfd->xl[i] = lfd->xl[i+MXDIM] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3828 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3829 lfd->y = lfd->w = lfd->c = lfd->b = NULL;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3830 lfd->d = lfd->n = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3831 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3832
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3833 void smpar_init(sp,lfd)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3834 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3835 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3836 { nn(sp) = 0.7;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3837 fixh(sp)= 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3838 pen(sp) = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3839 acri(sp)= ANONE;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3840 deg(sp) = deg0(sp) = 2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3841 ubas(sp) = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3842 kt(sp) = KSPH;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3843 ker(sp) = WTCUB;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3844 fam(sp) = 64+TGAUS;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3845 link(sp)= LDEFAU;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3846 npar(sp) = calcp(sp,lfd->d);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3847 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3848
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3849 void deriv_init(dv)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3850 deriv *dv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3851 { dv->nd = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3852 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3853
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3854 int des_reqd(n,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3855 int n, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3856 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3857 return(n*(p+5)+2*p*p+4*p + jac_reqd(p));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3858 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3859 int des_reqi(n,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3860 int n, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3861 { return(n+p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3862 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3863
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3864 void des_init(des,n,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3865 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3866 int n, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3867 { double *z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3868 int k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3869
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3870 if (n<=0) WARN(("des_init: n <= 0"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3871 if (p<=0) WARN(("des_init: p <= 0"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3872
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3873 if (des->des_init_id != DES_INIT_ID)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3874 { des->lwk = des->lind = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3875 des->des_init_id = DES_INIT_ID;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3876 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3877
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3878 k = des_reqd(n,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3879 if (k>des->lwk)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3880 { des->wk = (double *)calloc(k,sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3881 if ( des->wk == NULL ) {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3882 printf("Problem allocating memory for des->wk\n");fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3883 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3884 des->lwk = k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3885 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3886 z = des->wk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3887
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3888 des->X = z; z += n*p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3889 des->w = z; z += n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3890 des->res=z; z += n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3891 des->di =z; z += n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3892 des->th =z; z += n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3893 des->wd =z; z += n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3894 des->V =z; z += p*p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3895 des->P =z; z += p*p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3896 des->f1 =z; z += p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3897 des->ss =z; z += p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3898 des->oc =z; z += p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3899 des->cf =z; z += p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3900
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3901 z = jac_alloc(&des->xtwx,p,z);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3902
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3903 k = des_reqi(n,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3904 if (k>des->lind)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3905 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3906 des->ind = (int *)calloc(k,sizeof(int));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3907 if ( des->ind == NULL ) {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3908 printf("Problem allocating memory for des->ind\n");fflush(stdout);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3909 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3910 des->lind = k;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3911 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3912 des->fix = &des->ind[n];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3913 for (k=0; k<p; k++) des->fix[k] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3914
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3915 des->n = n; des->p = p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3916 des->smwt = n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3917 des->xtwx.p = p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3918 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3919
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3920 void deschk(des,n,p)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3921 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3922 int n, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3923 { WARN(("deschk deprecated - use des_init()"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3924 des_init(des,n,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3925 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3926
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3927 int likereg(coef, lk0, f1, Z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3928 double *coef, *lk0, *f1, *Z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3929 { int i, ii, j, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3930 double lk, ww, link[LLEN], *X;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3931
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3932 if (lf_debug>2) mut_printf(" likereg: %8.5f\n",coef[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3933 lf_status = LF_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3934 lk = 0.0; p = lf_des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3935 setzero(Z,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3936 setzero(f1,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3937 for (i=0; i<lf_des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3938 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3939 ii = lf_des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3940 X = d_xi(lf_des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3941 fitv(lf_des,ii) = base(lf_lfd,ii)+innerprod(coef,X,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3942 lf_status = stdlinks(link,lf_lfd,lf_sp,ii,fitv(lf_des,ii),robscale);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3943 if (lf_status == LF_BADP)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3944 { *lk0 = -1.0e300;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3945 return(NR_REDUCE);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3946 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3947 if (lf_error) lf_status = LF_ERR;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3948 if (lf_status != LF_OK) return(NR_BREAK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3949
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3950 ww = wght(lf_des,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3951 lk += ww*link[ZLIK];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3952 for (j=0; j<p; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3953 f1[j] += X[j]*ww*link[ZDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3954 addouter(Z, X, X, p, ww*link[ZDDLL]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3955 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3956 for (i=0; i<p; i++) if (lf_des->fix[i])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3957 { for (j=0; j<p; j++) Z[i*p+j] = Z[j*p+i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3958 Z[i*p+i] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3959 f1[i] = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3960 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3961
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3962 if (lf_debug>4) prresp(coef,Z,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3963 if (lf_debug>3) mut_printf(" likelihood: %8.5f\n",lk);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3964 *lk0 = lf_des->llk = lk;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3965
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3966 lf_status = fami(lf_sp)->pcheck(lf_sp,lf_des,lf_lfd);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3967 switch(lf_status)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3968 { case LF_DONE: return(NR_BREAK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3969 case LF_OOB: return(NR_REDUCE);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3970 case LF_PF: return(NR_REDUCE);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3971 case LF_NSLN: return(NR_BREAK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3972 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3973
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3974 return(NR_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3975 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3976
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3977 int reginit(lfd,des,sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3978 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3979 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3980 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3981 { int i, ii;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3982 double sb, link[LLEN];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3983 s0 = s1 = sb = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3984 for (i=0; i<des->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3985 { ii = des->ind[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3986 links(base(lfd,ii),resp(lfd,ii),fami(sp),LINIT,link,cens(lfd,ii),prwt(lfd,ii),1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3987 s1 += wght(des,ii)*link[ZDLL];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3988 s0 += wght(des,ii)*prwt(lfd,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3989 sb += wght(des,ii)*prwt(lfd,ii)*base(lfd,ii);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3990 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3991 if (s0==0) return(LF_NOPT); /* no observations with W>0 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3992 setzero(des->cf,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3993 lf_tol = 1.0e-6*s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3994 switch(link(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3995 { case LIDENT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3996 des->cf[0] = (s1-sb)/s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3997 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3998 case LLOG:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
3999 if (s1<=0.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4000 { des->cf[0] = -1000;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4001 return(LF_INFA);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4002 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4003 des->cf[0] = log(s1/s0) - sb/s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4004 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4005 case LLOGIT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4006 if (s1<=0.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4007 { des->cf[0] = -1000;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4008 return(LF_INFA);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4009 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4010 if (s1>=s0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4011 { des->cf[0] = 1000;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4012 return(LF_INFA);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4013 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4014 des->cf[0] = logit(s1/s0)-sb/s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4015 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4016 case LINVER:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4017 if (s1<=0.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4018 { des->cf[0] = 1e100;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4019 return(LF_INFA);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4020 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4021 des->cf[0] = s0/s1-sb/s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4022 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4023 case LSQRT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4024 des->cf[0] = sqrt(s1/s0)-sb/s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4025 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4026 case LASIN:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4027 des->cf[0] = asin(sqrt(s1/s0))-sb/s0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4028 return(LF_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4029 default:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4030 LERR(("reginit: invalid link %d",link(sp)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4031 return(LF_ERR);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4032 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4033 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4034
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4035 int lfinit(lfd,sp,des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4036 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4037 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4038 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4039 { int initstat;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4040 des->xtwx.sm = (deg0(sp)<deg(sp)) ? JAC_CHOL : JAC_EIGD;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4041
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4042 designmatrix(lfd,sp,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4043 setfamily(sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4044 initstat = fami(sp)->initial(lfd,des,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4045
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4046 return(initstat);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4047 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4048
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4049 void lfiter(lfd,sp,des,maxit)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4050 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4051 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4052 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4053 int maxit;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4054 { int err;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4055 if (lf_debug>1) mut_printf(" lfiter: %8.5f\n",des->cf[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4056
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4057 lf_des = des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4058 lf_lfd = lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4059 lf_sp = sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4060
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4061 max_nr(fami(sp)->like, des->cf, des->oc, des->res, des->f1,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4062 &des->xtwx, des->p, maxit, lf_tol, &err);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4063 switch(err)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4064 { case NR_OK: return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4065 case NR_NCON:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4066 WARN(("max_nr not converged"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4067 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4068 case NR_NDIV:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4069 WARN(("max_nr reduction problem"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4070 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4071 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4072 WARN(("max_nr return status %d",err));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4073 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4074
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4075 int use_robust_scale(int tg)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4076 { if ((tg&64)==0) return(0); /* not quasi - no scale */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4077 if (((tg&128)==0) & (((tg&63)!=TROBT) & ((tg&63)!=TCAUC))) return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4078 return(1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4079 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4080
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4081 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4082 * noit not really needed any more, since
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4083 * gauss->pcheck returns LF_DONE, and likereg NR_BREAK
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4084 * in gaussian case.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4085 * nb: 0/1: does local neighborhood and weights need computing?
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4086 * cv: 0/1: is variance/covariance matrix needed?
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4087 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4088 int locfit(lfd,des,sp,noit,nb,cv)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4089 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4090 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4091 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4092 int noit, nb, cv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4093 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4094
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4095 if (des->xev==NULL)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4096 { LERR(("locfit: NULL evaluation point?"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4097 return(246);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4098 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4099
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4100 if (lf_debug>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4101 { mut_printf("locfit: ");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4102 for (i=0; i<lfd->d; i++) mut_printf(" %10.6f",des->xev[i]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4103 mut_printf("\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4104 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4105
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4106 /* the 1e-12 avoids problems that can occur with roundoff */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4107 if (nb) nbhd(lfd,des,(int)(lfd->n*nn(sp)+1e-12),0,sp);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4108
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4109 lf_status = lfinit(lfd,sp,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4110
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4111 if (lf_status == LF_OK)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4112 { if (use_robust_scale(fam(sp)))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4113 lf_robust(lfd,sp,des,lf_maxit);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4114 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4115 { if ((fam(sp)&63)==TQUANT)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4116 lfquantile(lfd,sp,des,lf_maxit);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4117 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4118 { robscale = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4119 lfiter(lfd,sp,des,lf_maxit);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4120 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4121 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4122 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4123
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4124 if (lf_status == LF_DONE) lf_status = LF_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4125 if (lf_status == LF_OOB) lf_status = LF_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4126
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4127 if ((fam(sp)&63)==TDEN) /* convert from rate to density */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4128 { switch(link(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4129 { case LLOG:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4130 des->cf[0] -= log(des->smwt);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4131 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4132 case LIDENT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4133 multmatscal(des->cf,1.0/des->smwt,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4134 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4135 default: LERR(("Density adjustment; invalid link"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4136 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4137 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4138
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4139 /* variance calculations, if requested */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4140 if (cv)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4141 { switch(lf_status)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4142 { case LF_PF: /* for these cases, variance calc. would likely fail. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4143 case LF_NOPT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4144 case LF_NSLN:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4145 case LF_INFA:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4146 case LF_DEMP:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4147 case LF_XOOR:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4148 case LF_DNOP:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4149 case LF_BADP:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4150 des->llk = des->tr0 = des->tr1 = des->tr2 = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4151 setzero(des->V,des->p*des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4152 setzero(des->f1,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4153 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4154 default: lf_vcov(lfd,sp,des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4155 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4156 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4157
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4158 return(lf_status);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4159 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4160
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4161 void lf_status_msg(status)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4162 int status;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4163 { switch(status)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4164 { case LF_OK: return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4165 case LF_NCON: WARN(("locfit did not converge")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4166 case LF_OOB: WARN(("parameters out of bounds")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4167 case LF_PF: WARN(("perfect fit")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4168 case LF_NOPT: WARN(("no points with non-zero weight")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4169 case LF_NSLN: WARN(("no solution")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4170 case LF_INFA: WARN(("initial value problem")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4171 case LF_DEMP: WARN(("density estimate, empty integration region")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4172 case LF_XOOR: WARN(("procv: fit point outside xlim region")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4173 case LF_DNOP: WARN(("density estimation -- insufficient points in smoothing window")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4174 case LF_BADP: WARN(("bad parameters")); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4175 default: WARN(("procv: unknown return code %d",status)); return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4176 } }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4177 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4178 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4179 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4180 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4181 * Compute minimax weights for local regression.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4182 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4183
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4184 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4185 #define NR_EMPTY 834
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4186
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4187 int mmsm_ct;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4188
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4189 static int debug=0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4190 #define CONVTOL 1.0e-8
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4191 #define SINGTOL 1.0e-10
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4192 #define NR_SINGULAR 100
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4193
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4194 static lfdata *mm_lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4195 static design *mm_des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4196 static double mm_gam, mmf, lb;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4197 static int st;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4198
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4199 double ipower(x,n) /* use for n not too large!! */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4200 double x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4201 int n;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4202 { if (n==0) return(1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4203 if (n<0) return(1/ipower(x,-n));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4204 return(x*ipower(x,n-1));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4205 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4206
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4207 double setmmwt(des,a,gam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4208 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4209 double *a, gam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4210 { double ip, w0, w1, sw, wt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4211 int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4212 sw = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4213 for (i=0; i<mm_lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4214 { ip = innerprod(a,d_xi(des,i),des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4215 wt = prwt(mm_lfd,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4216 w0 = ip - gam*des->wd[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4217 w1 = ip + gam*des->wd[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4218 wght(des,i) = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4219 if (w0>0) { wght(des,i) = w0; sw += wt*w0*w0; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4220 if (w1<0) { wght(des,i) = w1; sw += wt*w1*w1; }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4221 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4222 return(sw/2-a[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4223 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4224
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4225 /* compute sum_{w!=0} AA^T; e1-sum wA */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4226 int mmsums(des,coef,f,z,J)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4227 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4228 double *coef, *f, *z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4229 jacobian *J;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4230 { int ct, i, j, p, sing;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4231 double *A;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4232
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4233 mmsm_ct++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4234 A = J->Z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4235 *f = setmmwt(des,coef,mm_gam);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4236
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4237 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4238 setzero(A,p*p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4239 setzero(z,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4240 z[0] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4241 ct = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4242
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4243 for (i=0; i<mm_lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4244 if (wght(des,i)!=0.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4245 { addouter(A,d_xi(des,i),d_xi(des,i),p,prwt(mm_lfd,i));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4246 for (j=0; j<p; j++) z[j] -= prwt(mm_lfd,i)*wght(des,i)*d_xij(des,i,j);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4247 ct++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4248 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4249 if (ct==0) return(NR_EMPTY);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4250
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4251 J->st = JAC_RAW;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4252 J->p = p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4253 jacob_dec(J,JAC_EIGD);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4254
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4255 sing = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4256 for (i=0; i<p; i++) sing |= (J->Z[i*p+i]<SINGTOL);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4257 if ((debug) & (sing)) mut_printf("SINGULAR!!!!\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4258
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4259 return((sing) ? NR_SINGULAR : NR_OK);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4260 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4261
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4262 int descenddir(des,coef,dlt,f,af)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4263 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4264 double *coef, *dlt, *f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4265 int af;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4266 { int i, p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4267 double f0, *oc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4268
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4269 if (debug) mut_printf("descenddir: %8.5f %8.5f\n",dlt[0],dlt[1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4270
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4271 f0 = *f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4272 oc = des->oc;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4273 p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4274 memcpy(oc,coef,p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4275
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4276 for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4277 st = mmsums(des,coef,f,des->f1,&des->xtwx);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4278
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4279 if (*f>f0) /* halve till we drop */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4280 { while (*f>f0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4281 { lb = lb/2.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4282 for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4283 st = mmsums(des,coef,f,des->f1,&des->xtwx);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4284 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4285 return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4286 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4287
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4288 if (!af) return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4289
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4290 /* double */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4291 while (*f<f0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4292 { f0 = *f;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4293 lb *= 2.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4294 for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4295 st = mmsums(des,coef,f,des->f1,&des->xtwx);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4296 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4297
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4298 lb /= 2.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4299 for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4300 st = mmsums(des,coef,f,des->f1,&des->xtwx);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4301
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4302 return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4303 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4304
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4305 int mm_initial(des)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4306 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4307 { double *dlt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4308
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4309 dlt = des->ss;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4310
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4311 setzero(des->cf,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4312 st = mmsums(des,des->cf,&mmf,des->f1,&des->xtwx);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4313
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4314 setzero(dlt,des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4315 dlt[0] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4316 lb = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4317 st = descenddir(des,des->cf,dlt,&mmf,1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4318 return(st);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4319 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4320
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4321 void getsingdir(des,dlt)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4322 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4323 double *dlt;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4324 { double f, sw, c0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4325 int i, j, p, sd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4326
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4327 sd = -1; p = des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4328 setzero(dlt,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4329 for (i=0; i<p; i++) if (des->xtwx.Z[i*p+i]<SINGTOL) sd = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4330 if (sd==-1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4331 { mut_printf("getsingdir: nonsing?\n");
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4332 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4333 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4334 if (des->xtwx.dg[sd]>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4335 for (i=0; i<p; i++) dlt[i] = des->xtwx.Q[p*i+sd]*des->xtwx.dg[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4336 else
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4337 { dlt[sd] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4338 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4339
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4340 c0 = innerprod(dlt,des->f1,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4341 if (c0<0) for (i=0; i<p; i++) dlt[i] = -dlt[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4342 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4343
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4344 void mmax(coef, old_coef, delta, J, p, maxit, tol, err)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4345 double *coef, *old_coef, *delta, tol;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4346 int p, maxit, *err;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4347 jacobian *J;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4348 { double old_f, lambda;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4349 int i, j;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4350
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4351 *err = NR_OK;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4352
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4353 for (j=0; j<maxit; j++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4354 { memcpy(old_coef,coef,p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4355 old_f = mmf;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4356
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4357 if (st == NR_SINGULAR)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4358 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4359 getsingdir(mm_des,delta);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4360 st = descenddir(mm_des,coef,delta,&mmf,1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4361 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4362 if (st == NR_EMPTY)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4363 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4364 setzero(delta,p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4365 delta[0] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4366 st = descenddir(mm_des,coef,delta,&mmf,1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4367 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4368 if (st == NR_OK)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4369 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4370 lb = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4371 jacob_solve(J,mm_des->f1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4372 memcpy(delta,mm_des->f1,p*sizeof(double));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4373 st = descenddir(mm_des,coef,delta,&mmf,0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4374 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4375
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4376 if ((j>0) & (fabs(mmf-old_f)<tol)) return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4377 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4378 WARN(("findab not converged"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4379 *err = NR_NCON;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4380 return;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4381 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4382
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4383 double findab(gam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4384 double gam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4385 { double sl;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4386 int i, p, nr_stat;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4387
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4388 if (debug) mut_printf(" findab: gam %8.5f\n",gam);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4389 mm_gam = gam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4390 p = mm_des->p;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4391 lb = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4392 st = mm_initial(mm_des);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4393
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4394 mmax(mm_des->cf, mm_des->oc, mm_des->ss,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4395 &mm_des->xtwx, p, lf_maxit, CONVTOL, &nr_stat);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4396
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4397 sl = 0.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4398 for (i=0; i<mm_lfd->n; i++) sl += fabs(wght(mm_des,i))*mm_des->wd[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4399
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4400 if (debug) mut_printf(" sl %8.5f gam %8.5f %8.5f %d\n", sl,gam,sl-gam,nr_stat);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4401 return(sl-gam);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4402 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4403
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4404 double weightmm(coef,di,ff,gam)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4405 double *coef, di, *ff, gam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4406 { double y1, y2, ip;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4407 ip = innerprod(ff,coef,mm_des->p);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4408 y1 = ip-gam*di; if (y1>0) return(y1/ip);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4409 y2 = ip+gam*di; if (y2<0) return(y2/ip);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4410 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4411 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4412
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4413 double minmax(lfd,des,sp)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4414 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4415 design *des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4416 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4417 { double h, u[MXDIM], gam;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4418 int i, j, m, d1, p1, err_flag;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4419
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4420 if (debug) mut_printf("minmax: x %8.5f\n",des->xev[0]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4421 mm_lfd = lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4422 mm_des = des;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4423
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4424 mmsm_ct = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4425 d1 = deg(sp)+1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4426 p1 = factorial(d1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4427 for (i=0; i<lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4428 { for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4429 des->wd[i] = sp->nn/p1*ipower(dist(des,i),d1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4430 des->ind[i] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4431 fitfun(lfd, sp, u,des->xev,d_xi(des,i),NULL);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4432 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4433
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4434 /* find gamma (i.e. solve eqn 13.17 from book), using the secant method.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4435 * As a side effect, this finds the other minimax coefficients.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4436 * Note that 13.17 is rewritten as
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4437 * g2 = sum |l_i(x)| (||xi-x||^(p+1) M/(s*(p+1)!))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4438 * where g2 = gamma * s * (p+1)! / M. The gam variable below is g2.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4439 * The smoothing parameter is sp->nn == M/s.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4440 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4441 gam = solve_secant(findab, 0.0, 0.0,1.0, 0.0000001, BDF_EXPRIGHT, &err_flag);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4442
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4443 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4444 * Set the smoothing weights, in preparation for the actual fit.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4445 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4446 h = 0.0; m = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4447 for (i=0; i<lfd->n; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4448 { wght(des,i) = weightmm(des->cf, des->wd[i],d_xi(des,i),gam);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4449 if (wght(des,i)>0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4450 { if (dist(des,i)>h) h = dist(des,i);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4451 des->ind[m] = i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4452 m++;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4453 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4454 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4455 des->n = m;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4456 return(h);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4457 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4458 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4459 * Copyright 1996-2006 Catherine Loader.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4460 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4461 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4462 *
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4463 * Defines the weight functions and related quantities used
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4464 * in LOCFIT.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4465 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4466
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4467 #include "locf.h"
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4468
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4469 /*
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4470 * convert kernel and kernel type strings to numeric codes.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4471 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4472 #define NWFUNS 13
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4473 static char *wfuns[NWFUNS] = {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4474 "rectangular", "epanechnikov", "bisquare", "tricube",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4475 "triweight", "gaussian", "triangular", "ququ",
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4476 "6cub", "minimax", "exponential", "maclean", "parametric" };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4477 static int wvals[NWFUNS] = { WRECT, WEPAN, WBISQ, WTCUB,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4478 WTRWT, WGAUS, WTRIA, WQUQU, W6CUB, WMINM, WEXPL, WMACL, WPARM };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4479 int lfkernel(char *z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4480 { return(pmatch(z, wfuns, wvals, NWFUNS, WTCUB));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4481 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4482
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4483 #define NKTYPE 5
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4484 static char *ktype[NKTYPE] = { "spherical", "product", "center", "lm", "zeon" };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4485 static int kvals[NKTYPE] = { KSPH, KPROD, KCE, KLM, KZEON };
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4486 int lfketype(char *z)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4487 { return(pmatch(z, ktype, kvals, NKTYPE, KSPH));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4488 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4489
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4490 /* The weight functions themselves. Used everywhere. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4491 double W(u,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4492 double u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4493 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4494 { u = fabs(u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4495 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4496 { case WRECT: return((u>1) ? 0.0 : 1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4497 case WEPAN: return((u>1) ? 0.0 : 1-u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4498 case WBISQ: if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4499 u = 1-u*u; return(u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4500 case WTCUB: if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4501 u = 1-u*u*u; return(u*u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4502 case WTRWT: if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4503 u = 1-u*u; return(u*u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4504 case WQUQU: if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4505 u = 1-u*u; return(u*u*u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4506 case WTRIA: if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4507 return(1-u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4508 case W6CUB: if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4509 u = 1-u*u*u; u = u*u*u; return(u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4510 case WGAUS: return(exp(-SQR(GFACT*u)/2.0));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4511 case WEXPL: return(exp(-EFACT*u));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4512 case WMACL: return(1/((u+1.0e-100)*(u+1.0e-100)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4513 case WMINM: LERR(("WMINM in W"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4514 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4515 case WPARM: return(1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4516 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4517 LERR(("W(): Unknown kernel %d\n",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4518 return(1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4519 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4520
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4521 int iscompact(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4522 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4523 { if ((ker==WEXPL) | (ker==WGAUS) | (ker==WMACL) | (ker==WPARM)) return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4524 return(1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4525 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4526
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4527 double weightprod(lfd,u,h,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4528 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4529 double *u, h;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4530 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4531 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4532 double sc, w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4533 w = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4534 for (i=0; i<lfd->d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4535 { sc = lfd->sca[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4536 switch(lfd->sty[i])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4537 { case STLEFT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4538 if (u[i]>0) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4539 w *= W(-u[i]/(h*sc),ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4540 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4541 case STRIGH:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4542 if (u[i]<0) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4543 w *= W(u[i]/(h*sc),ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4544 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4545 case STANGL:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4546 w *= W(2*fabs(sin(u[i]/(2*sc)))/h,ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4547 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4548 case STCPAR:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4549 break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4550 default:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4551 w *= W(fabs(u[i])/(h*sc),ker);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4552 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4553 if (w==0.0) return(w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4554 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4555 return(w);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4556 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4557
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4558 double weightsph(lfd,u,h,ker, hasdi,di)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4559 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4560 double *u, h, di;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4561 int ker, hasdi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4562 { int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4563
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4564 if (!hasdi) di = rho(u,lfd->sca,lfd->d,KSPH,lfd->sty);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4565
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4566 for (i=0; i<lfd->d; i++)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4567 { if ((lfd->sty[i]==STLEFT) && (u[i]>0.0)) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4568 if ((lfd->sty[i]==STRIGH) && (u[i]<0.0)) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4569 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4570 if (h==0) return((di==0.0) ? 1.0 : 0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4571
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4572 return(W(di/h,ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4573 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4574
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4575 double weight(lfd,sp,x,t,h, hasdi,di)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4576 lfdata *lfd;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4577 smpar *sp;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4578 double *x, *t, h, di;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4579 int hasdi;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4580 { double u[MXDIM];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4581 int i;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4582 for (i=0; i<lfd->d; i++) u[i] = (t==NULL) ? x[i] : x[i]-t[i];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4583 switch(kt(sp))
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4584 { case KPROD: return(weightprod(lfd,u,h,ker(sp)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4585 case KSPH: return(weightsph(lfd,u,h,ker(sp), hasdi,di));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4586 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4587 LERR(("weight: unknown kernel type %d",kt(sp)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4588 return(1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4589 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4590
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4591 double sgn(x)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4592 double x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4593 { if (x>0) return(1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4594 if (x<0) return(-1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4595 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4596 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4597
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4598 double WdW(u,ker) /* W'(u)/W(u) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4599 double u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4600 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4601 { double eps=1.0e-10;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4602 if (ker==WGAUS) return(-GFACT*GFACT*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4603 if (ker==WPARM) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4604 if (fabs(u)>=1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4605 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4606 { case WRECT: return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4607 case WTRIA: return(-sgn(u)/(1-fabs(u)+eps));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4608 case WEPAN: return(-2*u/(1-u*u+eps));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4609 case WBISQ: return(-4*u/(1-u*u+eps));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4610 case WTRWT: return(-6*u/(1-u*u+eps));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4611 case WTCUB: return(-9*sgn(u)*u*u/(1-u*u*fabs(u)+eps));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4612 case WEXPL: return((u>0) ? -EFACT : EFACT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4613 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4614 LERR(("WdW: invalid kernel"));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4615 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4616 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4617
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4618 /* deriv. weights .. spherical, product etc
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4619 u, sc, sty needed only in relevant direction
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4620 Acutally, returns (d/dx W(||x||/h) ) / W(.)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4621 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4622 double weightd(u,sc,d,ker,kt,h,sty,di)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4623 double u, sc, h, di;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4624 int d, ker, kt, sty;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4625 { if (sty==STANGL)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4626 { if (kt==KPROD)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4627 return(-WdW(2*sin(u/(2*sc)),ker)*cos(u/(2*sc))/(h*sc));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4628 if (di==0.0) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4629 return(-WdW(di/h,ker)*sin(u/sc)/(h*sc*di));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4630 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4631 if (sty==STCPAR) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4632 if (kt==KPROD)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4633 return(-WdW(u/(h*sc),ker)/(h*sc));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4634 if (di==0.0) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4635 return(-WdW(di/h,ker)*u/(h*di*sc*sc));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4636 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4637
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4638 double weightdd(u,sc,d,ker,kt,h,sty,di,i0,i1)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4639 double *u, *sc, h, di;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4640 int d, ker, kt, i0, i1, *sty;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4641 { double w;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4642 w = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4643 if (kt==KPROD)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4644 {
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4645 w = WdW(u[i0]/(h*sc[i0]),ker)*WdW(u[i1]/(h*sc[i1]),ker)/(h*h*sc[i0]*sc[i1]);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4646 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4647 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4648 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4649
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4650 /* Derivatives W'(u)/u.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4651 Used in simult. conf. band computations,
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4652 and kernel density bandwidth selectors. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4653 double Wd(u,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4654 double u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4655 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4656 { double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4657 if (ker==WGAUS) return(-SQR(GFACT)*exp(-SQR(GFACT*u)/2));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4658 if (ker==WPARM) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4659 if (fabs(u)>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4660 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4661 { case WEPAN: return(-2.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4662 case WBISQ: return(-4*(1-u*u));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4663 case WTCUB: v = 1-u*u*u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4664 return(-9*v*v*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4665 case WTRWT: v = 1-u*u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4666 return(-6*v*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4667 default: LERR(("Invalid kernel %d in Wd",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4668 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4669 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4670 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4671
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4672 /* Second derivatives W''(u)-W'(u)/u.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4673 used in simult. conf. band computations in >1 dimension. */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4674 double Wdd(u,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4675 double u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4676 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4677 { double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4678 if (ker==WGAUS) return(SQR(u*GFACT*GFACT)*exp(-SQR(u*GFACT)/2));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4679 if (ker==WPARM) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4680 if (u>1) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4681 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4682 { case WBISQ: return(12*u*u);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4683 case WTCUB: v = 1-u*u*u;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4684 return(-9*u*v*v+54*u*u*u*u*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4685 case WTRWT: return(24*u*u*(1-u*u));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4686 default: LERR(("Invalid kernel %d in Wdd",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4687 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4688 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4689 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4690
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4691 /* int u1^j1..ud^jd W(u) du.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4692 Used for local log-linear density estimation.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4693 Assume all j_i are even.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4694 Also in some bandwidth selection.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4695 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4696 double wint(d,j,nj,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4697 int d, *j, nj, ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4698 { double I, z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4699 int k, dj;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4700 dj = d;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4701 for (k=0; k<nj; k++) dj += j[k];
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4702 switch(ker) /* int_0^1 u^(dj-1) W(u)du */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4703 { case WRECT: I = 1.0/dj; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4704 case WEPAN: I = 2.0/(dj*(dj+2)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4705 case WBISQ: I = 8.0/(dj*(dj+2)*(dj+4)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4706 case WTCUB: I = 162.0/(dj*(dj+3)*(dj+6)*(dj+9)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4707 case WTRWT: I = 48.0/(dj*(dj+2)*(dj+4)*(dj+6)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4708 case WTRIA: I = 1.0/(dj*(dj+1)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4709 case WQUQU: I = 384.0/(dj*(dj+2)*(dj+4)*(dj+6)*(dj+8)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4710 case W6CUB: I = 524880.0/(dj*(dj+3)*(dj+6)*(dj+9)*(dj+12)*(dj+15)*(dj+18)); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4711 case WGAUS: switch(d)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4712 { case 1: I = S2PI/GFACT; break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4713 case 2: I = 2*PI/(GFACT*GFACT); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4714 default: I = exp(d*log(S2PI/GFACT)); /* for nj=0 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4715 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4716 for (k=0; k<nj; k++) /* deliberate drop */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4717 switch(j[k])
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4718 { case 4: I *= 3.0/(GFACT*GFACT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4719 case 2: I /= GFACT*GFACT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4720 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4721 return(I);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4722 case WEXPL: I = factorial(dj-1)/ipower(EFACT,dj); break;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4723 default: LERR(("Unknown kernel %d in exacint",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4724 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4725 if ((d==1) && (nj==0)) return(2*I); /* common case quick */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4726 z = (d-nj)*LOGPI/2-mut_lgammai(dj);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4727 for (k=0; k<nj; k++) z += mut_lgammai(j[k]+1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4728 return(2*I*exp(z));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4729 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4730
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4731 /* taylor series expansion of weight function around x.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4732 0 and 1 are common arguments, so are worth programming
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4733 as special cases.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4734 Used in density estimation.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4735 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4736 int wtaylor(f,x,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4737 double *f, x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4738 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4739 { double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4740 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4741 { case WRECT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4742 f[0] = 1.0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4743 return(1);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4744 case WEPAN:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4745 f[0] = 1-x*x; f[1] = -2*x; f[2] = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4746 return(3);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4747 case WBISQ:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4748 v = 1-x*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4749 f[0] = v*v; f[1] = -4*x*v; f[2] = 4-6*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4750 f[3] = 4*x; f[4] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4751 return(5);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4752 case WTCUB:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4753 if (x==1.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4754 { f[0] = f[1] = f[2] = 0; f[3] = -27; f[4] = -81; f[5] = -108;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4755 f[6] = -81; f[7] = -36; f[8] = -9; f[9] = -1; return(10); }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4756 if (x==0.0)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4757 { f[1] = f[2] = f[4] = f[5] = f[7] = f[8] = 0;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4758 f[0] = 1; f[3] = -3; f[6] = 3; f[9] = -1; return(10); }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4759 v = 1-x*x*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4760 f[0] = v*v*v; f[1] = -9*v*v*x*x; f[2] = x*v*(27-36*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4761 f[3] = -27+v*(108-84*v); f[4] = -3*x*x*(27-42*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4762 f[5] = x*(-108+126*v); f[6] = -81+84*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4763 f[7] = -36*x*x; f[8] = -9*x; f[9] = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4764 return(10);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4765 case WTRWT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4766 v = 1-x*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4767 f[0] = v*v*v; f[1] = -6*x*v*v; f[2] = v*(12-15*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4768 f[3] = x*(20*v-8); f[4] = 15*v-12; f[5] = -6; f[6] = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4769 return(7);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4770 case WTRIA:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4771 f[0] = 1-x; f[1] = -1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4772 return(2);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4773 case WQUQU:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4774 v = 1-x*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4775 f[0] = v*v*v*v; f[1] = -8*x*v*v*v; f[2] = v*v*(24-28*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4776 f[3] = v*x*(56*v-32); f[4] = (70*v-80)*v+16; f[5] = x*(32-56*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4777 f[6] = 24-28*v; f[7] = 8*x; f[8] = 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4778 return(9);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4779 case W6CUB:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4780 v = 1-x*x*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4781 f[0] = v*v*v*v*v*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4782 f[1] = -18*x*x*v*v*v*v*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4783 f[2] = x*v*v*v*v*(135-153*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4784 f[3] = v*v*v*(-540+v*(1350-816*v));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4785 f[4] = x*x*v*v*(1215-v*(4050-v*3060));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4786 f[5] = x*v*(-1458+v*(9234+v*(-16254+v*8568)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4787 f[6] = 729-v*(10206-v*(35154-v*(44226-v*18564)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4788 f[7] = x*x*(4374-v*(30132-v*(56862-v*31824)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4789 f[8] = x*(12393-v*(61479-v*(92664-v*43758)));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4790 f[9] = 21870-v*(89100-v*(115830-v*48620));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4791 f[10]= x*x*(26730-v*(69498-v*43758));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4792 f[11]= x*(23814-v*(55458-v*31824));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4793 f[12]= 15849-v*(34398-v*18564);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4794 f[13]= x*x*(7938-8568*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4795 f[14]= x*(2970-3060*v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4796 f[15]= 810-816*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4797 f[16]= 153*x*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4798 f[17]= 18*x;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4799 f[18]= 1;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4800 return(19);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4801 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4802 LERR(("Invalid kernel %d in wtaylor",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4803 return(0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4804 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4805
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4806 /* convolution int W(x)W(x+v)dx.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4807 used in kde bandwidth selection.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4808 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4809 double Wconv(v,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4810 double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4811 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4812 { double v2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4813 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4814 { case WGAUS: return(SQRPI/GFACT*exp(-SQR(GFACT*v)/4));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4815 case WRECT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4816 v = fabs(v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4817 if (v>2) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4818 return(2-v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4819 case WEPAN:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4820 v = fabs(v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4821 if (v>2) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4822 return((2-v)*(16+v*(8-v*(16-v*(2+v))))/30);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4823 case WBISQ:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4824 v = fabs(v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4825 if (v>2) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4826 v2 = 2-v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4827 return(v2*v2*v2*v2*v2*(16+v*(40+v*(36+v*(10+v))))/630);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4828 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4829 LERR(("Wconv not implemented for kernel %d",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4830 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4831 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4832
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4833 /* derivative of Wconv.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4834 1/v d/dv int W(x)W(x+v)dx
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4835 used in kde bandwidth selection.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4836 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4837 double Wconv1(v,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4838 double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4839 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4840 { double v2;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4841 v = fabs(v);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4842 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4843 { case WGAUS: return(-0.5*SQRPI*GFACT*exp(-SQR(GFACT*v)/4));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4844 case WRECT:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4845 if (v>2) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4846 return(1.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4847 case WEPAN:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4848 if (v>2) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4849 return((-16+v*(12-v*v))/6);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4850 case WBISQ:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4851 if (v>2) return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4852 v2 = 2-v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4853 return(-v2*v2*v2*v2*(32+v*(64+v*(24+v*3)))/210);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4854 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4855 LERR(("Wconv1 not implemented for kernel %d",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4856 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4857 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4858
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4859 /* 4th derivative of Wconv.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4860 used in kde bandwidth selection (BCV, SJPI, GKK)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4861 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4862 double Wconv4(v,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4863 double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4864 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4865 { double gv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4866 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4867 { case WGAUS:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4868 gv = GFACT*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4869 return(exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*(12-gv*gv*(12-gv*gv))*SQRPI/16);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4870 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4871 LERR(("Wconv4 not implemented for kernel %d",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4872 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4873 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4874
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4875 /* 5th derivative of Wconv.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4876 used in kde bandwidth selection (BCV method only)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4877 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4878 double Wconv5(v,ker) /* (d/dv)^5 int W(x)W(x+v)dx */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4879 double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4880 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4881 { double gv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4882 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4883 { case WGAUS:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4884 gv = GFACT*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4885 return(-exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*GFACT*gv*(60-gv*gv*(20-gv*gv))*SQRPI/32);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4886 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4887 LERR(("Wconv5 not implemented for kernel %d",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4888 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4889 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4890
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4891 /* 6th derivative of Wconv.
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4892 used in kde bandwidth selection (SJPI)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4893 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4894 double Wconv6(v,ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4895 double v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4896 int ker;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4897 { double gv, z;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4898 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4899 { case WGAUS:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4900 gv = GFACT*v;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4901 gv = gv*gv;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4902 z = exp(-gv/4)*(-120+gv*(180-gv*(30-gv)))*0.02769459142;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4903 gv = GFACT*GFACT;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4904 return(z*gv*gv*GFACT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4905 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4906 LERR(("Wconv6 not implemented for kernel %d",ker));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4907 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4908 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4909
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4910 /* int W(v)^2 dv / (int v^2 W(v) dv)^2
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4911 used in some bandwidth selectors
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4912 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4913 double Wikk(ker,deg)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4914 int ker, deg;
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4915 { switch(deg)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4916 { case 0:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4917 case 1: /* int W(v)^2 dv / (int v^2 W(v) dv)^2 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4918 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4919 { case WRECT: return(4.5);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4920 case WEPAN: return(15.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4921 case WBISQ: return(35.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4922 case WGAUS: return(0.2820947918*GFACT*GFACT*GFACT*GFACT*GFACT);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4923 case WTCUB: return(34.152111046847892); /* 59049 / 1729 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4924 case WTRWT: return(66.083916083916080); /* 9450/143 */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4925 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4926 case 2:
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4927 case 3: /* 4!^2/8*int(W1^2)/int(v^4W1)^2
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4928 W1=W*(n4-v^2n2)/(n0n4-n2n2) */
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4929 switch(ker)
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4930 { case WRECT: return(11025.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4931 case WEPAN: return(39690.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4932 case WBISQ: return(110346.9231);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4933 case WGAUS: return(14527.43412);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4934 case WTCUB: return(126500.5904);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4935 case WTRWT: return(254371.7647);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4936 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4937 }
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4938 LERR(("Wikk not implemented for kernel %d, deg %d",ker,deg));
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4939 return(0.0);
0f80a5141704 version 0.3 uploaded
vipints
parents:
diff changeset
4940 }