| 
0
 | 
     1 /*
 | 
| 
 | 
     2  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
     3  */
 | 
| 
 | 
     4 
 | 
| 
 | 
     5 #include "mex.h"
 | 
| 
 | 
     6 /*
 | 
| 
 | 
     7  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
     8  */
 | 
| 
 | 
     9 /*
 | 
| 
 | 
    10  *   Integration for hazard rate estimation. The functions in this
 | 
| 
 | 
    11  *   file are used to evaluate
 | 
| 
 | 
    12  *      sum int_0^{Ti} W_i(t,x) A()A()' exp( P() ) dt
 | 
| 
 | 
    13  *   for hazard rate models.
 | 
| 
 | 
    14  *
 | 
| 
 | 
    15  *   These routines assume the weight function is supported on [-1,1].
 | 
| 
 | 
    16  *   hasint_sph multiplies by exp(base(lf,i)), which allows estimating
 | 
| 
 | 
    17  *   the baseline in a proportional hazards model, when the covariate
 | 
| 
 | 
    18  *   effect base(lf,i) is known.
 | 
| 
 | 
    19  *
 | 
| 
 | 
    20  *   TODO:
 | 
| 
 | 
    21  *     hazint_sph, should be able to reduce mint in some cases with
 | 
| 
 | 
    22  *       small integration range. onedint could be used for beta-family
 | 
| 
 | 
    23  *       (RECT,EPAN,BISQ,TRWT) kernels.
 | 
| 
 | 
    24  *     hazint_prod, restrict terms from the sum based on x values.
 | 
| 
 | 
    25  *       I should count obs >= max, and only do that integration once.
 | 
| 
 | 
    26  */
 | 
| 
 | 
    27 
 | 
| 
 | 
    28 #include "locf.h"
 | 
| 
 | 
    29 
 | 
| 
 | 
    30 static double ilim[2*MXDIM], *ff, tmax;
 | 
| 
 | 
    31 static lfdata *haz_lfd;
 | 
| 
 | 
    32 static smpar  *haz_sp;
 | 
| 
 | 
    33 
 | 
| 
 | 
    34 /*
 | 
| 
 | 
    35  *  hrao returns 0 if integration region is empty.
 | 
| 
 | 
    36  *               1 otherwise.
 | 
| 
 | 
    37  */
 | 
| 
 | 
    38 int haz_sph_int(dfx,cf,h,r1)
 | 
| 
 | 
    39 double *dfx, *cf, h, *r1;
 | 
| 
 | 
    40 { double s, t0, t1, wt, th;
 | 
| 
 | 
    41   int j, dim, p;
 | 
| 
 | 
    42   s = 0; p = npar(haz_sp);
 | 
| 
 | 
    43   dim = haz_lfd->d;
 | 
| 
 | 
    44   for (j=1; j<dim; j++) s += SQR(dfx[j]/(h*haz_lfd->sca[j]));
 | 
| 
 | 
    45   if (s>1) return(0);
 | 
| 
 | 
    46 
 | 
| 
 | 
    47   setzero(r1,p*p);
 | 
| 
 | 
    48   t1 = sqrt(1-s)*h*haz_lfd->sca[0];
 | 
| 
 | 
    49   t0 = -t1;
 | 
| 
 | 
    50   if (t0<ilim[0])   t0 = ilim[0];
 | 
| 
 | 
    51   if (t1>ilim[dim]) t1 = ilim[dim];
 | 
| 
 | 
    52   if (t1>dfx[0]) t1 = dfx[0];
 | 
| 
 | 
    53   if (t1<t0) return(0);
 | 
| 
 | 
    54 
 | 
| 
 | 
    55 /*  Numerical integration by Simpson's rule.
 | 
| 
 | 
    56  */
 | 
| 
 | 
    57   for (j=0; j<=de_mint; j++)
 | 
| 
 | 
    58   { dfx[0] = t0+(t1-t0)*j/de_mint;
 | 
| 
 | 
    59     wt = weight(haz_lfd, haz_sp, dfx, NULL, h, 0, 0.0);
 | 
| 
 | 
    60     fitfun(haz_lfd, haz_sp, dfx,NULL,ff,NULL);
 | 
| 
 | 
    61     th = innerprod(cf,ff,p);
 | 
| 
 | 
    62     if (link(haz_sp)==LLOG) th = exp(th);
 | 
| 
 | 
    63     wt *= 2+2*(j&1)-(j==0)-(j==de_mint);
 | 
| 
 | 
    64     addouter(r1,ff,ff,p,wt*th);
 | 
| 
 | 
    65   }
 | 
| 
 | 
    66   multmatscal(r1,(t1-t0)/(3*de_mint),p*p);
 | 
| 
 | 
    67 
 | 
| 
 | 
    68   return(1);
 | 
| 
 | 
    69 }
 | 
| 
 | 
    70 
 | 
| 
 | 
    71 int hazint_sph(t,resp,r1,cf,h)
 | 
| 
 | 
    72 double *t, *resp, *r1, *cf, h;
 | 
| 
 | 
    73 { int i, j, n, p, st;
 | 
| 
 | 
    74   double dfx[MXDIM], eb, sb;
 | 
| 
 | 
    75   p = npar(haz_sp);
 | 
| 
 | 
    76   setzero(resp,p*p);
 | 
| 
 | 
    77   sb = 0.0;
 | 
| 
 | 
    78 
 | 
| 
 | 
    79   n = haz_lfd->n;
 | 
| 
 | 
    80   for (i=0; i<=n; i++)
 | 
| 
 | 
    81   {
 | 
| 
 | 
    82     if (i==n)
 | 
| 
 | 
    83     { dfx[0] = tmax-t[0];
 | 
| 
 | 
    84       for (j=1; j<haz_lfd->d; j++) dfx[j] = 0.0;
 | 
| 
 | 
    85       eb = exp(sb/n);
 | 
| 
 | 
    86     }
 | 
| 
 | 
    87     else
 | 
| 
 | 
    88     { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
 | 
| 
 | 
    89       for (j=0; j<haz_lfd->d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
 | 
| 
 | 
    90     }
 | 
| 
 | 
    91 
 | 
| 
 | 
    92     st = haz_sph_int(dfx,cf,h,r1);
 | 
| 
 | 
    93     if (st)
 | 
| 
 | 
    94       for (j=0; j<p*p; j++) resp[j] += eb*r1[j];
 | 
| 
 | 
    95   }
 | 
| 
 | 
    96   return(LF_OK);
 | 
| 
 | 
    97 }
 | 
| 
 | 
    98 
 | 
| 
 | 
    99 int hazint_prod(t,resp,x,cf,h)
 | 
| 
 | 
   100 double *t, *resp, *x, *cf, h;
 | 
| 
 | 
   101 { int d, p, i, j, k, st;
 | 
| 
 | 
   102   double dfx[MXDIM], t_prev,
 | 
| 
 | 
   103          hj, hs, ncf[MXDEG], ef, il1;
 | 
| 
 | 
   104   double prod_wk[MXDIM][2*MXDEG+1], eb, sb;
 | 
| 
 | 
   105 
 | 
| 
 | 
   106   p = npar(haz_sp);
 | 
| 
 | 
   107   d = haz_lfd->d;
 | 
| 
 | 
   108   setzero(resp,p*p);
 | 
| 
 | 
   109   hj = hs = h*haz_lfd->sca[0];
 | 
| 
 | 
   110 
 | 
| 
 | 
   111   ncf[0] = cf[0];
 | 
| 
 | 
   112   for (i=1; i<=deg(haz_sp); i++)
 | 
| 
 | 
   113   { ncf[i] = hj*cf[(i-1)*d+1]; hj *= hs;
 | 
| 
 | 
   114   }
 | 
| 
 | 
   115 
 | 
| 
 | 
   116 /*   for i=0..n....
 | 
| 
 | 
   117  *     First we compute prod_wk[j], j=0..d.
 | 
| 
 | 
   118  *     For j=0, this is int_0^T_i (u-t)^k W((u-t)/h) exp(b0*(u-t)) du
 | 
| 
 | 
   119  *     For remaining j,   (x(i,j)-x(j))^k Wj exp(bj*(x..-x.))
 | 
| 
 | 
   120  *
 | 
| 
 | 
   121  *     Second, we add to the integration (exp(a) incl. in integral)
 | 
| 
 | 
   122  *     with the right factorial denominators.
 | 
| 
 | 
   123  */
 | 
| 
 | 
   124   t_prev = ilim[0]; sb = 0.0;
 | 
| 
 | 
   125   for (i=0; i<=haz_lfd->n; i++)
 | 
| 
 | 
   126   { if (i==haz_lfd->n)
 | 
| 
 | 
   127     { dfx[0] = tmax-t[0];
 | 
| 
 | 
   128       for (j=1; j<d; j++) dfx[j] = 0.0;
 | 
| 
 | 
   129       eb = exp(sb/haz_lfd->n);
 | 
| 
 | 
   130     }
 | 
| 
 | 
   131     else
 | 
| 
 | 
   132     { eb = exp(base(haz_lfd,i)); sb += base(haz_lfd,i);
 | 
| 
 | 
   133       for (j=0; j<d; j++) dfx[j] = datum(haz_lfd,j,i)-t[j];
 | 
| 
 | 
   134     }
 | 
| 
 | 
   135 
 | 
| 
 | 
   136     if (dfx[0]>ilim[0]) /* else it doesn't contribute */
 | 
| 
 | 
   137     {
 | 
| 
 | 
   138 /* time integral */
 | 
| 
 | 
   139       il1 = (dfx[0]>ilim[d]) ? ilim[d] : dfx[0];
 | 
| 
 | 
   140       if (il1 != t_prev) /* don't repeat! */
 | 
| 
 | 
   141       { st = onedint(haz_sp,ncf,ilim[0]/hs,il1/hs,prod_wk[0]);
 | 
| 
 | 
   142         if (st>0) return(st);
 | 
| 
 | 
   143         hj = eb;
 | 
| 
 | 
   144         for (j=0; j<=2*deg(haz_sp); j++)
 | 
| 
 | 
   145         { hj *= hs;
 | 
| 
 | 
   146           prod_wk[0][j] *= hj;
 | 
| 
 | 
   147         }
 | 
| 
 | 
   148         t_prev = il1;
 | 
| 
 | 
   149       }
 | 
| 
 | 
   150 
 | 
| 
 | 
   151 /* covariate terms */
 | 
| 
 | 
   152       for (j=1; j<d; j++)
 | 
| 
 | 
   153       {
 | 
| 
 | 
   154         ef = 0.0;
 | 
| 
 | 
   155         for (k=deg(haz_sp); k>0; k--) ef = (ef+dfx[j])*cf[1+(k-1)*d+j];
 | 
| 
 | 
   156         ef = exp(ef);
 | 
| 
 | 
   157         prod_wk[j][0] = ef * W(dfx[j]/(h*haz_lfd->sca[j]),ker(haz_sp));
 | 
| 
 | 
   158         for (k=1; k<=2*deg(haz_sp); k++)
 | 
| 
 | 
   159           prod_wk[j][k] = prod_wk[j][k-1] * dfx[j];
 | 
| 
 | 
   160       }
 | 
| 
 | 
   161 
 | 
| 
 | 
   162 /*  add to the integration.  */
 | 
| 
 | 
   163       prodintresp(resp,prod_wk,d,deg(haz_sp),p);
 | 
| 
 | 
   164     } /* if dfx0 > ilim0 */
 | 
| 
 | 
   165   } /* n loop */
 | 
| 
 | 
   166 
 | 
| 
 | 
   167 /* symmetrize */
 | 
| 
 | 
   168   for (k=0; k<p; k++)
 | 
| 
 | 
   169     for (j=k; j<p; j++)
 | 
| 
 | 
   170       resp[j*p+k] = resp[k*p+j];
 | 
| 
 | 
   171   return(LF_OK);
 | 
| 
 | 
   172 }
 | 
| 
 | 
   173 
 | 
| 
 | 
   174 int hazint(t,resp,resp1,cf,h)
 | 
| 
 | 
   175 double *t, *resp, *resp1, *cf, h;
 | 
| 
 | 
   176 { if (haz_lfd->d==1) return(hazint_prod(t,resp,resp1,cf,h));
 | 
| 
 | 
   177   if (kt(haz_sp)==KPROD) return(hazint_prod(t,resp,resp1,cf,h));
 | 
| 
 | 
   178 
 | 
| 
 | 
   179   return(hazint_sph(t,resp,resp1,cf,h));
 | 
| 
 | 
   180 }
 | 
| 
 | 
   181 
 | 
| 
 | 
   182 void haz_init(lfd,des,sp,il)
 | 
| 
 | 
   183 lfdata *lfd;
 | 
| 
 | 
   184 design *des;
 | 
| 
 | 
   185 smpar *sp;
 | 
| 
 | 
   186 double *il;
 | 
| 
 | 
   187 { int i;
 | 
| 
 | 
   188   
 | 
| 
 | 
   189   haz_lfd = lfd;
 | 
| 
 | 
   190   haz_sp  = sp;
 | 
| 
 | 
   191 
 | 
| 
 | 
   192   tmax = datum(lfd,0,0);
 | 
| 
 | 
   193   for (i=1; i<lfd->n; i++) tmax = MAX(tmax,datum(lfd,0,i));
 | 
| 
 | 
   194   ff = des->xtwx.wk;
 | 
| 
 | 
   195   for (i=0; i<2*lfd->d; i++) ilim[i] = il[i];
 | 
| 
 | 
   196 }
 | 
| 
 | 
   197 /*
 | 
| 
 | 
   198  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
   199  */
 | 
| 
 | 
   200 /*
 | 
| 
 | 
   201  *
 | 
| 
 | 
   202  *  Routines for one-dimensional numerical integration
 | 
| 
 | 
   203  *  in density estimation. The entry point is
 | 
| 
 | 
   204  *
 | 
| 
 | 
   205  *  onedint(cf,mi,l0,l1,resp)
 | 
| 
 | 
   206  *
 | 
| 
 | 
   207  *  which evaluates int W(u)u^j exp( P(u) ), j=0..2*deg.
 | 
| 
 | 
   208  *  P(u) = cf[0] + cf[1]u + cf[2]u^2/2 + ... + cf[deg]u^deg/deg!
 | 
| 
 | 
   209  *  l0 and l1 are the integration limits.
 | 
| 
 | 
   210  *  The results are returned through the vector resp.
 | 
| 
 | 
   211  *
 | 
| 
 | 
   212  */
 | 
| 
 | 
   213 
 | 
| 
 | 
   214 #include "locf.h"
 | 
| 
 | 
   215 
 | 
| 
 | 
   216 static int debug;
 | 
| 
 | 
   217 
 | 
| 
 | 
   218 int exbctay(b,c,n,z) /* n-term taylor series of e^(bx+cx^2) */
 | 
| 
 | 
   219 double b, c, *z;
 | 
| 
 | 
   220 int n;
 | 
| 
 | 
   221 { double ec[20];
 | 
| 
 | 
   222   int i, j;
 | 
| 
 | 
   223   z[0] = 1;
 | 
| 
 | 
   224   for (i=1; i<=n; i++) z[i] = z[i-1]*b/i;
 | 
| 
 | 
   225   if (c==0.0) return(n);
 | 
| 
 | 
   226   if (n>=40)
 | 
| 
 | 
   227   { WARN(("exbctay limit to n<40"));
 | 
| 
 | 
   228     n = 39;
 | 
| 
 | 
   229   }
 | 
| 
 | 
   230   ec[0] = 1;
 | 
| 
 | 
   231   for (i=1; 2*i<=n; i++) ec[i] = ec[i-1]*c/i;
 | 
| 
 | 
   232   for (i=n; i>1; i--)
 | 
| 
 | 
   233     for (j=1; 2*j<=i; j++)
 | 
| 
 | 
   234       z[i] += ec[j]*z[i-2*j];
 | 
| 
 | 
   235   return(n);
 | 
| 
 | 
   236 }
 | 
| 
 | 
   237 
 | 
| 
 | 
   238 double explinjtay(l0,l1,j,cf)
 | 
| 
 | 
   239 /* int_l0^l1 x^j e^(a+bx+cx^2); exbctay aroud l1 */
 | 
| 
 | 
   240 double l0, l1, *cf;
 | 
| 
 | 
   241 int j;
 | 
| 
 | 
   242 { double tc[40], f, s;
 | 
| 
 | 
   243   int k, n;
 | 
| 
 | 
   244   if ((l0!=0.0) | (l1!=1.0)) WARN(("explinjtay: invalid l0, l1"));
 | 
| 
 | 
   245   n = exbctay(cf[1]+2*cf[2]*l1,cf[2],20,tc);
 | 
| 
 | 
   246   s = tc[0]/(j+1);
 | 
| 
 | 
   247   f = 1/(j+1);
 | 
| 
 | 
   248   for (k=1; k<=n; k++)
 | 
| 
 | 
   249   { f *= -k/(j+k+1.0);
 | 
| 
 | 
   250     s += tc[k]*f;
 | 
| 
 | 
   251   }
 | 
| 
 | 
   252   return(f);
 | 
| 
 | 
   253 }
 | 
| 
 | 
   254 
 | 
| 
 | 
   255 void explint1(l0,l1,cf,I,p) /* int x^j exp(a+bx); j=0..p-1 */
 | 
| 
 | 
   256 double l0, l1, *cf, *I;
 | 
| 
 | 
   257 int p;
 | 
| 
 | 
   258 { double y0, y1, f;
 | 
| 
 | 
   259   int j, k, k1;
 | 
| 
 | 
   260   y0 = mut_exp(cf[0]+l0*cf[1]);
 | 
| 
 | 
   261   y1 = mut_exp(cf[0]+l1*cf[1]);
 | 
| 
 | 
   262   if (p<2*fabs(cf[1])) k = p; else k = (int)fabs(cf[1]);
 | 
| 
 | 
   263 
 | 
| 
 | 
   264   if (k>0)
 | 
| 
 | 
   265   { I[0] = (y1-y0)/cf[1];
 | 
| 
 | 
   266     for (j=1; j<k; j++) /* forward steps for small j */
 | 
| 
 | 
   267     { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   268       I[j] = (y1-y0-j*I[j-1])/cf[1];
 | 
| 
 | 
   269     }
 | 
| 
 | 
   270     if (k==p) return;
 | 
| 
 | 
   271     y1 *= l1; y0 *= l0;
 | 
| 
 | 
   272   }
 | 
| 
 | 
   273 
 | 
| 
 | 
   274   f = 1; k1 = k;
 | 
| 
 | 
   275   while ((k<50) && (f>1.0e-8)) /* initially Ik = diff(x^{k+1}e^{a+bx}) */
 | 
| 
 | 
   276   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   277     I[k] = y1-y0;
 | 
| 
 | 
   278     if (k>=p) f *= fabs(cf[1])/(k+1);
 | 
| 
 | 
   279     k++;
 | 
| 
 | 
   280   }
 | 
| 
 | 
   281   if (k==50) WARN(("explint1: want k>50"));
 | 
| 
 | 
   282   I[k] = 0.0;
 | 
| 
 | 
   283   for (j=k-1; j>=k1; j--) /* now do back step recursion */
 | 
| 
 | 
   284     I[j] = (I[j]-cf[1]*I[j+1])/(j+1);
 | 
| 
 | 
   285 }
 | 
| 
 | 
   286 
 | 
| 
 | 
   287 void explintyl(l0,l1,cf,I,p) /* small c, use taylor series and explint1 */
 | 
| 
 | 
   288 double l0, l1, *cf, *I;
 | 
| 
 | 
   289 int p;
 | 
| 
 | 
   290 { int i;
 | 
| 
 | 
   291   double c;
 | 
| 
 | 
   292   explint1(l0,l1,cf,I,p+8);
 | 
| 
 | 
   293   c = cf[2];
 | 
| 
 | 
   294   for (i=0; i<p; i++)
 | 
| 
 | 
   295     I[i] = (((I[i+8]*c/4+I[i+6])*c/3+I[i+4])*c/2+I[i+2])*c+I[i];
 | 
| 
 | 
   296 }
 | 
| 
 | 
   297 
 | 
| 
 | 
   298 void solvetrid(X,y,m)
 | 
| 
 | 
   299 double *X, *y;
 | 
| 
 | 
   300 int m;
 | 
| 
 | 
   301 { int i;
 | 
| 
 | 
   302   double s;
 | 
| 
 | 
   303   for (i=1; i<m; i++)
 | 
| 
 | 
   304   { s = X[3*i]/X[3*i-2];
 | 
| 
 | 
   305     X[3*i] = 0; X[3*i+1] -= s*X[3*i-1];
 | 
| 
 | 
   306     y[i] -= s*y[i-1];
 | 
| 
 | 
   307   }
 | 
| 
 | 
   308   for (i=m-2; i>=0; i--)
 | 
| 
 | 
   309   { s = X[3*i+2]/X[3*i+4];
 | 
| 
 | 
   310     X[3*i+2] = 0;
 | 
| 
 | 
   311     y[i] -= s*y[i+1];
 | 
| 
 | 
   312   }
 | 
| 
 | 
   313   for (i=0; i<m; i++) y[i] /= X[3*i+1];
 | 
| 
 | 
   314 }
 | 
| 
 | 
   315 
 | 
| 
 | 
   316 void initi0i1(I,cf,y0,y1,l0,l1)
 | 
| 
 | 
   317 double *I, *cf, y0, y1, l0, l1;
 | 
| 
 | 
   318 { double a0, a1, c, d, bi;
 | 
| 
 | 
   319   d = -cf[1]/(2*cf[2]); c = sqrt(2*fabs(cf[2]));
 | 
| 
 | 
   320   a0 = c*(l0-d); a1 = c*(l1-d);
 | 
| 
 | 
   321   if (cf[2]<0)
 | 
| 
 | 
   322   { bi = mut_exp(cf[0]+cf[1]*d+cf[2]*d*d)/c;
 | 
| 
 | 
   323     if (a0>0)
 | 
| 
 | 
   324     { if (a0>6) I[0] = (y0*ptail(-a0)-y1*ptail(-a1))/c;
 | 
| 
 | 
   325       else I[0] = S2PI*(mut_pnorm(-a0)-mut_pnorm(-a1))*bi;
 | 
| 
 | 
   326     }
 | 
| 
 | 
   327     else
 | 
| 
 | 
   328     { if (a1< -6) I[0] = (y1*ptail(a1)-y0*ptail(a0))/c;
 | 
| 
 | 
   329       else I[0] = S2PI*(mut_pnorm(a1)-mut_pnorm(a0))*bi;
 | 
| 
 | 
   330     }
 | 
| 
 | 
   331   }
 | 
| 
 | 
   332   else
 | 
| 
 | 
   333     I[0] = (y1*mut_daws(a1)-y0*mut_daws(a0))/c;
 | 
| 
 | 
   334   I[1] = (y1-y0)/(2*cf[2])+d*I[0];
 | 
| 
 | 
   335 }
 | 
| 
 | 
   336 
 | 
| 
 | 
   337 void explinsid(l0,l1,cf,I,p) /* large b; don't use fwd recursion */
 | 
| 
 | 
   338 double l0, l1, *cf, *I;
 | 
| 
 | 
   339 int p;
 | 
| 
 | 
   340 { int k, k0, k1, k2;
 | 
| 
 | 
   341   double y0, y1, Z[150];
 | 
| 
 | 
   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);
 | 
| 
 | 
   343  
 | 
| 
 | 
   344   k0 = 2;
 | 
| 
 | 
   345   k1 = (int)(fabs(cf[1])+fabs(2*cf[2]));
 | 
| 
 | 
   346   if (k1<2) k1 = 2;
 | 
| 
 | 
   347   if (k1>p+20) k1 = p+20;
 | 
| 
 | 
   348   k2 = p+20;
 | 
| 
 | 
   349 
 | 
| 
 | 
   350 if (k2>50) { mut_printf("onedint: k2 warning\n"); k2 = 50; }
 | 
| 
 | 
   351   if (debug) mut_printf("k0 %2d  k1 %2d  k2 %2d  p %2d\n",k0,k1,k2,p);
 | 
| 
 | 
   352 
 | 
| 
 | 
   353   y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
 | 
| 
 | 
   354   y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
 | 
| 
 | 
   355   initi0i1(I,cf,y0,y1,l0,l1);
 | 
| 
 | 
   356 if (debug) mut_printf("i0 %8.5f  i1 %8.5f\n",I[0],I[1]);
 | 
| 
 | 
   357 
 | 
| 
 | 
   358   y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
 | 
| 
 | 
   359   if (k0<k1) /* center steps; initially x^k*exp(...) */
 | 
| 
 | 
   360     for (k=k0; k<k1; k++)
 | 
| 
 | 
   361     { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   362       I[k] = y1-y0;
 | 
| 
 | 
   363       Z[3*k] = k; Z[3*k+1] = cf[1]; Z[3*k+2] = 2*cf[2];
 | 
| 
 | 
   364     }
 | 
| 
 | 
   365    
 | 
| 
 | 
   366   y1 *= l1; y0 *= l0; /* should be x^(k1)*exp(..) */
 | 
| 
 | 
   367 if (debug) mut_printf("k1 %2d  y0 %8.5f  y1 %8.5f\n",k1,y0,y1);
 | 
| 
 | 
   368   for (k=k1; k<k2; k++)
 | 
| 
 | 
   369   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   370     I[k] = y1-y0;
 | 
| 
 | 
   371   }
 | 
| 
 | 
   372   I[k2] = I[k2+1] = 0.0;
 | 
| 
 | 
   373   for (k=k2-1; k>=k1; k--)
 | 
| 
 | 
   374     I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
 | 
| 
 | 
   375 
 | 
| 
 | 
   376   if (k0<k1)
 | 
| 
 | 
   377   { I[k0] -= k0*I[k0-1];
 | 
| 
 | 
   378     I[k1-1] -= 2*cf[2]*I[k1];
 | 
| 
 | 
   379     Z[3*k0] = Z[3*k1-1] = 0;
 | 
| 
 | 
   380     solvetrid(&Z[3*k0],&I[k0],k1-k0);
 | 
| 
 | 
   381   }
 | 
| 
 | 
   382 if (debug)
 | 
| 
 | 
   383 { mut_printf("explinsid:\n");
 | 
| 
 | 
   384   for (k=0; k<p; k++) mut_printf("  %8.5f\n",I[k]);
 | 
| 
 | 
   385 }
 | 
| 
 | 
   386 }
 | 
| 
 | 
   387 
 | 
| 
 | 
   388 void explinbkr(l0,l1,cf,I,p) /* small b,c; use back recursion */
 | 
| 
 | 
   389 double l0, l1, *cf, *I;
 | 
| 
 | 
   390 int p;
 | 
| 
 | 
   391 { int k, km;
 | 
| 
 | 
   392   double y0, y1;
 | 
| 
 | 
   393   y0 = mut_exp(cf[0]+l0*(cf[1]+cf[2]*l0));
 | 
| 
 | 
   394   y1 = mut_exp(cf[0]+l1*(cf[1]+cf[2]*l1));
 | 
| 
 | 
   395   km = p+10;
 | 
| 
 | 
   396   for (k=0; k<=km; k++)
 | 
| 
 | 
   397   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   398     I[k] = y1-y0;
 | 
| 
 | 
   399   }
 | 
| 
 | 
   400   I[km+1] = I[km+2] = 0;
 | 
| 
 | 
   401   for (k=km; k>=0; k--)
 | 
| 
 | 
   402     I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
 | 
| 
 | 
   403 }
 | 
| 
 | 
   404 
 | 
| 
 | 
   405 void explinfbk0(l0,l1,cf,I,p) /* fwd and bac recur; b=0; c<0 */
 | 
| 
 | 
   406 double l0, l1, *cf, *I;
 | 
| 
 | 
   407 int p;
 | 
| 
 | 
   408 { double y0, y1, f1, f2, f, ml2;
 | 
| 
 | 
   409   int k, ks;
 | 
| 
 | 
   410 
 | 
| 
 | 
   411   y0 = mut_exp(cf[0]+l0*l0*cf[2]);
 | 
| 
 | 
   412   y1 = mut_exp(cf[0]+l1*l1*cf[2]);
 | 
| 
 | 
   413   initi0i1(I,cf,y0,y1,l0,l1);
 | 
| 
 | 
   414 
 | 
| 
 | 
   415   ml2 = MAX(l0*l0,l1*l1);
 | 
| 
 | 
   416   ks = 1+(int)(2*fabs(cf[2])*ml2);
 | 
| 
 | 
   417   if (ks<2) ks = 2;
 | 
| 
 | 
   418   if (ks>p-3) ks = p;
 | 
| 
 | 
   419 
 | 
| 
 | 
   420   /* forward recursion for k < ks */
 | 
| 
 | 
   421   for (k=2; k<ks; k++)
 | 
| 
 | 
   422   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   423     I[k] = (y1-y0-(k-1)*I[k-2])/(2*cf[2]);
 | 
| 
 | 
   424   }
 | 
| 
 | 
   425   if (ks==p) return;
 | 
| 
 | 
   426 
 | 
| 
 | 
   427   y1 *= l1*l1; y0 *= l0*l0;
 | 
| 
 | 
   428   for (k=ks; k<p; k++) /* set I[k] = x^{k+1}e^(a+cx^2) | {l0,l1} */
 | 
| 
 | 
   429   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   430     I[k] = y1-y0;
 | 
| 
 | 
   431   }
 | 
| 
 | 
   432 
 | 
| 
 | 
   433   /* initialize I[p-2] and I[p-1] */
 | 
| 
 | 
   434   f1 = 1.0/p; f2 = 1.0/(p-1);
 | 
| 
 | 
   435   I[p-1] *= f1; I[p-2] *= f2;
 | 
| 
 | 
   436   k = p; f = 1.0;
 | 
| 
 | 
   437   while (f>1.0e-8)
 | 
| 
 | 
   438   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   439     if ((k-p)%2==0) /* add to I[p-2] */
 | 
| 
 | 
   440     { f2 *= -2*cf[2]/(k+1);
 | 
| 
 | 
   441       I[p-2] += (y1-y0)*f2;
 | 
| 
 | 
   442     }
 | 
| 
 | 
   443     else /* add to I[p-1] */
 | 
| 
 | 
   444     { f1 *= -2*cf[2]/(k+1);
 | 
| 
 | 
   445       I[p-1] += (y1-y0)*f1;
 | 
| 
 | 
   446       f *= 2*fabs(cf[2])*ml2/(k+1);
 | 
| 
 | 
   447     }
 | 
| 
 | 
   448     k++;
 | 
| 
 | 
   449   }
 | 
| 
 | 
   450   
 | 
| 
 | 
   451   /* use back recursion for I[ks..(p-3)] */
 | 
| 
 | 
   452   for (k=p-3; k>=ks; k--)
 | 
| 
 | 
   453     I[k] = (I[k]-2*cf[2]*I[k+2])/(k+1);
 | 
| 
 | 
   454 }
 | 
| 
 | 
   455 
 | 
| 
 | 
   456 void explinfbk(l0,l1,cf,I,p) /* fwd and bac recur; b not too large */
 | 
| 
 | 
   457 double l0, l1, *cf, *I;
 | 
| 
 | 
   458 int p;
 | 
| 
 | 
   459 { double y0, y1;
 | 
| 
 | 
   460   int k, ks, km;
 | 
| 
 | 
   461 
 | 
| 
 | 
   462   y0 = mut_exp(cf[0]+l0*(cf[1]+l0*cf[2]));
 | 
| 
 | 
   463   y1 = mut_exp(cf[0]+l1*(cf[1]+l1*cf[2]));
 | 
| 
 | 
   464   initi0i1(I,cf,y0,y1,l0,l1);
 | 
| 
 | 
   465 
 | 
| 
 | 
   466   ks = (int)(3*fabs(cf[2]));
 | 
| 
 | 
   467   if (ks<3) ks = 3;
 | 
| 
 | 
   468   if (ks>0.75*p) ks = p; /* stretch the forward recurs as far as poss. */
 | 
| 
 | 
   469   /* forward recursion for k < ks */
 | 
| 
 | 
   470   for (k=2; k<ks; k++)
 | 
| 
 | 
   471   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   472     I[k] = (y1-y0-cf[1]*I[k-1]-(k-1)*I[k-2])/(2*cf[2]);
 | 
| 
 | 
   473   }
 | 
| 
 | 
   474   if (ks==p) return;
 | 
| 
 | 
   475 
 | 
| 
 | 
   476   km = p+15;
 | 
| 
 | 
   477   y1 *= l1*l1; y0 *= l0*l0;
 | 
| 
 | 
   478   for (k=ks; k<=km; k++)
 | 
| 
 | 
   479   { y1 *= l1; y0 *= l0;
 | 
| 
 | 
   480     I[k] = y1-y0;
 | 
| 
 | 
   481   }
 | 
| 
 | 
   482   I[km+1] = I[km+2] = 0.0;
 | 
| 
 | 
   483   for (k=km; k>=ks; k--)
 | 
| 
 | 
   484     I[k] = (I[k]-cf[1]*I[k+1]-2*cf[2]*I[k+2])/(k+1);
 | 
| 
 | 
   485 }
 | 
| 
 | 
   486 
 | 
| 
 | 
   487 void recent(I,resp,wt,p,s,x)
 | 
| 
 | 
   488 double *I, *resp, *wt, x;
 | 
| 
 | 
   489 int p, s;
 | 
| 
 | 
   490 { int i, j;
 | 
| 
 | 
   491 
 | 
| 
 | 
   492   /* first, use W taylor series I -> resp */
 | 
| 
 | 
   493   for (i=0; i<=p; i++)
 | 
| 
 | 
   494   { resp[i] = 0.0;
 | 
| 
 | 
   495     for (j=0; j<s; j++) resp[i] += wt[j]*I[i+j];
 | 
| 
 | 
   496   }
 | 
| 
 | 
   497 
 | 
| 
 | 
   498   /* now, recenter x -> 0 */
 | 
| 
 | 
   499   if (x==0) return;
 | 
| 
 | 
   500   for (j=0; j<=p; j++) for (i=p; i>j; i--) resp[i] += x*resp[i-1];
 | 
| 
 | 
   501 }
 | 
| 
 | 
   502 
 | 
| 
 | 
   503 void recurint(l0,l2,cf,resp,p,ker)
 | 
| 
 | 
   504 double l0, l2, *cf, *resp;
 | 
| 
 | 
   505 int p, ker;
 | 
| 
 | 
   506 { int i, s;
 | 
| 
 | 
   507   double l1, d0, d1, d2, dl, z0, z1, z2, wt[20], ncf[3], I[50], r1[5], r2[5];
 | 
| 
 | 
   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);
 | 
| 
 | 
   509 
 | 
| 
 | 
   510   if (cf[2]==0) /* go straight to explint1 */
 | 
| 
 | 
   511   { s = wtaylor(wt,0.0,ker);
 | 
| 
 | 
   512 if (debug) mut_printf("case 1\n");
 | 
| 
 | 
   513     explint1(l0,l2,cf,I,p+s);
 | 
| 
 | 
   514     recent(I,resp,wt,p,s,0.0);
 | 
| 
 | 
   515     return;
 | 
| 
 | 
   516   }
 | 
| 
 | 
   517 
 | 
| 
 | 
   518   dl = l2-l0;
 | 
| 
 | 
   519   d0 = cf[1]+2*l0*cf[2];
 | 
| 
 | 
   520   d2 = cf[1]+2*l2*cf[2];
 | 
| 
 | 
   521   z0 = cf[0]+l0*(cf[1]+l0*cf[2]);
 | 
| 
 | 
   522   z2 = cf[0]+l2*(cf[1]+l2*cf[2]);
 | 
| 
 | 
   523 
 | 
| 
 | 
   524   if ((fabs(cf[1]*dl)<1) && (fabs(cf[2]*dl*dl)<1))
 | 
| 
 | 
   525   { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
 | 
| 
 | 
   526 if (debug) mut_printf("case 2\n");
 | 
| 
 | 
   527     s = wtaylor(wt,l0,ker);
 | 
| 
 | 
   528     explinbkr(0.0,dl,ncf,I,p+s);
 | 
| 
 | 
   529     recent(I,resp,wt,p,s,l0);
 | 
| 
 | 
   530     return;
 | 
| 
 | 
   531   }
 | 
| 
 | 
   532 
 | 
| 
 | 
   533   if (fabs(cf[2]*dl*dl)<0.001) /* small c, use explint1+tay.ser */
 | 
| 
 | 
   534   { ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
 | 
| 
 | 
   535 if (debug) mut_printf("case small c\n");
 | 
| 
 | 
   536     s = wtaylor(wt,l0,ker);
 | 
| 
 | 
   537     explintyl(0.0,l2-l0,ncf,I,p+s);
 | 
| 
 | 
   538     recent(I,resp,wt,p,s,l0);
 | 
| 
 | 
   539     return;
 | 
| 
 | 
   540   }
 | 
| 
 | 
   541 
 | 
| 
 | 
   542   if (d0*d2<=0) /* max/min in [l0,l2] */
 | 
| 
 | 
   543   { l1 = -cf[1]/(2*cf[2]);
 | 
| 
 | 
   544     z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
 | 
| 
 | 
   545     d1 = 0.0;
 | 
| 
 | 
   546     if (cf[2]<0) /* peak, integrate around l1 */
 | 
| 
 | 
   547     { s = wtaylor(wt,l1,ker);
 | 
| 
 | 
   548       ncf[0] = z1; ncf[1] = 0.0; ncf[2] = cf[2];
 | 
| 
 | 
   549 if (debug) mut_printf("case peak  p %2d  s %2d\n",p,s);
 | 
| 
 | 
   550       explinfbk0(l0-l1,l2-l1,ncf,I,p+s);
 | 
| 
 | 
   551       recent(I,resp,wt,p,s,l1);
 | 
| 
 | 
   552       return;
 | 
| 
 | 
   553     }
 | 
| 
 | 
   554   }
 | 
| 
 | 
   555 
 | 
| 
 | 
   556   if ((d0-2*cf[2]*dl)*(d2+2*cf[2]*dl)<0) /* max/min is close to [l0,l2] */
 | 
| 
 | 
   557   { l1 = -cf[1]/(2*cf[2]);
 | 
| 
 | 
   558     z1 = cf[0]+l1*(cf[1]+l1*cf[2]);
 | 
| 
 | 
   559     if (l1<l0) { l1 = l0; z1 = z0; }
 | 
| 
 | 
   560     if (l1>l2) { l1 = l2; z1 = z2; }
 | 
| 
 | 
   561 
 | 
| 
 | 
   562     if ((z1>=z0) & (z1>=z2)) /* peak; integrate around l1 */
 | 
| 
 | 
   563     { s = wtaylor(wt,l1,ker);
 | 
| 
 | 
   564 if (debug) mut_printf("case 4\n");
 | 
| 
 | 
   565       d1 = cf[1]+2*l1*cf[2];
 | 
| 
 | 
   566       ncf[0] = z1; ncf[1] = d1; ncf[2] = cf[2];
 | 
| 
 | 
   567       explinfbk(l0-l1,l2-l1,ncf,I,p+s);
 | 
| 
 | 
   568       recent(I,resp,wt,p,s,l1);
 | 
| 
 | 
   569       return;
 | 
| 
 | 
   570     }
 | 
| 
 | 
   571 
 | 
| 
 | 
   572     /* trough; integrate [l0,l1] and [l1,l2] */
 | 
| 
 | 
   573     for (i=0; i<=p; i++) r1[i] = r2[i] = 0.0;
 | 
| 
 | 
   574     if (l0<l1)
 | 
| 
 | 
   575     { s = wtaylor(wt,l0,ker);
 | 
| 
 | 
   576 if (debug) mut_printf("case 5\n");
 | 
| 
 | 
   577       ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
 | 
| 
 | 
   578       explinfbk(0.0,l1-l0,ncf,I,p+s);
 | 
| 
 | 
   579       recent(I,r1,wt,p,s,l0);
 | 
| 
 | 
   580     }
 | 
| 
 | 
   581     if (l1<l2)
 | 
| 
 | 
   582     { s = wtaylor(wt,l2,ker);
 | 
| 
 | 
   583 if (debug) mut_printf("case 6\n");
 | 
| 
 | 
   584       ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
 | 
| 
 | 
   585       explinfbk(l1-l2,0.0,ncf,I,p+s);
 | 
| 
 | 
   586       recent(I,r2,wt,p,s,l2);
 | 
| 
 | 
   587     }
 | 
| 
 | 
   588     for (i=0; i<=p; i++) resp[i] = r1[i]+r2[i];
 | 
| 
 | 
   589     return;
 | 
| 
 | 
   590   }
 | 
| 
 | 
   591 
 | 
| 
 | 
   592   /* Now, quadratic is monotone on [l0,l2]; big b; moderate c */
 | 
| 
 | 
   593   if (z2>z0+3) /* steep increase, expand around l2 */
 | 
| 
 | 
   594   { s = wtaylor(wt,l2,ker);
 | 
| 
 | 
   595 if (debug) mut_printf("case 7\n");
 | 
| 
 | 
   596 
 | 
| 
 | 
   597 
 | 
| 
 | 
   598     ncf[0] = z2; ncf[1] = d2; ncf[2] = cf[2];
 | 
| 
 | 
   599     explinsid(l0-l2,0.0,ncf,I,p+s);
 | 
| 
 | 
   600     recent(I,resp,wt,p,s,l2);
 | 
| 
 | 
   601 if (debug) mut_printf("7 resp: %8.5f %8.5f %8.5f %8.5f\n",resp[0],resp[1],resp[2],resp[3]);
 | 
| 
 | 
   602     return;
 | 
| 
 | 
   603   }
 | 
| 
 | 
   604 
 | 
| 
 | 
   605   /* bias towards expansion around l0, because it's often 0 */
 | 
| 
 | 
   606 if (debug) mut_printf("case 8\n");
 | 
| 
 | 
   607   s = wtaylor(wt,l0,ker);
 | 
| 
 | 
   608   ncf[0] = z0; ncf[1] = d0; ncf[2] = cf[2];
 | 
| 
 | 
   609   explinsid(0.0,l2-l0,ncf,I,p+s);
 | 
| 
 | 
   610   recent(I,resp,wt,p,s,l0);
 | 
| 
 | 
   611   return;
 | 
| 
 | 
   612 }
 | 
| 
 | 
   613 
 | 
| 
 | 
   614 int onedexpl(cf,deg,resp)
 | 
| 
 | 
   615 double *cf, *resp;
 | 
| 
 | 
   616 int deg;
 | 
| 
 | 
   617 { int i;
 | 
| 
 | 
   618   double f0, fr, fl;
 | 
| 
 | 
   619   if (deg>=2) LERR(("onedexpl only valid for deg=0,1"));
 | 
| 
 | 
   620   if (fabs(cf[1])>=EFACT) return(LF_BADP);
 | 
| 
 | 
   621 
 | 
| 
 | 
   622   f0 = exp(cf[0]); fl = fr = 1.0;
 | 
| 
 | 
   623   for (i=0; i<=2*deg; i++)
 | 
| 
 | 
   624   { f0 *= i+1;
 | 
| 
 | 
   625     fl /=-(EFACT+cf[1]);
 | 
| 
 | 
   626     fr /=  EFACT-cf[1];
 | 
| 
 | 
   627     resp[i] = f0*(fr-fl);
 | 
| 
 | 
   628   }
 | 
| 
 | 
   629   return(LF_OK);
 | 
| 
 | 
   630 }
 | 
| 
 | 
   631 
 | 
| 
 | 
   632 int onedgaus(cf,deg,resp)
 | 
| 
 | 
   633 double *cf, *resp;
 | 
| 
 | 
   634 int deg;
 | 
| 
 | 
   635 { int i;
 | 
| 
 | 
   636   double f0, mu, s2;
 | 
| 
 | 
   637   if (deg==3)
 | 
| 
 | 
   638   { LERR(("onedgaus only valid for deg=0,1,2"));
 | 
| 
 | 
   639     return(LF_ERR);
 | 
| 
 | 
   640   }
 | 
| 
 | 
   641   if (2*cf[2]>=GFACT*GFACT) return(LF_BADP);
 | 
| 
 | 
   642 
 | 
| 
 | 
   643   s2 = 1/(GFACT*GFACT-2*cf[2]);
 | 
| 
 | 
   644   mu = cf[1]*s2;
 | 
| 
 | 
   645   resp[0] = 1.0;
 | 
| 
 | 
   646   if (deg>=1)
 | 
| 
 | 
   647   { resp[1] = mu;
 | 
| 
 | 
   648     resp[2] = s2+mu*mu;
 | 
| 
 | 
   649     if (deg==2)
 | 
| 
 | 
   650     { resp[3] = mu*(3*s2+mu*mu);
 | 
| 
 | 
   651       resp[4] = 3*s2*s2 + mu*mu*(6*s2+mu*mu);
 | 
| 
 | 
   652     }
 | 
| 
 | 
   653   }
 | 
| 
 | 
   654   f0 = S2PI * exp(cf[0]+mu*mu/(2*s2))*sqrt(s2);
 | 
| 
 | 
   655   for (i=0; i<=2*deg; i++) resp[i] *= f0;
 | 
| 
 | 
   656   return(LF_OK);
 | 
| 
 | 
   657 }
 | 
| 
 | 
   658 
 | 
| 
 | 
   659 int onedint(sp,cf,l0,l1,resp) /* int W(u)u^j exp(..), j=0..2*deg */
 | 
| 
 | 
   660 smpar *sp;
 | 
| 
 | 
   661 double *cf, l0, l1, *resp;
 | 
| 
 | 
   662 { double u, uj, y, ncf[4], rr[5];
 | 
| 
 | 
   663   int i, j;
 | 
| 
 | 
   664 
 | 
| 
 | 
   665 if (debug) mut_printf("onedint: %f %f %f   %f %f\n",cf[0],cf[1],cf[2],l0,l1);
 | 
| 
 | 
   666 
 | 
| 
 | 
   667   if (deg(sp)<=2)
 | 
| 
 | 
   668   { for (i=0; i<3; i++) ncf[i] = (i>deg(sp)) ? 0.0 : cf[i];
 | 
| 
 | 
   669     ncf[2] /= 2;
 | 
| 
 | 
   670 
 | 
| 
 | 
   671     if (ker(sp)==WEXPL) return(onedexpl(ncf,deg(sp),resp));
 | 
| 
 | 
   672     if (ker(sp)==WGAUS) return(onedgaus(ncf,deg(sp),resp));
 | 
| 
 | 
   673 
 | 
| 
 | 
   674     if (l1>0)
 | 
| 
 | 
   675       recurint(MAX(l0,0.0),l1,ncf,resp,2*deg(sp),ker(sp));
 | 
| 
 | 
   676     else for (i=0; i<=2*deg(sp); i++) resp[i] = 0;
 | 
| 
 | 
   677 
 | 
| 
 | 
   678     if (l0<0)
 | 
| 
 | 
   679     { ncf[1] = -ncf[1];
 | 
| 
 | 
   680       l0 = -l0; l1 = -l1;
 | 
| 
 | 
   681       recurint(MAX(l1,0.0),l0,ncf,rr,2*deg(sp),ker(sp));
 | 
| 
 | 
   682     }
 | 
| 
 | 
   683     else for (i=0; i<=2*deg(sp); i++) rr[i] = 0.0;
 | 
| 
 | 
   684 
 | 
| 
 | 
   685     for (i=0; i<=2*deg(sp); i++)
 | 
| 
 | 
   686       resp[i] += (i%2==0) ? rr[i] : -rr[i];
 | 
| 
 | 
   687 
 | 
| 
 | 
   688     return(LF_OK);
 | 
| 
 | 
   689   }
 | 
| 
 | 
   690 
 | 
| 
 | 
   691   /* For degree >= 3, we use Simpson's rule. */
 | 
| 
 | 
   692   for (j=0; j<=2*deg(sp); j++) resp[j] = 0.0;
 | 
| 
 | 
   693   for (i=0; i<=de_mint; i++)
 | 
| 
 | 
   694   { u = l0+(l1-l0)*i/de_mint;
 | 
| 
 | 
   695     y = cf[0]; uj = 1;
 | 
| 
 | 
   696     for (j=1; j<=deg(sp); j++)
 | 
| 
 | 
   697     { uj *= u;
 | 
| 
 | 
   698       y += cf[j]*uj/fact[j];
 | 
| 
 | 
   699     }
 | 
| 
 | 
   700     y = (4-2*(i%2==0)-(i==0)-(i==de_mint)) *
 | 
| 
 | 
   701           W(fabs(u),ker(sp))*exp(MIN(y,300.0));
 | 
| 
 | 
   702     for (j=0; j<=2*deg(sp); j++)
 | 
| 
 | 
   703     { resp[j] += y;
 | 
| 
 | 
   704       y *= u;
 | 
| 
 | 
   705     }
 | 
| 
 | 
   706   }
 | 
| 
 | 
   707   for (j=0; j<=2*deg(sp); j++) resp[j] = resp[j]*(l1-l0)/(3*de_mint);
 | 
| 
 | 
   708   return(LF_OK);
 | 
| 
 | 
   709 }
 | 
| 
 | 
   710 /*
 | 
| 
 | 
   711  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
   712  */
 | 
| 
 | 
   713 #include "locf.h"
 | 
| 
 | 
   714 
 | 
| 
 | 
   715 extern int lf_status;
 | 
| 
 | 
   716 static double u[MXDIM], ilim[2*MXDIM], *ff, hh, *cff;
 | 
| 
 | 
   717 static lfdata *den_lfd;
 | 
| 
 | 
   718 static design *den_des;
 | 
| 
 | 
   719 static smpar *den_sp;
 | 
| 
 | 
   720 int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
 | 
| 
 | 
   721 int de_mint  = 20;
 | 
| 
 | 
   722 int de_itype = IDEFA;
 | 
| 
 | 
   723 int de_renorm= 0;
 | 
| 
 | 
   724 
 | 
| 
 | 
   725 int multint(), prodint(), gausint(), mlinint();
 | 
| 
 | 
   726 
 | 
| 
 | 
   727 #define NITYPE 7
 | 
| 
 | 
   728 static char *itype[NITYPE] = { "default", "multi", "product", "mlinear",
 | 
| 
 | 
   729                           "hazard",  "sphere", "monte" };
 | 
| 
 | 
   730 static int   ivals[NITYPE] =
 | 
| 
 | 
   731    { IDEFA, IMULT, IPROD, IMLIN, IHAZD, ISPHR, IMONT };
 | 
| 
 | 
   732 int deitype(char *z)
 | 
| 
 | 
   733 { return(pmatch(z, itype, ivals, NITYPE, IDEFA));
 | 
| 
 | 
   734 }
 | 
| 
 | 
   735 
 | 
| 
 | 
   736 void prresp(coef,resp,p)
 | 
| 
 | 
   737 double *coef, *resp;
 | 
| 
 | 
   738 int p;
 | 
| 
 | 
   739 { int i, j;
 | 
| 
 | 
   740   mut_printf("Coefficients:\n");
 | 
| 
 | 
   741   for (i=0; i<p; i++) mut_printf("%8.5f ",coef[i]);
 | 
| 
 | 
   742   mut_printf("\n");
 | 
| 
 | 
   743   mut_printf("Response matrix:\n");
 | 
| 
 | 
   744   for (i=0; i<p; i++)
 | 
| 
 | 
   745   { for (j=0; j<p; j++) mut_printf("%9.6f, ",resp[i+j*p]);
 | 
| 
 | 
   746     mut_printf("\n");
 | 
| 
 | 
   747   }
 | 
| 
 | 
   748 }
 | 
| 
 | 
   749 
 | 
| 
 | 
   750 int mif(u,d,resp,M)
 | 
| 
 | 
   751 double *u, *resp, *M;
 | 
| 
 | 
   752 int d;
 | 
| 
 | 
   753 { double wt;
 | 
| 
 | 
   754   int i, j, p;
 | 
| 
 | 
   755 
 | 
| 
 | 
   756   p = den_des->p;
 | 
| 
 | 
   757   wt = weight(den_lfd, den_sp, u, NULL, hh, 0, 0.0);
 | 
| 
 | 
   758   if (wt==0)
 | 
| 
 | 
   759   { setzero(resp,p*p);
 | 
| 
 | 
   760     return(p*p);
 | 
| 
 | 
   761   }
 | 
| 
 | 
   762 
 | 
| 
 | 
   763   fitfun(den_lfd, den_sp, u,NULL,ff,NULL);
 | 
| 
 | 
   764   if (link(den_sp)==LLOG)
 | 
| 
 | 
   765     wt *= mut_exp(innerprod(ff,cff,p));
 | 
| 
 | 
   766   for (i=0; i<p; i++)
 | 
| 
 | 
   767     for (j=0; j<p; j++)
 | 
| 
 | 
   768       resp[i*p+j] = wt*ff[i]*ff[j];
 | 
| 
 | 
   769   return(p*p);
 | 
| 
 | 
   770 }
 | 
| 
 | 
   771 
 | 
| 
 | 
   772 int multint(t,resp1,resp2,cf,h)
 | 
| 
 | 
   773 double *t, *resp1, *resp2, *cf, h;
 | 
| 
 | 
   774 { int d, i, mg[MXDIM];
 | 
| 
 | 
   775 
 | 
| 
 | 
   776   if (ker(den_sp)==WGAUS) return(gausint(t,resp1,resp2,cf,h,den_lfd->sca));
 | 
| 
 | 
   777 
 | 
| 
 | 
   778   d = den_lfd->d;
 | 
| 
 | 
   779   for (i=0; i<d; i++) mg[i] = de_mint;
 | 
| 
 | 
   780 
 | 
| 
 | 
   781   hh = h;
 | 
| 
 | 
   782   cff= cf;
 | 
| 
 | 
   783   simpsonm(mif,ilim,&ilim[d],d,resp1,mg,resp2);
 | 
| 
 | 
   784   return(LF_OK);
 | 
| 
 | 
   785 }
 | 
| 
 | 
   786 
 | 
| 
 | 
   787 int mlinint(t,resp1,resp2,cf,h)
 | 
| 
 | 
   788 double *t, *resp1, *resp2, *cf, h;
 | 
| 
 | 
   789 {
 | 
| 
 | 
   790   double hd, nb, wt, wu, g[4], w0, w1, v, *sca;
 | 
| 
 | 
   791   int d, p, i, j, jmax, k, l, z, jj[2];
 | 
| 
 | 
   792 
 | 
| 
 | 
   793   d = den_lfd->d; p = den_des->p; sca = den_lfd->sca;
 | 
| 
 | 
   794   hd = 1;
 | 
| 
 | 
   795   for (i=0; i<d; i++) hd *= h*sca[i];
 | 
| 
 | 
   796 
 | 
| 
 | 
   797   if (link(den_sp)==LIDENT)
 | 
| 
 | 
   798   { setzero(resp1,p*p);
 | 
| 
 | 
   799     resp1[0] = wint(d,NULL,0,ker(den_sp))*hd;
 | 
| 
 | 
   800     if (deg(den_sp)==0) return(LF_OK);
 | 
| 
 | 
   801     jj[0] = 2; w0 = wint(d,jj,1,ker(den_sp))*hd*h*h;
 | 
| 
 | 
   802     for (i=0; i<d; i++) resp1[(i+1)*p+i+1] = w0*sca[i]*sca[i];
 | 
| 
 | 
   803     if (deg(den_sp)==1) return(LF_OK);
 | 
| 
 | 
   804     for (i=0; i<d; i++)
 | 
| 
 | 
   805     { j = p-(d-i)*(d-i+1)/2;
 | 
| 
 | 
   806       resp1[j] = resp1[p*j] = w0*sca[i]*sca[i]/2;
 | 
| 
 | 
   807     }
 | 
| 
 | 
   808     if (d>1)
 | 
| 
 | 
   809     { jj[1] = 2;
 | 
| 
 | 
   810       w0 = wint(d,jj,2,ker(den_sp)) * hd*h*h*h*h;
 | 
| 
 | 
   811     }
 | 
| 
 | 
   812     jj[0] = 4;
 | 
| 
 | 
   813     w1 = wint(d,jj,1,ker(den_sp)) * hd*h*h*h*h/4;
 | 
| 
 | 
   814     z = d+1;
 | 
| 
 | 
   815     for (i=0; i<d; i++)
 | 
| 
 | 
   816     { k = p-(d-i)*(d-i+1)/2;
 | 
| 
 | 
   817       for (j=i; j<d; j++)
 | 
| 
 | 
   818       { l = p-(d-j)*(d-j+1)/2;
 | 
| 
 | 
   819         if (i==j) resp1[z*p+z] = w1*SQR(sca[i])*SQR(sca[i]);
 | 
| 
 | 
   820         else
 | 
| 
 | 
   821         { resp1[z*p+z] = w0*SQR(sca[i])*SQR(sca[j]);
 | 
| 
 | 
   822           resp1[k*p+l] = resp1[k+p*l] = w0/4*SQR(sca[i])*SQR(sca[j]);
 | 
| 
 | 
   823         }
 | 
| 
 | 
   824         z++;
 | 
| 
 | 
   825     } }
 | 
| 
 | 
   826     return(LF_OK);
 | 
| 
 | 
   827   }
 | 
| 
 | 
   828   switch(deg(den_sp))
 | 
| 
 | 
   829   { case 0:
 | 
| 
 | 
   830       resp1[0] = mut_exp(cf[0])*wint(d,NULL,0,ker(den_sp))*hd;
 | 
| 
 | 
   831       return(LF_OK);
 | 
| 
 | 
   832     case 1:
 | 
| 
 | 
   833       nb = 0.0;
 | 
| 
 | 
   834       for (i=1; i<=d; i++)
 | 
| 
 | 
   835       { v = h*cf[i]*sca[i-1];
 | 
| 
 | 
   836         nb += v*v;
 | 
| 
 | 
   837       }
 | 
| 
 | 
   838       if (ker(den_sp)==WGAUS)
 | 
| 
 | 
   839       { w0 = 1/(GFACT*GFACT);
 | 
| 
 | 
   840         g[0] = mut_exp(cf[0]+w0*nb/2+d*log(S2PI/2.5));
 | 
| 
 | 
   841         g[1] = g[3] = g[0]*w0;
 | 
| 
 | 
   842         g[2] = g[0]*w0*w0;
 | 
| 
 | 
   843       }
 | 
| 
 | 
   844       else
 | 
| 
 | 
   845       { wt = wu = mut_exp(cf[0]);
 | 
| 
 | 
   846         w0 = wint(d,NULL,0,ker(den_sp)); g[0] = wt*w0;
 | 
| 
 | 
   847         g[1] = g[2] = g[3] = 0.0;
 | 
| 
 | 
   848         j = 0; jmax = (d+2)*de_mint;
 | 
| 
 | 
   849         while ((j<jmax) && (wt*w0/g[0]>1.0e-8))
 | 
| 
 | 
   850         { j++;
 | 
| 
 | 
   851           jj[0] = 2*j; w0 = wint(d,jj,1,ker(den_sp));
 | 
| 
 | 
   852           if (d==1) g[3] += wt * w0;
 | 
| 
 | 
   853           else
 | 
| 
 | 
   854           { jj[0] = 2; jj[1] = 2*j-2; w1 = wint(d,jj,2,ker(den_sp));
 | 
| 
 | 
   855             g[3] += wt*w1;
 | 
| 
 | 
   856             g[2] += wu*(w0-w1);
 | 
| 
 | 
   857           }
 | 
| 
 | 
   858           wt /= (2*j-1.0); g[1] += wt*w0;
 | 
| 
 | 
   859           wt *= nb/(2*j); g[0] += wt*w0;
 | 
| 
 | 
   860           wu /= (2*j-1.0)*(2*j);
 | 
| 
 | 
   861           if (j>1) wu *= nb;
 | 
| 
 | 
   862         }
 | 
| 
 | 
   863         if (j==jmax) WARN(("mlinint: series not converged"));
 | 
| 
 | 
   864       }
 | 
| 
 | 
   865       g[0] *= hd; g[1] *= hd;
 | 
| 
 | 
   866       g[2] *= hd; g[3] *= hd;
 | 
| 
 | 
   867       resp1[0] = g[0];
 | 
| 
 | 
   868       for (i=1; i<=d; i++)
 | 
| 
 | 
   869       { resp1[i] = resp1[(d+1)*i] = cf[i]*SQR(h*sca[i-1])*g[1];
 | 
| 
 | 
   870         for (j=1; j<=d; j++)
 | 
| 
 | 
   871         { resp1[(d+1)*i+j] = (i==j) ? g[3]*SQR(h*sca[i-1]) : 0;
 | 
| 
 | 
   872           resp1[(d+1)*i+j] += g[2]*SQR(h*h*sca[i-1]*sca[j-1])*cf[i]*cf[j];
 | 
| 
 | 
   873         }
 | 
| 
 | 
   874       }
 | 
| 
 | 
   875       return(LF_OK);
 | 
| 
 | 
   876   }
 | 
| 
 | 
   877   LERR(("mlinint: deg=0,1 only"));
 | 
| 
 | 
   878   return(LF_ERR);
 | 
| 
 | 
   879 }
 | 
| 
 | 
   880 
 | 
| 
 | 
   881 void prodintresp(resp,prod_wk,dim,deg,p)
 | 
| 
 | 
   882 double *resp, prod_wk[MXDIM][2*MXDEG+1];
 | 
| 
 | 
   883 int dim, deg, p;
 | 
| 
 | 
   884 { double prod;
 | 
| 
 | 
   885   int i, j, k, j1, k1;
 | 
| 
 | 
   886 
 | 
| 
 | 
   887   prod = 1.0;
 | 
| 
 | 
   888   for (i=0; i<dim; i++) prod *= prod_wk[i][0];
 | 
| 
 | 
   889   resp[0] += prod;
 | 
| 
 | 
   890   if (deg==0) return;
 | 
| 
 | 
   891 
 | 
| 
 | 
   892   for (j1=1; j1<=deg; j1++)
 | 
| 
 | 
   893   { for (j=0; j<dim; j++)
 | 
| 
 | 
   894     { prod = 1.0;
 | 
| 
 | 
   895       for (i=0; i<dim; i++) prod *= prod_wk[i][j1*(j==i)];
 | 
| 
 | 
   896       prod /= fact[j1];
 | 
| 
 | 
   897       resp[1 + (j1-1)*dim +j] += prod;
 | 
| 
 | 
   898     }
 | 
| 
 | 
   899   }
 | 
| 
 | 
   900 
 | 
| 
 | 
   901   for (k1=1; k1<=deg; k1++)
 | 
| 
 | 
   902     for (j1=k1; j1<=deg; j1++)
 | 
| 
 | 
   903     { for (k=0; k<dim; k++)
 | 
| 
 | 
   904         for (j=0; j<dim; j++)
 | 
| 
 | 
   905         { prod = 1.0;
 | 
| 
 | 
   906           for (i=0; i<dim; i++) prod *= prod_wk[i][k1*(k==i) + j1*(j==i)];
 | 
| 
 | 
   907           prod /= fact[k1]*fact[j1];
 | 
| 
 | 
   908           resp[ (1+(k1-1)*dim+k)*p + 1+(j1-1)*dim+j] += prod;
 | 
| 
 | 
   909         }
 | 
| 
 | 
   910     }
 | 
| 
 | 
   911 }
 | 
| 
 | 
   912 
 | 
| 
 | 
   913 int prodint(t,resp,resp2,coef,h)
 | 
| 
 | 
   914 double *t, *resp, *resp2, *coef, h;
 | 
| 
 | 
   915 { int dim, p, i, j, k, st;
 | 
| 
 | 
   916   double cf[MXDEG+1], hj, hs, prod_wk[MXDIM][2*MXDEG+1];
 | 
| 
 | 
   917 
 | 
| 
 | 
   918   dim = den_lfd->d;
 | 
| 
 | 
   919   p = den_des->p;
 | 
| 
 | 
   920   for (i=0; i<p*p; i++) resp[i] = 0.0;
 | 
| 
 | 
   921   cf[0] = coef[0];
 | 
| 
 | 
   922 
 | 
| 
 | 
   923 /*  compute the one dimensional terms
 | 
| 
 | 
   924  */
 | 
| 
 | 
   925   for (i=0; i<dim; i++)
 | 
| 
 | 
   926   { hj = 1; hs = h*den_lfd->sca[i];
 | 
| 
 | 
   927     for (j=0; j<deg(den_sp); j++)
 | 
| 
 | 
   928     { hj *= hs;
 | 
| 
 | 
   929       cf[j+1] = hj*coef[ j*dim+i+1 ];
 | 
| 
 | 
   930     }
 | 
| 
 | 
   931     st = onedint(den_sp,cf,ilim[i]/hs,ilim[i+dim]/hs,prod_wk[i]);
 | 
| 
 | 
   932     if (st==LF_BADP) return(st);
 | 
| 
 | 
   933     hj = 1;
 | 
| 
 | 
   934     for (j=0; j<=2*deg(den_sp); j++)
 | 
| 
 | 
   935     { hj *= hs;
 | 
| 
 | 
   936       prod_wk[i][j] *= hj;
 | 
| 
 | 
   937     }
 | 
| 
 | 
   938     cf[0] = 0.0; /* so we only include it once, when d>=2 */
 | 
| 
 | 
   939   }
 | 
| 
 | 
   940 
 | 
| 
 | 
   941 /*  transfer to the resp array
 | 
| 
 | 
   942  */
 | 
| 
 | 
   943   prodintresp(resp,prod_wk,dim,deg(den_sp),p);
 | 
| 
 | 
   944 
 | 
| 
 | 
   945 /* Symmetrize.
 | 
| 
 | 
   946 */
 | 
| 
 | 
   947   for (k=0; k<p; k++)
 | 
| 
 | 
   948     for (j=k; j<p; j++)
 | 
| 
 | 
   949       resp[j*p+k] = resp[k*p+j];
 | 
| 
 | 
   950 
 | 
| 
 | 
   951   return(st);
 | 
| 
 | 
   952 }
 | 
| 
 | 
   953 
 | 
| 
 | 
   954 int gausint(t,resp,C,cf,h,sca)
 | 
| 
 | 
   955 double *t, *resp, *C, *cf, h, *sca;
 | 
| 
 | 
   956 { double nb, det, z, *P;
 | 
| 
 | 
   957   int d, p, i, j, k, l, m1, m2, f;
 | 
| 
 | 
   958   d = den_lfd->d; p = den_des->p;
 | 
| 
 | 
   959   m1 = d+1; nb = 0;
 | 
| 
 | 
   960   P = &C[d*d];
 | 
| 
 | 
   961   resp[0] = 1;
 | 
| 
 | 
   962   for (i=0; i<d; i++)
 | 
| 
 | 
   963   { C[i*d+i] = SQR(GFACT/(h*sca[i]))-cf[m1++];
 | 
| 
 | 
   964     for (j=i+1; j<d; j++) C[i*d+j] = C[j*d+i] = -cf[m1++];
 | 
| 
 | 
   965   }
 | 
| 
 | 
   966   eig_dec(C,P,d);
 | 
| 
 | 
   967   det = 1;
 | 
| 
 | 
   968   for (i=1; i<=d; i++)
 | 
| 
 | 
   969   { det *= C[(i-1)*(d+1)];
 | 
| 
 | 
   970     if (det <= 0) return(LF_BADP);
 | 
| 
 | 
   971     resp[i] = cf[i];
 | 
| 
 | 
   972     for (j=1; j<=d; j++) resp[j+i*p] = 0;
 | 
| 
 | 
   973     resp[i+i*p] = 1;
 | 
| 
 | 
   974     svdsolve(&resp[i*p+1],u,P,C,P,d,0.0);
 | 
| 
 | 
   975   }
 | 
| 
 | 
   976   svdsolve(&resp[1],u,P,C,P,d,0.0);
 | 
| 
 | 
   977   det = sqrt(det);
 | 
| 
 | 
   978   for (i=1; i<=d; i++)
 | 
| 
 | 
   979   { nb += cf[i]*resp[i];
 | 
| 
 | 
   980     resp[i*p] = resp[i];
 | 
| 
 | 
   981     for (j=1; j<=d; j++)
 | 
| 
 | 
   982       resp[i+p*j] += resp[i]*resp[j];
 | 
| 
 | 
   983   }
 | 
| 
 | 
   984   m1 = d;
 | 
| 
 | 
   985   for (i=1; i<=d; i++)
 | 
| 
 | 
   986     for (j=i; j<=d; j++)
 | 
| 
 | 
   987     { m1++; f = 1+(i==j);
 | 
| 
 | 
   988       resp[m1] = resp[m1*p] = resp[i*p+j]/f;
 | 
| 
 | 
   989       m2 = d;
 | 
| 
 | 
   990       for (k=1; k<=d; k++)
 | 
| 
 | 
   991       { resp[m1+k*p] = resp[k+m1*p] =
 | 
| 
 | 
   992         ( resp[i]*resp[j*p+k] + resp[j]*resp[i*p+k]
 | 
| 
 | 
   993         + resp[k]*resp[i*p+j] - 2*resp[i]*resp[j]*resp[k] )/f;
 | 
| 
 | 
   994         for (l=k; l<=d; l++)
 | 
| 
 | 
   995         { m2++; f = (1+(i==j))*(1+(k==l));
 | 
| 
 | 
   996           resp[m1+m2*p] = resp[m2+m1*p] = ( resp[i+j*p]*resp[k+l*p]
 | 
| 
 | 
   997             + resp[i+k*p]*resp[j+l*p] + resp[i+l*p]*resp[j+k*p]
 | 
| 
 | 
   998             - 2*resp[i]*resp[j]*resp[k]*resp[l] )/f;
 | 
| 
 | 
   999     } } }
 | 
| 
 | 
  1000   z = mut_exp(d*0.918938533+cf[0]+nb/2)/det;
 | 
| 
 | 
  1001   multmatscal(resp,z,p*p);
 | 
| 
 | 
  1002   return(LF_OK);
 | 
| 
 | 
  1003 }
 | 
| 
 | 
  1004 
 | 
| 
 | 
  1005 int likeden(coef, lk0, f1, A)
 | 
| 
 | 
  1006 double *coef, *lk0, *f1, *A;
 | 
| 
 | 
  1007 { double lk, r;
 | 
| 
 | 
  1008   int i, j, p, rstat;
 | 
| 
 | 
  1009 
 | 
| 
 | 
  1010   lf_status = LF_OK;
 | 
| 
 | 
  1011   p = den_des->p;
 | 
| 
 | 
  1012   if ((link(den_sp)==LIDENT) && (coef[0] != 0.0)) return(NR_BREAK);
 | 
| 
 | 
  1013   lf_status = (den_des->itype)(den_des->xev,A,den_des->xtwx.Q,coef,den_des->h);
 | 
| 
 | 
  1014   if (lf_error) lf_status = LF_ERR;
 | 
| 
 | 
  1015   if (lf_status==LF_BADP)
 | 
| 
 | 
  1016   { *lk0 = -1.0e300;
 | 
| 
 | 
  1017     return(NR_REDUCE);
 | 
| 
 | 
  1018   }
 | 
| 
 | 
  1019   if (lf_status!=LF_OK) return(NR_BREAK);
 | 
| 
 | 
  1020   if (lf_debug>2) prresp(coef,A,p);
 | 
| 
 | 
  1021 
 | 
| 
 | 
  1022   den_des->xtwx.p = p;
 | 
| 
 | 
  1023   rstat = NR_OK;
 | 
| 
 | 
  1024   switch(link(den_sp))
 | 
| 
 | 
  1025   { case LLOG:
 | 
| 
 | 
  1026       r = den_des->ss[0]/A[0];
 | 
| 
 | 
  1027       coef[0] += log(r);
 | 
| 
 | 
  1028       multmatscal(A,r,p*p);
 | 
| 
 | 
  1029       A[0] = den_des->ss[0];
 | 
| 
 | 
  1030       lk = -A[0];
 | 
| 
 | 
  1031       if (fabs(coef[0]) > 700)
 | 
| 
 | 
  1032       { lf_status = LF_OOB;
 | 
| 
 | 
  1033         rstat = NR_REDUCE;
 | 
| 
 | 
  1034       }
 | 
| 
 | 
  1035       for (i=0; i<p; i++)
 | 
| 
 | 
  1036       { lk += coef[i]*den_des->ss[i];
 | 
| 
 | 
  1037         f1[i] = den_des->ss[i]-A[i];
 | 
| 
 | 
  1038       }
 | 
| 
 | 
  1039       break;
 | 
| 
 | 
  1040     case LIDENT:
 | 
| 
 | 
  1041       lk = 0.0;
 | 
| 
 | 
  1042       for (i=0; i<p; i++)
 | 
| 
 | 
  1043       { f1[i] = den_des->ss[i];
 | 
| 
 | 
  1044         for (j=0; j<p; j++)
 | 
| 
 | 
  1045           den_des->res[i] -= A[i*p+j]*coef[j];
 | 
| 
 | 
  1046       }
 | 
| 
 | 
  1047       break;
 | 
| 
 | 
  1048   }
 | 
| 
 | 
  1049   *lk0 = den_des->llk = lk;
 | 
| 
 | 
  1050 
 | 
| 
 | 
  1051   return(rstat);
 | 
| 
 | 
  1052 }
 | 
| 
 | 
  1053 
 | 
| 
 | 
  1054 int inre(x,bound,d)
 | 
| 
 | 
  1055 double *x, *bound;
 | 
| 
 | 
  1056 int d;
 | 
| 
 | 
  1057 { int i, z;
 | 
| 
 | 
  1058   z = 1;
 | 
| 
 | 
  1059   for (i=0; i<d; i++)
 | 
| 
 | 
  1060     if (bound[i]<bound[i+d])
 | 
| 
 | 
  1061       z &= (x[i]>=bound[i]) & (x[i]<=bound[i+d]);
 | 
| 
 | 
  1062   return(z);
 | 
| 
 | 
  1063 }
 | 
| 
 | 
  1064 
 | 
| 
 | 
  1065 int setintlimits(lfd, x, h, ang, lset)
 | 
| 
 | 
  1066 lfdata *lfd;
 | 
| 
 | 
  1067 int *ang, *lset;
 | 
| 
 | 
  1068 double *x, h;
 | 
| 
 | 
  1069 { int d, i;
 | 
| 
 | 
  1070   d = lfd->d;
 | 
| 
 | 
  1071   *ang = *lset = 0;
 | 
| 
 | 
  1072   for (i=0; i<d; i++)
 | 
| 
 | 
  1073   { if (lfd->sty[i]==STANGL)
 | 
| 
 | 
  1074     { ilim[i+d] = ((h<2) ? 2*asin(h/2) : PI)*lfd->sca[i];
 | 
| 
 | 
  1075       ilim[i] = -ilim[i+d];
 | 
| 
 | 
  1076       *ang = 1;
 | 
| 
 | 
  1077     }
 | 
| 
 | 
  1078     else
 | 
| 
 | 
  1079     { ilim[i+d] = h*lfd->sca[i];
 | 
| 
 | 
  1080       ilim[i] = -ilim[i+d];
 | 
| 
 | 
  1081 
 | 
| 
 | 
  1082       if (lfd->sty[i]==STLEFT) { ilim[i+d] = 0; *lset = 1; }
 | 
| 
 | 
  1083       if (lfd->sty[i]==STRIGH) { ilim[i] = 0;   *lset = 1; }
 | 
| 
 | 
  1084 
 | 
| 
 | 
  1085       if (lfd->xl[i]<lfd->xl[i+d]) /* user limits for this variable */
 | 
| 
 | 
  1086       { if (lfd->xl[i]-x[i]> ilim[i])
 | 
| 
 | 
  1087         { ilim[i] = lfd->xl[i]-x[i]; *lset=1; }
 | 
| 
 | 
  1088         if (lfd->xl[i+d]-x[i]< ilim[i+d])
 | 
| 
 | 
  1089         { ilim[i+d] = lfd->xl[i+d]-x[i]; *lset=1; }
 | 
| 
 | 
  1090       }
 | 
| 
 | 
  1091     }
 | 
| 
 | 
  1092     if (ilim[i]==ilim[i+d]) return(LF_DEMP); /* empty integration */
 | 
| 
 | 
  1093   }
 | 
| 
 | 
  1094   return(LF_OK);
 | 
| 
 | 
  1095 }
 | 
| 
 | 
  1096 
 | 
| 
 | 
  1097 int selectintmeth(itype,lset,ang)
 | 
| 
 | 
  1098 int itype, lset, ang;
 | 
| 
 | 
  1099 {
 | 
| 
 | 
  1100   if (itype==IDEFA) /* select the default method */
 | 
| 
 | 
  1101   { if (fam(den_sp)==THAZ)
 | 
| 
 | 
  1102     { if (ang) return(IDEFA);
 | 
| 
 | 
  1103       return( IHAZD );
 | 
| 
 | 
  1104     }
 | 
| 
 | 
  1105 
 | 
| 
 | 
  1106     if (ubas(den_sp)) return(IMULT);
 | 
| 
 | 
  1107 
 | 
| 
 | 
  1108     if (ang) return(IMULT);
 | 
| 
 | 
  1109 
 | 
| 
 | 
  1110     if (iscompact(ker(den_sp)))
 | 
| 
 | 
  1111     { if (kt(den_sp)==KPROD) return(IPROD);
 | 
| 
 | 
  1112       if (lset)
 | 
| 
 | 
  1113         return( (den_lfd->d==1) ? IPROD : IMULT );
 | 
| 
 | 
  1114       if (deg(den_sp)<=1) return(IMLIN);
 | 
| 
 | 
  1115       if (den_lfd->d==1) return(IPROD);
 | 
| 
 | 
  1116       return(IMULT);
 | 
| 
 | 
  1117     }
 | 
| 
 | 
  1118 
 | 
| 
 | 
  1119     if (ker(den_sp)==WGAUS)
 | 
| 
 | 
  1120     { if (lset) WARN(("Integration for Gaussian weights ignores limits"));
 | 
| 
 | 
  1121       if ((den_lfd->d==1)|(kt(den_sp)==KPROD)) return(IPROD);
 | 
| 
 | 
  1122       if (deg(den_sp)<=1) return(IMLIN);
 | 
| 
 | 
  1123       if (deg(den_sp)==2) return(IMULT);
 | 
| 
 | 
  1124     }
 | 
| 
 | 
  1125 
 | 
| 
 | 
  1126     return(IDEFA);
 | 
| 
 | 
  1127   }
 | 
| 
 | 
  1128 
 | 
| 
 | 
  1129   /* user provided an integration method, check it is valid */
 | 
| 
 | 
  1130 
 | 
| 
 | 
  1131   if (fam(den_sp)==THAZ)
 | 
| 
 | 
  1132   { if (ang) return(INVLD);
 | 
| 
 | 
  1133     if (!iscompact(ker(den_sp))) return(INVLD);
 | 
| 
 | 
  1134     return( ((kt(den_sp)==KPROD) | (kt(den_sp)==KSPH)) ? IHAZD : INVLD );
 | 
| 
 | 
  1135   }
 | 
| 
 | 
  1136 
 | 
| 
 | 
  1137   if ((ang) && (itype != IMULT)) return(INVLD);
 | 
| 
 | 
  1138 
 | 
| 
 | 
  1139   switch(itype)
 | 
| 
 | 
  1140   { case IMULT:
 | 
| 
 | 
  1141       if (ker(den_sp)==WGAUS) return(deg(den_sp)==2);
 | 
| 
 | 
  1142       return( iscompact(ker(den_sp)) ? IMULT : INVLD );
 | 
| 
 | 
  1143     case IPROD: return( ((den_lfd->d==1) | (kt(den_sp)==KPROD)) ? IPROD : INVLD );
 | 
| 
 | 
  1144     case IMLIN: return( ((kt(den_sp)==KSPH) && (!lset) &&
 | 
| 
 | 
  1145       (deg(den_sp)<=1)) ? IMLIN : INVLD );
 | 
| 
 | 
  1146   }
 | 
| 
 | 
  1147 
 | 
| 
 | 
  1148   return(INVLD);
 | 
| 
 | 
  1149 }
 | 
| 
 | 
  1150 
 | 
| 
 | 
  1151 extern double lf_tol;
 | 
| 
 | 
  1152 
 | 
| 
 | 
  1153 int densinit(lfd,des,sp)
 | 
| 
 | 
  1154 lfdata *lfd;
 | 
| 
 | 
  1155 design *des;
 | 
| 
 | 
  1156 smpar *sp;
 | 
| 
 | 
  1157 { int p, i, ii, j, nnz, rnz, ang, lset, status;
 | 
| 
 | 
  1158   double w, *cf;
 | 
| 
 | 
  1159 
 | 
| 
 | 
  1160   den_lfd = lfd;
 | 
| 
 | 
  1161   den_des = des;
 | 
| 
 | 
  1162   den_sp  = sp;
 | 
| 
 | 
  1163   cf = des->cf;
 | 
| 
 | 
  1164 
 | 
| 
 | 
  1165   lf_tol = (link(sp)==LLOG) ? 1.0e-6 : 0.0;
 | 
| 
 | 
  1166 
 | 
| 
 | 
  1167   p = des->p;
 | 
| 
 | 
  1168   ff = des->xtwx.wk;
 | 
| 
 | 
  1169   cf[0] = NOSLN;
 | 
| 
 | 
  1170   for (i=1; i<p; i++) cf[i] = 0.0;
 | 
| 
 | 
  1171 
 | 
| 
 | 
  1172   if (!inre(des->xev,lfd->xl,lfd->d)) return(LF_XOOR);
 | 
| 
 | 
  1173 
 | 
| 
 | 
  1174   status = setintlimits(lfd,des->xev,des->h,&ang,&lset);
 | 
| 
 | 
  1175   if (status != LF_OK) return(status);
 | 
| 
 | 
  1176 
 | 
| 
 | 
  1177   switch(selectintmeth(de_itype,lset,ang))
 | 
| 
 | 
  1178   { case IMULT: des->itype = multint; break;
 | 
| 
 | 
  1179     case IPROD: des->itype = prodint; break;
 | 
| 
 | 
  1180     case IMLIN: des->itype = mlinint; break;
 | 
| 
 | 
  1181     case IHAZD: des->itype = hazint; break;
 | 
| 
 | 
  1182     case INVLD: LERR(("Invalid integration method %d",de_itype));
 | 
| 
 | 
  1183                 break;
 | 
| 
 | 
  1184     case IDEFA: LERR(("No integration type available for this model"));
 | 
| 
 | 
  1185                 break;
 | 
| 
 | 
  1186     default: LERR(("densinit: unknown integral type"));
 | 
| 
 | 
  1187   }
 | 
| 
 | 
  1188 
 | 
| 
 | 
  1189   switch(deg(den_sp))
 | 
| 
 | 
  1190   { case 0: rnz = 1; break;
 | 
| 
 | 
  1191     case 1: rnz = 1; break;
 | 
| 
 | 
  1192     case 2: rnz = lfd->d+1; break;
 | 
| 
 | 
  1193     case 3: rnz = lfd->d+2; break;
 | 
| 
 | 
  1194     default: LERR(("densinit: invalid degree %d",deg(den_sp)));
 | 
| 
 | 
  1195   }
 | 
| 
 | 
  1196   if (lf_error) return(LF_ERR);
 | 
| 
 | 
  1197 
 | 
| 
 | 
  1198   setzero(des->ss,p);
 | 
| 
 | 
  1199   nnz = 0;
 | 
| 
 | 
  1200   for (i=0; i<des->n; i++)
 | 
| 
 | 
  1201   { ii = des->ind[i];
 | 
| 
 | 
  1202     if (!cens(lfd,ii))
 | 
| 
 | 
  1203     { w = wght(des,ii)*prwt(lfd,ii);
 | 
| 
 | 
  1204       for (j=0; j<p; j++) des->ss[j] += d_xij(des,ii,j)*w;
 | 
| 
 | 
  1205       if (wght(des,ii)>0.00001) nnz++;
 | 
| 
 | 
  1206   } }
 | 
| 
 | 
  1207 
 | 
| 
 | 
  1208   if (fam(den_sp)==THAZ) haz_init(lfd,des,sp,ilim);
 | 
| 
 | 
  1209 /* this should really only be done once. Not sure how to enforce that,
 | 
| 
 | 
  1210  * esp. when locfit() has been called directly.
 | 
| 
 | 
  1211  */
 | 
| 
 | 
  1212   if (fam(den_sp)==TDEN)
 | 
| 
 | 
  1213     des->smwt = (lfd->w==NULL) ? lfd->n : vecsum(lfd->w,lfd->n);
 | 
| 
 | 
  1214 
 | 
| 
 | 
  1215   if (lf_debug>2)
 | 
| 
 | 
  1216   { mut_printf("    LHS: ");
 | 
| 
 | 
  1217     for (i=0; i<p; i++) mut_printf(" %8.5f",des->ss[i]);
 | 
| 
 | 
  1218     mut_printf("\n");
 | 
| 
 | 
  1219   }
 | 
| 
 | 
  1220 
 | 
| 
 | 
  1221   switch(link(den_sp))
 | 
| 
 | 
  1222   { case LIDENT:
 | 
| 
 | 
  1223       cf[0] = 0.0;
 | 
| 
 | 
  1224       return(LF_OK);
 | 
| 
 | 
  1225     case LLOG:
 | 
| 
 | 
  1226       if (nnz<rnz) { cf[0] = -1000; return(LF_DNOP); }
 | 
| 
 | 
  1227       cf[0] = 0.0;
 | 
| 
 | 
  1228       return(LF_OK);
 | 
| 
 | 
  1229     default:
 | 
| 
 | 
  1230       LERR(("unknown link in densinit"));
 | 
| 
 | 
  1231       return(LF_ERR);
 | 
| 
 | 
  1232   }
 | 
| 
 | 
  1233 }
 | 
| 
 | 
  1234 /*
 | 
| 
 | 
  1235  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1236  */
 | 
| 
 | 
  1237 #include "locf.h"
 | 
| 
 | 
  1238 
 | 
| 
 | 
  1239 int bino_vallink(link)
 | 
| 
 | 
  1240 int link;
 | 
| 
 | 
  1241 { return((link==LLOGIT) | (link==LIDENT) | (link==LASIN));
 | 
| 
 | 
  1242 }
 | 
| 
 | 
  1243 
 | 
| 
 | 
  1244 int bino_fam(y,p,th,link,res,cens,w)
 | 
| 
 | 
  1245 double y, p, th, *res, w;
 | 
| 
 | 
  1246 int link, cens;
 | 
| 
 | 
  1247 { double wp;
 | 
| 
 | 
  1248   if (link==LINIT)
 | 
| 
 | 
  1249   { if (y<0) y = 0;
 | 
| 
 | 
  1250     if (y>w) y = w;
 | 
| 
 | 
  1251     res[ZDLL] = y;
 | 
| 
 | 
  1252     return(LF_OK);
 | 
| 
 | 
  1253   }
 | 
| 
 | 
  1254   wp = w*p;
 | 
| 
 | 
  1255   if (link==LIDENT)
 | 
| 
 | 
  1256   { if ((p<=0) && (y>0)) return(LF_BADP);
 | 
| 
 | 
  1257     if ((p>=1) && (y<w)) return(LF_BADP);
 | 
| 
 | 
  1258     res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
 | 
| 
 | 
  1259     if (y>0)
 | 
| 
 | 
  1260     { res[ZLIK] += y*log(wp/y);
 | 
| 
 | 
  1261       res[ZDLL] += y/p;
 | 
| 
 | 
  1262       res[ZDDLL]+= y/(p*p);
 | 
| 
 | 
  1263     }
 | 
| 
 | 
  1264     if (y<w)
 | 
| 
 | 
  1265     { res[ZLIK] += (w-y)*log((w-wp)/(w-y));
 | 
| 
 | 
  1266       res[ZDLL] -= (w-y)/(1-p);
 | 
| 
 | 
  1267       res[ZDDLL]+= (w-y)/SQR(1-p);
 | 
| 
 | 
  1268     }
 | 
| 
 | 
  1269     return(LF_OK);
 | 
| 
 | 
  1270   }
 | 
| 
 | 
  1271   if (link==LLOGIT)
 | 
| 
 | 
  1272   { if ((y<0) | (y>w)) /* goon observation; delete it */
 | 
| 
 | 
  1273     { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
 | 
| 
 | 
  1274       return(LF_OK);
 | 
| 
 | 
  1275     }
 | 
| 
 | 
  1276     res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
 | 
| 
 | 
  1277     if (y>0) res[ZLIK] -= y*log(y/w);
 | 
| 
 | 
  1278     if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
 | 
| 
 | 
  1279     res[ZDLL] = (y-wp);
 | 
| 
 | 
  1280     res[ZDDLL]= wp*(1-p);
 | 
| 
 | 
  1281     return(LF_OK);
 | 
| 
 | 
  1282   }
 | 
| 
 | 
  1283   if (link==LASIN)
 | 
| 
 | 
  1284   { if ((p<=0) && (y>0)) return(LF_BADP);
 | 
| 
 | 
  1285     if ((p>=1) && (y<w)) return(LF_BADP);
 | 
| 
 | 
  1286     if ((th<0) | (th>PI/2)) return(LF_BADP);
 | 
| 
 | 
  1287     res[ZDLL] = res[ZDDLL] = res[ZLIK] = 0;
 | 
| 
 | 
  1288     if (y>0)
 | 
| 
 | 
  1289     { res[ZDLL] += 2*y*sqrt((1-p)/p);
 | 
| 
 | 
  1290       res[ZLIK] += y*log(wp/y);
 | 
| 
 | 
  1291     }
 | 
| 
 | 
  1292     if (y<w)
 | 
| 
 | 
  1293     { res[ZDLL] -= 2*(w-y)*sqrt(p/(1-p));
 | 
| 
 | 
  1294       res[ZLIK] += (w-y)*log((w-wp)/(w-y));
 | 
| 
 | 
  1295     }
 | 
| 
 | 
  1296     res[ZDDLL] = 4*w;
 | 
| 
 | 
  1297     return(LF_OK);
 | 
| 
 | 
  1298   }
 | 
| 
 | 
  1299   LERR(("link %d invalid for binomial family",link));
 | 
| 
 | 
  1300   return(LF_LNK);
 | 
| 
 | 
  1301 }
 | 
| 
 | 
  1302 
 | 
| 
 | 
  1303 int bino_check(sp,des,lfd)
 | 
| 
 | 
  1304 smpar *sp;
 | 
| 
 | 
  1305 design *des;
 | 
| 
 | 
  1306 lfdata *lfd;
 | 
| 
 | 
  1307 { int i, ii;
 | 
| 
 | 
  1308   double t0, t1;
 | 
| 
 | 
  1309 
 | 
| 
 | 
  1310   if (fabs(des->cf[0])>700) return(LF_OOB);
 | 
| 
 | 
  1311 
 | 
| 
 | 
  1312   /* check for separation.
 | 
| 
 | 
  1313    * this won't detect separation if there's boundary points with
 | 
| 
 | 
  1314    *   both 0 and 1 responses.
 | 
| 
 | 
  1315    */
 | 
| 
 | 
  1316   t0 = -1e100; t1 = 1e100;
 | 
| 
 | 
  1317   for (i=0; i<des->n; i++)
 | 
| 
 | 
  1318   { ii = des->ind[i];
 | 
| 
 | 
  1319     if ((resp(lfd,ii)<prwt(lfd,ii)) && (fitv(des,ii) > t0)) t0 = fitv(des,ii);
 | 
| 
 | 
  1320     if ((resp(lfd,ii)>0) && (fitv(des,ii) < t1)) t1 = fitv(des,ii);
 | 
| 
 | 
  1321     if (t1 <= t0) return(LF_OK);
 | 
| 
 | 
  1322   }
 | 
| 
 | 
  1323   mut_printf("separated %8.5f %8.5f\n",t0,t1);
 | 
| 
 | 
  1324   return(LF_NSLN);
 | 
| 
 | 
  1325 }
 | 
| 
 | 
  1326 
 | 
| 
 | 
  1327 void setfbino(fam)
 | 
| 
 | 
  1328 family *fam;
 | 
| 
 | 
  1329 { fam->deflink = LLOGIT;
 | 
| 
 | 
  1330   fam->canlink = LLOGIT;
 | 
| 
 | 
  1331   fam->vallink = bino_vallink;
 | 
| 
 | 
  1332   fam->family  = bino_fam;
 | 
| 
 | 
  1333   fam->pcheck  = bino_check;
 | 
| 
 | 
  1334 }
 | 
| 
 | 
  1335 
 | 
| 
 | 
  1336 int rbin_vallink(link)
 | 
| 
 | 
  1337 int link;
 | 
| 
 | 
  1338 { return(link==LLOGIT);
 | 
| 
 | 
  1339 }
 | 
| 
 | 
  1340 
 | 
| 
 | 
  1341 int rbin_fam(y,p,th,link,res,cens,w)
 | 
| 
 | 
  1342 double y, p, th, *res, w;
 | 
| 
 | 
  1343 int link, cens;
 | 
| 
 | 
  1344 { double s2y;
 | 
| 
 | 
  1345   if (link==LINIT)
 | 
| 
 | 
  1346   { res[ZDLL] = y;
 | 
| 
 | 
  1347     return(LF_OK);
 | 
| 
 | 
  1348   }
 | 
| 
 | 
  1349   if ((y<0) | (y>w)) /* goon observation; delete it */
 | 
| 
 | 
  1350   { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
 | 
| 
 | 
  1351     return(LF_OK);
 | 
| 
 | 
  1352   }
 | 
| 
 | 
  1353   res[ZLIK] = (th<0) ? th*y-w*log(1+exp(th)) : th*(y-w)-w*log(1+exp(-th));
 | 
| 
 | 
  1354   if (y>0) res[ZLIK] -= y*log(y/w);
 | 
| 
 | 
  1355   if (y<w) res[ZLIK] -= (w-y)*log(1-y/w);
 | 
| 
 | 
  1356   res[ZDLL] = (y-w*p);
 | 
| 
 | 
  1357   res[ZDDLL]= w*p*(1-p);
 | 
| 
 | 
  1358   if (-res[ZLIK]>HUBERC*HUBERC/2.0)
 | 
| 
 | 
  1359   { s2y = sqrt(-2*res[ZLIK]);
 | 
| 
 | 
  1360     res[ZLIK] = HUBERC*(HUBERC/2.0-s2y);
 | 
| 
 | 
  1361     res[ZDLL] *= HUBERC/s2y;
 | 
| 
 | 
  1362     res[ZDDLL] = HUBERC/s2y*(res[ZDDLL]-1/(s2y*s2y)*w*p*(1-p));
 | 
| 
 | 
  1363   }
 | 
| 
 | 
  1364   return(LF_OK);
 | 
| 
 | 
  1365 }
 | 
| 
 | 
  1366 
 | 
| 
 | 
  1367 void setfrbino(fam)
 | 
| 
 | 
  1368 family *fam;
 | 
| 
 | 
  1369 { fam->deflink = LLOGIT;
 | 
| 
 | 
  1370   fam->canlink = LLOGIT;
 | 
| 
 | 
  1371   fam->vallink = rbin_vallink;
 | 
| 
 | 
  1372   fam->family  = rbin_fam;
 | 
| 
 | 
  1373   fam->pcheck  = bino_check;
 | 
| 
 | 
  1374 }
 | 
| 
 | 
  1375 /*
 | 
| 
 | 
  1376  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1377  */
 | 
| 
 | 
  1378 #include "locf.h"
 | 
| 
 | 
  1379 
 | 
| 
 | 
  1380 int circ_vallink(link)
 | 
| 
 | 
  1381 int link;
 | 
| 
 | 
  1382 { return(link==LIDENT);
 | 
| 
 | 
  1383 }
 | 
| 
 | 
  1384 
 | 
| 
 | 
  1385 int circ_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  1386 double y, mean, th, *res, w;
 | 
| 
 | 
  1387 int link, cens;
 | 
| 
 | 
  1388 { if (link==LINIT)
 | 
| 
 | 
  1389   { res[ZDLL] = w*sin(y);
 | 
| 
 | 
  1390     res[ZLIK] = w*cos(y);
 | 
| 
 | 
  1391     return(LF_OK);
 | 
| 
 | 
  1392   }
 | 
| 
 | 
  1393   res[ZDLL] = w*sin(y-mean);
 | 
| 
 | 
  1394   res[ZDDLL]= w*cos(y-mean);
 | 
| 
 | 
  1395   res[ZLIK] = res[ZDDLL]-w;
 | 
| 
 | 
  1396   return(LF_OK);
 | 
| 
 | 
  1397 }
 | 
| 
 | 
  1398 
 | 
| 
 | 
  1399 extern double lf_tol;
 | 
| 
 | 
  1400 int circ_init(lfd,des,sp)
 | 
| 
 | 
  1401 lfdata *lfd;
 | 
| 
 | 
  1402 design *des;
 | 
| 
 | 
  1403 smpar *sp;
 | 
| 
 | 
  1404 { int i, ii;
 | 
| 
 | 
  1405   double s0, s1;
 | 
| 
 | 
  1406   s0 = s1 = 0.0;
 | 
| 
 | 
  1407   for (i=0; i<des->n; i++)
 | 
| 
 | 
  1408   { ii = des->ind[i];
 | 
| 
 | 
  1409     s0 += wght(des,ii)*prwt(lfd,ii)*sin(resp(lfd,ii)-base(lfd,ii));
 | 
| 
 | 
  1410     s1 += wght(des,ii)*prwt(lfd,ii)*cos(resp(lfd,ii)-base(lfd,ii));
 | 
| 
 | 
  1411   }
 | 
| 
 | 
  1412   des->cf[0] = atan2(s0,s1);
 | 
| 
 | 
  1413   for (i=1; i<des->p; i++) des->cf[i] = 0.0;
 | 
| 
 | 
  1414   lf_tol = 1.0e-6;
 | 
| 
 | 
  1415   return(LF_OK);
 | 
| 
 | 
  1416 }
 | 
| 
 | 
  1417 
 | 
| 
 | 
  1418 
 | 
| 
 | 
  1419 void setfcirc(fam)
 | 
| 
 | 
  1420 family *fam;
 | 
| 
 | 
  1421 { fam->deflink = LIDENT;
 | 
| 
 | 
  1422   fam->canlink = LIDENT;
 | 
| 
 | 
  1423   fam->vallink = circ_vallink;
 | 
| 
 | 
  1424   fam->family  = circ_fam;
 | 
| 
 | 
  1425   fam->initial = circ_init;
 | 
| 
 | 
  1426 }
 | 
| 
 | 
  1427 /*
 | 
| 
 | 
  1428  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1429  */
 | 
| 
 | 
  1430 #include "locf.h"
 | 
| 
 | 
  1431 
 | 
| 
 | 
  1432 int dens_vallink(link)
 | 
| 
 | 
  1433 int link;
 | 
| 
 | 
  1434 { return((link==LIDENT) | (link==LLOG));
 | 
| 
 | 
  1435 }
 | 
| 
 | 
  1436 
 | 
| 
 | 
  1437 int dens_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  1438 double y, mean, th, *res, w;
 | 
| 
 | 
  1439 int link, cens;
 | 
| 
 | 
  1440 { if (cens)
 | 
| 
 | 
  1441     res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
 | 
| 
 | 
  1442   else
 | 
| 
 | 
  1443   { res[ZLIK] = w*th;
 | 
| 
 | 
  1444     res[ZDLL] = res[ZDDLL] = w;
 | 
| 
 | 
  1445   }
 | 
| 
 | 
  1446   return(LF_OK);
 | 
| 
 | 
  1447 }
 | 
| 
 | 
  1448 
 | 
| 
 | 
  1449 void setfdensity(fam)
 | 
| 
 | 
  1450 family *fam;
 | 
| 
 | 
  1451 { fam->deflink = LLOG;
 | 
| 
 | 
  1452   fam->canlink = LLOG;
 | 
| 
 | 
  1453   fam->vallink = dens_vallink;
 | 
| 
 | 
  1454   fam->family  = dens_fam;
 | 
| 
 | 
  1455   fam->initial = densinit;
 | 
| 
 | 
  1456   fam->like = likeden;
 | 
| 
 | 
  1457 }
 | 
| 
 | 
  1458 /*
 | 
| 
 | 
  1459  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1460  */
 | 
| 
 | 
  1461 #include "locf.h"
 | 
| 
 | 
  1462 
 | 
| 
 | 
  1463 int gamma_vallink(link)
 | 
| 
 | 
  1464 int link;
 | 
| 
 | 
  1465 { return((link==LIDENT) | (link==LLOG) | (link==LINVER));
 | 
| 
 | 
  1466 }
 | 
| 
 | 
  1467 
 | 
| 
 | 
  1468 int gamma_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  1469 double y, mean, th, *res, w;
 | 
| 
 | 
  1470 int link, cens;
 | 
| 
 | 
  1471 { double lb, pt, dg;
 | 
| 
 | 
  1472   if (link==LINIT)
 | 
| 
 | 
  1473   { res[ZDLL] = MAX(y,0.0);
 | 
| 
 | 
  1474     return(LF_OK);
 | 
| 
 | 
  1475   }
 | 
| 
 | 
  1476   res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
 | 
| 
 | 
  1477   if (w==0.0) return(LF_OK);
 | 
| 
 | 
  1478   if ((mean<=0) & (y>0)) return(LF_BADP);
 | 
| 
 | 
  1479   if (link==LIDENT) lb = 1/th;
 | 
| 
 | 
  1480   if (link==LINVER) lb = th;
 | 
| 
 | 
  1481   if (link==LLOG)   lb = mut_exp(-th);
 | 
| 
 | 
  1482   if (cens)
 | 
| 
 | 
  1483   { if (y<=0) return(LF_OK);
 | 
| 
 | 
  1484     pt = 1-igamma(lb*y,w);
 | 
| 
 | 
  1485     dg = dgamma(lb*y,w,1.0,0);
 | 
| 
 | 
  1486     res[ZLIK] = log(pt);
 | 
| 
 | 
  1487     res[ZDLL] = -y*dg/pt;
 | 
| 
 | 
  1488 /*
 | 
| 
 | 
  1489  * res[ZDLL]  = -y*dg/pt * dlb/dth.
 | 
| 
 | 
  1490  * res[ZDDLL] =  y*dg/pt * (d2lb/dth2 + ((w-1)/lb-y)*(dlb/dth)^2)
 | 
| 
 | 
  1491  *              + res[ZDLL]^2.
 | 
| 
 | 
  1492  */
 | 
| 
 | 
  1493     if (link==LLOG)       /* lambda = exp(-theta) */
 | 
| 
 | 
  1494     { res[ZDLL] *= -lb;
 | 
| 
 | 
  1495       res[ZDDLL] = dg*y*lb*(w-lb*y)/pt + SQR(res[ZDLL]);
 | 
| 
 | 
  1496       return(LF_OK);
 | 
| 
 | 
  1497     }
 | 
| 
 | 
  1498     if (link==LINVER)     /* lambda = theta */
 | 
| 
 | 
  1499     { res[ZDLL] *= 1.0;
 | 
| 
 | 
  1500       res[ZDDLL] = dg*y*((w-1)*mean-y)/pt + SQR(res[ZDLL]);
 | 
| 
 | 
  1501       return(LF_OK);
 | 
| 
 | 
  1502     }
 | 
| 
 | 
  1503     if (link==LIDENT)     /* lambda = 1/theta */
 | 
| 
 | 
  1504     { res[ZDLL] *= -lb*lb;
 | 
| 
 | 
  1505       res[ZDDLL] = dg*y*lb*lb*lb*(1+w-lb*y)/pt + SQR(res[ZDLL]);
 | 
| 
 | 
  1506       return(LF_OK);
 | 
| 
 | 
  1507     }
 | 
| 
 | 
  1508   }
 | 
| 
 | 
  1509   else
 | 
| 
 | 
  1510   { if (y<0) WARN(("Negative Gamma observation"));
 | 
| 
 | 
  1511     if (link==LLOG)
 | 
| 
 | 
  1512     { res[ZLIK] = -lb*y+w*(1-th);
 | 
| 
 | 
  1513       if (y>0) res[ZLIK] += w*log(y/w);
 | 
| 
 | 
  1514       res[ZDLL] = lb*y-w;
 | 
| 
 | 
  1515       res[ZDDLL]= lb*y;
 | 
| 
 | 
  1516       return(LF_OK);
 | 
| 
 | 
  1517     }
 | 
| 
 | 
  1518     if (link==LINVER)
 | 
| 
 | 
  1519     { res[ZLIK] = -lb*y+w-w*log(mean);
 | 
| 
 | 
  1520       if (y>0) res[ZLIK] += w*log(y/w);
 | 
| 
 | 
  1521       res[ZDLL] = -y+w*mean;
 | 
| 
 | 
  1522       res[ZDDLL]= w*mean*mean;
 | 
| 
 | 
  1523       return(LF_OK);
 | 
| 
 | 
  1524     }
 | 
| 
 | 
  1525     if (link==LIDENT)
 | 
| 
 | 
  1526     { res[ZLIK] = -lb*y+w-w*log(mean);
 | 
| 
 | 
  1527       if (y>0) res[ZLIK] += w*log(y/w);
 | 
| 
 | 
  1528       res[ZDLL] = lb*lb*(y-w*mean);
 | 
| 
 | 
  1529       res[ZDDLL]= lb*lb*lb*(2*y-w*mean);
 | 
| 
 | 
  1530       return(LF_OK);
 | 
| 
 | 
  1531     }
 | 
| 
 | 
  1532   }
 | 
| 
 | 
  1533   LERR(("link %d invalid for Gamma family",link));
 | 
| 
 | 
  1534   return(LF_LNK);
 | 
| 
 | 
  1535 }
 | 
| 
 | 
  1536 
 | 
| 
 | 
  1537 void setfgamma(fam)
 | 
| 
 | 
  1538 family *fam;
 | 
| 
 | 
  1539 { fam->deflink = LLOG;
 | 
| 
 | 
  1540   fam->canlink = LINVER;
 | 
| 
 | 
  1541   fam->vallink = gamma_vallink;
 | 
| 
 | 
  1542   fam->family  = gamma_fam;
 | 
| 
 | 
  1543 }
 | 
| 
 | 
  1544 /*
 | 
| 
 | 
  1545  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1546  */
 | 
| 
 | 
  1547 #include "locf.h"
 | 
| 
 | 
  1548 
 | 
| 
 | 
  1549 int gaus_vallink(link)
 | 
| 
 | 
  1550 int link;
 | 
| 
 | 
  1551 { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
 | 
| 
 | 
  1552 }
 | 
| 
 | 
  1553 
 | 
| 
 | 
  1554 int gaus_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  1555 double y, mean, th, *res, w;
 | 
| 
 | 
  1556 int link, cens;
 | 
| 
 | 
  1557 { double z, pz, dp;
 | 
| 
 | 
  1558   if (link==LINIT)
 | 
| 
 | 
  1559   { res[ZDLL] = w*y;
 | 
| 
 | 
  1560     return(LF_OK);
 | 
| 
 | 
  1561   }
 | 
| 
 | 
  1562   z = y-mean;
 | 
| 
 | 
  1563   if (cens)
 | 
| 
 | 
  1564   { if (link!=LIDENT)
 | 
| 
 | 
  1565     { LERR(("Link invalid for censored Gaussian family"));
 | 
| 
 | 
  1566       return(LF_LNK);
 | 
| 
 | 
  1567     }
 | 
| 
 | 
  1568     pz = mut_pnorm(-z);
 | 
| 
 | 
  1569     dp = ((z>6) ? ptail(-z) : exp(-z*z/2)/pz)/2.5066283;
 | 
| 
 | 
  1570     res[ZLIK] = w*log(pz);
 | 
| 
 | 
  1571     res[ZDLL] = w*dp;
 | 
| 
 | 
  1572     res[ZDDLL]= w*dp*(dp-z);
 | 
| 
 | 
  1573     return(LF_OK);
 | 
| 
 | 
  1574   }
 | 
| 
 | 
  1575   res[ZLIK] = -w*z*z/2; 
 | 
| 
 | 
  1576   switch(link)
 | 
| 
 | 
  1577   { case LIDENT:
 | 
| 
 | 
  1578       res[ZDLL] = w*z;
 | 
| 
 | 
  1579       res[ZDDLL]= w;
 | 
| 
 | 
  1580       break;
 | 
| 
 | 
  1581     case LLOG:
 | 
| 
 | 
  1582       res[ZDLL] = w*z*mean;
 | 
| 
 | 
  1583       res[ZDDLL]= w*mean*mean;
 | 
| 
 | 
  1584       break;
 | 
| 
 | 
  1585     case LLOGIT:
 | 
| 
 | 
  1586       res[ZDLL] = w*z*mean*(1-mean);
 | 
| 
 | 
  1587       res[ZDDLL]= w*mean*mean*(1-mean)*(1-mean);
 | 
| 
 | 
  1588       break;
 | 
| 
 | 
  1589     default:
 | 
| 
 | 
  1590       LERR(("Invalid link for Gaussian family"));
 | 
| 
 | 
  1591       return(LF_LNK);
 | 
| 
 | 
  1592   }
 | 
| 
 | 
  1593   return(LF_OK);
 | 
| 
 | 
  1594 }
 | 
| 
 | 
  1595 
 | 
| 
 | 
  1596 int gaus_check(sp,des,lfd)
 | 
| 
 | 
  1597 smpar *sp;
 | 
| 
 | 
  1598 design *des;
 | 
| 
 | 
  1599 lfdata *lfd;
 | 
| 
 | 
  1600 { int i, ii;
 | 
| 
 | 
  1601   if (fami(sp)->robust) return(LF_OK);
 | 
| 
 | 
  1602   if (link(sp)==LIDENT)
 | 
| 
 | 
  1603   { for (i=0; i<des->n; i++)
 | 
| 
 | 
  1604     { ii = des->ind[i];
 | 
| 
 | 
  1605       if (cens(lfd,ii)) return(LF_OK);
 | 
| 
 | 
  1606     }
 | 
| 
 | 
  1607     return(LF_DONE);
 | 
| 
 | 
  1608   }
 | 
| 
 | 
  1609   return(LF_OK);
 | 
| 
 | 
  1610 }
 | 
| 
 | 
  1611 
 | 
| 
 | 
  1612 void setfgauss(fam)
 | 
| 
 | 
  1613 family *fam;
 | 
| 
 | 
  1614 { fam->deflink = LIDENT;
 | 
| 
 | 
  1615   fam->canlink = LIDENT;
 | 
| 
 | 
  1616   fam->vallink = gaus_vallink;
 | 
| 
 | 
  1617   fam->family  = gaus_fam;
 | 
| 
 | 
  1618   fam->pcheck  = gaus_check;
 | 
| 
 | 
  1619 }
 | 
| 
 | 
  1620 /*
 | 
| 
 | 
  1621  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1622  */
 | 
| 
 | 
  1623 #include "locf.h"
 | 
| 
 | 
  1624 
 | 
| 
 | 
  1625 int geom_vallink(link)
 | 
| 
 | 
  1626 int link;
 | 
| 
 | 
  1627 { return((link==LIDENT) | (link==LLOG));
 | 
| 
 | 
  1628 }
 | 
| 
 | 
  1629 
 | 
| 
 | 
  1630 int geom_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  1631 double y, mean, th, *res, w;
 | 
| 
 | 
  1632 int link, cens;
 | 
| 
 | 
  1633 { double p, pt, dp, p1;
 | 
| 
 | 
  1634   if (link==LINIT)
 | 
| 
 | 
  1635   { res[ZDLL] = MAX(y,0.0);
 | 
| 
 | 
  1636     return(LF_OK);
 | 
| 
 | 
  1637   }
 | 
| 
 | 
  1638   p = 1/(1+mean);
 | 
| 
 | 
  1639   if (cens) /* censored observation */
 | 
| 
 | 
  1640   { if (y<=0)
 | 
| 
 | 
  1641     { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
 | 
| 
 | 
  1642       return(LF_OK);
 | 
| 
 | 
  1643     }
 | 
| 
 | 
  1644     p1 = (link==LIDENT) ? -p*p : -p*(1-p);
 | 
| 
 | 
  1645     pt = 1-ibeta(p,w,y);
 | 
| 
 | 
  1646     dp = dbeta(p,w,y,0)/pt;
 | 
| 
 | 
  1647     res[ZLIK] = log(pt);
 | 
| 
 | 
  1648     res[ZDLL] = -dp*p1;
 | 
| 
 | 
  1649     res[ZDDLL] = dp*dp*p1*p1;
 | 
| 
 | 
  1650     if (link==LIDENT)
 | 
| 
 | 
  1651       res[ZDDLL] += dp*p*p*p*(1+w*(1-p)-p*y)/(1-p);
 | 
| 
 | 
  1652     else
 | 
| 
 | 
  1653       res[ZDDLL] += dp*p*(1-p)*(w*(1-p)-p*y);
 | 
| 
 | 
  1654     return(LF_OK);
 | 
| 
 | 
  1655   }
 | 
| 
 | 
  1656   else
 | 
| 
 | 
  1657   { res[ZLIK] = (y+w)*log((y/w+1)/(mean+1));
 | 
| 
 | 
  1658     if (y>0) res[ZLIK] += y*log(w*mean/y);
 | 
| 
 | 
  1659     if (link==LLOG)
 | 
| 
 | 
  1660     { res[ZDLL] = (y-w*mean)*p;
 | 
| 
 | 
  1661       res[ZDDLL]= (y+w)*p*(1-p);
 | 
| 
 | 
  1662       return(LF_OK);
 | 
| 
 | 
  1663     }
 | 
| 
 | 
  1664     if (link==LIDENT)
 | 
| 
 | 
  1665     { res[ZDLL] = (y-w*mean)/(mean*(1+mean));
 | 
| 
 | 
  1666       res[ZDDLL]= w/(mean*(1+mean));
 | 
| 
 | 
  1667       return(LF_OK);
 | 
| 
 | 
  1668     }
 | 
| 
 | 
  1669   }
 | 
| 
 | 
  1670   LERR(("link %d invalid for geometric family",link));
 | 
| 
 | 
  1671   return(LF_LNK);
 | 
| 
 | 
  1672 }
 | 
| 
 | 
  1673 
 | 
| 
 | 
  1674 void setfgeom(fam)
 | 
| 
 | 
  1675 family *fam;
 | 
| 
 | 
  1676 { fam->deflink = LLOG;
 | 
| 
 | 
  1677   fam->canlink = LIDENT; /* this isn't correct. I haven't prog. canon */
 | 
| 
 | 
  1678   fam->vallink = geom_vallink;
 | 
| 
 | 
  1679   fam->family  = geom_fam;
 | 
| 
 | 
  1680 }
 | 
| 
 | 
  1681 /*
 | 
| 
 | 
  1682  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1683  */
 | 
| 
 | 
  1684 #include "locf.h"
 | 
| 
 | 
  1685 
 | 
| 
 | 
  1686 #define HUBERC 2.0
 | 
| 
 | 
  1687 
 | 
| 
 | 
  1688 double links_rs;
 | 
| 
 | 
  1689 int inllmix=0;
 | 
| 
 | 
  1690 
 | 
| 
 | 
  1691 /*
 | 
| 
 | 
  1692  * lffamily("name") converts family names into a numeric value.
 | 
| 
 | 
  1693  * typical usage is  fam(&lf->sp) = lffamily("gaussian");
 | 
| 
 | 
  1694  * Note that family can be preceded by q and/or r for quasi, robust.
 | 
| 
 | 
  1695  *
 | 
| 
 | 
  1696  * link(&lf->sp) = lflink("log") does the same for the link function.
 | 
| 
 | 
  1697  */
 | 
| 
 | 
  1698 #define NFAMILY 18
 | 
| 
 | 
  1699 static char *famil[NFAMILY] =
 | 
| 
 | 
  1700   { "density", "ate",   "hazard",    "gaussian", "binomial",
 | 
| 
 | 
  1701     "poisson", "gamma", "geometric", "circular", "obust", "huber",
 | 
| 
 | 
  1702     "weibull", "cauchy","probab",    "logistic", "nbinomial",
 | 
| 
 | 
  1703     "vonmises", "quant" };
 | 
| 
 | 
  1704 static int   fvals[NFAMILY] =
 | 
| 
 | 
  1705   { TDEN,  TRAT,  THAZ,  TGAUS, TLOGT,
 | 
| 
 | 
  1706     TPOIS, TGAMM, TGEOM, TCIRC, TROBT, TROBT,
 | 
| 
 | 
  1707     TWEIB, TCAUC, TPROB, TLOGT, TGEOM, TCIRC, TQUANT };
 | 
| 
 | 
  1708 int lffamily(z)
 | 
| 
 | 
  1709 char *z;
 | 
| 
 | 
  1710 { int quasi, robu, f;
 | 
| 
 | 
  1711   quasi = robu = 0;
 | 
| 
 | 
  1712   while ((z[0]=='q') | (z[0]=='r'))
 | 
| 
 | 
  1713   { quasi |= (z[0]=='q');
 | 
| 
 | 
  1714     robu  |= (z[0]=='r');
 | 
| 
 | 
  1715     z++;
 | 
| 
 | 
  1716   }
 | 
| 
 | 
  1717   z[0] = tolower(z[0]);
 | 
| 
 | 
  1718   f = pmatch(z,famil,fvals,NFAMILY,-1);
 | 
| 
 | 
  1719   if ((z[0]=='o') | (z[0]=='a')) robu = 0;
 | 
| 
 | 
  1720   if (f==-1)
 | 
| 
 | 
  1721   { WARN(("unknown family %s",z));
 | 
| 
 | 
  1722     f = TGAUS;
 | 
| 
 | 
  1723   }
 | 
| 
 | 
  1724   if (quasi) f += 64;
 | 
| 
 | 
  1725   if (robu)  f += 128;
 | 
| 
 | 
  1726   return(f);
 | 
| 
 | 
  1727 }
 | 
| 
 | 
  1728 
 | 
| 
 | 
  1729 #define NLINKS 8
 | 
| 
 | 
  1730 static char *ltype[NLINKS] = { "default", "canonical", "identity", "log",
 | 
| 
 | 
  1731                           "logi",    "inverse",   "sqrt",     "arcsin" };
 | 
| 
 | 
  1732 static int   lvals[NLINKS] = { LDEFAU, LCANON, LIDENT, LLOG,
 | 
| 
 | 
  1733                           LLOGIT, LINVER, LSQRT,  LASIN };
 | 
| 
 | 
  1734 int lflink(char *z)
 | 
| 
 | 
  1735 { int f;
 | 
| 
 | 
  1736   if (z==NULL) return(LDEFAU);
 | 
| 
 | 
  1737   z[0] = tolower(z[0]);
 | 
| 
 | 
  1738   f = pmatch(z, ltype, lvals, NLINKS, -1);
 | 
| 
 | 
  1739   if (f==-1)
 | 
| 
 | 
  1740   { WARN(("unknown link %s",z));
 | 
| 
 | 
  1741     f = LDEFAU;
 | 
| 
 | 
  1742   }
 | 
| 
 | 
  1743   return(f);
 | 
| 
 | 
  1744 }
 | 
| 
 | 
  1745 
 | 
| 
 | 
  1746 int defaultlink(link,fam)
 | 
| 
 | 
  1747 int link;
 | 
| 
 | 
  1748 family *fam;
 | 
| 
 | 
  1749 { if (link==LDEFAU) return(fam->deflink);
 | 
| 
 | 
  1750   if (link==LCANON) return(fam->canlink);
 | 
| 
 | 
  1751   return(link);
 | 
| 
 | 
  1752 }
 | 
| 
 | 
  1753 
 | 
| 
 | 
  1754 /*
 | 
| 
 | 
  1755 void robustify(res,rs)
 | 
| 
 | 
  1756 double *res, rs;
 | 
| 
 | 
  1757 { double sc, z;
 | 
| 
 | 
  1758   sc = rs*HUBERC;
 | 
| 
 | 
  1759   if (res[ZLIK] > -sc*sc/2) return;
 | 
| 
 | 
  1760   z = sqrt(-2*res[ZLIK]);
 | 
| 
 | 
  1761   res[ZDDLL]= -sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z;
 | 
| 
 | 
  1762   res[ZDLL]*= sc/z;
 | 
| 
 | 
  1763   res[ZLIK] = sc*sc/2-sc*z;
 | 
| 
 | 
  1764 }
 | 
| 
 | 
  1765 */
 | 
| 
 | 
  1766 void robustify(res,rs)
 | 
| 
 | 
  1767 double *res, rs;
 | 
| 
 | 
  1768 { double sc, z;
 | 
| 
 | 
  1769   sc = rs*HUBERC;
 | 
| 
 | 
  1770   if (res[ZLIK] > -sc*sc/2)
 | 
| 
 | 
  1771   { res[ZLIK] /= sc*sc;
 | 
| 
 | 
  1772     res[ZDLL] /= sc*sc;
 | 
| 
 | 
  1773     res[ZDDLL] /= sc*sc;
 | 
| 
 | 
  1774     return;
 | 
| 
 | 
  1775   }
 | 
| 
 | 
  1776   z = sqrt(-2*res[ZLIK]);
 | 
| 
 | 
  1777   res[ZDDLL]= (-sc*res[ZDLL]*res[ZDLL]/(z*z*z)+sc*res[ZDDLL]/z)/(sc*sc);
 | 
| 
 | 
  1778   res[ZDLL]*= 1.0/(z*sc);
 | 
| 
 | 
  1779   res[ZLIK] = 0.5-z/sc;
 | 
| 
 | 
  1780 }
 | 
| 
 | 
  1781 
 | 
| 
 | 
  1782 double lf_link(y,lin)
 | 
| 
 | 
  1783 double y;
 | 
| 
 | 
  1784 int lin;
 | 
| 
 | 
  1785 { switch(lin)
 | 
| 
 | 
  1786   { case LIDENT: return(y);
 | 
| 
 | 
  1787     case LLOG:   return(log(y));
 | 
| 
 | 
  1788     case LLOGIT: return(logit(y));
 | 
| 
 | 
  1789     case LINVER: return(1/y);
 | 
| 
 | 
  1790     case LSQRT:  return(sqrt(fabs(y)));
 | 
| 
 | 
  1791     case LASIN:  return(asin(sqrt(y)));
 | 
| 
 | 
  1792   }
 | 
| 
 | 
  1793   LERR(("link: unknown link %d",lin));
 | 
| 
 | 
  1794   return(0.0);
 | 
| 
 | 
  1795 }
 | 
| 
 | 
  1796 
 | 
| 
 | 
  1797 double invlink(th,lin)
 | 
| 
 | 
  1798 double th;
 | 
| 
 | 
  1799 int lin;
 | 
| 
 | 
  1800 { switch(lin)
 | 
| 
 | 
  1801   { case LIDENT: return(th);
 | 
| 
 | 
  1802     case LLOG:   return(mut_exp(th));
 | 
| 
 | 
  1803     case LLOGIT: return(expit(th));
 | 
| 
 | 
  1804     case LINVER: return(1/th);
 | 
| 
 | 
  1805     case LSQRT:  return(th*fabs(th));
 | 
| 
 | 
  1806     case LASIN:  return(sin(th)*sin(th));
 | 
| 
 | 
  1807     case LINIT:  return(0.0);
 | 
| 
 | 
  1808   }
 | 
| 
 | 
  1809   LERR(("invlink: unknown link %d",lin));
 | 
| 
 | 
  1810   return(0.0);
 | 
| 
 | 
  1811 }
 | 
| 
 | 
  1812 
 | 
| 
 | 
  1813 /* the link and various related functions */
 | 
| 
 | 
  1814 int links(th,y,fam,link,res,c,w,rs)
 | 
| 
 | 
  1815 double th, y, *res, w, rs;
 | 
| 
 | 
  1816 int link, c;
 | 
| 
 | 
  1817 family *fam;
 | 
| 
 | 
  1818 { double mean;
 | 
| 
 | 
  1819   int st;
 | 
| 
 | 
  1820 
 | 
| 
 | 
  1821   mean = res[ZMEAN] = invlink(th,link);
 | 
| 
 | 
  1822   if (lf_error) return(LF_LNK);
 | 
| 
 | 
  1823   links_rs = rs;
 | 
| 
 | 
  1824 /*  mut_printf("links: rs %8.5f\n",rs); */
 | 
| 
 | 
  1825 
 | 
| 
 | 
  1826   st = fam->family(y,mean,th,link,res,c,w);
 | 
| 
 | 
  1827 
 | 
| 
 | 
  1828   if (st!=LF_OK) return(st);
 | 
| 
 | 
  1829   if (link==LINIT) return(st);
 | 
| 
 | 
  1830   if (isrobust(fam)) robustify(res,rs);
 | 
| 
 | 
  1831   return(st);
 | 
| 
 | 
  1832 }
 | 
| 
 | 
  1833 
 | 
| 
 | 
  1834 /*
 | 
| 
 | 
  1835   stdlinks is a version of links when family, link, response e.t.c
 | 
| 
 | 
  1836   all come from the standard places.
 | 
| 
 | 
  1837 */
 | 
| 
 | 
  1838 int stdlinks(res,lfd,sp,i,th,rs)
 | 
| 
 | 
  1839 lfdata *lfd;
 | 
| 
 | 
  1840 smpar *sp;
 | 
| 
 | 
  1841 double th, rs, *res;
 | 
| 
 | 
  1842 int i;
 | 
| 
 | 
  1843 {
 | 
| 
 | 
  1844   return(links(th,resp(lfd,i),fami(sp),link(sp),res,cens(lfd,i),prwt(lfd,i),rs));
 | 
| 
 | 
  1845 }
 | 
| 
 | 
  1846 
 | 
| 
 | 
  1847 /*
 | 
| 
 | 
  1848  *  functions used in variance, skewness, kurtosis calculations
 | 
| 
 | 
  1849  *  in scb corrections.
 | 
| 
 | 
  1850  */
 | 
| 
 | 
  1851 
 | 
| 
 | 
  1852 double b2(th,tg,w)
 | 
| 
 | 
  1853 double th, w;
 | 
| 
 | 
  1854 int tg;
 | 
| 
 | 
  1855 { double y;
 | 
| 
 | 
  1856   switch(tg&63)
 | 
| 
 | 
  1857   { case TGAUS: return(w);
 | 
| 
 | 
  1858     case TPOIS: return(w*mut_exp(th));
 | 
| 
 | 
  1859     case TLOGT:
 | 
| 
 | 
  1860       y = expit(th);
 | 
| 
 | 
  1861       return(w*y*(1-y));
 | 
| 
 | 
  1862   }
 | 
| 
 | 
  1863   LERR(("b2: invalid family %d",tg));
 | 
| 
 | 
  1864   return(0.0);
 | 
| 
 | 
  1865 }
 | 
| 
 | 
  1866 
 | 
| 
 | 
  1867 double b3(th,tg,w)
 | 
| 
 | 
  1868 double th, w;
 | 
| 
 | 
  1869 int tg;
 | 
| 
 | 
  1870 { double y;
 | 
| 
 | 
  1871   switch(tg&63)
 | 
| 
 | 
  1872   { case TGAUS: return(0.0);
 | 
| 
 | 
  1873     case TPOIS: return(w*mut_exp(th));
 | 
| 
 | 
  1874     case TLOGT:
 | 
| 
 | 
  1875       y = expit(th);
 | 
| 
 | 
  1876       return(w*y*(1-y)*(1-2*y));
 | 
| 
 | 
  1877   }
 | 
| 
 | 
  1878   LERR(("b3: invalid family %d",tg));
 | 
| 
 | 
  1879   return(0.0);
 | 
| 
 | 
  1880 }
 | 
| 
 | 
  1881 
 | 
| 
 | 
  1882 double b4(th,tg,w)
 | 
| 
 | 
  1883 double th, w;
 | 
| 
 | 
  1884 int tg;
 | 
| 
 | 
  1885 { double y;
 | 
| 
 | 
  1886   switch(tg&63)
 | 
| 
 | 
  1887   { case TGAUS: return(0.0);
 | 
| 
 | 
  1888     case TPOIS: return(w*mut_exp(th));
 | 
| 
 | 
  1889     case TLOGT:
 | 
| 
 | 
  1890       y = expit(th); y = y*(1-y);
 | 
| 
 | 
  1891       return(w*y*(1-6*y));
 | 
| 
 | 
  1892   }
 | 
| 
 | 
  1893   LERR(("b4: invalid family %d",tg));
 | 
| 
 | 
  1894   return(0.0);
 | 
| 
 | 
  1895 }
 | 
| 
 | 
  1896 
 | 
| 
 | 
  1897 int def_check(sp,des,lfd)
 | 
| 
 | 
  1898 smpar *sp;
 | 
| 
 | 
  1899 design *des;
 | 
| 
 | 
  1900 lfdata *lfd;
 | 
| 
 | 
  1901 { switch(link(sp))
 | 
| 
 | 
  1902   { case LLOG: if (des->cf[0]>700) return(LF_OOB);
 | 
| 
 | 
  1903                break;
 | 
| 
 | 
  1904   }
 | 
| 
 | 
  1905   return(LF_OK);
 | 
| 
 | 
  1906 }
 | 
| 
 | 
  1907 extern void setfdensity(), setfgauss(), setfbino(), setfpoisson();
 | 
| 
 | 
  1908 extern void setfgamma(), setfgeom(), setfcirc(), setfweibull();
 | 
| 
 | 
  1909 extern void setfrbino(), setfrobust(), setfcauchy(), setfquant();
 | 
| 
 | 
  1910 
 | 
| 
 | 
  1911 void setfamily(sp)
 | 
| 
 | 
  1912 smpar *sp;
 | 
| 
 | 
  1913 { int tg, lnk;
 | 
| 
 | 
  1914   family *f;
 | 
| 
 | 
  1915 
 | 
| 
 | 
  1916   tg = fam(sp);
 | 
| 
 | 
  1917   f = fami(sp);
 | 
| 
 | 
  1918   f->quasi = tg&64;
 | 
| 
 | 
  1919   f->robust = tg&128;
 | 
| 
 | 
  1920   f->initial = reginit;
 | 
| 
 | 
  1921   f->like = likereg;
 | 
| 
 | 
  1922   f->pcheck = def_check;
 | 
| 
 | 
  1923 
 | 
| 
 | 
  1924   switch(tg&63)
 | 
| 
 | 
  1925   { case TDEN:
 | 
| 
 | 
  1926     case THAZ:
 | 
| 
 | 
  1927     case TRAT:	setfdensity(f); break;
 | 
| 
 | 
  1928     case TGAUS: setfgauss(f); break;
 | 
| 
 | 
  1929     case TLOGT: setfbino(f); break;
 | 
| 
 | 
  1930     case TRBIN: setfrbino(f); break;
 | 
| 
 | 
  1931     case TPROB:
 | 
| 
 | 
  1932     case TPOIS: setfpoisson(f); break;
 | 
| 
 | 
  1933     case TGAMM: setfgamma(f); break;
 | 
| 
 | 
  1934     case TGEOM: setfgeom(f); break;
 | 
| 
 | 
  1935     case TWEIB: setfweibull(f);
 | 
| 
 | 
  1936     case TCIRC: setfcirc(f); break;
 | 
| 
 | 
  1937     case TROBT: setfrobust(f); break;
 | 
| 
 | 
  1938     case TCAUC: setfcauchy(f); break;
 | 
| 
 | 
  1939     case TQUANT: setfquant(f); break;
 | 
| 
 | 
  1940     default: LERR(("setfamily: unknown family %d",tg&63));
 | 
| 
 | 
  1941              return;
 | 
| 
 | 
  1942   }
 | 
| 
 | 
  1943   
 | 
| 
 | 
  1944   lnk = defaultlink(link(sp),f);
 | 
| 
 | 
  1945   if (!f->vallink(lnk))
 | 
| 
 | 
  1946   { WARN(("setfamily: invalid link %d - revert to default",link(sp)));
 | 
| 
 | 
  1947     link(sp) = f->deflink;
 | 
| 
 | 
  1948   }
 | 
| 
 | 
  1949   else
 | 
| 
 | 
  1950     link(sp) = lnk;
 | 
| 
 | 
  1951 }
 | 
| 
 | 
  1952 /*
 | 
| 
 | 
  1953  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  1954  */
 | 
| 
 | 
  1955 #include "locf.h"
 | 
| 
 | 
  1956 
 | 
| 
 | 
  1957 int pois_vallink(link)
 | 
| 
 | 
  1958 int link;
 | 
| 
 | 
  1959 { return((link==LLOG) | (link==LIDENT) | (link==LSQRT));
 | 
| 
 | 
  1960 }
 | 
| 
 | 
  1961 
 | 
| 
 | 
  1962 int pois_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  1963 double y, mean, th, *res, w;
 | 
| 
 | 
  1964 int link, cens;
 | 
| 
 | 
  1965 { double wmu, pt, dp;
 | 
| 
 | 
  1966   if (link==LINIT)
 | 
| 
 | 
  1967   { res[ZDLL] = MAX(y,0.0);
 | 
| 
 | 
  1968     return(LF_OK);
 | 
| 
 | 
  1969   }
 | 
| 
 | 
  1970   wmu = w*mean;
 | 
| 
 | 
  1971   if (inllmix) y = w*y;
 | 
| 
 | 
  1972   if (cens)
 | 
| 
 | 
  1973   { if (y<=0)
 | 
| 
 | 
  1974     { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0.0;
 | 
| 
 | 
  1975       return(LF_OK);
 | 
| 
 | 
  1976     }
 | 
| 
 | 
  1977     pt = igamma(wmu,y);
 | 
| 
 | 
  1978     dp = dgamma(wmu,y,1.0,0)/pt;
 | 
| 
 | 
  1979     res[ZLIK] = log(pt);
 | 
| 
 | 
  1980 /*
 | 
| 
 | 
  1981  * res[ZDLL] = dp * w*dmu/dth
 | 
| 
 | 
  1982  * res[ZDDLL]= -dp*(w*d2mu/dth2 + (y-1)/mu*(dmu/dth)^2) + res[ZDLL]^2
 | 
| 
 | 
  1983  */
 | 
| 
 | 
  1984     if (link==LLOG)
 | 
| 
 | 
  1985     { res[ZDLL] = dp*wmu;
 | 
| 
 | 
  1986       res[ZDDLL]= -dp*wmu*(y-wmu) + SQR(res[ZDLL]);
 | 
| 
 | 
  1987       return(LF_OK);
 | 
| 
 | 
  1988     }
 | 
| 
 | 
  1989     if (link==LIDENT)
 | 
| 
 | 
  1990     { res[ZDLL] = dp*w;
 | 
| 
 | 
  1991       res[ZDDLL]= -dp*(y-1-wmu)*w/mean + SQR(res[ZDLL]);
 | 
| 
 | 
  1992       return(LF_OK);
 | 
| 
 | 
  1993     }
 | 
| 
 | 
  1994     if (link==LSQRT)
 | 
| 
 | 
  1995     { res[ZDLL] = dp*2*w*th;
 | 
| 
 | 
  1996       res[ZDDLL]= -dp*w*(4*y-2-4*wmu) + SQR(res[ZDLL]);
 | 
| 
 | 
  1997       return(LF_OK);
 | 
| 
 | 
  1998   } }
 | 
| 
 | 
  1999   if (link==LLOG)
 | 
| 
 | 
  2000   { if (y<0) /* goon observation - delete it */
 | 
| 
 | 
  2001     { res[ZLIK] = res[ZDLL] = res[ZDDLL] = 0;
 | 
| 
 | 
  2002       return(LF_OK);
 | 
| 
 | 
  2003     }
 | 
| 
 | 
  2004     res[ZLIK] = res[ZDLL] = y-wmu;
 | 
| 
 | 
  2005     if (y>0) res[ZLIK] += y*(th-log(y/w));
 | 
| 
 | 
  2006     res[ZDDLL] = wmu;
 | 
| 
 | 
  2007     return(LF_OK);
 | 
| 
 | 
  2008   }
 | 
| 
 | 
  2009   if (link==LIDENT)
 | 
| 
 | 
  2010   { if ((mean<=0) && (y>0)) return(LF_BADP);
 | 
| 
 | 
  2011     res[ZLIK] = y-wmu;
 | 
| 
 | 
  2012     res[ZDLL] = -w;
 | 
| 
 | 
  2013     res[ZDDLL] = 0;
 | 
| 
 | 
  2014     if (y>0)
 | 
| 
 | 
  2015     { res[ZLIK] += y*log(wmu/y);
 | 
| 
 | 
  2016       res[ZDLL] += y/mean;
 | 
| 
 | 
  2017       res[ZDDLL]= y/(mean*mean);
 | 
| 
 | 
  2018     }
 | 
| 
 | 
  2019     return(LF_OK);
 | 
| 
 | 
  2020   }
 | 
| 
 | 
  2021   if (link==LSQRT)
 | 
| 
 | 
  2022   { if ((mean<=0) && (y>0)) return(LF_BADP);
 | 
| 
 | 
  2023     res[ZLIK] = y-wmu;
 | 
| 
 | 
  2024     res[ZDLL] = -2*w*th;
 | 
| 
 | 
  2025     res[ZDDLL]= 2*w;
 | 
| 
 | 
  2026     if (y>0)
 | 
| 
 | 
  2027     { res[ZLIK] += y*log(wmu/y);
 | 
| 
 | 
  2028       res[ZDLL] += 2*y/th;
 | 
| 
 | 
  2029       res[ZDDLL]+= 2*y/mean;
 | 
| 
 | 
  2030     }
 | 
| 
 | 
  2031     return(LF_OK);
 | 
| 
 | 
  2032   }
 | 
| 
 | 
  2033   LERR(("link %d invalid for Poisson family",link));
 | 
| 
 | 
  2034   return(LF_LNK);
 | 
| 
 | 
  2035 }
 | 
| 
 | 
  2036 
 | 
| 
 | 
  2037 void setfpoisson(fam)
 | 
| 
 | 
  2038 family *fam;
 | 
| 
 | 
  2039 { fam->deflink = LLOG;
 | 
| 
 | 
  2040   fam->canlink = LLOG;
 | 
| 
 | 
  2041   fam->vallink = pois_vallink;
 | 
| 
 | 
  2042   fam->family  = pois_fam;
 | 
| 
 | 
  2043 }
 | 
| 
 | 
  2044 /*
 | 
| 
 | 
  2045  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  2046  */
 | 
| 
 | 
  2047 #include "locf.h"
 | 
| 
 | 
  2048 
 | 
| 
 | 
  2049 #define QTOL 1.0e-10
 | 
| 
 | 
  2050 extern int lf_status;
 | 
| 
 | 
  2051 static double q0;
 | 
| 
 | 
  2052 
 | 
| 
 | 
  2053 int quant_vallink(int link) { return(1); }
 | 
| 
 | 
  2054 
 | 
| 
 | 
  2055 int quant_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  2056 double y, mean, th, *res, w;
 | 
| 
 | 
  2057 int link, cens;
 | 
| 
 | 
  2058 { double z, p;
 | 
| 
 | 
  2059   if (link==LINIT)
 | 
| 
 | 
  2060   { res[ZDLL] = w*y;
 | 
| 
 | 
  2061     return(LF_OK);
 | 
| 
 | 
  2062   }
 | 
| 
 | 
  2063 p = 0.5; /* should be pen(sp) */
 | 
| 
 | 
  2064   z = y-mean;
 | 
| 
 | 
  2065   res[ZLIK] = (z<0) ? (w*z/p) : (-w*z/(1-p));
 | 
| 
 | 
  2066   res[ZDLL] = (z<0) ? -w/p : w/(1-p);
 | 
| 
 | 
  2067   res[ZDDLL]= w/(p*(1-p));
 | 
| 
 | 
  2068   return(LF_OK);
 | 
| 
 | 
  2069 }
 | 
| 
 | 
  2070 
 | 
| 
 | 
  2071 int quant_check(sp,des,lfd)
 | 
| 
 | 
  2072 smpar *sp;
 | 
| 
 | 
  2073 design *des;
 | 
| 
 | 
  2074 lfdata *lfd;
 | 
| 
 | 
  2075 { return(LF_DONE);
 | 
| 
 | 
  2076 }
 | 
| 
 | 
  2077 
 | 
| 
 | 
  2078 void setfquant(fam)
 | 
| 
 | 
  2079 family *fam;
 | 
| 
 | 
  2080 { fam->deflink = LIDENT;
 | 
| 
 | 
  2081   fam->canlink = LIDENT;
 | 
| 
 | 
  2082   fam->vallink = quant_vallink;
 | 
| 
 | 
  2083   fam->family  = quant_fam;
 | 
| 
 | 
  2084   fam->pcheck  = quant_check;
 | 
| 
 | 
  2085 }
 | 
| 
 | 
  2086 
 | 
| 
 | 
  2087 /*
 | 
| 
 | 
  2088  * cycling rule for choosing among ties.
 | 
| 
 | 
  2089  */
 | 
| 
 | 
  2090 int tiecycle(ind,i0,i1,oi)
 | 
| 
 | 
  2091 int *ind, i0, i1, oi;
 | 
| 
 | 
  2092 { int i, ii, im;
 | 
| 
 | 
  2093   im = ind[i0];
 | 
| 
 | 
  2094   for (i=i0+1; i<=i1; i++)
 | 
| 
 | 
  2095   { ii = ind[i];
 | 
| 
 | 
  2096     if (im<=oi)
 | 
| 
 | 
  2097     { if ((ii<im) | (ii>oi)) im = ii;
 | 
| 
 | 
  2098     }
 | 
| 
 | 
  2099     else
 | 
| 
 | 
  2100     { if ((ii<im) & (ii>oi)) im = ii;
 | 
| 
 | 
  2101     }
 | 
| 
 | 
  2102   }
 | 
| 
 | 
  2103   return(im);
 | 
| 
 | 
  2104 }
 | 
| 
 | 
  2105 
 | 
| 
 | 
  2106 /*
 | 
| 
 | 
  2107  * move coefficient vector cf, as far as possible, in direction dc.
 | 
| 
 | 
  2108  */
 | 
| 
 | 
  2109 int movecoef(lfd,des,p,cf,dc,oi)
 | 
| 
 | 
  2110 lfdata *lfd;
 | 
| 
 | 
  2111 design *des;
 | 
| 
 | 
  2112 double p, *cf, *dc;
 | 
| 
 | 
  2113 int oi;
 | 
| 
 | 
  2114 { int i, ii, im, i0, i1, j;
 | 
| 
 | 
  2115   double *lb, *el, e, sp, sn, sw, sum1, sum2, tol1;
 | 
| 
 | 
  2116 
 | 
| 
 | 
  2117   lb = des->th;
 | 
| 
 | 
  2118   el = des->res;
 | 
| 
 | 
  2119   sum1 = sum2 = 0.0;
 | 
| 
 | 
  2120 
 | 
| 
 | 
  2121   sp = sn = sw = 0.0;
 | 
| 
 | 
  2122   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2123   { ii = des->ind[i];
 | 
| 
 | 
  2124     lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
 | 
| 
 | 
  2125     e = resp(lfd,ii) - innerprod(cf,d_xi(des,ii),des->p);
 | 
| 
 | 
  2126     el[ii] = (fabs(lb[ii])<QTOL) ? 1e100 : e/lb[ii];
 | 
| 
 | 
  2127     if (lb[ii]>0)
 | 
| 
 | 
  2128       sp += prwt(lfd,ii)*wght(des,ii)*lb[ii];
 | 
| 
 | 
  2129     else
 | 
| 
 | 
  2130       sn -= prwt(lfd,ii)*wght(des,ii)*lb[ii];
 | 
| 
 | 
  2131     sw += prwt(lfd,ii)*wght(des,ii);
 | 
| 
 | 
  2132   }
 | 
| 
 | 
  2133 printf("sp %8.5f  sn %8.5f\n",sn,sp);
 | 
| 
 | 
  2134 /* if sn, sp are both zero, should return an LF_PF.
 | 
| 
 | 
  2135  * but within numerical tolerance? what does it mean?
 | 
| 
 | 
  2136  */
 | 
| 
 | 
  2137   if (sn+sp <= QTOL*q0) { lf_status = LF_PF; return(0); }
 | 
| 
 | 
  2138 
 | 
| 
 | 
  2139   sum1 = sp/(1-p) + sn/p;
 | 
| 
 | 
  2140   tol1 = QTOL*(sp+sn);
 | 
| 
 | 
  2141   mut_order(el,des->ind,0,des->n-1);
 | 
| 
 | 
  2142 
 | 
| 
 | 
  2143   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2144   { ii = des->ind[i];
 | 
| 
 | 
  2145     sum2 += prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/p : -lb[ii]/(1-p) );
 | 
| 
 | 
  2146     sum1 -= prwt(lfd,ii)*wght(des,ii)*((lb[ii]>0) ? lb[ii]/(1-p) : -lb[ii]/p );
 | 
| 
 | 
  2147     if (sum1<=sum2+tol1)
 | 
| 
 | 
  2148     {
 | 
| 
 | 
  2149 /* determine the range of ties [i0,i1]
 | 
| 
 | 
  2150  *   el[ind[i0..i1]] = el[ind[i]].
 | 
| 
 | 
  2151  *   if sum1==sum2, el[ind[i+1]]..el[ind[i1]]] = el[ind[i1]], else i1 = i.
 | 
| 
 | 
  2152  */
 | 
| 
 | 
  2153       i0 = i1 = i;
 | 
| 
 | 
  2154       while ((i0>0) && (el[des->ind[i0-1]]==el[ii])) i0--;
 | 
| 
 | 
  2155       while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[ii])) i1++;
 | 
| 
 | 
  2156       if (sum1>=sum2-tol1)
 | 
| 
 | 
  2157         while ((i1<des->n-1) && (el[des->ind[i1+1]]==el[des->ind[i+1]])) i1++;
 | 
| 
 | 
  2158 
 | 
| 
 | 
  2159       if (i0<i1) ii = tiecycle(des->ind,i0,i1,oi);
 | 
| 
 | 
  2160       for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
 | 
| 
 | 
  2161       return(ii);
 | 
| 
 | 
  2162     }
 | 
| 
 | 
  2163   }
 | 
| 
 | 
  2164 mut_printf("Big finddlt problem.\n");
 | 
| 
 | 
  2165 ii = des->ind[des->n-1];
 | 
| 
 | 
  2166 for (j=0; j<des->p; j++) cf[j] += el[ii]*dc[j];
 | 
| 
 | 
  2167 return(ii);
 | 
| 
 | 
  2168 }
 | 
| 
 | 
  2169 
 | 
| 
 | 
  2170 /*
 | 
| 
 | 
  2171  * special version of movecoef for min/max.
 | 
| 
 | 
  2172  */
 | 
| 
 | 
  2173 int movemin(lfd,des,f,cf,dc,oi)
 | 
| 
 | 
  2174 design *des;
 | 
| 
 | 
  2175 lfdata *lfd;
 | 
| 
 | 
  2176 double *cf, *dc, f;
 | 
| 
 | 
  2177 int oi;
 | 
| 
 | 
  2178 { int i, ii, im, p, s, ssum;
 | 
| 
 | 
  2179   double *lb, sum, lb0, lb1, z0, z1;
 | 
| 
 | 
  2180 
 | 
| 
 | 
  2181   lb = des->th;
 | 
| 
 | 
  2182   s = (f<=0.0) ? 1 : -1;
 | 
| 
 | 
  2183 
 | 
| 
 | 
  2184 /* first, determine whether move should be in positive or negative direction */
 | 
| 
 | 
  2185   p = des->p;
 | 
| 
 | 
  2186   sum = 0;
 | 
| 
 | 
  2187   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2188   { ii = des->ind[i];
 | 
| 
 | 
  2189     lb[ii] = innerprod(dc,d_xi(des,ii),des->p);
 | 
| 
 | 
  2190     sum += prwt(lfd,ii)*wght(des,ii)*lb[ii];
 | 
| 
 | 
  2191   }
 | 
| 
 | 
  2192   if (fabs(sum) <= QTOL*q0)
 | 
| 
 | 
  2193   { lf_status = LF_PF;
 | 
| 
 | 
  2194     return(0);
 | 
| 
 | 
  2195   }
 | 
| 
 | 
  2196   ssum = (sum<=0.0) ? -1 : 1;
 | 
| 
 | 
  2197   if (ssum != s)
 | 
| 
 | 
  2198     for (i=0; i<p; i++) dc[i] = -dc[i];
 | 
| 
 | 
  2199 
 | 
| 
 | 
  2200 /* now, move positively. How far can we move? */
 | 
| 
 | 
  2201   lb0 = 1.0e100; im = oi;
 | 
| 
 | 
  2202   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2203   { ii = des->ind[i];
 | 
| 
 | 
  2204     lb[ii] = innerprod(dc,d_xi(des,ii),des->p); /* must recompute - signs! */
 | 
| 
 | 
  2205     if (s*lb[ii]>QTOL) /* should have scale-free tolerance here */
 | 
| 
 | 
  2206     { z0 = innerprod(cf,d_xi(des,ii),p);
 | 
| 
 | 
  2207       lb1 = (resp(lfd,ii) - z0)/lb[ii];
 | 
| 
 | 
  2208       if (lb1<lb0)
 | 
| 
 | 
  2209       { if (fabs(lb1-lb0)<QTOL) /* cycle */
 | 
| 
 | 
  2210         { if (im<=oi)
 | 
| 
 | 
  2211           { if ((ii>oi) | (ii<im)) im = ii; }
 | 
| 
 | 
  2212           else
 | 
| 
 | 
  2213           { if ((ii>oi) & (ii<im)) im = ii; }
 | 
| 
 | 
  2214         }
 | 
| 
 | 
  2215         else
 | 
| 
 | 
  2216         { im = ii; lb0 = lb1; }
 | 
| 
 | 
  2217       }
 | 
| 
 | 
  2218     }
 | 
| 
 | 
  2219   }
 | 
| 
 | 
  2220 
 | 
| 
 | 
  2221   for (i=0; i<p; i++) cf[i] = cf[i]+lb0*dc[i];
 | 
| 
 | 
  2222   if (im==-1) lf_status = LF_PF;
 | 
| 
 | 
  2223   return(im);
 | 
| 
 | 
  2224 }
 | 
| 
 | 
  2225 
 | 
| 
 | 
  2226 double qll(lfd,spr,des,cf)
 | 
| 
 | 
  2227 lfdata *lfd;
 | 
| 
 | 
  2228 smpar *spr;
 | 
| 
 | 
  2229 design *des;
 | 
| 
 | 
  2230 double *cf;
 | 
| 
 | 
  2231 { int i, ii;
 | 
| 
 | 
  2232   double th, sp, sn, p, e;
 | 
| 
 | 
  2233 
 | 
| 
 | 
  2234   p = pen(spr);
 | 
| 
 | 
  2235   sp = sn = 0.0;
 | 
| 
 | 
  2236   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2237   { ii = des->ind[i];
 | 
| 
 | 
  2238     th = innerprod(d_xi(des,ii),cf,des->p);
 | 
| 
 | 
  2239     e = resp(lfd,ii)-th;
 | 
| 
 | 
  2240     if (e<0) sn -= prwt(lfd,ii)*wght(des,ii)*e;
 | 
| 
 | 
  2241     if (e>0) sp += prwt(lfd,ii)*wght(des,ii)*e;
 | 
| 
 | 
  2242   }
 | 
| 
 | 
  2243   if (p<=0.0) return((sn<QTOL) ? -sp : -1e300);
 | 
| 
 | 
  2244   if (p>=1.0) return((sp<QTOL) ? -sn : -1e300);
 | 
| 
 | 
  2245   return(-sp/(1-p)-sn/p);
 | 
| 
 | 
  2246 }
 | 
| 
 | 
  2247 
 | 
| 
 | 
  2248 /*
 | 
| 
 | 
  2249  * running quantile smoother.
 | 
| 
 | 
  2250  */
 | 
| 
 | 
  2251 void lfquantile(lfd,sp,des,maxit)
 | 
| 
 | 
  2252 lfdata *lfd;
 | 
| 
 | 
  2253 smpar *sp;
 | 
| 
 | 
  2254 design *des;
 | 
| 
 | 
  2255 int maxit;
 | 
| 
 | 
  2256 { int i, ii, im, j, k, p, *ci, (*mover)();
 | 
| 
 | 
  2257   double *cf, *db, *dc, *cm, f, q1, q2, l0;
 | 
| 
 | 
  2258 
 | 
| 
 | 
  2259 printf("in lfquantile\n");
 | 
| 
 | 
  2260   f = pen(sp);
 | 
| 
 | 
  2261   p = des->p;
 | 
| 
 | 
  2262   cf = des->cf;
 | 
| 
 | 
  2263   dc = des->oc;
 | 
| 
 | 
  2264   db = des->ss;
 | 
| 
 | 
  2265   setzero(cf,p);
 | 
| 
 | 
  2266   setzero(dc,p);
 | 
| 
 | 
  2267   cm = des->V;
 | 
| 
 | 
  2268   setzero(cm,p*p);
 | 
| 
 | 
  2269   ci = (int *)des->fix;
 | 
| 
 | 
  2270 
 | 
| 
 | 
  2271   q1 = -qll(lfd,sp,des,cf);
 | 
| 
 | 
  2272   if (q1==0.0) { lf_status = LF_PF; return; }
 | 
| 
 | 
  2273   for (i=0; i<p; i++) cm[i*(p+1)] = 1;
 | 
| 
 | 
  2274   mover = movecoef;
 | 
| 
 | 
  2275   if ((f<=0.0) | (f>=1.0)) mover = movemin;
 | 
| 
 | 
  2276 
 | 
| 
 | 
  2277   dc[0] = 1.0;
 | 
| 
 | 
  2278   im = mover(lfd,des,f,cf,dc,-1);
 | 
| 
 | 
  2279   if (lf_status != LF_OK) return;
 | 
| 
 | 
  2280   ci[0] = im;
 | 
| 
 | 
  2281 printf("init const %2d\n",ci[0]);
 | 
| 
 | 
  2282   q0 = -qll(lfd,sp,des,cf);
 | 
| 
 | 
  2283   if (q0<QTOL*q1) { lf_status = LF_PF; return; }
 | 
| 
 | 
  2284 
 | 
| 
 | 
  2285 printf("loop 0\n"); fflush(stdout);
 | 
| 
 | 
  2286   for (i=1; i<p; i++)
 | 
| 
 | 
  2287   {
 | 
| 
 | 
  2288 printf("i %2d\n",i);
 | 
| 
 | 
  2289     memcpy(&cm[(i-1)*p],d_xi(des,im),p*sizeof(double));
 | 
| 
 | 
  2290     setzero(db,p);
 | 
| 
 | 
  2291     db[i] = 1.0;
 | 
| 
 | 
  2292     resproj(db,cm,dc,p,i);
 | 
| 
 | 
  2293 printf("call mover\n"); fflush(stdout);
 | 
| 
 | 
  2294     im = mover(lfd,des,f,cf,dc,-1);
 | 
| 
 | 
  2295     if (lf_status != LF_OK) return;
 | 
| 
 | 
  2296 printf("mover %2d\n",im); fflush(stdout);
 | 
| 
 | 
  2297     ci[i] = im;
 | 
| 
 | 
  2298   }
 | 
| 
 | 
  2299 printf("call qll\n"); fflush(stdout);
 | 
| 
 | 
  2300   q1 = qll(lfd,sp,des,cf);
 | 
| 
 | 
  2301 
 | 
| 
 | 
  2302 printf("loop 1    %d %d %d %d\n",ci[0],ci[1],ci[2],ci[3]); fflush(stdout);
 | 
| 
 | 
  2303   for (k=0; k<maxit; k++)
 | 
| 
 | 
  2304   { for (i=0; i<p; i++)
 | 
| 
 | 
  2305     { for (j=0; j<p; j++)
 | 
| 
 | 
  2306         if (j!=i) memcpy(&cm[(j-(j>i))*p],d_xi(des,ci[j]),p*sizeof(double));
 | 
| 
 | 
  2307       memcpy(db,d_xi(des,ci[i]),p*sizeof(double));
 | 
| 
 | 
  2308       resproj(db,cm,dc,p,p-1);
 | 
| 
 | 
  2309 printf("call mover\n"); fflush(stdout);
 | 
| 
 | 
  2310       im = mover(lfd,des,f,cf,dc,ci[i]);
 | 
| 
 | 
  2311       if (lf_status != LF_OK) return;
 | 
| 
 | 
  2312 printf("mover %2d\n",im); fflush(stdout);
 | 
| 
 | 
  2313       ci[i] = im;
 | 
| 
 | 
  2314     }
 | 
| 
 | 
  2315     q2 = qll(lfd,sp,des,cf);
 | 
| 
 | 
  2316 /*
 | 
| 
 | 
  2317  * convergence: require no change -- reasonable, since discrete?
 | 
| 
 | 
  2318  * remember we're maximizing, and q's are negative.
 | 
| 
 | 
  2319  */
 | 
| 
 | 
  2320      if (q2 <= q1) return;
 | 
| 
 | 
  2321      q1 = q2;
 | 
| 
 | 
  2322   }
 | 
| 
 | 
  2323 printf("loop 2\n");
 | 
| 
 | 
  2324   mut_printf("Warning: lfquantile not converged.\n");
 | 
| 
 | 
  2325 }
 | 
| 
 | 
  2326 /*
 | 
| 
 | 
  2327  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  2328  */
 | 
| 
 | 
  2329 #include "locf.h"
 | 
| 
 | 
  2330 
 | 
| 
 | 
  2331 extern double links_rs;
 | 
| 
 | 
  2332 
 | 
| 
 | 
  2333 int robust_vallink(link)
 | 
| 
 | 
  2334 int link;
 | 
| 
 | 
  2335 { return(link==LIDENT);
 | 
| 
 | 
  2336 }
 | 
| 
 | 
  2337 
 | 
| 
 | 
  2338 int robust_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  2339 double y, mean, th, *res, w;
 | 
| 
 | 
  2340 int link, cens;
 | 
| 
 | 
  2341 { double z, sw;
 | 
| 
 | 
  2342   if (link==LINIT)
 | 
| 
 | 
  2343   { res[ZDLL] = w*y;
 | 
| 
 | 
  2344     return(LF_OK);
 | 
| 
 | 
  2345   }
 | 
| 
 | 
  2346   sw = (w==1.0) ? 1.0 : sqrt(w); /* don't want unnecess. sqrt! */
 | 
| 
 | 
  2347   z = sw*(y-mean)/links_rs;
 | 
| 
 | 
  2348   res[ZLIK] = (fabs(z)<HUBERC) ? -z*z/2 : HUBERC*(HUBERC/2.0-fabs(z));
 | 
| 
 | 
  2349   if (z< -HUBERC)
 | 
| 
 | 
  2350   { res[ZDLL] = -sw*HUBERC/links_rs;
 | 
| 
 | 
  2351     res[ZDDLL]= 0.0;
 | 
| 
 | 
  2352     return(LF_OK);
 | 
| 
 | 
  2353   }
 | 
| 
 | 
  2354   if (z> HUBERC)
 | 
| 
 | 
  2355   { res[ZDLL] = sw*HUBERC/links_rs;
 | 
| 
 | 
  2356     res[ZDDLL]= 0.0;
 | 
| 
 | 
  2357     return(LF_OK);
 | 
| 
 | 
  2358   }
 | 
| 
 | 
  2359   res[ZDLL] =  sw*z/links_rs;
 | 
| 
 | 
  2360   res[ZDDLL] = w/(links_rs*links_rs);
 | 
| 
 | 
  2361   return(LF_OK);
 | 
| 
 | 
  2362 }
 | 
| 
 | 
  2363 
 | 
| 
 | 
  2364 int cauchy_fam(y,p,th,link,res,cens,w)
 | 
| 
 | 
  2365 double y, p, th, *res, w;
 | 
| 
 | 
  2366 int link, cens;
 | 
| 
 | 
  2367 { double z;
 | 
| 
 | 
  2368   if (link!=LIDENT)
 | 
| 
 | 
  2369   { LERR(("Invalid link in famcauc"));
 | 
| 
 | 
  2370     return(LF_LNK);
 | 
| 
 | 
  2371   }
 | 
| 
 | 
  2372   z = w*(y-th)/links_rs;
 | 
| 
 | 
  2373   res[ZLIK] = -log(1+z*z);
 | 
| 
 | 
  2374   res[ZDLL] = 2*w*z/(links_rs*(1+z*z));
 | 
| 
 | 
  2375   res[ZDDLL] = 2*w*w*(1-z*z)/(links_rs*links_rs*(1+z*z)*(1+z*z));
 | 
| 
 | 
  2376   return(LF_OK);
 | 
| 
 | 
  2377 }
 | 
| 
 | 
  2378 
 | 
| 
 | 
  2379 extern double lf_tol;
 | 
| 
 | 
  2380 int robust_init(lfd,des,sp)
 | 
| 
 | 
  2381 lfdata *lfd;
 | 
| 
 | 
  2382 design *des;
 | 
| 
 | 
  2383 smpar *sp;
 | 
| 
 | 
  2384 { int i;
 | 
| 
 | 
  2385   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2386   des->res[i] = resp(lfd,(int)des->ind[i]) - base(lfd,(int)des->ind[i]);
 | 
| 
 | 
  2387   des->cf[0] = median(des->res,des->n);
 | 
| 
 | 
  2388   for (i=1; i<des->p; i++) des->cf[i] = 0.0;
 | 
| 
 | 
  2389   lf_tol = 1.0e-6;
 | 
| 
 | 
  2390   return(LF_OK);
 | 
| 
 | 
  2391 }
 | 
| 
 | 
  2392 
 | 
| 
 | 
  2393 void setfrobust(fam)
 | 
| 
 | 
  2394 family *fam;
 | 
| 
 | 
  2395 { fam->deflink = LIDENT;
 | 
| 
 | 
  2396   fam->canlink = LIDENT;
 | 
| 
 | 
  2397   fam->vallink = robust_vallink;
 | 
| 
 | 
  2398   fam->family  = robust_fam;
 | 
| 
 | 
  2399   fam->initial = robust_init;
 | 
| 
 | 
  2400   fam->robust = 0;
 | 
| 
 | 
  2401 }
 | 
| 
 | 
  2402 
 | 
| 
 | 
  2403 void setfcauchy(fam)
 | 
| 
 | 
  2404 family *fam;
 | 
| 
 | 
  2405 { fam->deflink = LIDENT;
 | 
| 
 | 
  2406   fam->canlink = LIDENT;
 | 
| 
 | 
  2407   fam->vallink = robust_vallink;
 | 
| 
 | 
  2408   fam->family  = cauchy_fam;
 | 
| 
 | 
  2409   fam->initial = robust_init;
 | 
| 
 | 
  2410   fam->robust = 0;
 | 
| 
 | 
  2411 }
 | 
| 
 | 
  2412 /*
 | 
| 
 | 
  2413  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  2414  */
 | 
| 
 | 
  2415 #include "locf.h"
 | 
| 
 | 
  2416 
 | 
| 
 | 
  2417 int weibull_vallink(link)
 | 
| 
 | 
  2418 int link;
 | 
| 
 | 
  2419 { return((link==LIDENT) | (link==LLOG) | (link==LLOGIT));
 | 
| 
 | 
  2420 }
 | 
| 
 | 
  2421 
 | 
| 
 | 
  2422 int weibull_fam(y,mean,th,link,res,cens,w)
 | 
| 
 | 
  2423 double y, mean, th, *res, w;
 | 
| 
 | 
  2424 int link, cens;
 | 
| 
 | 
  2425 { double yy;
 | 
| 
 | 
  2426   yy = pow(y,w);
 | 
| 
 | 
  2427   if (link==LINIT)
 | 
| 
 | 
  2428   { res[ZDLL] = MAX(yy,0.0);
 | 
| 
 | 
  2429     return(LF_OK);
 | 
| 
 | 
  2430   }
 | 
| 
 | 
  2431   if (cens)
 | 
| 
 | 
  2432   { res[ZLIK] = -yy/mean;
 | 
| 
 | 
  2433     res[ZDLL] = res[ZDDLL] = yy/mean;
 | 
| 
 | 
  2434     return(LF_OK);
 | 
| 
 | 
  2435   }
 | 
| 
 | 
  2436   res[ZLIK] = 1-yy/mean-th;
 | 
| 
 | 
  2437   if (yy>0) res[ZLIK] += log(w*yy);
 | 
| 
 | 
  2438   res[ZDLL] = -1+yy/mean;
 | 
| 
 | 
  2439   res[ZDDLL]= yy/mean;
 | 
| 
 | 
  2440   return(LF_OK);
 | 
| 
 | 
  2441 }
 | 
| 
 | 
  2442 
 | 
| 
 | 
  2443 void setfweibull(fam)
 | 
| 
 | 
  2444 family *fam;
 | 
| 
 | 
  2445 { fam->deflink = LLOG;
 | 
| 
 | 
  2446   fam->canlink = LLOG;
 | 
| 
 | 
  2447   fam->vallink = weibull_vallink;
 | 
| 
 | 
  2448   fam->family  = weibull_fam;
 | 
| 
 | 
  2449   fam->robust = 0;
 | 
| 
 | 
  2450 }
 | 
| 
 | 
  2451 /*
 | 
| 
 | 
  2452  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  2453  */
 | 
| 
 | 
  2454 /*
 | 
| 
 | 
  2455   Functions implementing the adaptive bandwidth selection.
 | 
| 
 | 
  2456   Will make the final call to nbhd() to set smoothing weights
 | 
| 
 | 
  2457   for selected bandwidth, But will **not** make the
 | 
| 
 | 
  2458   final call to locfit().
 | 
| 
 | 
  2459 */
 | 
| 
 | 
  2460 
 | 
| 
 | 
  2461 #include "locf.h"
 | 
| 
 | 
  2462 
 | 
| 
 | 
  2463 static double hmin;
 | 
| 
 | 
  2464 
 | 
| 
 | 
  2465 #define NACRI 5
 | 
| 
 | 
  2466 static char *atype[NACRI] = { "none", "cp", "ici", "mindex", "ok" };
 | 
| 
 | 
  2467 static int   avals[NACRI] = { ANONE, ACP, AKAT, AMDI, AOK };
 | 
| 
 | 
  2468 int lfacri(char *z)
 | 
| 
 | 
  2469 { return(pmatch(z, atype, avals, NACRI, ANONE));
 | 
| 
 | 
  2470 }
 | 
| 
 | 
  2471 
 | 
| 
 | 
  2472 double adcri(lk,t0,t2,pen)
 | 
| 
 | 
  2473 double lk, t0, t2, pen;
 | 
| 
 | 
  2474 { double y;
 | 
| 
 | 
  2475 /* return(-2*lk/(t0*exp(pen*log(1-t2/t0)))); */
 | 
| 
 | 
  2476   /* return((-2*lk+pen*t2)/t0); */
 | 
| 
 | 
  2477   y = (MAX(-2*lk,t0-t2)+pen*t2)/t0;
 | 
| 
 | 
  2478   return(y);
 | 
| 
 | 
  2479 }
 | 
| 
 | 
  2480 
 | 
| 
 | 
  2481 double mmse(lfd,sp,dv,des)
 | 
| 
 | 
  2482 lfdata *lfd;
 | 
| 
 | 
  2483 smpar *sp;
 | 
| 
 | 
  2484 deriv *dv;
 | 
| 
 | 
  2485 design *des;
 | 
| 
 | 
  2486 { int i, ii, j, p, p1;
 | 
| 
 | 
  2487   double sv, sb, *l, dp;
 | 
| 
 | 
  2488 
 | 
| 
 | 
  2489   l = des->wd;
 | 
| 
 | 
  2490   wdiag(lfd, sp, des,l,dv,0,1,0);
 | 
| 
 | 
  2491   sv = sb = 0;
 | 
| 
 | 
  2492   p = npar(sp);
 | 
| 
 | 
  2493   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2494   { sv += l[i]*l[i];
 | 
| 
 | 
  2495     ii = des->ind[i];
 | 
| 
 | 
  2496     dp = dist(des,ii);
 | 
| 
 | 
  2497     for (j=0; j<deg(sp); j++) dp *= dist(des,ii);
 | 
| 
 | 
  2498     sb += fabs(l[i])*dp;
 | 
| 
 | 
  2499   }
 | 
| 
 | 
  2500   p1 = factorial(deg(sp)+1);
 | 
| 
 | 
  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));
 | 
| 
 | 
  2502   return(sv+sb*sb*pen(sp)*pen(sp)/(p1*p1));
 | 
| 
 | 
  2503 }
 | 
| 
 | 
  2504 
 | 
| 
 | 
  2505 static double mcp, clo, cup;
 | 
| 
 | 
  2506 
 | 
| 
 | 
  2507 /*
 | 
| 
 | 
  2508   Initial bandwidth will be (by default)
 | 
| 
 | 
  2509   k-nearest neighbors for k small, just large enough to
 | 
| 
 | 
  2510   get defined estimate (unless user provided nonzero nn or fix-h components)
 | 
| 
 | 
  2511 */
 | 
| 
 | 
  2512 
 | 
| 
 | 
  2513 int ainitband(lfd,sp,dv,des)
 | 
| 
 | 
  2514 lfdata *lfd;
 | 
| 
 | 
  2515 smpar *sp;
 | 
| 
 | 
  2516 deriv *dv;
 | 
| 
 | 
  2517 design *des;
 | 
| 
 | 
  2518 { int lf_status, p, z, cri, noit, redo;
 | 
| 
 | 
  2519   double ho, t[6];
 | 
| 
 | 
  2520 
 | 
| 
 | 
  2521   if (lf_debug >= 2) mut_printf("ainitband:\n");
 | 
| 
 | 
  2522   p = des->p;
 | 
| 
 | 
  2523   cri = acri(sp);
 | 
| 
 | 
  2524   noit = (cri!=AOK);
 | 
| 
 | 
  2525   z = (int)(lfd->n*nn(sp));
 | 
| 
 | 
  2526   if ((noit) && (z<p+2)) z = p+2;
 | 
| 
 | 
  2527   redo = 0; ho = -1;
 | 
| 
 | 
  2528   do
 | 
| 
 | 
  2529   { 
 | 
| 
 | 
  2530     nbhd(lfd,des,z,redo,sp);
 | 
| 
 | 
  2531     if (z<des->n) z = des->n;
 | 
| 
 | 
  2532     if (des->h>ho) lf_status = locfit(lfd,des,sp,noit,0,0);
 | 
| 
 | 
  2533     z++;
 | 
| 
 | 
  2534     redo = 1;
 | 
| 
 | 
  2535   } while ((z<=lfd->n) && ((des->h==0)||(lf_status!=LF_OK)));
 | 
| 
 | 
  2536   hmin = des->h;
 | 
| 
 | 
  2537 
 | 
| 
 | 
  2538   switch(cri)
 | 
| 
 | 
  2539   { case ACP:
 | 
| 
 | 
  2540       local_df(lfd,sp,des,t);
 | 
| 
 | 
  2541       mcp = adcri(des->llk,t[0],t[2],pen(sp));
 | 
| 
 | 
  2542       return(lf_status);
 | 
| 
 | 
  2543     case AKAT:
 | 
| 
 | 
  2544       local_df(lfd,sp,des,t);
 | 
| 
 | 
  2545       clo = des->cf[0]-pen(sp)*t[5];
 | 
| 
 | 
  2546       cup = des->cf[0]+pen(sp)*t[5];
 | 
| 
 | 
  2547       return(lf_status);
 | 
| 
 | 
  2548     case AMDI:
 | 
| 
 | 
  2549       mcp = mmse(lfd,sp,dv,des);
 | 
| 
 | 
  2550       return(lf_status);
 | 
| 
 | 
  2551     case AOK: return(lf_status);
 | 
| 
 | 
  2552   }
 | 
| 
 | 
  2553   LERR(("aband1: unknown criterion"));
 | 
| 
 | 
  2554   return(LF_ERR);
 | 
| 
 | 
  2555 }
 | 
| 
 | 
  2556 
 | 
| 
 | 
  2557 /*
 | 
| 
 | 
  2558   aband2 increases the initial bandwidth until lack of fit results,
 | 
| 
 | 
  2559   or the fit is close to a global fit. Increase h by 1+0.3/d at
 | 
| 
 | 
  2560   each iteration.
 | 
| 
 | 
  2561 */
 | 
| 
 | 
  2562 
 | 
| 
 | 
  2563 double aband2(lfd,sp,dv,des,h0)
 | 
| 
 | 
  2564 lfdata *lfd;
 | 
| 
 | 
  2565 smpar *sp;
 | 
| 
 | 
  2566 deriv *dv;
 | 
| 
 | 
  2567 design *des;
 | 
| 
 | 
  2568 double h0;
 | 
| 
 | 
  2569 { double t[6], h1, nu1, cp, ncp, tlo, tup;
 | 
| 
 | 
  2570   int d, inc, n, p, done;
 | 
| 
 | 
  2571 
 | 
| 
 | 
  2572   if (lf_debug >= 2) mut_printf("aband2:\n");
 | 
| 
 | 
  2573   d = lfd->d; n = lfd->n; p = npar(sp);
 | 
| 
 | 
  2574   h1 = des->h = h0;
 | 
| 
 | 
  2575   done = 0; nu1 = 0.0;
 | 
| 
 | 
  2576   inc = 0; ncp = 0.0;
 | 
| 
 | 
  2577   while ((!done) & (nu1<(n-p)*0.95))
 | 
| 
 | 
  2578   { fixh(sp) = (1+0.3/d)*des->h;
 | 
| 
 | 
  2579     nbhd(lfd,des,0,1,sp);
 | 
| 
 | 
  2580     if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband2: failed fit"));
 | 
| 
 | 
  2581     local_df(lfd,sp,des,t);
 | 
| 
 | 
  2582     nu1 = t[0]-t[2]; /* tr(A) */
 | 
| 
 | 
  2583     switch(acri(sp))
 | 
| 
 | 
  2584     { case AKAT:
 | 
| 
 | 
  2585         tlo = des->cf[0]-pen(sp)*t[5];
 | 
| 
 | 
  2586         tup = des->cf[0]+pen(sp)*t[5];
 | 
| 
 | 
  2587 /* mut_printf("h %8.5f  tlo %8.5f  tup %8.5f\n",des->h,tlo,tup); */
 | 
| 
 | 
  2588         done = ((tlo>cup) | (tup<clo));
 | 
| 
 | 
  2589         if (!done)
 | 
| 
 | 
  2590         { clo = MAX(clo,tlo);
 | 
| 
 | 
  2591           cup = MIN(cup,tup);
 | 
| 
 | 
  2592           h1 = des->h;
 | 
| 
 | 
  2593         }
 | 
| 
 | 
  2594         break;
 | 
| 
 | 
  2595       case ACP:
 | 
| 
 | 
  2596         cp = adcri(des->llk,t[0],t[2],pen(sp));
 | 
| 
 | 
  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); */
 | 
| 
 | 
  2598         if (cp<mcp) { mcp = cp; h1 = des->h; }
 | 
| 
 | 
  2599         if (cp>=ncp) inc++; else inc = 0;
 | 
| 
 | 
  2600         ncp = cp;
 | 
| 
 | 
  2601         done = (inc>=10) | ((inc>=3) & ((t[0]-t[2])>=10) & (cp>1.5*mcp));
 | 
| 
 | 
  2602         break;
 | 
| 
 | 
  2603       case AMDI:
 | 
| 
 | 
  2604         cp = mmse(lfd,sp,dv,des);
 | 
| 
 | 
  2605         if (cp<mcp) { mcp = cp; h1 = des->h; }
 | 
| 
 | 
  2606         if (cp>ncp) inc++; else inc = 0;
 | 
| 
 | 
  2607         ncp = cp;
 | 
| 
 | 
  2608         done = (inc>=3);
 | 
| 
 | 
  2609         break;
 | 
| 
 | 
  2610     }
 | 
| 
 | 
  2611   }
 | 
| 
 | 
  2612   return(h1);
 | 
| 
 | 
  2613 }
 | 
| 
 | 
  2614 
 | 
| 
 | 
  2615 /*
 | 
| 
 | 
  2616   aband3 does a finer search around best h so far. Try
 | 
| 
 | 
  2617   h*(1-0.2/d), h/(1-0.1/d), h*(1+0.1/d), h*(1+0.2/d)
 | 
| 
 | 
  2618 */
 | 
| 
 | 
  2619 double aband3(lfd,sp,dv,des,h0)
 | 
| 
 | 
  2620 lfdata *lfd;
 | 
| 
 | 
  2621 smpar *sp;
 | 
| 
 | 
  2622 deriv *dv;
 | 
| 
 | 
  2623 design *des;
 | 
| 
 | 
  2624 double h0;
 | 
| 
 | 
  2625 { double t[6], h1, cp, tlo, tup;
 | 
| 
 | 
  2626   int i, i0, d, n;
 | 
| 
 | 
  2627 
 | 
| 
 | 
  2628   if (lf_debug >= 2) mut_printf("aband3:\n");
 | 
| 
 | 
  2629   d = lfd->d; n = lfd->n;
 | 
| 
 | 
  2630   h1 = h0;
 | 
| 
 | 
  2631   i0 = (acri(sp)==AKAT) ? 1 : -2;
 | 
| 
 | 
  2632   if (h0==hmin) i0 = 1;
 | 
| 
 | 
  2633 
 | 
| 
 | 
  2634   for (i=i0; i<=2; i++)
 | 
| 
 | 
  2635   { if (i==0) i++;
 | 
| 
 | 
  2636     fixh(sp) = h0*(1+0.1*i/d);
 | 
| 
 | 
  2637     nbhd(lfd,des,0,1,sp);
 | 
| 
 | 
  2638     if (locfit(lfd,des,sp,1,0,0) > 0) WARN(("aband3: failed fit"));
 | 
| 
 | 
  2639     local_df(lfd,sp,des,t);
 | 
| 
 | 
  2640     switch (acri(sp))
 | 
| 
 | 
  2641     { case AKAT:
 | 
| 
 | 
  2642         tlo = des->cf[0]-pen(sp)*t[5];
 | 
| 
 | 
  2643         tup = des->cf[0]+pen(sp)*t[5];
 | 
| 
 | 
  2644         if ((tlo>cup) | (tup<clo)) /* done */
 | 
| 
 | 
  2645           i = 2;
 | 
| 
 | 
  2646         else
 | 
| 
 | 
  2647         { h1 = des->h;
 | 
| 
 | 
  2648           clo = MAX(clo,tlo);
 | 
| 
 | 
  2649           cup = MIN(cup,tup);
 | 
| 
 | 
  2650         }
 | 
| 
 | 
  2651         break;
 | 
| 
 | 
  2652       case ACP:
 | 
| 
 | 
  2653         cp = adcri(des->llk,t[0],t[2],pen(sp));
 | 
| 
 | 
  2654         if (cp<mcp) { mcp = cp; h1 = des->h; }
 | 
| 
 | 
  2655         else
 | 
| 
 | 
  2656         { if (i>0) i = 2; }
 | 
| 
 | 
  2657         break;
 | 
| 
 | 
  2658       case AMDI:
 | 
| 
 | 
  2659         cp = mmse(lfd,sp,dv,des);
 | 
| 
 | 
  2660         if (cp<mcp) { mcp = cp; h1 = des->h; }
 | 
| 
 | 
  2661         else
 | 
| 
 | 
  2662         { if (i>0) i = 2; }
 | 
| 
 | 
  2663     }
 | 
| 
 | 
  2664   }
 | 
| 
 | 
  2665   return(h1);
 | 
| 
 | 
  2666 }
 | 
| 
 | 
  2667 
 | 
| 
 | 
  2668 int alocfit(lfd,sp,dv,des,cv)
 | 
| 
 | 
  2669 lfdata *lfd;
 | 
| 
 | 
  2670 smpar *sp;
 | 
| 
 | 
  2671 deriv *dv;
 | 
| 
 | 
  2672 design *des;
 | 
| 
 | 
  2673 int cv;
 | 
| 
 | 
  2674 { int lf_status;
 | 
| 
 | 
  2675   double h0;
 | 
| 
 | 
  2676 
 | 
| 
 | 
  2677   lf_status = ainitband(lfd,sp,dv,des);
 | 
| 
 | 
  2678   if (lf_error) return(lf_status);
 | 
| 
 | 
  2679   if (acri(sp) == AOK) return(lf_status);
 | 
| 
 | 
  2680 
 | 
| 
 | 
  2681   h0 = fixh(sp);
 | 
| 
 | 
  2682   fixh(sp) = aband2(lfd,sp,dv,des,des->h);
 | 
| 
 | 
  2683   fixh(sp) = aband3(lfd,sp,dv,des,fixh(sp));
 | 
| 
 | 
  2684   nbhd(lfd,des,0,1,sp);
 | 
| 
 | 
  2685   lf_status = locfit(lfd,des,sp,0,0,cv);
 | 
| 
 | 
  2686   fixh(sp) = h0;
 | 
| 
 | 
  2687 
 | 
| 
 | 
  2688   return(lf_status);
 | 
| 
 | 
  2689 }
 | 
| 
 | 
  2690 /*
 | 
| 
 | 
  2691  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  2692  */
 | 
| 
 | 
  2693 /*
 | 
| 
 | 
  2694  *
 | 
| 
 | 
  2695  *   Evaluate the locfit fitting functions.
 | 
| 
 | 
  2696  *     calcp(sp,d)
 | 
| 
 | 
  2697  *       calculates the number of fitting functions.
 | 
| 
 | 
  2698  *     makecfn(sp,des,dv,d)
 | 
| 
 | 
  2699  *       makes the coef.number vector.
 | 
| 
 | 
  2700  *     fitfun(lfd, sp, x,t,f,dv)
 | 
| 
 | 
  2701  *       lfd is the local fit structure.
 | 
| 
 | 
  2702  *       sp  smoothing parameter structure.
 | 
| 
 | 
  2703  *       x is the data point.
 | 
| 
 | 
  2704  *       t is the fitting point.
 | 
| 
 | 
  2705  *       f is a vector to return the results.
 | 
| 
 | 
  2706  *       dv derivative structure.
 | 
| 
 | 
  2707  *     designmatrix(lfd, sp, des)
 | 
| 
 | 
  2708  *       is a wrapper for fitfun to build the design matrix.
 | 
| 
 | 
  2709  *
 | 
| 
 | 
  2710  */
 | 
| 
 | 
  2711 
 | 
| 
 | 
  2712 #include "locf.h"
 | 
| 
 | 
  2713 
 | 
| 
 | 
  2714 int calcp(sp,d)
 | 
| 
 | 
  2715 smpar *sp;
 | 
| 
 | 
  2716 int d;
 | 
| 
 | 
  2717 { int i, k;
 | 
| 
 | 
  2718 
 | 
| 
 | 
  2719   if (ubas(sp)) return(npar(sp));
 | 
| 
 | 
  2720 
 | 
| 
 | 
  2721   switch (kt(sp))
 | 
| 
 | 
  2722   { case KSPH:
 | 
| 
 | 
  2723     case KCE:
 | 
| 
 | 
  2724       k = 1;
 | 
| 
 | 
  2725       for (i=1; i<=deg(sp); i++) k = k*(d+i)/i;
 | 
| 
 | 
  2726       return(k);
 | 
| 
 | 
  2727     case KPROD: return(d*deg(sp)+1);
 | 
| 
 | 
  2728     case KLM: return(d);
 | 
| 
 | 
  2729     case KZEON: return(1);
 | 
| 
 | 
  2730   }
 | 
| 
 | 
  2731   LERR(("calcp: invalid kt %d",kt(sp)));
 | 
| 
 | 
  2732   return(0);
 | 
| 
 | 
  2733 }
 | 
| 
 | 
  2734 
 | 
| 
 | 
  2735 int coefnumber(dv,kt,d,deg)
 | 
| 
 | 
  2736 int kt, d, deg;
 | 
| 
 | 
  2737 deriv *dv;
 | 
| 
 | 
  2738 { int d0, d1, t;
 | 
| 
 | 
  2739 
 | 
| 
 | 
  2740   if (d==1)
 | 
| 
 | 
  2741   { if (dv->nd<=deg) return(dv->nd);
 | 
| 
 | 
  2742     return(-1);
 | 
| 
 | 
  2743   }
 | 
| 
 | 
  2744 
 | 
| 
 | 
  2745   if (dv->nd==0) return(0);
 | 
| 
 | 
  2746   if (deg==0) return(-1);
 | 
| 
 | 
  2747   if (dv->nd==1) return(1+dv->deriv[0]);
 | 
| 
 | 
  2748   if (deg==1) return(-1);
 | 
| 
 | 
  2749   if (kt==KPROD) return(-1);
 | 
| 
 | 
  2750 
 | 
| 
 | 
  2751   if (dv->nd==2)
 | 
| 
 | 
  2752   { d0 = dv->deriv[0]; d1 = dv->deriv[1];
 | 
| 
 | 
  2753     if (d0<d1) { t = d0; d0 = d1; d1 = t; }
 | 
| 
 | 
  2754     return((d+1)*(d0+1)-d0*(d0+3)/2+d1);
 | 
| 
 | 
  2755   }
 | 
| 
 | 
  2756   if (deg==2) return(-1);
 | 
| 
 | 
  2757 
 | 
| 
 | 
  2758   LERR(("coefnumber not programmed for nd>=3"));
 | 
| 
 | 
  2759   return(-1);
 | 
| 
 | 
  2760 }
 | 
| 
 | 
  2761 
 | 
| 
 | 
  2762 void makecfn(sp,des,dv,d)
 | 
| 
 | 
  2763 smpar *sp;
 | 
| 
 | 
  2764 design *des;
 | 
| 
 | 
  2765 deriv *dv;
 | 
| 
 | 
  2766 int d;
 | 
| 
 | 
  2767 { int i, nd;
 | 
| 
 | 
  2768   
 | 
| 
 | 
  2769   nd = dv->nd;
 | 
| 
 | 
  2770 
 | 
| 
 | 
  2771   des->cfn[0] = coefnumber(dv,kt(sp),d,deg(sp));
 | 
| 
 | 
  2772   des->ncoef = 1;
 | 
| 
 | 
  2773   if (nd >= deg(sp)) return;
 | 
| 
 | 
  2774   if (kt(sp)==KZEON) return;
 | 
| 
 | 
  2775 
 | 
| 
 | 
  2776   if (d>1)
 | 
| 
 | 
  2777   { if (nd>=2) return;
 | 
| 
 | 
  2778     if ((nd>=1) && (kt(sp)==KPROD)) return;
 | 
| 
 | 
  2779   }
 | 
| 
 | 
  2780 
 | 
| 
 | 
  2781   dv->nd = nd+1;
 | 
| 
 | 
  2782   for (i=0; i<d; i++)
 | 
| 
 | 
  2783   { dv->deriv[nd] = i;
 | 
| 
 | 
  2784     des->cfn[i+1] = coefnumber(dv,kt(sp),d,deg(sp));
 | 
| 
 | 
  2785   }
 | 
| 
 | 
  2786   dv->nd = nd;
 | 
| 
 | 
  2787 
 | 
| 
 | 
  2788   des->ncoef = 1+d;
 | 
| 
 | 
  2789 }
 | 
| 
 | 
  2790 
 | 
| 
 | 
  2791 void fitfunangl(dx,ff,sca,cd,deg)
 | 
| 
 | 
  2792 double dx, *ff, sca;
 | 
| 
 | 
  2793 int deg, cd;
 | 
| 
 | 
  2794 {
 | 
| 
 | 
  2795   if (deg>=3) WARN(("Can't handle angular model with deg>=3"));
 | 
| 
 | 
  2796 
 | 
| 
 | 
  2797   switch(cd)
 | 
| 
 | 
  2798   { case 0:
 | 
| 
 | 
  2799       ff[0] = 1;
 | 
| 
 | 
  2800       ff[1] = sin(dx/sca)*sca;
 | 
| 
 | 
  2801       ff[2] = (1-cos(dx/sca))*sca*sca;
 | 
| 
 | 
  2802       return;
 | 
| 
 | 
  2803     case 1:
 | 
| 
 | 
  2804       ff[0] = 0;
 | 
| 
 | 
  2805       ff[1] = cos(dx/sca);
 | 
| 
 | 
  2806       ff[2] = sin(dx/sca)*sca;
 | 
| 
 | 
  2807       return;
 | 
| 
 | 
  2808     case 2:
 | 
| 
 | 
  2809       ff[0] = 0;
 | 
| 
 | 
  2810       ff[1] = -sin(dx/sca)/sca;
 | 
| 
 | 
  2811       ff[2] = cos(dx/sca);
 | 
| 
 | 
  2812       return;
 | 
| 
 | 
  2813     default: WARN(("Can't handle angular model with >2 derivs"));
 | 
| 
 | 
  2814   }
 | 
| 
 | 
  2815 }
 | 
| 
 | 
  2816 
 | 
| 
 | 
  2817 void fitfun(lfd,sp,x,t,f,dv)
 | 
| 
 | 
  2818 lfdata *lfd;
 | 
| 
 | 
  2819 smpar *sp;
 | 
| 
 | 
  2820 double *x, *t, *f;
 | 
| 
 | 
  2821 deriv *dv;
 | 
| 
 | 
  2822 { int d, deg, nd, m, i, j, k, ct_deriv[MXDIM];
 | 
| 
 | 
  2823   double ff[MXDIM][1+MXDEG], dx[MXDIM], *xx[MXDIM];
 | 
| 
 | 
  2824 
 | 
| 
 | 
  2825   if (ubas(sp))
 | 
| 
 | 
  2826   { for (i=0; i<lfd->d; i++) xx[i] = &x[i];
 | 
| 
 | 
  2827     i = 0;
 | 
| 
 | 
  2828     sp->vbasis(xx,t,1,lfd->d,1,npar(sp),f);
 | 
| 
 | 
  2829     return;
 | 
| 
 | 
  2830   }
 | 
| 
 | 
  2831 
 | 
| 
 | 
  2832   d = lfd->d;
 | 
| 
 | 
  2833   deg = deg(sp);
 | 
| 
 | 
  2834   m = 0;
 | 
| 
 | 
  2835   nd = (dv==NULL) ? 0 : dv->nd;
 | 
| 
 | 
  2836 
 | 
| 
 | 
  2837   if (kt(sp)==KZEON)
 | 
| 
 | 
  2838   { f[0] = 1.0;
 | 
| 
 | 
  2839     return;
 | 
| 
 | 
  2840   }
 | 
| 
 | 
  2841 
 | 
| 
 | 
  2842   if (kt(sp)==KLM)
 | 
| 
 | 
  2843   { for (i=0; i<d; i++) f[m++] = x[i];
 | 
| 
 | 
  2844     return;
 | 
| 
 | 
  2845   }
 | 
| 
 | 
  2846 
 | 
| 
 | 
  2847   f[m++] = (nd==0);
 | 
| 
 | 
  2848   if (deg==0) return;
 | 
| 
 | 
  2849 
 | 
| 
 | 
  2850   for (i=0; i<d; i++)
 | 
| 
 | 
  2851   { ct_deriv[i] = 0;
 | 
| 
 | 
  2852     dx[i] = (t==NULL) ? x[i] : x[i]-t[i];
 | 
| 
 | 
  2853   }
 | 
| 
 | 
  2854   for (i=0; i<nd; i++) ct_deriv[dv->deriv[i]]++;
 | 
| 
 | 
  2855 
 | 
| 
 | 
  2856   for (i=0; i<d; i++)
 | 
| 
 | 
  2857   { switch(lfd->sty[i])
 | 
| 
 | 
  2858     {
 | 
| 
 | 
  2859       case STANGL:
 | 
| 
 | 
  2860         fitfunangl(dx[i],ff[i],lfd->sca[i],ct_deriv[i],deg(sp));
 | 
| 
 | 
  2861         break;
 | 
| 
 | 
  2862       default:
 | 
| 
 | 
  2863         for (j=0; j<ct_deriv[i]; j++) ff[i][j] = 0.0;
 | 
| 
 | 
  2864         ff[i][ct_deriv[i]] = 1.0;
 | 
| 
 | 
  2865         for (j=ct_deriv[i]+1; j<=deg; j++)
 | 
| 
 | 
  2866           ff[i][j] = ff[i][j-1]*dx[i]/(j-ct_deriv[i]);
 | 
| 
 | 
  2867     }
 | 
| 
 | 
  2868   }
 | 
| 
 | 
  2869 
 | 
| 
 | 
  2870 /*
 | 
| 
 | 
  2871  *  Product kernels. Note that if ct_deriv[i] != nd, that implies
 | 
| 
 | 
  2872  *  there is differentiation wrt another variable, and all components
 | 
| 
 | 
  2873  *  involving x[i] are 0.
 | 
| 
 | 
  2874  */
 | 
| 
 | 
  2875   if ((d==1) || (kt(sp)==KPROD))
 | 
| 
 | 
  2876   { for (j=1; j<=deg; j++)
 | 
| 
 | 
  2877       for (i=0; i<d; i++)
 | 
| 
 | 
  2878         f[m++] = (ct_deriv[i]==nd) ? ff[i][j] : 0.0;
 | 
| 
 | 
  2879     return;
 | 
| 
 | 
  2880   }
 | 
| 
 | 
  2881 
 | 
| 
 | 
  2882 /*
 | 
| 
 | 
  2883  *  Spherical kernels with the full polynomial basis.
 | 
| 
 | 
  2884  *  Presently implemented up to deg=3.
 | 
| 
 | 
  2885  */
 | 
| 
 | 
  2886   for (i=0; i<d; i++)
 | 
| 
 | 
  2887     f[m++] = (ct_deriv[i]==nd) ? ff[i][1] : 0.0;
 | 
| 
 | 
  2888   if (deg==1) return;
 | 
| 
 | 
  2889 
 | 
| 
 | 
  2890   for (i=0; i<d; i++)
 | 
| 
 | 
  2891   {
 | 
| 
 | 
  2892     /* xi^2/2 terms. */
 | 
| 
 | 
  2893     f[m++] = (ct_deriv[i]==nd) ? ff[i][2] : 0.0;
 | 
| 
 | 
  2894 
 | 
| 
 | 
  2895     /* xi xj terms */
 | 
| 
 | 
  2896     for (j=i+1; j<d; j++)
 | 
| 
 | 
  2897       f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][1] : 0.0;
 | 
| 
 | 
  2898   }
 | 
| 
 | 
  2899   if (deg==2) return;
 | 
| 
 | 
  2900 
 | 
| 
 | 
  2901   for (i=0; i<d; i++)
 | 
| 
 | 
  2902   { 
 | 
| 
 | 
  2903     /* xi^3/6 terms */
 | 
| 
 | 
  2904     f[m++] = (ct_deriv[i]==nd) ? ff[i][3] : 0.0;
 | 
| 
 | 
  2905 
 | 
| 
 | 
  2906     /* xi^2/2 xk terms */
 | 
| 
 | 
  2907     for (k=i+1; k<d; k++)
 | 
| 
 | 
  2908       f[m++] = (ct_deriv[i]+ct_deriv[k]==nd) ? ff[i][2]*ff[k][1] : 0.0;
 | 
| 
 | 
  2909 
 | 
| 
 | 
  2910     /* xi xj xk terms */
 | 
| 
 | 
  2911     for (j=i+1; j<d; j++)
 | 
| 
 | 
  2912     { f[m++] = (ct_deriv[i]+ct_deriv[j]==nd) ? ff[i][1]*ff[j][2] : 0.0;
 | 
| 
 | 
  2913       for (k=j+1; k<d; k++)
 | 
| 
 | 
  2914         f[m++] = (ct_deriv[i]+ct_deriv[j]+ct_deriv[k]==nd) ?
 | 
| 
 | 
  2915                     ff[i][1]*ff[j][1]*ff[k][1] : 0.0;
 | 
| 
 | 
  2916     }
 | 
| 
 | 
  2917   }
 | 
| 
 | 
  2918   if (deg==3) return;
 | 
| 
 | 
  2919 
 | 
| 
 | 
  2920   LERR(("fitfun: can't handle deg=%d for spherical kernels",deg));
 | 
| 
 | 
  2921 }
 | 
| 
 | 
  2922 
 | 
| 
 | 
  2923 /*
 | 
| 
 | 
  2924  *  Build the design matrix. Assumes des->ind contains the indices of
 | 
| 
 | 
  2925  *  the required data points; des->n the number of points; des->xev
 | 
| 
 | 
  2926  *  the fitting point.
 | 
| 
 | 
  2927  */
 | 
| 
 | 
  2928 void designmatrix(lfd,sp,des)
 | 
| 
 | 
  2929 lfdata *lfd;
 | 
| 
 | 
  2930 smpar *sp;
 | 
| 
 | 
  2931 design *des;
 | 
| 
 | 
  2932 { int i, ii, j, p;
 | 
| 
 | 
  2933   double *X, u[MXDIM];
 | 
| 
 | 
  2934 
 | 
| 
 | 
  2935   X = d_x(des);
 | 
| 
 | 
  2936   p = des->p;
 | 
| 
 | 
  2937 
 | 
| 
 | 
  2938   if (ubas(sp))
 | 
| 
 | 
  2939   {
 | 
| 
 | 
  2940     sp->vbasis(lfd->x,des->xev,lfd->n,lfd->d,des->n,p,X);
 | 
| 
 | 
  2941     return;
 | 
| 
 | 
  2942   }
 | 
| 
 | 
  2943 
 | 
| 
 | 
  2944   for (i=0; i<des->n; i++)
 | 
| 
 | 
  2945   { ii = des->ind[i];
 | 
| 
 | 
  2946     for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,ii);
 | 
| 
 | 
  2947     fitfun(lfd,sp,u,des->xev,&X[ii*p],NULL);
 | 
| 
 | 
  2948   }
 | 
| 
 | 
  2949 }
 | 
| 
 | 
  2950 /*
 | 
| 
 | 
  2951  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  2952  */
 | 
| 
 | 
  2953 /*
 | 
| 
 | 
  2954  *
 | 
| 
 | 
  2955  *
 | 
| 
 | 
  2956  *  Functions for determining bandwidth; smoothing neighborhood
 | 
| 
 | 
  2957  *  and smoothing weights.
 | 
| 
 | 
  2958  */
 | 
| 
 | 
  2959 
 | 
| 
 | 
  2960 #include "locf.h"
 | 
| 
 | 
  2961 
 | 
| 
 | 
  2962 double rho(x,sc,d,kt,sty) /* ||x|| for appropriate distance metric */
 | 
| 
 | 
  2963 double *x, *sc;
 | 
| 
 | 
  2964 int d, kt, *sty;
 | 
| 
 | 
  2965 { double rhoi[MXDIM], s;
 | 
| 
 | 
  2966   int i;
 | 
| 
 | 
  2967   for (i=0; i<d; i++)
 | 
| 
 | 
  2968   { if (sty!=NULL)
 | 
| 
 | 
  2969     { switch(sty[i])
 | 
| 
 | 
  2970       { case STANGL:  rhoi[i] = 2*sin(x[i]/(2*sc[i])); break;
 | 
| 
 | 
  2971         case STCPAR: rhoi[i] = 0; break;
 | 
| 
 | 
  2972         default: rhoi[i] = x[i]/sc[i];
 | 
| 
 | 
  2973     } }
 | 
| 
 | 
  2974     else rhoi[i] = x[i]/sc[i];
 | 
| 
 | 
  2975   }
 | 
| 
 | 
  2976 
 | 
| 
 | 
  2977   if (d==1) return(fabs(rhoi[0]));
 | 
| 
 | 
  2978 
 | 
| 
 | 
  2979   s = 0;
 | 
| 
 | 
  2980   if (kt==KPROD)
 | 
| 
 | 
  2981   { for (i=0; i<d; i++)
 | 
| 
 | 
  2982     { rhoi[i] = fabs(rhoi[i]);
 | 
| 
 | 
  2983       if (rhoi[i]>s) s = rhoi[i];
 | 
| 
 | 
  2984     }
 | 
| 
 | 
  2985     return(s);
 | 
| 
 | 
  2986   }
 | 
| 
 | 
  2987 
 | 
| 
 | 
  2988   if (kt==KSPH)
 | 
| 
 | 
  2989   { for (i=0; i<d; i++)
 | 
| 
 | 
  2990       s += rhoi[i]*rhoi[i];
 | 
| 
 | 
  2991     return(sqrt(s));
 | 
| 
 | 
  2992   }
 | 
| 
 | 
  2993 
 | 
| 
 | 
  2994   LERR(("rho: invalid kt"));
 | 
| 
 | 
  2995   return(0.0);
 | 
| 
 | 
  2996 }
 | 
| 
 | 
  2997 
 | 
| 
 | 
  2998 double kordstat(x,k,n,ind)
 | 
| 
 | 
  2999 double *x;
 | 
| 
 | 
  3000 int k, n, *ind;
 | 
| 
 | 
  3001 { int i, i0, i1, l, r;
 | 
| 
 | 
  3002   double piv;
 | 
| 
 | 
  3003   if (k<1) return(0.0);
 | 
| 
 | 
  3004   i0 = 0; i1 = n-1;
 | 
| 
 | 
  3005   while (1)
 | 
| 
 | 
  3006   { piv = x[ind[(i0+i1)/2]];
 | 
| 
 | 
  3007     l = i0; r = i1;
 | 
| 
 | 
  3008     while (l<=r)
 | 
| 
 | 
  3009     { while ((l<=i1) && (x[ind[l]]<=piv)) l++;
 | 
| 
 | 
  3010       while ((r>=i0) && (x[ind[r]]>piv)) r--;
 | 
| 
 | 
  3011       if (l<=r) ISWAP(ind[l],ind[r]);
 | 
| 
 | 
  3012     } /* now, x[ind[i0..r]] <= piv < x[ind[l..i1]] */
 | 
| 
 | 
  3013     if (r<k-1) i0 = l;  /* go right */
 | 
| 
 | 
  3014     else /* put pivots in middle */
 | 
| 
 | 
  3015     { for (i=i0; i<=r; )
 | 
| 
 | 
  3016         if (x[ind[i]]==piv) { ISWAP(ind[i],ind[r]); r--; }
 | 
| 
 | 
  3017         else i++;
 | 
| 
 | 
  3018       if (r<k-1) return(piv);
 | 
| 
 | 
  3019       i1 = r;
 | 
| 
 | 
  3020     }
 | 
| 
 | 
  3021   }
 | 
| 
 | 
  3022 }
 | 
| 
 | 
  3023 
 | 
| 
 | 
  3024 /* check if i'th data point is in limits */
 | 
| 
 | 
  3025 int inlim(lfd,i)
 | 
| 
 | 
  3026 lfdata *lfd;
 | 
| 
 | 
  3027 int i;
 | 
| 
 | 
  3028 { int d, j, k;
 | 
| 
 | 
  3029   double *xlim;
 | 
| 
 | 
  3030 
 | 
| 
 | 
  3031   xlim = lfd->xl;
 | 
| 
 | 
  3032   d = lfd->d;
 | 
| 
 | 
  3033   k = 1;
 | 
| 
 | 
  3034   for (j=0; j<d; j++)
 | 
| 
 | 
  3035   { if (xlim[j]<xlim[j+d])
 | 
| 
 | 
  3036       k &= ((datum(lfd,j,i)>=xlim[j]) & (datum(lfd,j,i)<=xlim[j+d]));
 | 
| 
 | 
  3037   }
 | 
| 
 | 
  3038   return(k);
 | 
| 
 | 
  3039 }
 | 
| 
 | 
  3040 
 | 
| 
 | 
  3041 double compbandwid(di,ind,x,n,d,nn,fxh)
 | 
| 
 | 
  3042 double *di, *x, fxh;
 | 
| 
 | 
  3043 int n, d, nn, *ind;
 | 
| 
 | 
  3044 { int i;
 | 
| 
 | 
  3045   double nnh;
 | 
| 
 | 
  3046 
 | 
| 
 | 
  3047   if (nn==0) return(fxh);
 | 
| 
 | 
  3048 
 | 
| 
 | 
  3049   if (nn<n)
 | 
| 
 | 
  3050     nnh = kordstat(di,nn,n,ind);
 | 
| 
 | 
  3051   else
 | 
| 
 | 
  3052   { nnh = 0;
 | 
| 
 | 
  3053     for (i=0; i<n; i++) nnh = MAX(nnh,di[i]);
 | 
| 
 | 
  3054     nnh = nnh*exp(log(1.0*nn/n)/d);
 | 
| 
 | 
  3055   }
 | 
| 
 | 
  3056   return(MAX(fxh,nnh));
 | 
| 
 | 
  3057 }
 | 
| 
 | 
  3058 
 | 
| 
 | 
  3059 /*
 | 
| 
 | 
  3060   fast version of nbhd for ordered 1-d data
 | 
| 
 | 
  3061 */
 | 
| 
 | 
  3062 void nbhd1(lfd,sp,des,k)
 | 
| 
 | 
  3063 lfdata *lfd;
 | 
| 
 | 
  3064 smpar *sp;
 | 
| 
 | 
  3065 design *des;
 | 
| 
 | 
  3066 int k;
 | 
| 
 | 
  3067 { double x, h, *xd, sc;
 | 
| 
 | 
  3068   int i, l, r, m, n, z;
 | 
| 
 | 
  3069 
 | 
| 
 | 
  3070   n = lfd->n;
 | 
| 
 | 
  3071   x = des->xev[0];
 | 
| 
 | 
  3072   xd = dvari(lfd,0);
 | 
| 
 | 
  3073   sc = lfd->sca[0];
 | 
| 
 | 
  3074 
 | 
| 
 | 
  3075   /* find closest data point to x */
 | 
| 
 | 
  3076   if (x<=xd[0]) z = 0;
 | 
| 
 | 
  3077   else
 | 
| 
 | 
  3078   if (x>=xd[n-1]) z = n-1;
 | 
| 
 | 
  3079   else
 | 
| 
 | 
  3080   { l = 0; r = n-1;
 | 
| 
 | 
  3081     while (r-l>1)
 | 
| 
 | 
  3082     { z = (r+l)/2;
 | 
| 
 | 
  3083       if (xd[z]>x) r = z;
 | 
| 
 | 
  3084               else l = z;
 | 
| 
 | 
  3085     }
 | 
| 
 | 
  3086     /* now, xd[0..l] <= x < x[r..n-1] */
 | 
| 
 | 
  3087     if ((x-xd[l])>(xd[r]-x)) z = r; else z = l;
 | 
| 
 | 
  3088   }
 | 
| 
 | 
  3089   /* closest point to x is xd[z] */
 | 
| 
 | 
  3090 
 | 
| 
 | 
  3091   if (nn(sp)<0)  /* user bandwidth */
 | 
| 
 | 
  3092     h = sp->vb(des->xev);
 | 
| 
 | 
  3093   else
 | 
| 
 | 
  3094   { if (k>0) /* set h to nearest neighbor bandwidth */
 | 
| 
 | 
  3095     { l = r = z;
 | 
| 
 | 
  3096       if (l==0) r = k-1;
 | 
| 
 | 
  3097       if (r==n-1) l = n-k;
 | 
| 
 | 
  3098       while (r-l<k-1)
 | 
| 
 | 
  3099       { if ((x-xd[l-1])<(xd[r+1]-x)) l--; else r++;
 | 
| 
 | 
  3100         if (l==0) r = k-1;
 | 
| 
 | 
  3101         if (r==n-1) l = n-k;
 | 
| 
 | 
  3102       }
 | 
| 
 | 
  3103       h = x-xd[l];
 | 
| 
 | 
  3104       if (h<xd[r]-x) h = xd[r]-x;
 | 
| 
 | 
  3105     }
 | 
| 
 | 
  3106     else h = 0;
 | 
| 
 | 
  3107     h /= sc;
 | 
| 
 | 
  3108     if (h<fixh(sp)) h = fixh(sp);
 | 
| 
 | 
  3109   }
 | 
| 
 | 
  3110 
 | 
| 
 | 
  3111   m = 0;
 | 
| 
 | 
  3112   if (xd[z]>x) z--; /* so xd[z]<=x */
 | 
| 
 | 
  3113   /* look left */
 | 
| 
 | 
  3114   for (i=z; i>=0; i--) if (inlim(lfd,i))
 | 
| 
 | 
  3115   { dist(des,i) = (x-xd[i])/sc;
 | 
| 
 | 
  3116     wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
 | 
| 
 | 
  3117     if (wght(des,i)>0)
 | 
| 
 | 
  3118     { des->ind[m] = i;
 | 
| 
 | 
  3119       m++; 
 | 
| 
 | 
  3120     } else i = 0;
 | 
| 
 | 
  3121   }
 | 
| 
 | 
  3122   /* look right */
 | 
| 
 | 
  3123   for (i=z+1; i<n; i++) if (inlim(lfd,i))
 | 
| 
 | 
  3124   { dist(des,i) = (xd[i]-x)/sc;
 | 
| 
 | 
  3125     wght(des,i) = weight(lfd, sp, &xd[i], &x, h, 1, dist(des,i));
 | 
| 
 | 
  3126     if (wght(des,i)>0)
 | 
| 
 | 
  3127     { des->ind[m] = i;
 | 
| 
 | 
  3128       m++; 
 | 
| 
 | 
  3129     } else i = n;
 | 
| 
 | 
  3130   }
 | 
| 
 | 
  3131 
 | 
| 
 | 
  3132   des->n = m;
 | 
| 
 | 
  3133   des->h = h;
 | 
| 
 | 
  3134 }
 | 
| 
 | 
  3135 
 | 
| 
 | 
  3136 void nbhd_zeon(lfd,des)
 | 
| 
 | 
  3137 lfdata *lfd;
 | 
| 
 | 
  3138 design *des;
 | 
| 
 | 
  3139 { int i, j, m, eq;
 | 
| 
 | 
  3140 
 | 
| 
 | 
  3141   m = 0;
 | 
| 
 | 
  3142   for (i=0; i<lfd->n; i++)
 | 
| 
 | 
  3143   { eq = 1;
 | 
| 
 | 
  3144     for (j=0; j<lfd->d; j++) eq = eq && (des->xev[j] == datum(lfd,j,i));
 | 
| 
 | 
  3145     if (eq)
 | 
| 
 | 
  3146     { wght(des,i) = 1;
 | 
| 
 | 
  3147       des->ind[m] = i;
 | 
| 
 | 
  3148       m++;
 | 
| 
 | 
  3149     }
 | 
| 
 | 
  3150   }
 | 
| 
 | 
  3151   des->n = m;
 | 
| 
 | 
  3152   des->h = 1.0;
 | 
| 
 | 
  3153 }
 | 
| 
 | 
  3154 
 | 
| 
 | 
  3155 void nbhd(lfd,des,nn,redo,sp)
 | 
| 
 | 
  3156 lfdata *lfd;
 | 
| 
 | 
  3157 design *des;
 | 
| 
 | 
  3158 int redo, nn;
 | 
| 
 | 
  3159 smpar *sp;
 | 
| 
 | 
  3160 { int d, i, j, m, n;
 | 
| 
 | 
  3161   double h, u[MXDIM];
 | 
| 
 | 
  3162 
 | 
| 
 | 
  3163   if (lf_debug>1) mut_printf("nbhd: nn %d  fixh %8.5f\n",nn,fixh(sp));
 | 
| 
 | 
  3164   
 | 
| 
 | 
  3165   d = lfd->d; n = lfd->n;
 | 
| 
 | 
  3166 
 | 
| 
 | 
  3167   if (ker(sp)==WPARM)
 | 
| 
 | 
  3168   { for (i=0; i<n; i++)
 | 
| 
 | 
  3169     { wght(des,i) = 1.0;
 | 
| 
 | 
  3170       des->ind[i] = i;
 | 
| 
 | 
  3171     }
 | 
| 
 | 
  3172     des->n = n;
 | 
| 
 | 
  3173     return;
 | 
| 
 | 
  3174   }
 | 
| 
 | 
  3175 
 | 
| 
 | 
  3176   if (kt(sp)==KZEON)
 | 
| 
 | 
  3177   { nbhd_zeon(lfd,des);
 | 
| 
 | 
  3178     return;
 | 
| 
 | 
  3179   }
 | 
| 
 | 
  3180 
 | 
| 
 | 
  3181   if (kt(sp)==KCE)
 | 
| 
 | 
  3182   { des->h = 0.0;
 | 
| 
 | 
  3183     return;
 | 
| 
 | 
  3184   }
 | 
| 
 | 
  3185 
 | 
| 
 | 
  3186   /* ordered 1-dim; use fast searches */
 | 
| 
 | 
  3187   if ((nn<=n) & (lfd->ord) & (ker(sp)!=WMINM) & (lfd->sty[0]!=STANGL))
 | 
| 
 | 
  3188   { nbhd1(lfd,sp,des,nn);
 | 
| 
 | 
  3189     return;
 | 
| 
 | 
  3190   }
 | 
| 
 | 
  3191 
 | 
| 
 | 
  3192   if (!redo)
 | 
| 
 | 
  3193   { for (i=0; i<n; i++)
 | 
| 
 | 
  3194     { for (j=0; j<d; j++) u[j] = datum(lfd,j,i)-des->xev[j];
 | 
| 
 | 
  3195       dist(des,i) = rho(u,lfd->sca,d,kt(sp),lfd->sty);
 | 
| 
 | 
  3196       des->ind[i] = i;
 | 
| 
 | 
  3197     }
 | 
| 
 | 
  3198   }
 | 
| 
 | 
  3199   else
 | 
| 
 | 
  3200     for (i=0; i<n; i++) des->ind[i] = i;
 | 
| 
 | 
  3201 
 | 
| 
 | 
  3202   if (ker(sp)==WMINM)
 | 
| 
 | 
  3203   { des->h = minmax(lfd,des,sp);
 | 
| 
 | 
  3204     return;
 | 
| 
 | 
  3205   }
 | 
| 
 | 
  3206 
 | 
| 
 | 
  3207   if (nn<0)
 | 
| 
 | 
  3208     h = sp->vb(des->xev);
 | 
| 
 | 
  3209   else
 | 
| 
 | 
  3210     h = compbandwid(des->di,des->ind,des->xev,n,lfd->d,nn,fixh(sp));
 | 
| 
 | 
  3211   m = 0;
 | 
| 
 | 
  3212   for (i=0; i<n; i++) if (inlim(lfd,i))
 | 
| 
 | 
  3213   { for (j=0; j<d; j++) u[j] = datum(lfd,j,i);
 | 
| 
 | 
  3214     wght(des,i) = weight(lfd, sp, u, des->xev, h, 1, dist(des,i));
 | 
| 
 | 
  3215     if (wght(des,i)>0)
 | 
| 
 | 
  3216     { des->ind[m] = i;
 | 
| 
 | 
  3217       m++;
 | 
| 
 | 
  3218     }
 | 
| 
 | 
  3219   }
 | 
| 
 | 
  3220   des->n = m;
 | 
| 
 | 
  3221   des->h = h;
 | 
| 
 | 
  3222 }
 | 
| 
 | 
  3223 /*
 | 
| 
 | 
  3224  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  3225  */
 | 
| 
 | 
  3226 /*
 | 
| 
 | 
  3227  *
 | 
| 
 | 
  3228  *   This file includes functions to solve for the scale estimate in
 | 
| 
 | 
  3229  *   local robust regression and likelihood. The main entry point is
 | 
| 
 | 
  3230  *   lf_robust(lfd,sp,des,mxit),
 | 
| 
 | 
  3231  *   called from the locfit() function.
 | 
| 
 | 
  3232  *
 | 
| 
 | 
  3233  *   The update_rs(x) accepts a residual scale x as the argument (actually,
 | 
| 
 | 
  3234  *   it works on the log-scale). The function computes the local fit
 | 
| 
 | 
  3235  *   assuming this residual scale, and re-estimates the scale from this
 | 
| 
 | 
  3236  *   new fit. The final solution satisfies the fixed point equation
 | 
| 
 | 
  3237  *   update_rs(x)=x. The function lf_robust() automatically calls
 | 
| 
 | 
  3238  *   update_rs() through the fixed point iterations.
 | 
| 
 | 
  3239  *
 | 
| 
 | 
  3240  *   The estimation of the scale from the fit is based on the sqrt of
 | 
| 
 | 
  3241  *   the median deviance of observations with non-zero weights (in the
 | 
| 
 | 
  3242  *   gaussian case, this is the median absolute residual).
 | 
| 
 | 
  3243  *
 | 
| 
 | 
  3244  *   TODO:
 | 
| 
 | 
  3245  *     Should use smoothing weights in the median.
 | 
| 
 | 
  3246  */
 | 
| 
 | 
  3247 
 | 
| 
 | 
  3248 #include "locf.h"
 | 
| 
 | 
  3249 
 | 
| 
 | 
  3250 extern int lf_status;
 | 
| 
 | 
  3251 double robscale;
 | 
| 
 | 
  3252 
 | 
| 
 | 
  3253 static lfdata *rob_lfd;
 | 
| 
 | 
  3254 static smpar *rob_sp;
 | 
| 
 | 
  3255 static design *rob_des;
 | 
| 
 | 
  3256 static int rob_mxit;
 | 
| 
 | 
  3257 
 | 
| 
 | 
  3258 double median(x,n)
 | 
| 
 | 
  3259 double *x;
 | 
| 
 | 
  3260 int n;
 | 
| 
 | 
  3261 { int i, j, lt, eq, gt;
 | 
| 
 | 
  3262   double lo, hi, s;
 | 
| 
 | 
  3263   lo = hi = x[0];
 | 
| 
 | 
  3264   for (i=0; i<n; i++)
 | 
| 
 | 
  3265   { lo = MIN(lo,x[i]);
 | 
| 
 | 
  3266     hi = MAX(hi,x[i]);
 | 
| 
 | 
  3267   }
 | 
| 
 | 
  3268   if (lo==hi) return(lo);
 | 
| 
 | 
  3269   lo -= (hi-lo);
 | 
| 
 | 
  3270   hi += (hi-lo);
 | 
| 
 | 
  3271   for (i=0; i<n; i++)
 | 
| 
 | 
  3272   { if ((x[i]>lo) & (x[i]<hi))
 | 
| 
 | 
  3273     { s = x[i]; lt = eq = gt = 0;
 | 
| 
 | 
  3274       for (j=0; j<n; j++)
 | 
| 
 | 
  3275       { lt += (x[j]<s);
 | 
| 
 | 
  3276         eq += (x[j]==s);
 | 
| 
 | 
  3277         gt += (x[j]>s);
 | 
| 
 | 
  3278       }
 | 
| 
 | 
  3279       if ((2*(lt+eq)>n) && (2*(gt+eq)>n)) return(s);
 | 
| 
 | 
  3280       if (2*(lt+eq)<=n) lo = s;
 | 
| 
 | 
  3281       if (2*(gt+eq)<=n) hi = s;
 | 
| 
 | 
  3282     }
 | 
| 
 | 
  3283   }
 | 
| 
 | 
  3284   return((hi+lo)/2);
 | 
| 
 | 
  3285 }
 | 
| 
 | 
  3286 
 | 
| 
 | 
  3287 double nrobustscale(lfd,sp,des,rs)
 | 
| 
 | 
  3288 lfdata *lfd;
 | 
| 
 | 
  3289 smpar *sp;
 | 
| 
 | 
  3290 design *des;
 | 
| 
 | 
  3291 double rs;
 | 
| 
 | 
  3292 { int i, ii, p;
 | 
| 
 | 
  3293   double link[LLEN], sc, sd, sw, e;
 | 
| 
 | 
  3294   p = des->p; sc = sd = sw = 0.0;
 | 
| 
 | 
  3295   for (i=0; i<des->n; i++)
 | 
| 
 | 
  3296   { ii = des->ind[i];
 | 
| 
 | 
  3297     fitv(des,ii) = base(lfd,ii)+innerprod(des->cf,d_xi(des,ii),p);
 | 
| 
 | 
  3298     e = resp(lfd,ii)-fitv(des,ii);
 | 
| 
 | 
  3299     stdlinks(link,lfd,sp,ii,fitv(des,ii),rs);
 | 
| 
 | 
  3300     sc += wght(des,ii)*e*link[ZDLL];
 | 
| 
 | 
  3301     sd += wght(des,ii)*e*e*link[ZDDLL];
 | 
| 
 | 
  3302     sw += wght(des,ii);
 | 
| 
 | 
  3303   }
 | 
| 
 | 
  3304 
 | 
| 
 | 
  3305   /* newton-raphson iteration for log(s)
 | 
| 
 | 
  3306      -psi(ei/s) - log(s); s = e^{-th}
 | 
| 
 | 
  3307   */
 | 
| 
 | 
  3308   rs *= exp((sc-sw)/(sd+sc));
 | 
| 
 | 
  3309   return(rs);
 | 
| 
 | 
  3310 }
 | 
| 
 | 
  3311 
 | 
| 
 | 
  3312 double robustscale(lfd,sp,des)
 | 
| 
 | 
  3313 lfdata *lfd;
 | 
| 
 | 
  3314 smpar *sp;
 | 
| 
 | 
  3315 design *des;
 | 
| 
 | 
  3316 { int i, ii, p, fam, lin, or;
 | 
| 
 | 
  3317   double rs, link[LLEN];
 | 
| 
 | 
  3318   p = des->p;
 | 
| 
 | 
  3319   fam = fam(sp);
 | 
| 
 | 
  3320   lin = link(sp);
 | 
| 
 | 
  3321   or = fami(sp)->robust;
 | 
| 
 | 
  3322   fami(sp)->robust = 0;
 | 
| 
 | 
  3323 
 | 
| 
 | 
  3324   for (i=0; i<des->n; i++)
 | 
| 
 | 
  3325   { ii = des->ind[i];
 | 
| 
 | 
  3326     fitv(des,ii) = base(lfd,ii) + innerprod(des->cf,d_xi(des,ii),p);
 | 
| 
 | 
  3327     links(fitv(des,ii),resp(lfd,ii),fami(sp),lin,link,cens(lfd,ii),prwt(lfd,ii),1.0);
 | 
| 
 | 
  3328     des->res[i] = -2*link[ZLIK];
 | 
| 
 | 
  3329   }
 | 
| 
 | 
  3330   fami(sp)->robust = or;
 | 
| 
 | 
  3331   rs = sqrt(median(des->res,des->n));
 | 
| 
 | 
  3332 
 | 
| 
 | 
  3333   if (rs==0.0) rs = 1.0;
 | 
| 
 | 
  3334   return(rs);
 | 
| 
 | 
  3335 }
 | 
| 
 | 
  3336 
 | 
| 
 | 
  3337 double update_rs(x)
 | 
| 
 | 
  3338 double x;
 | 
| 
 | 
  3339 { double nx;
 | 
| 
 | 
  3340   if (lf_status != LF_OK) return(x);
 | 
| 
 | 
  3341   robscale = exp(x);
 | 
| 
 | 
  3342   lfiter(rob_lfd,rob_sp,rob_des,rob_mxit);
 | 
| 
 | 
  3343   if (lf_status != LF_OK) return(x);
 | 
| 
 | 
  3344 
 | 
| 
 | 
  3345   nx = log(robustscale(rob_lfd,rob_sp,rob_des));
 | 
| 
 | 
  3346   if (nx<x-0.2) nx = x-0.2;
 | 
| 
 | 
  3347   return(nx);
 | 
| 
 | 
  3348 }
 | 
| 
 | 
  3349 
 | 
| 
 | 
  3350 void lf_robust(lfd,sp,des,mxit)
 | 
| 
 | 
  3351 lfdata *lfd;
 | 
| 
 | 
  3352 design *des;
 | 
| 
 | 
  3353 smpar *sp;
 | 
| 
 | 
  3354 int mxit;
 | 
| 
 | 
  3355 { double x;
 | 
| 
 | 
  3356   rob_lfd = lfd;
 | 
| 
 | 
  3357   rob_des = des;
 | 
| 
 | 
  3358   rob_sp = sp;
 | 
| 
 | 
  3359   rob_mxit = mxit;
 | 
| 
 | 
  3360   lf_status = LF_OK;
 | 
| 
 | 
  3361 
 | 
| 
 | 
  3362   x = log(robustscale(lfd,sp,des));
 | 
| 
 | 
  3363 
 | 
| 
 | 
  3364   solve_fp(update_rs, x, 1.0e-6, mxit);
 | 
| 
 | 
  3365 }
 | 
| 
 | 
  3366 /*
 | 
| 
 | 
  3367  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  3368  */
 | 
| 
 | 
  3369 /*
 | 
| 
 | 
  3370  *   Post-fitting functions to compute the local variance and
 | 
| 
 | 
  3371  *   influence functions. Also the local degrees of freedom
 | 
| 
 | 
  3372  *   calculations for adaptive smoothing.
 | 
| 
 | 
  3373  */
 | 
| 
 | 
  3374 
 | 
| 
 | 
  3375 #include "locf.h"
 | 
| 
 | 
  3376 
 | 
| 
 | 
  3377 extern double robscale;
 | 
| 
 | 
  3378 
 | 
| 
 | 
  3379 /*
 | 
| 
 | 
  3380   vmat() computes (after the local fit..) the matrix 
 | 
| 
 | 
  3381   M2  = X^T W^2 V X.
 | 
| 
 | 
  3382   M12 = (X^T W V X)^{-1} M2
 | 
| 
 | 
  3383   Also, for convenience, tr[0] = sum(wi) tr[1] = sum(wi^2).
 | 
| 
 | 
  3384 */
 | 
| 
 | 
  3385 void vmat(lfd, sp, des, M12, M2)
 | 
| 
 | 
  3386 lfdata *lfd;
 | 
| 
 | 
  3387 smpar *sp;
 | 
| 
 | 
  3388 design *des;
 | 
| 
 | 
  3389 double *M12, *M2;
 | 
| 
 | 
  3390 { int i, ii, p, nk, ok;
 | 
| 
 | 
  3391   double link[LLEN], h, ww, tr0, tr1;
 | 
| 
 | 
  3392   p = des->p;
 | 
| 
 | 
  3393   setzero(M2,p*p);
 | 
| 
 | 
  3394 
 | 
| 
 | 
  3395   nk = -1;
 | 
| 
 | 
  3396 
 | 
| 
 | 
  3397   /* for density estimation, use integral rather than
 | 
| 
 | 
  3398      sum form, if W^2 is programmed...
 | 
| 
 | 
  3399   */
 | 
| 
 | 
  3400   if ((fam(sp)<=THAZ) && (link(sp)==LLOG))
 | 
| 
 | 
  3401   { switch(ker(sp))
 | 
| 
 | 
  3402     { case WGAUS: nk = WGAUS; h = des->h/SQRT2; break;
 | 
| 
 | 
  3403       case WRECT: nk = WRECT; h = des->h; break;
 | 
| 
 | 
  3404       case WEPAN: nk = WBISQ; h = des->h; break;
 | 
| 
 | 
  3405       case WBISQ: nk = WQUQU; h = des->h; break;
 | 
| 
 | 
  3406       case WTCUB: nk = W6CUB; h = des->h; break;
 | 
| 
 | 
  3407       case WEXPL: nk = WEXPL; h = des->h/2; break;
 | 
| 
 | 
  3408     }
 | 
| 
 | 
  3409   }
 | 
| 
 | 
  3410 
 | 
| 
 | 
  3411   tr0 = tr1 = 0.0;
 | 
| 
 | 
  3412   if (nk != -1)
 | 
| 
 | 
  3413   { ok = ker(sp); ker(sp) = nk;
 | 
| 
 | 
  3414 /* compute M2 using integration. Use M12 as work matrix. */
 | 
| 
 | 
  3415     (des->itype)(des->xev, M2, M12, des->cf, h);
 | 
| 
 | 
  3416     ker(sp) = ok;
 | 
| 
 | 
  3417     if (fam(sp)==TDEN) multmatscal(M2,des->smwt,p*p);
 | 
| 
 | 
  3418     tr0 = des->ss[0];
 | 
| 
 | 
  3419     tr1 = M2[0]; /* n int W e^<a,A> */
 | 
| 
 | 
  3420   }
 | 
| 
 | 
  3421   else
 | 
| 
 | 
  3422   { for (i=0; i<des->n; i++)
 | 
| 
 | 
  3423     { ii = des->ind[i];
 | 
| 
 | 
  3424       stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
 | 
| 
 | 
  3425       ww = SQR(wght(des,ii))*link[ZDDLL];
 | 
| 
 | 
  3426       tr0 += wght(des,ii);
 | 
| 
 | 
  3427       tr1 += SQR(wght(des,ii));
 | 
| 
 | 
  3428       addouter(M2,d_xi(des,ii),d_xi(des,ii),p,ww);
 | 
| 
 | 
  3429     }
 | 
| 
 | 
  3430   }
 | 
| 
 | 
  3431   des->tr0 = tr0;
 | 
| 
 | 
  3432   des->tr1 = tr1;
 | 
| 
 | 
  3433 
 | 
| 
 | 
  3434   memcpy(M12,M2,p*p*sizeof(double));
 | 
| 
 | 
  3435   for (i=0; i<p; i++)
 | 
| 
 | 
  3436     jacob_solve(&des->xtwx,&M12[i*p]);
 | 
| 
 | 
  3437 }
 | 
| 
 | 
  3438 
 | 
| 
 | 
  3439 void lf_vcov(lfd,sp,des)
 | 
| 
 | 
  3440 lfdata *lfd;
 | 
| 
 | 
  3441 smpar *sp;
 | 
| 
 | 
  3442 design *des;
 | 
| 
 | 
  3443 { int i, j, k, p;
 | 
| 
 | 
  3444   double *M12, *M2;
 | 
| 
 | 
  3445   M12 = des->V; M2 = des->P; p = des->p;
 | 
| 
 | 
  3446   vmat(lfd,sp,des,M12,M2); /* M2 = X^T W^2 V X  tr0=sum(W) tr1=sum(W*W) */
 | 
| 
 | 
  3447   des->tr2 = m_trace(M12,p);   /* tr (XTWVX)^{-1}(XTW^2VX) */
 | 
| 
 | 
  3448 
 | 
| 
 | 
  3449 /*
 | 
| 
 | 
  3450  * Covariance matrix is M1^{-1} * M2 * M1^{-1}
 | 
| 
 | 
  3451  * We compute this using the cholesky decomposition of
 | 
| 
 | 
  3452  * M2; premultiplying by M1^{-1} and squaring. This
 | 
| 
 | 
  3453  * is more stable than direct computation in near-singular cases.
 | 
| 
 | 
  3454  */
 | 
| 
 | 
  3455   chol_dec(M2,p,p);
 | 
| 
 | 
  3456   for (i=0; i<p; i++)
 | 
| 
 | 
  3457     for (j=0; j<i; j++)
 | 
| 
 | 
  3458     { M2[j*p+i] = M2[i*p+j];
 | 
| 
 | 
  3459       M2[i*p+j] = 0.0;
 | 
| 
 | 
  3460     }
 | 
| 
 | 
  3461   for (i=0; i<p; i++) jacob_solve(&des->xtwx,&M2[i*p]);
 | 
| 
 | 
  3462   for (i=0; i<p; i++)
 | 
| 
 | 
  3463   { for (j=0; j<p; j++)
 | 
| 
 | 
  3464     { M12[i*p+j] = 0;
 | 
| 
 | 
  3465       for (k=0; k<p; k++)
 | 
| 
 | 
  3466         M12[i*p+j] += M2[k*p+i]*M2[k*p+j]; /* ith column of covariance */
 | 
| 
 | 
  3467     }
 | 
| 
 | 
  3468   }
 | 
| 
 | 
  3469   if ((fam(sp)==TDEN) && (link(sp)==LIDENT))
 | 
| 
 | 
  3470     multmatscal(M12,1/SQR(des->smwt),p*p);
 | 
| 
 | 
  3471 
 | 
| 
 | 
  3472 /* this computes the influence function as des->f1[0]. */
 | 
| 
 | 
  3473   unitvec(des->f1,0,des->p);
 | 
| 
 | 
  3474   jacob_solve(&des->xtwx,des->f1);
 | 
| 
 | 
  3475 }
 | 
| 
 | 
  3476 
 | 
| 
 | 
  3477 /* local_df computes:
 | 
| 
 | 
  3478  *   tr[0] = trace(W)
 | 
| 
 | 
  3479  *   tr[1] = trace(W*W)
 | 
| 
 | 
  3480  *   tr[2] = trace( M1^{-1} M2 )
 | 
| 
 | 
  3481  *   tr[3] = trace( M1^{-1} M3 )
 | 
| 
 | 
  3482  *   tr[4] = trace( (M1^{-1} M2)^2 )
 | 
| 
 | 
  3483  *   tr[5] = var(theta-hat).
 | 
| 
 | 
  3484  */
 | 
| 
 | 
  3485 void local_df(lfd,sp,des,tr)
 | 
| 
 | 
  3486 lfdata *lfd;
 | 
| 
 | 
  3487 smpar *sp;
 | 
| 
 | 
  3488 design *des;
 | 
| 
 | 
  3489 double *tr;
 | 
| 
 | 
  3490 { int i, ii, j, p;
 | 
| 
 | 
  3491   double *m2, *V, ww, link[LLEN];
 | 
| 
 | 
  3492 
 | 
| 
 | 
  3493   tr[0] = tr[1] = tr[2] = tr[3] = tr[4] = tr[5] = 0.0;
 | 
| 
 | 
  3494   m2 = des->V; V = des->P; p = des->p;
 | 
| 
 | 
  3495 
 | 
| 
 | 
  3496   vmat(lfd,sp,des,m2,V);  /* M = X^T W^2 V X  tr0=sum(W) tr1=sum(W*W) */
 | 
| 
 | 
  3497   tr[0] = des->tr0;
 | 
| 
 | 
  3498   tr[1] = des->tr1;
 | 
| 
 | 
  3499   tr[2] = m_trace(m2,p);   /* tr (XTWVX)^{-1}(XTW^2VX) */
 | 
| 
 | 
  3500 
 | 
| 
 | 
  3501   unitvec(des->f1,0,p);
 | 
| 
 | 
  3502   jacob_solve(&des->xtwx,des->f1);
 | 
| 
 | 
  3503   for (i=0; i<p; i++)
 | 
| 
 | 
  3504     for (j=0; j<p; j++)
 | 
| 
 | 
  3505     { tr[4] += m2[i*p+j]*m2[j*p+i];  /* tr(M^2) */
 | 
| 
 | 
  3506       tr[5] += des->f1[i]*V[i*p+j]*des->f1[j]; /* var(thetahat) */
 | 
| 
 | 
  3507   }
 | 
| 
 | 
  3508   tr[5] = sqrt(tr[5]);
 | 
| 
 | 
  3509 
 | 
| 
 | 
  3510   setzero(m2,p*p);
 | 
| 
 | 
  3511   for (i=0; i<des->n; i++)
 | 
| 
 | 
  3512   { ii = des->ind[i];
 | 
| 
 | 
  3513     stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
 | 
| 
 | 
  3514     ww = wght(des,ii)*wght(des,ii)*wght(des,ii)*link[ZDDLL];
 | 
| 
 | 
  3515     addouter(m2,d_xi(des,ii),d_xi(des,ii),p,ww);
 | 
| 
 | 
  3516   }
 | 
| 
 | 
  3517   for (i=0; i<p; i++)
 | 
| 
 | 
  3518   { jacob_solve(&des->xtwx,&m2[i*p]);
 | 
| 
 | 
  3519     tr[3] += m2[i*(p+1)];
 | 
| 
 | 
  3520   }
 | 
| 
 | 
  3521 
 | 
| 
 | 
  3522   return;
 | 
| 
 | 
  3523 }
 | 
| 
 | 
  3524 /*
 | 
| 
 | 
  3525  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  3526  */
 | 
| 
 | 
  3527 /*
 | 
| 
 | 
  3528  *  Routines for computing weight diagrams.
 | 
| 
 | 
  3529  *     wdiag(lf,des,lx,deg,ty,exp)
 | 
| 
 | 
  3530  *  Must locfit() first, unless ker==WPARM and has par. comp.
 | 
| 
 | 
  3531  *  
 | 
| 
 | 
  3532  */
 | 
| 
 | 
  3533 
 | 
| 
 | 
  3534 #include "locf.h"
 | 
| 
 | 
  3535 
 | 
| 
 | 
  3536 static double *wd;
 | 
| 
 | 
  3537 extern double robscale;
 | 
| 
 | 
  3538 void nnresproj(lfd,sp,des,u,m,p)
 | 
| 
 | 
  3539 lfdata *lfd;
 | 
| 
 | 
  3540 smpar *sp;
 | 
| 
 | 
  3541 design *des;
 | 
| 
 | 
  3542 double *u;
 | 
| 
 | 
  3543 int m, p;
 | 
| 
 | 
  3544 { int i, ii, j;
 | 
| 
 | 
  3545   double link[LLEN];
 | 
| 
 | 
  3546   setzero(des->f1,p);
 | 
| 
 | 
  3547   for (j=0; j<m; j++)
 | 
| 
 | 
  3548   { ii = des->ind[j];
 | 
| 
 | 
  3549     stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
 | 
| 
 | 
  3550     for (i=0; i<p; i++) des->f1[i] += link[ZDDLL]*d_xij(des,j,ii)*u[j];
 | 
| 
 | 
  3551   }
 | 
| 
 | 
  3552   jacob_solve(&des->xtwx,des->f1);
 | 
| 
 | 
  3553   for (i=0; i<m; i++)
 | 
| 
 | 
  3554   { ii = des->ind[i];
 | 
| 
 | 
  3555     u[i] -= innerprod(des->f1,d_xi(des,ii),p)*wght(des,ii);
 | 
| 
 | 
  3556   }
 | 
| 
 | 
  3557 }
 | 
| 
 | 
  3558 
 | 
| 
 | 
  3559 void wdexpand(l,n,ind,m)
 | 
| 
 | 
  3560 double *l;
 | 
| 
 | 
  3561 int *ind, n, m;
 | 
| 
 | 
  3562 { int i, j, t;
 | 
| 
 | 
  3563   double z;
 | 
| 
 | 
  3564   for (j=m; j<n; j++) { l[j] = 0.0; ind[j] = -1; }
 | 
| 
 | 
  3565   j = m-1;
 | 
| 
 | 
  3566   while (j>=0)
 | 
| 
 | 
  3567   { if (ind[j]==j) j--;
 | 
| 
 | 
  3568     else
 | 
| 
 | 
  3569     { i = ind[j];
 | 
| 
 | 
  3570       z = l[j]; l[j] = l[i]; l[i] = z;
 | 
| 
 | 
  3571       t = ind[j]; ind[j] = ind[i]; ind[i] = t;
 | 
| 
 | 
  3572       if (ind[j]==-1) j--;
 | 
| 
 | 
  3573     }
 | 
| 
 | 
  3574   }
 | 
| 
 | 
  3575 
 | 
| 
 | 
  3576 /*  for (i=n-1; i>=0; i--)
 | 
| 
 | 
  3577   { l[i] = ((j>=0) && (ind[j]==i)) ? l[j--] : 0.0; } */
 | 
| 
 | 
  3578 }
 | 
| 
 | 
  3579 
 | 
| 
 | 
  3580 int wdiagp(lfd,sp,des,lx,pc,dv,deg,ty,exp)
 | 
| 
 | 
  3581 lfdata *lfd;
 | 
| 
 | 
  3582 smpar *sp;
 | 
| 
 | 
  3583 design *des;
 | 
| 
 | 
  3584 paramcomp *pc;
 | 
| 
 | 
  3585 deriv *dv;
 | 
| 
 | 
  3586 double *lx;
 | 
| 
 | 
  3587 int deg, ty, exp;
 | 
| 
 | 
  3588 { int i, j, p, nd;
 | 
| 
 | 
  3589   double *l1;
 | 
| 
 | 
  3590 
 | 
| 
 | 
  3591   p = des->p;
 | 
| 
 | 
  3592 
 | 
| 
 | 
  3593   fitfun(lfd,sp,des->xev,pc->xbar,des->f1,dv);
 | 
| 
 | 
  3594   if (exp)
 | 
| 
 | 
  3595   { jacob_solve(&pc->xtwx,des->f1);
 | 
| 
 | 
  3596     for (i=0; i<lfd->n; i++)
 | 
| 
 | 
  3597       lx[i] = innerprod(des->f1,d_xi(des,des->ind[i]),p);
 | 
| 
 | 
  3598     return(lfd->n);
 | 
| 
 | 
  3599   }
 | 
| 
 | 
  3600   jacob_hsolve(&pc->xtwx,des->f1);
 | 
| 
 | 
  3601   for (i=0; i<p; i++) lx[i] = des->f1[i];
 | 
| 
 | 
  3602 
 | 
| 
 | 
  3603   nd = dv->nd;
 | 
| 
 | 
  3604   dv->nd = nd+1;
 | 
| 
 | 
  3605   if (deg>=1)
 | 
| 
 | 
  3606     for (i=0; i<lfd->d; i++)
 | 
| 
 | 
  3607     { dv->deriv[nd] = i;
 | 
| 
 | 
  3608       l1 = &lx[(i+1)*p];
 | 
| 
 | 
  3609       fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
 | 
| 
 | 
  3610       jacob_hsolve(&pc->xtwx,l1);
 | 
| 
 | 
  3611     }
 | 
| 
 | 
  3612 
 | 
| 
 | 
  3613   dv->nd = nd+2;
 | 
| 
 | 
  3614   if (deg>=2)
 | 
| 
 | 
  3615     for (i=0; i<lfd->d; i++)
 | 
| 
 | 
  3616     { dv->deriv[nd] = i;
 | 
| 
 | 
  3617       for (j=0; j<lfd->d; j++)
 | 
| 
 | 
  3618       { dv->deriv[nd+1] = j;
 | 
| 
 | 
  3619         l1 = &lx[(i*lfd->d+j+lfd->d+1)*p];
 | 
| 
 | 
  3620         fitfun(lfd,sp,des->xev,pc->xbar,l1,dv);
 | 
| 
 | 
  3621         jacob_hsolve(&pc->xtwx,l1);
 | 
| 
 | 
  3622     } }
 | 
| 
 | 
  3623   dv->nd = nd;
 | 
| 
 | 
  3624   return(p);
 | 
| 
 | 
  3625 }
 | 
| 
 | 
  3626 
 | 
| 
 | 
  3627 int wdiag(lfd,sp,des,lx,dv,deg,ty,exp)
 | 
| 
 | 
  3628 lfdata *lfd;
 | 
| 
 | 
  3629 smpar *sp;
 | 
| 
 | 
  3630 design *des;
 | 
| 
 | 
  3631 deriv *dv;
 | 
| 
 | 
  3632 double *lx;
 | 
| 
 | 
  3633 int deg, ty, exp;
 | 
| 
 | 
  3634 /* deg=0: l(x) only.
 | 
| 
 | 
  3635    deg=1: l(x), l'(x)
 | 
| 
 | 
  3636    deg=2: l(x), l'(x), l''(x)
 | 
| 
 | 
  3637    ty = 1: e1 (X^T WVX)^{-1} X^T W        -- hat matrix
 | 
| 
 | 
  3638    ty = 2: e1 (X^T WVX)^{-1} X^T WV^{1/2} -- scb's
 | 
| 
 | 
  3639 */
 | 
| 
 | 
  3640 { double w, *X, *lxd, *lxdd, wdd, wdw, *ulx, link[LLEN], h;
 | 
| 
 | 
  3641   double dfx[MXDIM], hs[MXDIM];
 | 
| 
 | 
  3642   int i, ii, j, k, l, m, d, p, nd;
 | 
| 
 | 
  3643 
 | 
| 
 | 
  3644   h = des->h;
 | 
| 
 | 
  3645   nd = dv->nd;
 | 
| 
 | 
  3646   wd = des->wd;
 | 
| 
 | 
  3647   d = lfd->d; p = des->p; X = d_x(des);
 | 
| 
 | 
  3648   ulx = des->res;
 | 
| 
 | 
  3649   m = des->n;
 | 
| 
 | 
  3650   for (i=0; i<d; i++) hs[i] = h*lfd->sca[i];
 | 
| 
 | 
  3651   if (deg>0)
 | 
| 
 | 
  3652   { lxd = &lx[m];
 | 
| 
 | 
  3653     setzero(lxd,m*d);
 | 
| 
 | 
  3654     if (deg>1)
 | 
| 
 | 
  3655     { lxdd = &lxd[d*m];
 | 
| 
 | 
  3656       setzero(lxdd,m*d*d);
 | 
| 
 | 
  3657   } }
 | 
| 
 | 
  3658 
 | 
| 
 | 
  3659   if (nd>0) fitfun(lfd,sp,des->xev,des->xev,des->f1,dv); /* c(0) */
 | 
| 
 | 
  3660     else unitvec(des->f1,0,p);
 | 
| 
 | 
  3661   jacob_solve(&des->xtwx,des->f1);   /* c(0) (X^TWX)^{-1} */
 | 
| 
 | 
  3662   for (i=0; i<m; i++)
 | 
| 
 | 
  3663   { ii = des->ind[i];
 | 
| 
 | 
  3664     lx[i] = innerprod(des->f1,&X[ii*p],p); /* c(0)(XTWX)^{-1}X^T */
 | 
| 
 | 
  3665     if (deg>0)
 | 
| 
 | 
  3666     { wd[i] = Wd(dist(des,ii)/h,ker(sp));
 | 
| 
 | 
  3667       for (j=0; j<d; j++)
 | 
| 
 | 
  3668       { dfx[j] = datum(lfd,j,ii)-des->xev[j];
 | 
| 
 | 
  3669         lxd[j*m+i] = lx[i]*wght(des,ii)*weightd(dfx[j],lfd->sca[j],
 | 
| 
 | 
  3670           d,ker(sp),kt(sp),h,lfd->sty[j],dist(des,ii));
 | 
| 
 | 
  3671              /* c(0) (XTWX)^{-1}XTW' */
 | 
| 
 | 
  3672       }
 | 
| 
 | 
  3673       if (deg>1)
 | 
| 
 | 
  3674       { wdd = Wdd(dist(des,ii)/h,ker(sp));
 | 
| 
 | 
  3675         for (j=0; j<d; j++)
 | 
| 
 | 
  3676           for (k=0; k<d; k++)
 | 
| 
 | 
  3677           { w = (dist(des,ii)==0) ? 0 : h/dist(des,ii);
 | 
| 
 | 
  3678             w = wdd * (des->xev[k]-datum(lfd,k,ii)) * (des->xev[j]-datum(lfd,j,ii))
 | 
| 
 | 
  3679                   * w*w / (hs[k]*hs[k]*hs[j]*hs[j]);
 | 
| 
 | 
  3680             if (j==k) w += wd[i]/(hs[j]*hs[j]);
 | 
| 
 | 
  3681             lxdd[(j*d+k)*m+i] = lx[i]*w;
 | 
| 
 | 
  3682               /* c(0)(XTWX)^{-1}XTW'' */
 | 
| 
 | 
  3683           }
 | 
| 
 | 
  3684       }
 | 
| 
 | 
  3685     }
 | 
| 
 | 
  3686     lx[i] *= wght(des,ii);
 | 
| 
 | 
  3687   }
 | 
| 
 | 
  3688 
 | 
| 
 | 
  3689   dv->nd = nd+1;
 | 
| 
 | 
  3690   if (deg==2)
 | 
| 
 | 
  3691   { for (i=0; i<d; i++)
 | 
| 
 | 
  3692     { dv->deriv[nd] = i;
 | 
| 
 | 
  3693       fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
 | 
| 
 | 
  3694       for (k=0; k<m; k++)
 | 
| 
 | 
  3695       { ii = des->ind[i];
 | 
| 
 | 
  3696         stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
 | 
| 
 | 
  3697         for (j=0; j<p; j++)
 | 
| 
 | 
  3698           des->f1[j] -= link[ZDDLL]*lxd[i*m+k]*X[ii*p+j];
 | 
| 
 | 
  3699         /* c'(x)-c(x)(XTWX)^{-1}XTW'X */
 | 
| 
 | 
  3700       }
 | 
| 
 | 
  3701       jacob_solve(&des->xtwx,des->f1); /* (...)(XTWX)^{-1} */
 | 
| 
 | 
  3702       for (j=0; j<m; j++)
 | 
| 
 | 
  3703       { ii = des->ind[j];
 | 
| 
 | 
  3704         ulx[j] = innerprod(des->f1,&X[ii*p],p); /* (...)XT */
 | 
| 
 | 
  3705       }
 | 
| 
 | 
  3706       for (j=0; j<d; j++)
 | 
| 
 | 
  3707         for (k=0; k<m; k++)
 | 
| 
 | 
  3708         { ii = des->ind[k];
 | 
| 
 | 
  3709           dfx[j] = datum(lfd,j,ii)-des->xev[j];
 | 
| 
 | 
  3710           wdw = wght(des,ii)*weightd(dfx[j],lfd->sca[j],d,ker(sp),
 | 
| 
 | 
  3711             kt(sp),h,lfd->sty[j],dist(des,ii));
 | 
| 
 | 
  3712           lxdd[(i*d+j)*m+k] += ulx[k]*wdw;
 | 
| 
 | 
  3713           lxdd[(j*d+i)*m+k] += ulx[k]*wdw;
 | 
| 
 | 
  3714         } /* + 2(c'-c(XTWX)^{-1}XTW'X)(XTWX)^{-1}XTW' */
 | 
| 
 | 
  3715     }
 | 
| 
 | 
  3716     for (j=0; j<d*d; j++) nnresproj(lfd,sp,des,&lxdd[j*m],m,p);
 | 
| 
 | 
  3717         /* * (I-X(XTWX)^{-1} XTW */
 | 
| 
 | 
  3718   }
 | 
| 
 | 
  3719   if (deg>0)
 | 
| 
 | 
  3720   { for (j=0; j<d; j++) nnresproj(lfd,sp,des,&lxd[j*m],m,p);
 | 
| 
 | 
  3721       /* c(0)(XTWX)^{-1}XTW'(I-X(XTWX)^{-1}XTW) */
 | 
| 
 | 
  3722     for (i=0; i<d; i++)
 | 
| 
 | 
  3723     { dv->deriv[nd]=i;
 | 
| 
 | 
  3724       fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
 | 
| 
 | 
  3725       jacob_solve(&des->xtwx,des->f1);
 | 
| 
 | 
  3726       for (k=0; k<m; k++)
 | 
| 
 | 
  3727       { ii = des->ind[k];
 | 
| 
 | 
  3728         for (l=0; l<p; l++)
 | 
| 
 | 
  3729           lxd[i*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
 | 
| 
 | 
  3730       } /* add c'(0)(XTWX)^{-1}XTW */
 | 
| 
 | 
  3731     }
 | 
| 
 | 
  3732   }
 | 
| 
 | 
  3733 
 | 
| 
 | 
  3734   dv->nd = nd+2;
 | 
| 
 | 
  3735   if (deg==2)
 | 
| 
 | 
  3736   { for (i=0; i<d; i++)
 | 
| 
 | 
  3737     { dv->deriv[nd]=i;
 | 
| 
 | 
  3738       for (j=0; j<d; j++)
 | 
| 
 | 
  3739       { dv->deriv[nd+1]=j;
 | 
| 
 | 
  3740         fitfun(lfd,sp,des->xev,des->xev,des->f1,dv);
 | 
| 
 | 
  3741         jacob_solve(&des->xtwx,des->f1);
 | 
| 
 | 
  3742         for (k=0; k<m; k++)
 | 
| 
 | 
  3743         { ii = des->ind[k];
 | 
| 
 | 
  3744           for (l=0; l<p; l++)
 | 
| 
 | 
  3745             lxdd[(i*d+j)*m+k] += des->f1[l]*X[ii*p+l]*wght(des,ii);
 | 
| 
 | 
  3746         } /* + c''(x)(XTWX)^{-1}XTW */
 | 
| 
 | 
  3747       }
 | 
| 
 | 
  3748     }
 | 
| 
 | 
  3749   }
 | 
| 
 | 
  3750   dv->nd = nd;
 | 
| 
 | 
  3751 
 | 
| 
 | 
  3752   k = 1+d*(deg>0)+d*d*(deg==2);
 | 
| 
 | 
  3753 
 | 
| 
 | 
  3754   if (exp) wdexpand(lx,lfd->n,des->ind,m);
 | 
| 
 | 
  3755  
 | 
| 
 | 
  3756   if (ty==1) return(m);
 | 
| 
 | 
  3757   for (i=0; i<m; i++)
 | 
| 
 | 
  3758   { ii = des->ind[i];
 | 
| 
 | 
  3759     stdlinks(link,lfd,sp,ii,fitv(des,ii),robscale);
 | 
| 
 | 
  3760     link[ZDDLL] = sqrt(fabs(link[ZDDLL]));
 | 
| 
 | 
  3761     for (j=0; j<k; j++) lx[j*m+i] *= link[ZDDLL];
 | 
| 
 | 
  3762   }
 | 
| 
 | 
  3763   return(m);
 | 
| 
 | 
  3764 }
 | 
| 
 | 
  3765 /*
 | 
| 
 | 
  3766  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  3767  */
 | 
| 
 | 
  3768 /*
 | 
| 
 | 
  3769  *  String  matching functions. For a given argument string, find
 | 
| 
 | 
  3770  *  the best match from an array of possibilities. Is there a library
 | 
| 
 | 
  3771  *  function somewhere to do something like this?
 | 
| 
 | 
  3772  *
 | 
| 
 | 
  3773  *  return values of -1 indicate failure/unknown string.
 | 
| 
 | 
  3774  */
 | 
| 
 | 
  3775 
 | 
| 
 | 
  3776 #include "locf.h"
 | 
| 
 | 
  3777 
 | 
| 
 | 
  3778 int ct_match(z1, z2)
 | 
| 
 | 
  3779 char *z1, *z2;
 | 
| 
 | 
  3780 { int ct = 0;
 | 
| 
 | 
  3781   while (z1[ct]==z2[ct])
 | 
| 
 | 
  3782   { if (z1[ct]=='\0') return(ct+1);
 | 
| 
 | 
  3783     ct++;
 | 
| 
 | 
  3784   }
 | 
| 
 | 
  3785   return(ct);
 | 
| 
 | 
  3786 }
 | 
| 
 | 
  3787 
 | 
| 
 | 
  3788 int pmatch(z, strings, vals, n, def)
 | 
| 
 | 
  3789 char *z, **strings;
 | 
| 
 | 
  3790 int *vals, n, def;
 | 
| 
 | 
  3791 { int i, ct, best, best_ct;
 | 
| 
 | 
  3792   best = -1;
 | 
| 
 | 
  3793   best_ct = 0;
 | 
| 
 | 
  3794 
 | 
| 
 | 
  3795   for (i=0; i<n; i++)
 | 
| 
 | 
  3796   { ct = ct_match(z,strings[i]);
 | 
| 
 | 
  3797     if (ct==strlen(z)+1) return(vals[i]);
 | 
| 
 | 
  3798     if (ct>best_ct) { best = i; best_ct = ct; }
 | 
| 
 | 
  3799   }
 | 
| 
 | 
  3800   if (best==-1) return(def);
 | 
| 
 | 
  3801   return(vals[best]);
 | 
| 
 | 
  3802 }
 | 
| 
 | 
  3803 /*
 | 
| 
 | 
  3804  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  3805  */
 | 
| 
 | 
  3806 #include "locf.h"
 | 
| 
 | 
  3807 
 | 
| 
 | 
  3808 int lf_maxit = 20;
 | 
| 
 | 
  3809 int lf_debug = 0;
 | 
| 
 | 
  3810 int lf_error = 0;
 | 
| 
 | 
  3811 
 | 
| 
 | 
  3812 double s0, s1;
 | 
| 
 | 
  3813 static lfdata *lf_lfd;
 | 
| 
 | 
  3814 static design *lf_des;
 | 
| 
 | 
  3815 static smpar   *lf_sp;
 | 
| 
 | 
  3816 int lf_status;
 | 
| 
 | 
  3817 int ident=0;
 | 
| 
 | 
  3818 double lf_tol;
 | 
| 
 | 
  3819 extern double robscale;
 | 
| 
 | 
  3820 
 | 
| 
 | 
  3821 void lfdata_init(lfd)
 | 
| 
 | 
  3822 lfdata *lfd;
 | 
| 
 | 
  3823 { int i;
 | 
| 
 | 
  3824   for (i=0; i<MXDIM; i++)
 | 
| 
 | 
  3825   { lfd->sty[i] = 0;
 | 
| 
 | 
  3826     lfd->sca[i] = 1.0;
 | 
| 
 | 
  3827     lfd->xl[i] = lfd->xl[i+MXDIM] = 0.0;
 | 
| 
 | 
  3828   }
 | 
| 
 | 
  3829   lfd->y = lfd->w = lfd->c = lfd->b = NULL;
 | 
| 
 | 
  3830   lfd->d = lfd->n = 0;
 | 
| 
 | 
  3831 }
 | 
| 
 | 
  3832 
 | 
| 
 | 
  3833 void smpar_init(sp,lfd)
 | 
| 
 | 
  3834 smpar *sp;
 | 
| 
 | 
  3835 lfdata *lfd;
 | 
| 
 | 
  3836 { nn(sp)  = 0.7;
 | 
| 
 | 
  3837   fixh(sp)= 0.0;
 | 
| 
 | 
  3838   pen(sp) = 0.0;
 | 
| 
 | 
  3839   acri(sp)= ANONE;
 | 
| 
 | 
  3840   deg(sp) = deg0(sp) = 2;
 | 
| 
 | 
  3841   ubas(sp) = 0;
 | 
| 
 | 
  3842   kt(sp) = KSPH;
 | 
| 
 | 
  3843   ker(sp) = WTCUB;
 | 
| 
 | 
  3844   fam(sp) = 64+TGAUS;
 | 
| 
 | 
  3845   link(sp)= LDEFAU;
 | 
| 
 | 
  3846   npar(sp) = calcp(sp,lfd->d);
 | 
| 
 | 
  3847 }
 | 
| 
 | 
  3848 
 | 
| 
 | 
  3849 void deriv_init(dv)
 | 
| 
 | 
  3850 deriv *dv;
 | 
| 
 | 
  3851 { dv->nd = 0;
 | 
| 
 | 
  3852 }
 | 
| 
 | 
  3853 
 | 
| 
 | 
  3854 int des_reqd(n,p)
 | 
| 
 | 
  3855 int n, p;
 | 
| 
 | 
  3856 {
 | 
| 
 | 
  3857   return(n*(p+5)+2*p*p+4*p + jac_reqd(p));
 | 
| 
 | 
  3858 }
 | 
| 
 | 
  3859 int des_reqi(n,p)
 | 
| 
 | 
  3860 int n, p;
 | 
| 
 | 
  3861 { return(n+p);
 | 
| 
 | 
  3862 }
 | 
| 
 | 
  3863  
 | 
| 
 | 
  3864 void des_init(des,n,p)
 | 
| 
 | 
  3865 design *des;
 | 
| 
 | 
  3866 int n, p;
 | 
| 
 | 
  3867 { double *z;
 | 
| 
 | 
  3868   int k;
 | 
| 
 | 
  3869 
 | 
| 
 | 
  3870   if (n<=0) WARN(("des_init: n <= 0"));
 | 
| 
 | 
  3871   if (p<=0) WARN(("des_init: p <= 0"));
 | 
| 
 | 
  3872 
 | 
| 
 | 
  3873   if (des->des_init_id != DES_INIT_ID)
 | 
| 
 | 
  3874   { des->lwk = des->lind = 0;
 | 
| 
 | 
  3875     des->des_init_id = DES_INIT_ID;
 | 
| 
 | 
  3876   }
 | 
| 
 | 
  3877 
 | 
| 
 | 
  3878   k = des_reqd(n,p);
 | 
| 
 | 
  3879   if (k>des->lwk)
 | 
| 
 | 
  3880   { des->wk = (double *)calloc(k,sizeof(double));
 | 
| 
 | 
  3881     if ( des->wk == NULL ) {
 | 
| 
 | 
  3882       printf("Problem allocating memory for des->wk\n");fflush(stdout);
 | 
| 
 | 
  3883     }
 | 
| 
 | 
  3884     des->lwk = k;
 | 
| 
 | 
  3885   }
 | 
| 
 | 
  3886   z = des->wk;
 | 
| 
 | 
  3887 
 | 
| 
 | 
  3888   des->X = z; z += n*p;
 | 
| 
 | 
  3889   des->w = z; z += n;
 | 
| 
 | 
  3890   des->res=z; z += n;
 | 
| 
 | 
  3891   des->di =z; z += n;
 | 
| 
 | 
  3892   des->th =z; z += n;
 | 
| 
 | 
  3893   des->wd =z; z += n;
 | 
| 
 | 
  3894   des->V  =z; z += p*p;
 | 
| 
 | 
  3895   des->P  =z; z += p*p;
 | 
| 
 | 
  3896   des->f1 =z; z += p;
 | 
| 
 | 
  3897   des->ss =z; z += p;
 | 
| 
 | 
  3898   des->oc =z; z += p;
 | 
| 
 | 
  3899   des->cf =z; z += p;
 | 
| 
 | 
  3900  
 | 
| 
 | 
  3901   z = jac_alloc(&des->xtwx,p,z);
 | 
| 
 | 
  3902  
 | 
| 
 | 
  3903   k = des_reqi(n,p);
 | 
| 
 | 
  3904   if (k>des->lind)
 | 
| 
 | 
  3905   {
 | 
| 
 | 
  3906     des->ind = (int *)calloc(k,sizeof(int));
 | 
| 
 | 
  3907     if ( des->ind == NULL ) {
 | 
| 
 | 
  3908       printf("Problem allocating memory for des->ind\n");fflush(stdout);
 | 
| 
 | 
  3909     }
 | 
| 
 | 
  3910     des->lind = k;
 | 
| 
 | 
  3911   }
 | 
| 
 | 
  3912   des->fix = &des->ind[n];
 | 
| 
 | 
  3913   for (k=0; k<p; k++) des->fix[k] = 0;
 | 
| 
 | 
  3914 
 | 
| 
 | 
  3915   des->n = n; des->p = p;
 | 
| 
 | 
  3916   des->smwt = n;
 | 
| 
 | 
  3917   des->xtwx.p = p;                                                              
 | 
| 
 | 
  3918 }
 | 
| 
 | 
  3919 
 | 
| 
 | 
  3920 void deschk(des,n,p)
 | 
| 
 | 
  3921 design *des;
 | 
| 
 | 
  3922 int n, p;
 | 
| 
 | 
  3923 { WARN(("deschk deprecated - use des_init()"));
 | 
| 
 | 
  3924   des_init(des,n,p);
 | 
| 
 | 
  3925 }
 | 
| 
 | 
  3926 
 | 
| 
 | 
  3927 int likereg(coef, lk0, f1, Z)
 | 
| 
 | 
  3928 double *coef, *lk0, *f1, *Z;
 | 
| 
 | 
  3929 { int i, ii, j, p;
 | 
| 
 | 
  3930   double lk, ww, link[LLEN], *X;
 | 
| 
 | 
  3931 
 | 
| 
 | 
  3932   if (lf_debug>2) mut_printf("  likereg: %8.5f\n",coef[0]);
 | 
| 
 | 
  3933   lf_status = LF_OK;
 | 
| 
 | 
  3934   lk = 0.0; p = lf_des->p;
 | 
| 
 | 
  3935   setzero(Z,p*p);
 | 
| 
 | 
  3936   setzero(f1,p);
 | 
| 
 | 
  3937   for (i=0; i<lf_des->n; i++)
 | 
| 
 | 
  3938   {
 | 
| 
 | 
  3939     ii = lf_des->ind[i];
 | 
| 
 | 
  3940     X = d_xi(lf_des,ii);
 | 
| 
 | 
  3941     fitv(lf_des,ii) = base(lf_lfd,ii)+innerprod(coef,X,p);
 | 
| 
 | 
  3942     lf_status = stdlinks(link,lf_lfd,lf_sp,ii,fitv(lf_des,ii),robscale);
 | 
| 
 | 
  3943     if (lf_status == LF_BADP)
 | 
| 
 | 
  3944     { *lk0 = -1.0e300;
 | 
| 
 | 
  3945       return(NR_REDUCE);
 | 
| 
 | 
  3946     }
 | 
| 
 | 
  3947     if (lf_error) lf_status = LF_ERR;
 | 
| 
 | 
  3948     if (lf_status != LF_OK) return(NR_BREAK);
 | 
| 
 | 
  3949 
 | 
| 
 | 
  3950     ww = wght(lf_des,ii);
 | 
| 
 | 
  3951     lk += ww*link[ZLIK];
 | 
| 
 | 
  3952     for (j=0; j<p; j++)
 | 
| 
 | 
  3953       f1[j] += X[j]*ww*link[ZDLL];
 | 
| 
 | 
  3954     addouter(Z, X, X, p, ww*link[ZDDLL]);
 | 
| 
 | 
  3955   }
 | 
| 
 | 
  3956   for (i=0; i<p; i++) if (lf_des->fix[i])
 | 
| 
 | 
  3957   { for (j=0; j<p; j++) Z[i*p+j] = Z[j*p+i] = 0.0;
 | 
| 
 | 
  3958     Z[i*p+i] = 1.0;
 | 
| 
 | 
  3959     f1[i] = 0.0;
 | 
| 
 | 
  3960   }
 | 
| 
 | 
  3961 
 | 
| 
 | 
  3962   if (lf_debug>4) prresp(coef,Z,p);
 | 
| 
 | 
  3963   if (lf_debug>3) mut_printf("  likelihood: %8.5f\n",lk);
 | 
| 
 | 
  3964   *lk0 = lf_des->llk = lk;
 | 
| 
 | 
  3965 
 | 
| 
 | 
  3966   lf_status = fami(lf_sp)->pcheck(lf_sp,lf_des,lf_lfd);
 | 
| 
 | 
  3967   switch(lf_status)
 | 
| 
 | 
  3968   { case LF_DONE: return(NR_BREAK);
 | 
| 
 | 
  3969     case LF_OOB:  return(NR_REDUCE);
 | 
| 
 | 
  3970     case LF_PF:   return(NR_REDUCE);
 | 
| 
 | 
  3971     case LF_NSLN: return(NR_BREAK);
 | 
| 
 | 
  3972   }
 | 
| 
 | 
  3973 
 | 
| 
 | 
  3974   return(NR_OK);
 | 
| 
 | 
  3975 }
 | 
| 
 | 
  3976 
 | 
| 
 | 
  3977 int reginit(lfd,des,sp)
 | 
| 
 | 
  3978 lfdata *lfd;
 | 
| 
 | 
  3979 design *des;
 | 
| 
 | 
  3980 smpar *sp;
 | 
| 
 | 
  3981 { int i, ii;
 | 
| 
 | 
  3982   double sb, link[LLEN];
 | 
| 
 | 
  3983   s0 = s1 = sb = 0;
 | 
| 
 | 
  3984   for (i=0; i<des->n; i++)
 | 
| 
 | 
  3985   { ii = des->ind[i];
 | 
| 
 | 
  3986     links(base(lfd,ii),resp(lfd,ii),fami(sp),LINIT,link,cens(lfd,ii),prwt(lfd,ii),1.0);
 | 
| 
 | 
  3987     s1 += wght(des,ii)*link[ZDLL];
 | 
| 
 | 
  3988     s0 += wght(des,ii)*prwt(lfd,ii);
 | 
| 
 | 
  3989     sb += wght(des,ii)*prwt(lfd,ii)*base(lfd,ii);
 | 
| 
 | 
  3990   }
 | 
| 
 | 
  3991   if (s0==0) return(LF_NOPT); /* no observations with W>0 */
 | 
| 
 | 
  3992   setzero(des->cf,des->p);
 | 
| 
 | 
  3993   lf_tol = 1.0e-6*s0;
 | 
| 
 | 
  3994   switch(link(sp))
 | 
| 
 | 
  3995   { case LIDENT:
 | 
| 
 | 
  3996       des->cf[0] = (s1-sb)/s0;
 | 
| 
 | 
  3997       return(LF_OK);
 | 
| 
 | 
  3998     case LLOG:
 | 
| 
 | 
  3999       if (s1<=0.0)
 | 
| 
 | 
  4000       { des->cf[0] = -1000;
 | 
| 
 | 
  4001         return(LF_INFA);
 | 
| 
 | 
  4002       }
 | 
| 
 | 
  4003       des->cf[0] = log(s1/s0) - sb/s0;
 | 
| 
 | 
  4004       return(LF_OK);
 | 
| 
 | 
  4005     case LLOGIT:
 | 
| 
 | 
  4006       if (s1<=0.0)
 | 
| 
 | 
  4007       { des->cf[0] = -1000;
 | 
| 
 | 
  4008         return(LF_INFA);
 | 
| 
 | 
  4009       }
 | 
| 
 | 
  4010       if (s1>=s0)
 | 
| 
 | 
  4011       { des->cf[0] = 1000;
 | 
| 
 | 
  4012         return(LF_INFA);
 | 
| 
 | 
  4013       }
 | 
| 
 | 
  4014       des->cf[0] = logit(s1/s0)-sb/s0;
 | 
| 
 | 
  4015       return(LF_OK);
 | 
| 
 | 
  4016     case LINVER:
 | 
| 
 | 
  4017       if (s1<=0.0)
 | 
| 
 | 
  4018       { des->cf[0] = 1e100;
 | 
| 
 | 
  4019         return(LF_INFA);
 | 
| 
 | 
  4020       }
 | 
| 
 | 
  4021       des->cf[0] = s0/s1-sb/s0;
 | 
| 
 | 
  4022       return(LF_OK);
 | 
| 
 | 
  4023     case LSQRT:
 | 
| 
 | 
  4024       des->cf[0] = sqrt(s1/s0)-sb/s0;
 | 
| 
 | 
  4025       return(LF_OK);
 | 
| 
 | 
  4026     case LASIN:
 | 
| 
 | 
  4027       des->cf[0] = asin(sqrt(s1/s0))-sb/s0;
 | 
| 
 | 
  4028       return(LF_OK);
 | 
| 
 | 
  4029     default:
 | 
| 
 | 
  4030       LERR(("reginit: invalid link %d",link(sp)));
 | 
| 
 | 
  4031       return(LF_ERR);
 | 
| 
 | 
  4032   }
 | 
| 
 | 
  4033 }
 | 
| 
 | 
  4034 
 | 
| 
 | 
  4035 int lfinit(lfd,sp,des)
 | 
| 
 | 
  4036 lfdata *lfd;
 | 
| 
 | 
  4037 smpar *sp;
 | 
| 
 | 
  4038 design *des;
 | 
| 
 | 
  4039 { int initstat;
 | 
| 
 | 
  4040   des->xtwx.sm = (deg0(sp)<deg(sp)) ? JAC_CHOL : JAC_EIGD;
 | 
| 
 | 
  4041 
 | 
| 
 | 
  4042   designmatrix(lfd,sp,des);
 | 
| 
 | 
  4043   setfamily(sp);
 | 
| 
 | 
  4044   initstat = fami(sp)->initial(lfd,des,sp);
 | 
| 
 | 
  4045 
 | 
| 
 | 
  4046   return(initstat);
 | 
| 
 | 
  4047 }
 | 
| 
 | 
  4048 
 | 
| 
 | 
  4049 void lfiter(lfd,sp,des,maxit)
 | 
| 
 | 
  4050 lfdata *lfd;
 | 
| 
 | 
  4051 smpar *sp;
 | 
| 
 | 
  4052 design *des;
 | 
| 
 | 
  4053 int maxit;
 | 
| 
 | 
  4054 { int err;
 | 
| 
 | 
  4055   if (lf_debug>1) mut_printf(" lfiter: %8.5f\n",des->cf[0]);
 | 
| 
 | 
  4056 
 | 
| 
 | 
  4057   lf_des = des;
 | 
| 
 | 
  4058   lf_lfd = lfd;
 | 
| 
 | 
  4059   lf_sp  = sp;
 | 
| 
 | 
  4060 
 | 
| 
 | 
  4061   max_nr(fami(sp)->like, des->cf, des->oc, des->res, des->f1,
 | 
| 
 | 
  4062     &des->xtwx, des->p, maxit, lf_tol, &err);
 | 
| 
 | 
  4063   switch(err)
 | 
| 
 | 
  4064   { case NR_OK: return;
 | 
| 
 | 
  4065     case NR_NCON:
 | 
| 
 | 
  4066       WARN(("max_nr not converged"));
 | 
| 
 | 
  4067       return;
 | 
| 
 | 
  4068     case NR_NDIV:
 | 
| 
 | 
  4069       WARN(("max_nr reduction problem"));
 | 
| 
 | 
  4070       return;
 | 
| 
 | 
  4071   }
 | 
| 
 | 
  4072   WARN(("max_nr return status %d",err));
 | 
| 
 | 
  4073 }
 | 
| 
 | 
  4074 
 | 
| 
 | 
  4075 int use_robust_scale(int tg)
 | 
| 
 | 
  4076 { if ((tg&64)==0) return(0); /* not quasi - no scale */
 | 
| 
 | 
  4077   if (((tg&128)==0) & (((tg&63)!=TROBT) & ((tg&63)!=TCAUC))) return(0);
 | 
| 
 | 
  4078   return(1);
 | 
| 
 | 
  4079 }
 | 
| 
 | 
  4080 
 | 
| 
 | 
  4081 /*
 | 
| 
 | 
  4082  * noit not really needed any more, since
 | 
| 
 | 
  4083  * gauss->pcheck returns LF_DONE, and likereg NR_BREAK
 | 
| 
 | 
  4084  * in gaussian case.
 | 
| 
 | 
  4085  * nb: 0/1: does local neighborhood and weights need computing?
 | 
| 
 | 
  4086  * cv: 0/1: is variance/covariance matrix needed?
 | 
| 
 | 
  4087  */
 | 
| 
 | 
  4088 int locfit(lfd,des,sp,noit,nb,cv)
 | 
| 
 | 
  4089 lfdata *lfd;
 | 
| 
 | 
  4090 design *des;
 | 
| 
 | 
  4091 smpar *sp;
 | 
| 
 | 
  4092 int noit, nb, cv;
 | 
| 
 | 
  4093 { int i;
 | 
| 
 | 
  4094 
 | 
| 
 | 
  4095   if (des->xev==NULL)
 | 
| 
 | 
  4096   { LERR(("locfit: NULL evaluation point?"));
 | 
| 
 | 
  4097     return(246);
 | 
| 
 | 
  4098   }
 | 
| 
 | 
  4099 
 | 
| 
 | 
  4100   if (lf_debug>0)
 | 
| 
 | 
  4101   { mut_printf("locfit: ");
 | 
| 
 | 
  4102     for (i=0; i<lfd->d; i++) mut_printf(" %10.6f",des->xev[i]);
 | 
| 
 | 
  4103     mut_printf("\n");
 | 
| 
 | 
  4104   }
 | 
| 
 | 
  4105 
 | 
| 
 | 
  4106 /* the 1e-12 avoids problems that can occur with roundoff */
 | 
| 
 | 
  4107   if (nb) nbhd(lfd,des,(int)(lfd->n*nn(sp)+1e-12),0,sp);
 | 
| 
 | 
  4108 
 | 
| 
 | 
  4109   lf_status = lfinit(lfd,sp,des);
 | 
| 
 | 
  4110 
 | 
| 
 | 
  4111   if (lf_status == LF_OK)
 | 
| 
 | 
  4112   { if (use_robust_scale(fam(sp)))
 | 
| 
 | 
  4113       lf_robust(lfd,sp,des,lf_maxit);
 | 
| 
 | 
  4114     else
 | 
| 
 | 
  4115     { if ((fam(sp)&63)==TQUANT)
 | 
| 
 | 
  4116         lfquantile(lfd,sp,des,lf_maxit);
 | 
| 
 | 
  4117       else
 | 
| 
 | 
  4118       { robscale = 1.0;
 | 
| 
 | 
  4119         lfiter(lfd,sp,des,lf_maxit);
 | 
| 
 | 
  4120       }
 | 
| 
 | 
  4121     }
 | 
| 
 | 
  4122   }
 | 
| 
 | 
  4123 
 | 
| 
 | 
  4124   if (lf_status == LF_DONE) lf_status = LF_OK;
 | 
| 
 | 
  4125   if (lf_status == LF_OOB) lf_status = LF_OK;
 | 
| 
 | 
  4126 
 | 
| 
 | 
  4127   if ((fam(sp)&63)==TDEN) /* convert from rate to density */
 | 
| 
 | 
  4128   { switch(link(sp))
 | 
| 
 | 
  4129     { case LLOG:
 | 
| 
 | 
  4130         des->cf[0] -= log(des->smwt);
 | 
| 
 | 
  4131         break;
 | 
| 
 | 
  4132       case LIDENT:
 | 
| 
 | 
  4133         multmatscal(des->cf,1.0/des->smwt,des->p);
 | 
| 
 | 
  4134         break;
 | 
| 
 | 
  4135       default: LERR(("Density adjustment; invalid link"));
 | 
| 
 | 
  4136     }
 | 
| 
 | 
  4137   }
 | 
| 
 | 
  4138 
 | 
| 
 | 
  4139   /* variance calculations, if requested */
 | 
| 
 | 
  4140   if (cv)
 | 
| 
 | 
  4141   { switch(lf_status)
 | 
| 
 | 
  4142     { case LF_PF:  /* for these cases, variance calc. would likely fail. */
 | 
| 
 | 
  4143       case LF_NOPT:
 | 
| 
 | 
  4144       case LF_NSLN:
 | 
| 
 | 
  4145       case LF_INFA:
 | 
| 
 | 
  4146       case LF_DEMP:
 | 
| 
 | 
  4147       case LF_XOOR:
 | 
| 
 | 
  4148       case LF_DNOP:
 | 
| 
 | 
  4149       case LF_BADP:
 | 
| 
 | 
  4150         des->llk = des->tr0 = des->tr1 = des->tr2 = 0.0;
 | 
| 
 | 
  4151         setzero(des->V,des->p*des->p);
 | 
| 
 | 
  4152         setzero(des->f1,des->p);
 | 
| 
 | 
  4153         break;
 | 
| 
 | 
  4154       default: lf_vcov(lfd,sp,des);
 | 
| 
 | 
  4155     }
 | 
| 
 | 
  4156   }
 | 
| 
 | 
  4157 
 | 
| 
 | 
  4158   return(lf_status);
 | 
| 
 | 
  4159 }
 | 
| 
 | 
  4160 
 | 
| 
 | 
  4161 void lf_status_msg(status)
 | 
| 
 | 
  4162 int status;
 | 
| 
 | 
  4163 { switch(status)
 | 
| 
 | 
  4164 { case LF_OK: return;
 | 
| 
 | 
  4165   case LF_NCON: WARN(("locfit did not converge")); return;
 | 
| 
 | 
  4166   case LF_OOB: WARN(("parameters out of bounds")); return;
 | 
| 
 | 
  4167   case LF_PF: WARN(("perfect fit")); return;
 | 
| 
 | 
  4168   case LF_NOPT: WARN(("no points with non-zero weight")); return;
 | 
| 
 | 
  4169   case LF_NSLN: WARN(("no solution")); return;
 | 
| 
 | 
  4170   case LF_INFA: WARN(("initial value problem")); return;
 | 
| 
 | 
  4171   case LF_DEMP: WARN(("density estimate, empty integration region")); return;
 | 
| 
 | 
  4172   case LF_XOOR: WARN(("procv: fit point outside xlim region")); return;
 | 
| 
 | 
  4173   case LF_DNOP: WARN(("density estimation -- insufficient points in smoothing window")); return;
 | 
| 
 | 
  4174   case LF_BADP: WARN(("bad parameters")); return;
 | 
| 
 | 
  4175   default: WARN(("procv: unknown return code %d",status)); return;
 | 
| 
 | 
  4176 } }
 | 
| 
 | 
  4177 /*
 | 
| 
 | 
  4178  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  4179  */
 | 
| 
 | 
  4180 /*
 | 
| 
 | 
  4181  *   Compute minimax weights for local regression.
 | 
| 
 | 
  4182  */
 | 
| 
 | 
  4183 
 | 
| 
 | 
  4184 #include "locf.h"
 | 
| 
 | 
  4185 #define NR_EMPTY 834
 | 
| 
 | 
  4186 
 | 
| 
 | 
  4187 int mmsm_ct;
 | 
| 
 | 
  4188 
 | 
| 
 | 
  4189 static int debug=0;
 | 
| 
 | 
  4190 #define CONVTOL 1.0e-8
 | 
| 
 | 
  4191 #define SINGTOL 1.0e-10
 | 
| 
 | 
  4192 #define NR_SINGULAR 100
 | 
| 
 | 
  4193 
 | 
| 
 | 
  4194 static lfdata *mm_lfd;
 | 
| 
 | 
  4195 static design *mm_des;
 | 
| 
 | 
  4196 static double mm_gam, mmf, lb;
 | 
| 
 | 
  4197 static int st;
 | 
| 
 | 
  4198 
 | 
| 
 | 
  4199 double ipower(x,n) /* use for n not too large!! */
 | 
| 
 | 
  4200 double x;
 | 
| 
 | 
  4201 int n;
 | 
| 
 | 
  4202 { if (n==0) return(1.0);
 | 
| 
 | 
  4203   if (n<0) return(1/ipower(x,-n));
 | 
| 
 | 
  4204   return(x*ipower(x,n-1));
 | 
| 
 | 
  4205 }
 | 
| 
 | 
  4206 
 | 
| 
 | 
  4207 double setmmwt(des,a,gam)
 | 
| 
 | 
  4208 design *des;
 | 
| 
 | 
  4209 double *a, gam;
 | 
| 
 | 
  4210 { double ip, w0, w1, sw, wt;
 | 
| 
 | 
  4211   int i;
 | 
| 
 | 
  4212   sw = 0.0;
 | 
| 
 | 
  4213   for (i=0; i<mm_lfd->n; i++)
 | 
| 
 | 
  4214   { ip = innerprod(a,d_xi(des,i),des->p);
 | 
| 
 | 
  4215     wt = prwt(mm_lfd,i);
 | 
| 
 | 
  4216     w0 = ip - gam*des->wd[i];
 | 
| 
 | 
  4217     w1 = ip + gam*des->wd[i];
 | 
| 
 | 
  4218     wght(des,i) = 0.0;
 | 
| 
 | 
  4219     if (w0>0) { wght(des,i) = w0; sw += wt*w0*w0; }
 | 
| 
 | 
  4220     if (w1<0) { wght(des,i) = w1; sw += wt*w1*w1; }
 | 
| 
 | 
  4221   }
 | 
| 
 | 
  4222   return(sw/2-a[0]);
 | 
| 
 | 
  4223 }
 | 
| 
 | 
  4224 
 | 
| 
 | 
  4225 /* compute sum_{w!=0} AA^T; e1-sum wA  */
 | 
| 
 | 
  4226 int mmsums(des,coef,f,z,J)
 | 
| 
 | 
  4227 design *des;
 | 
| 
 | 
  4228 double *coef, *f, *z;
 | 
| 
 | 
  4229 jacobian *J;
 | 
| 
 | 
  4230 { int ct, i, j, p, sing;
 | 
| 
 | 
  4231   double *A;
 | 
| 
 | 
  4232 
 | 
| 
 | 
  4233 mmsm_ct++;
 | 
| 
 | 
  4234   A = J->Z;
 | 
| 
 | 
  4235   *f = setmmwt(des,coef,mm_gam);
 | 
| 
 | 
  4236 
 | 
| 
 | 
  4237   p = des->p;
 | 
| 
 | 
  4238   setzero(A,p*p);
 | 
| 
 | 
  4239   setzero(z,p);
 | 
| 
 | 
  4240   z[0] = 1.0;
 | 
| 
 | 
  4241   ct = 0;
 | 
| 
 | 
  4242 
 | 
| 
 | 
  4243   for (i=0; i<mm_lfd->n; i++)
 | 
| 
 | 
  4244     if (wght(des,i)!=0.0)
 | 
| 
 | 
  4245     { addouter(A,d_xi(des,i),d_xi(des,i),p,prwt(mm_lfd,i));
 | 
| 
 | 
  4246       for (j=0; j<p; j++) z[j] -= prwt(mm_lfd,i)*wght(des,i)*d_xij(des,i,j);
 | 
| 
 | 
  4247       ct++;
 | 
| 
 | 
  4248     }
 | 
| 
 | 
  4249   if (ct==0) return(NR_EMPTY);
 | 
| 
 | 
  4250 
 | 
| 
 | 
  4251   J->st = JAC_RAW;
 | 
| 
 | 
  4252   J->p = p;
 | 
| 
 | 
  4253   jacob_dec(J,JAC_EIGD);
 | 
| 
 | 
  4254 
 | 
| 
 | 
  4255   sing = 0;
 | 
| 
 | 
  4256   for (i=0; i<p; i++) sing |= (J->Z[i*p+i]<SINGTOL);
 | 
| 
 | 
  4257   if ((debug) & (sing)) mut_printf("SINGULAR!!!!\n");
 | 
| 
 | 
  4258 
 | 
| 
 | 
  4259   return((sing) ? NR_SINGULAR : NR_OK);
 | 
| 
 | 
  4260 }
 | 
| 
 | 
  4261 
 | 
| 
 | 
  4262 int descenddir(des,coef,dlt,f,af)
 | 
| 
 | 
  4263 design *des;
 | 
| 
 | 
  4264 double *coef, *dlt, *f;
 | 
| 
 | 
  4265 int af;
 | 
| 
 | 
  4266 { int i, p;
 | 
| 
 | 
  4267   double f0, *oc;
 | 
| 
 | 
  4268 
 | 
| 
 | 
  4269   if (debug) mut_printf("descenddir: %8.5f %8.5f\n",dlt[0],dlt[1]);
 | 
| 
 | 
  4270 
 | 
| 
 | 
  4271   f0 = *f;
 | 
| 
 | 
  4272   oc = des->oc;
 | 
| 
 | 
  4273   p = des->p;
 | 
| 
 | 
  4274   memcpy(oc,coef,p*sizeof(double));
 | 
| 
 | 
  4275 
 | 
| 
 | 
  4276   for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
 | 
| 
 | 
  4277   st = mmsums(des,coef,f,des->f1,&des->xtwx);
 | 
| 
 | 
  4278 
 | 
| 
 | 
  4279   if (*f>f0) /* halve till we drop */
 | 
| 
 | 
  4280   { while (*f>f0)
 | 
| 
 | 
  4281     { lb = lb/2.0;
 | 
| 
 | 
  4282       for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
 | 
| 
 | 
  4283       st = mmsums(des,coef,f,des->f1,&des->xtwx);
 | 
| 
 | 
  4284     }
 | 
| 
 | 
  4285     return(st);
 | 
| 
 | 
  4286   }
 | 
| 
 | 
  4287 
 | 
| 
 | 
  4288   if (!af) return(st);
 | 
| 
 | 
  4289 
 | 
| 
 | 
  4290   /* double */
 | 
| 
 | 
  4291   while (*f<f0)
 | 
| 
 | 
  4292   { f0 = *f;
 | 
| 
 | 
  4293     lb *= 2.0;
 | 
| 
 | 
  4294     for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
 | 
| 
 | 
  4295     st = mmsums(des,coef,f,des->f1,&des->xtwx);
 | 
| 
 | 
  4296   }
 | 
| 
 | 
  4297 
 | 
| 
 | 
  4298   lb /= 2.0;
 | 
| 
 | 
  4299   for (i=0; i<p; i++) coef[i] = oc[i]+lb*dlt[i];
 | 
| 
 | 
  4300   st = mmsums(des,coef,f,des->f1,&des->xtwx);
 | 
| 
 | 
  4301 
 | 
| 
 | 
  4302   return(st);
 | 
| 
 | 
  4303 }
 | 
| 
 | 
  4304 
 | 
| 
 | 
  4305 int mm_initial(des)
 | 
| 
 | 
  4306 design *des;
 | 
| 
 | 
  4307 { double *dlt;
 | 
| 
 | 
  4308 
 | 
| 
 | 
  4309   dlt = des->ss;
 | 
| 
 | 
  4310 
 | 
| 
 | 
  4311   setzero(des->cf,des->p);
 | 
| 
 | 
  4312   st = mmsums(des,des->cf,&mmf,des->f1,&des->xtwx);
 | 
| 
 | 
  4313 
 | 
| 
 | 
  4314   setzero(dlt,des->p);
 | 
| 
 | 
  4315   dlt[0] = 1;
 | 
| 
 | 
  4316   lb = 1.0;
 | 
| 
 | 
  4317   st = descenddir(des,des->cf,dlt,&mmf,1);
 | 
| 
 | 
  4318   return(st);
 | 
| 
 | 
  4319 }
 | 
| 
 | 
  4320 
 | 
| 
 | 
  4321 void getsingdir(des,dlt)
 | 
| 
 | 
  4322 design *des;
 | 
| 
 | 
  4323 double *dlt;
 | 
| 
 | 
  4324 { double f, sw, c0;
 | 
| 
 | 
  4325   int i, j, p, sd;
 | 
| 
 | 
  4326 
 | 
| 
 | 
  4327   sd = -1; p = des->p;
 | 
| 
 | 
  4328   setzero(dlt,p);
 | 
| 
 | 
  4329   for (i=0; i<p; i++) if (des->xtwx.Z[i*p+i]<SINGTOL) sd = i;
 | 
| 
 | 
  4330   if (sd==-1)
 | 
| 
 | 
  4331   { mut_printf("getsingdir: nonsing?\n");
 | 
| 
 | 
  4332     return;
 | 
| 
 | 
  4333   }
 | 
| 
 | 
  4334   if (des->xtwx.dg[sd]>0)
 | 
| 
 | 
  4335     for (i=0; i<p; i++) dlt[i] = des->xtwx.Q[p*i+sd]*des->xtwx.dg[i];
 | 
| 
 | 
  4336   else
 | 
| 
 | 
  4337   { dlt[sd] = 1.0;
 | 
| 
 | 
  4338   }
 | 
| 
 | 
  4339 
 | 
| 
 | 
  4340   c0 = innerprod(dlt,des->f1,p);
 | 
| 
 | 
  4341   if (c0<0) for (i=0; i<p; i++) dlt[i] = -dlt[i];
 | 
| 
 | 
  4342 }
 | 
| 
 | 
  4343 
 | 
| 
 | 
  4344 void mmax(coef, old_coef, delta, J, p, maxit, tol, err)
 | 
| 
 | 
  4345 double *coef, *old_coef, *delta, tol;
 | 
| 
 | 
  4346 int p, maxit, *err;
 | 
| 
 | 
  4347 jacobian *J;
 | 
| 
 | 
  4348 { double old_f, lambda;
 | 
| 
 | 
  4349   int i, j;
 | 
| 
 | 
  4350 
 | 
| 
 | 
  4351   *err = NR_OK;
 | 
| 
 | 
  4352  
 | 
| 
 | 
  4353   for (j=0; j<maxit; j++)
 | 
| 
 | 
  4354   { memcpy(old_coef,coef,p*sizeof(double));
 | 
| 
 | 
  4355     old_f = mmf;
 | 
| 
 | 
  4356 
 | 
| 
 | 
  4357     if (st == NR_SINGULAR)
 | 
| 
 | 
  4358     {
 | 
| 
 | 
  4359       getsingdir(mm_des,delta);
 | 
| 
 | 
  4360       st = descenddir(mm_des,coef,delta,&mmf,1);
 | 
| 
 | 
  4361     }
 | 
| 
 | 
  4362     if (st == NR_EMPTY)
 | 
| 
 | 
  4363     { 
 | 
| 
 | 
  4364       setzero(delta,p);
 | 
| 
 | 
  4365       delta[0] = 1.0;
 | 
| 
 | 
  4366       st = descenddir(mm_des,coef,delta,&mmf,1);
 | 
| 
 | 
  4367     }
 | 
| 
 | 
  4368     if (st == NR_OK)
 | 
| 
 | 
  4369     { 
 | 
| 
 | 
  4370       lb = 1.0;
 | 
| 
 | 
  4371       jacob_solve(J,mm_des->f1);
 | 
| 
 | 
  4372       memcpy(delta,mm_des->f1,p*sizeof(double));
 | 
| 
 | 
  4373       st = descenddir(mm_des,coef,delta,&mmf,0);
 | 
| 
 | 
  4374     }
 | 
| 
 | 
  4375 
 | 
| 
 | 
  4376     if ((j>0) & (fabs(mmf-old_f)<tol)) return;
 | 
| 
 | 
  4377   }
 | 
| 
 | 
  4378   WARN(("findab not converged"));
 | 
| 
 | 
  4379   *err = NR_NCON;
 | 
| 
 | 
  4380   return;
 | 
| 
 | 
  4381 }
 | 
| 
 | 
  4382 
 | 
| 
 | 
  4383 double findab(gam)
 | 
| 
 | 
  4384 double gam;
 | 
| 
 | 
  4385 { double sl;
 | 
| 
 | 
  4386   int i, p, nr_stat;
 | 
| 
 | 
  4387 
 | 
| 
 | 
  4388   if (debug) mut_printf("  findab: gam %8.5f\n",gam);
 | 
| 
 | 
  4389   mm_gam = gam;
 | 
| 
 | 
  4390   p = mm_des->p;
 | 
| 
 | 
  4391   lb = 1.0;
 | 
| 
 | 
  4392   st = mm_initial(mm_des);
 | 
| 
 | 
  4393 
 | 
| 
 | 
  4394     mmax(mm_des->cf, mm_des->oc, mm_des->ss,
 | 
| 
 | 
  4395        &mm_des->xtwx, p, lf_maxit, CONVTOL, &nr_stat);
 | 
| 
 | 
  4396 
 | 
| 
 | 
  4397   sl = 0.0;
 | 
| 
 | 
  4398   for (i=0; i<mm_lfd->n; i++) sl += fabs(wght(mm_des,i))*mm_des->wd[i];
 | 
| 
 | 
  4399 
 | 
| 
 | 
  4400   if (debug) mut_printf("  sl %8.5f  gam %8.5f    %8.5f %d\n", sl,gam,sl-gam,nr_stat);
 | 
| 
 | 
  4401   return(sl-gam);
 | 
| 
 | 
  4402 }
 | 
| 
 | 
  4403 
 | 
| 
 | 
  4404 double weightmm(coef,di,ff,gam)
 | 
| 
 | 
  4405 double *coef, di, *ff, gam;
 | 
| 
 | 
  4406 { double y1, y2, ip;
 | 
| 
 | 
  4407   ip = innerprod(ff,coef,mm_des->p);
 | 
| 
 | 
  4408   y1 = ip-gam*di; if (y1>0) return(y1/ip);
 | 
| 
 | 
  4409   y2 = ip+gam*di; if (y2<0) return(y2/ip);
 | 
| 
 | 
  4410   return(0.0);
 | 
| 
 | 
  4411 }
 | 
| 
 | 
  4412 
 | 
| 
 | 
  4413 double minmax(lfd,des,sp)
 | 
| 
 | 
  4414 lfdata *lfd;
 | 
| 
 | 
  4415 design *des;
 | 
| 
 | 
  4416 smpar *sp;
 | 
| 
 | 
  4417 { double h, u[MXDIM], gam;
 | 
| 
 | 
  4418   int i, j, m, d1, p1, err_flag;
 | 
| 
 | 
  4419 
 | 
| 
 | 
  4420   if (debug) mut_printf("minmax: x %8.5f\n",des->xev[0]);
 | 
| 
 | 
  4421   mm_lfd = lfd;
 | 
| 
 | 
  4422   mm_des = des;
 | 
| 
 | 
  4423 
 | 
| 
 | 
  4424 mmsm_ct = 0;
 | 
| 
 | 
  4425   d1 = deg(sp)+1;
 | 
| 
 | 
  4426   p1 = factorial(d1);
 | 
| 
 | 
  4427   for (i=0; i<lfd->n; i++)
 | 
| 
 | 
  4428   { for (j=0; j<lfd->d; j++) u[j] = datum(lfd,j,i);
 | 
| 
 | 
  4429     des->wd[i] = sp->nn/p1*ipower(dist(des,i),d1);
 | 
| 
 | 
  4430     des->ind[i] = i;
 | 
| 
 | 
  4431     fitfun(lfd, sp, u,des->xev,d_xi(des,i),NULL);
 | 
| 
 | 
  4432   }
 | 
| 
 | 
  4433 
 | 
| 
 | 
  4434 /* find gamma (i.e. solve eqn 13.17 from book), using the secant method.
 | 
| 
 | 
  4435  * As a side effect, this finds the other minimax coefficients.
 | 
| 
 | 
  4436  * Note that 13.17 is rewritten as
 | 
| 
 | 
  4437  *   g2 = sum |l_i(x)| (||xi-x||^(p+1) M/(s*(p+1)!))
 | 
| 
 | 
  4438  * where g2 = gamma * s * (p+1)! / M. The gam variable below is g2.
 | 
| 
 | 
  4439  * The smoothing parameter is sp->nn == M/s.
 | 
| 
 | 
  4440  */
 | 
| 
 | 
  4441   gam = solve_secant(findab, 0.0, 0.0,1.0, 0.0000001, BDF_EXPRIGHT, &err_flag);
 | 
| 
 | 
  4442 
 | 
| 
 | 
  4443 /*
 | 
| 
 | 
  4444  * Set the smoothing weights, in preparation for the actual fit.
 | 
| 
 | 
  4445  */
 | 
| 
 | 
  4446   h = 0.0; m = 0;
 | 
| 
 | 
  4447   for (i=0; i<lfd->n; i++)
 | 
| 
 | 
  4448   { wght(des,i) = weightmm(des->cf, des->wd[i],d_xi(des,i),gam);
 | 
| 
 | 
  4449     if (wght(des,i)>0)
 | 
| 
 | 
  4450     { if (dist(des,i)>h) h = dist(des,i);
 | 
| 
 | 
  4451       des->ind[m] = i;
 | 
| 
 | 
  4452       m++;
 | 
| 
 | 
  4453     }
 | 
| 
 | 
  4454   }
 | 
| 
 | 
  4455   des->n = m;
 | 
| 
 | 
  4456   return(h);
 | 
| 
 | 
  4457 }
 | 
| 
 | 
  4458 /*
 | 
| 
 | 
  4459  * Copyright 1996-2006 Catherine Loader.
 | 
| 
 | 
  4460  */
 | 
| 
 | 
  4461 /*
 | 
| 
 | 
  4462  *
 | 
| 
 | 
  4463  *  Defines the weight functions and related quantities used
 | 
| 
 | 
  4464  *  in LOCFIT.
 | 
| 
 | 
  4465  */
 | 
| 
 | 
  4466 
 | 
| 
 | 
  4467 #include "locf.h"
 | 
| 
 | 
  4468 
 | 
| 
 | 
  4469 /*
 | 
| 
 | 
  4470  * convert kernel and kernel type strings to numeric codes.
 | 
| 
 | 
  4471  */
 | 
| 
 | 
  4472 #define NWFUNS 13
 | 
| 
 | 
  4473 static char *wfuns[NWFUNS] = {
 | 
| 
 | 
  4474   "rectangular", "epanechnikov", "bisquare",    "tricube",
 | 
| 
 | 
  4475   "triweight",   "gaussian",     "triangular",  "ququ",
 | 
| 
 | 
  4476   "6cub",        "minimax",      "exponential", "maclean", "parametric" };
 | 
| 
 | 
  4477 static int wvals[NWFUNS] = { WRECT, WEPAN, WBISQ, WTCUB,
 | 
| 
 | 
  4478   WTRWT, WGAUS, WTRIA, WQUQU, W6CUB, WMINM, WEXPL, WMACL, WPARM };
 | 
| 
 | 
  4479 int lfkernel(char *z)
 | 
| 
 | 
  4480 { return(pmatch(z, wfuns, wvals, NWFUNS, WTCUB));
 | 
| 
 | 
  4481 }
 | 
| 
 | 
  4482 
 | 
| 
 | 
  4483 #define NKTYPE 5
 | 
| 
 | 
  4484 static char *ktype[NKTYPE] = { "spherical", "product", "center", "lm", "zeon" };
 | 
| 
 | 
  4485 static int   kvals[NKTYPE] = { KSPH, KPROD, KCE, KLM, KZEON };
 | 
| 
 | 
  4486 int lfketype(char *z)
 | 
| 
 | 
  4487 { return(pmatch(z, ktype, kvals, NKTYPE, KSPH));
 | 
| 
 | 
  4488 }
 | 
| 
 | 
  4489 
 | 
| 
 | 
  4490 /* The weight functions themselves.  Used everywhere. */
 | 
| 
 | 
  4491 double W(u,ker)
 | 
| 
 | 
  4492 double u;
 | 
| 
 | 
  4493 int ker;
 | 
| 
 | 
  4494 { u = fabs(u);
 | 
| 
 | 
  4495   switch(ker)
 | 
| 
 | 
  4496   { case WRECT: return((u>1) ? 0.0 : 1.0);
 | 
| 
 | 
  4497     case WEPAN: return((u>1) ? 0.0 : 1-u*u);
 | 
| 
 | 
  4498     case WBISQ: if (u>1) return(0.0);
 | 
| 
 | 
  4499                 u = 1-u*u; return(u*u);
 | 
| 
 | 
  4500     case WTCUB: if (u>1) return(0.0);
 | 
| 
 | 
  4501                 u = 1-u*u*u; return(u*u*u);
 | 
| 
 | 
  4502     case WTRWT: if (u>1) return(0.0);
 | 
| 
 | 
  4503                 u = 1-u*u; return(u*u*u);
 | 
| 
 | 
  4504     case WQUQU: if (u>1) return(0.0);
 | 
| 
 | 
  4505                 u = 1-u*u; return(u*u*u*u);
 | 
| 
 | 
  4506     case WTRIA: if (u>1) return(0.0);
 | 
| 
 | 
  4507                 return(1-u);
 | 
| 
 | 
  4508     case W6CUB: if (u>1) return(0.0);
 | 
| 
 | 
  4509                 u = 1-u*u*u; u = u*u*u; return(u*u);
 | 
| 
 | 
  4510     case WGAUS: return(exp(-SQR(GFACT*u)/2.0));
 | 
| 
 | 
  4511     case WEXPL: return(exp(-EFACT*u));
 | 
| 
 | 
  4512     case WMACL: return(1/((u+1.0e-100)*(u+1.0e-100)));
 | 
| 
 | 
  4513     case WMINM: LERR(("WMINM in W"));
 | 
| 
 | 
  4514                 return(0.0);
 | 
| 
 | 
  4515     case WPARM: return(1.0);
 | 
| 
 | 
  4516   }
 | 
| 
 | 
  4517   LERR(("W(): Unknown kernel %d\n",ker));
 | 
| 
 | 
  4518   return(1.0);
 | 
| 
 | 
  4519 }
 | 
| 
 | 
  4520 
 | 
| 
 | 
  4521 int iscompact(ker)
 | 
| 
 | 
  4522 int ker;
 | 
| 
 | 
  4523 { if ((ker==WEXPL) | (ker==WGAUS) | (ker==WMACL) | (ker==WPARM)) return(0);
 | 
| 
 | 
  4524   return(1);
 | 
| 
 | 
  4525 }
 | 
| 
 | 
  4526 
 | 
| 
 | 
  4527 double weightprod(lfd,u,h,ker)
 | 
| 
 | 
  4528 lfdata *lfd;
 | 
| 
 | 
  4529 double *u, h;
 | 
| 
 | 
  4530 int ker;
 | 
| 
 | 
  4531 { int i;
 | 
| 
 | 
  4532   double sc, w;
 | 
| 
 | 
  4533   w = 1.0;
 | 
| 
 | 
  4534   for (i=0; i<lfd->d; i++)
 | 
| 
 | 
  4535   { sc = lfd->sca[i];
 | 
| 
 | 
  4536     switch(lfd->sty[i])
 | 
| 
 | 
  4537     { case STLEFT:
 | 
| 
 | 
  4538         if (u[i]>0) return(0.0);
 | 
| 
 | 
  4539         w *= W(-u[i]/(h*sc),ker);
 | 
| 
 | 
  4540         break;
 | 
| 
 | 
  4541       case STRIGH:
 | 
| 
 | 
  4542         if (u[i]<0) return(0.0);
 | 
| 
 | 
  4543         w *= W(u[i]/(h*sc),ker);
 | 
| 
 | 
  4544         break;
 | 
| 
 | 
  4545       case STANGL:
 | 
| 
 | 
  4546         w *= W(2*fabs(sin(u[i]/(2*sc)))/h,ker);
 | 
| 
 | 
  4547         break;
 | 
| 
 | 
  4548       case STCPAR:
 | 
| 
 | 
  4549         break;
 | 
| 
 | 
  4550       default:
 | 
| 
 | 
  4551         w *= W(fabs(u[i])/(h*sc),ker);
 | 
| 
 | 
  4552     }
 | 
| 
 | 
  4553     if (w==0.0) return(w);
 | 
| 
 | 
  4554   }
 | 
| 
 | 
  4555   return(w);
 | 
| 
 | 
  4556 }
 | 
| 
 | 
  4557 
 | 
| 
 | 
  4558 double weightsph(lfd,u,h,ker, hasdi,di)
 | 
| 
 | 
  4559 lfdata *lfd;
 | 
| 
 | 
  4560 double *u, h, di;
 | 
| 
 | 
  4561 int ker, hasdi;
 | 
| 
 | 
  4562 { int i;
 | 
| 
 | 
  4563 
 | 
| 
 | 
  4564   if (!hasdi) di = rho(u,lfd->sca,lfd->d,KSPH,lfd->sty);
 | 
| 
 | 
  4565 
 | 
| 
 | 
  4566   for (i=0; i<lfd->d; i++)
 | 
| 
 | 
  4567   { if ((lfd->sty[i]==STLEFT) && (u[i]>0.0)) return(0.0);
 | 
| 
 | 
  4568     if ((lfd->sty[i]==STRIGH) && (u[i]<0.0)) return(0.0);
 | 
| 
 | 
  4569   }
 | 
| 
 | 
  4570   if (h==0) return((di==0.0) ? 1.0 : 0.0);
 | 
| 
 | 
  4571 
 | 
| 
 | 
  4572   return(W(di/h,ker));
 | 
| 
 | 
  4573 }
 | 
| 
 | 
  4574 
 | 
| 
 | 
  4575 double weight(lfd,sp,x,t,h, hasdi,di)
 | 
| 
 | 
  4576 lfdata *lfd;
 | 
| 
 | 
  4577 smpar *sp;
 | 
| 
 | 
  4578 double *x, *t, h, di;
 | 
| 
 | 
  4579 int hasdi;
 | 
| 
 | 
  4580 { double u[MXDIM];
 | 
| 
 | 
  4581   int i;
 | 
| 
 | 
  4582   for (i=0; i<lfd->d; i++) u[i] = (t==NULL) ? x[i] : x[i]-t[i];
 | 
| 
 | 
  4583   switch(kt(sp))
 | 
| 
 | 
  4584   { case KPROD: return(weightprod(lfd,u,h,ker(sp)));
 | 
| 
 | 
  4585     case KSPH:  return(weightsph(lfd,u,h,ker(sp), hasdi,di));
 | 
| 
 | 
  4586   }
 | 
| 
 | 
  4587   LERR(("weight: unknown kernel type %d",kt(sp)));
 | 
| 
 | 
  4588   return(1.0);
 | 
| 
 | 
  4589 }
 | 
| 
 | 
  4590 
 | 
| 
 | 
  4591 double sgn(x)
 | 
| 
 | 
  4592 double x;
 | 
| 
 | 
  4593 { if (x>0) return(1.0);
 | 
| 
 | 
  4594   if (x<0) return(-1.0);
 | 
| 
 | 
  4595   return(0.0);
 | 
| 
 | 
  4596 }
 | 
| 
 | 
  4597 
 | 
| 
 | 
  4598 double WdW(u,ker) /* W'(u)/W(u) */
 | 
| 
 | 
  4599 double u;
 | 
| 
 | 
  4600 int ker;
 | 
| 
 | 
  4601 { double eps=1.0e-10;
 | 
| 
 | 
  4602   if (ker==WGAUS) return(-GFACT*GFACT*u);
 | 
| 
 | 
  4603   if (ker==WPARM) return(0.0);
 | 
| 
 | 
  4604   if (fabs(u)>=1) return(0.0);
 | 
| 
 | 
  4605   switch(ker)
 | 
| 
 | 
  4606   { case WRECT: return(0.0);
 | 
| 
 | 
  4607     case WTRIA: return(-sgn(u)/(1-fabs(u)+eps));
 | 
| 
 | 
  4608     case WEPAN: return(-2*u/(1-u*u+eps));
 | 
| 
 | 
  4609     case WBISQ: return(-4*u/(1-u*u+eps));
 | 
| 
 | 
  4610     case WTRWT: return(-6*u/(1-u*u+eps));
 | 
| 
 | 
  4611     case WTCUB: return(-9*sgn(u)*u*u/(1-u*u*fabs(u)+eps));
 | 
| 
 | 
  4612     case WEXPL: return((u>0) ? -EFACT : EFACT);
 | 
| 
 | 
  4613   }
 | 
| 
 | 
  4614   LERR(("WdW: invalid kernel"));
 | 
| 
 | 
  4615   return(0.0);
 | 
| 
 | 
  4616 }
 | 
| 
 | 
  4617 
 | 
| 
 | 
  4618 /* deriv. weights .. spherical, product etc
 | 
| 
 | 
  4619    u, sc, sty needed only in relevant direction
 | 
| 
 | 
  4620    Acutally, returns (d/dx W(||x||/h) ) / W(.)
 | 
| 
 | 
  4621 */
 | 
| 
 | 
  4622 double weightd(u,sc,d,ker,kt,h,sty,di)
 | 
| 
 | 
  4623 double u, sc, h, di;
 | 
| 
 | 
  4624 int d, ker, kt, sty;
 | 
| 
 | 
  4625 { if (sty==STANGL)
 | 
| 
 | 
  4626   { if (kt==KPROD)
 | 
| 
 | 
  4627       return(-WdW(2*sin(u/(2*sc)),ker)*cos(u/(2*sc))/(h*sc));
 | 
| 
 | 
  4628     if (di==0.0) return(0.0);
 | 
| 
 | 
  4629     return(-WdW(di/h,ker)*sin(u/sc)/(h*sc*di));
 | 
| 
 | 
  4630   }
 | 
| 
 | 
  4631   if (sty==STCPAR) return(0.0);
 | 
| 
 | 
  4632   if (kt==KPROD)
 | 
| 
 | 
  4633     return(-WdW(u/(h*sc),ker)/(h*sc));
 | 
| 
 | 
  4634   if (di==0.0) return(0.0);
 | 
| 
 | 
  4635   return(-WdW(di/h,ker)*u/(h*di*sc*sc));
 | 
| 
 | 
  4636 }
 | 
| 
 | 
  4637 
 | 
| 
 | 
  4638 double weightdd(u,sc,d,ker,kt,h,sty,di,i0,i1)
 | 
| 
 | 
  4639 double *u, *sc, h, di;
 | 
| 
 | 
  4640 int d, ker, kt, i0, i1, *sty;
 | 
| 
 | 
  4641 { double w;
 | 
| 
 | 
  4642   w = 1;
 | 
| 
 | 
  4643   if (kt==KPROD)
 | 
| 
 | 
  4644   {
 | 
| 
 | 
  4645     w = WdW(u[i0]/(h*sc[i0]),ker)*WdW(u[i1]/(h*sc[i1]),ker)/(h*h*sc[i0]*sc[i1]);
 | 
| 
 | 
  4646   }
 | 
| 
 | 
  4647   return(0.0);
 | 
| 
 | 
  4648 }
 | 
| 
 | 
  4649 
 | 
| 
 | 
  4650 /* Derivatives W'(u)/u.
 | 
| 
 | 
  4651    Used in simult. conf. band computations,
 | 
| 
 | 
  4652    and kernel density bandwidth selectors. */
 | 
| 
 | 
  4653 double Wd(u,ker)
 | 
| 
 | 
  4654 double u;
 | 
| 
 | 
  4655 int ker;
 | 
| 
 | 
  4656 { double v;
 | 
| 
 | 
  4657   if (ker==WGAUS) return(-SQR(GFACT)*exp(-SQR(GFACT*u)/2));
 | 
| 
 | 
  4658   if (ker==WPARM) return(0.0);
 | 
| 
 | 
  4659   if (fabs(u)>1) return(0.0);
 | 
| 
 | 
  4660   switch(ker)
 | 
| 
 | 
  4661   { case WEPAN: return(-2.0);
 | 
| 
 | 
  4662     case WBISQ: return(-4*(1-u*u));
 | 
| 
 | 
  4663     case WTCUB: v = 1-u*u*u;
 | 
| 
 | 
  4664                 return(-9*v*v*u);
 | 
| 
 | 
  4665     case WTRWT: v = 1-u*u;
 | 
| 
 | 
  4666                 return(-6*v*v);
 | 
| 
 | 
  4667     default: LERR(("Invalid kernel %d in Wd",ker));
 | 
| 
 | 
  4668   }
 | 
| 
 | 
  4669   return(0.0);
 | 
| 
 | 
  4670 }
 | 
| 
 | 
  4671 
 | 
| 
 | 
  4672 /* Second derivatives W''(u)-W'(u)/u.
 | 
| 
 | 
  4673    used in simult. conf. band computations in >1 dimension. */
 | 
| 
 | 
  4674 double Wdd(u,ker)
 | 
| 
 | 
  4675 double u;
 | 
| 
 | 
  4676 int ker;
 | 
| 
 | 
  4677 { double v;
 | 
| 
 | 
  4678   if (ker==WGAUS) return(SQR(u*GFACT*GFACT)*exp(-SQR(u*GFACT)/2));
 | 
| 
 | 
  4679   if (ker==WPARM) return(0.0);
 | 
| 
 | 
  4680   if (u>1) return(0.0);
 | 
| 
 | 
  4681   switch(ker)
 | 
| 
 | 
  4682   { case WBISQ: return(12*u*u);
 | 
| 
 | 
  4683     case WTCUB: v = 1-u*u*u;
 | 
| 
 | 
  4684                 return(-9*u*v*v+54*u*u*u*u*v);
 | 
| 
 | 
  4685     case WTRWT: return(24*u*u*(1-u*u));
 | 
| 
 | 
  4686     default: LERR(("Invalid kernel %d in Wdd",ker));
 | 
| 
 | 
  4687   }
 | 
| 
 | 
  4688   return(0.0);
 | 
| 
 | 
  4689 }
 | 
| 
 | 
  4690 
 | 
| 
 | 
  4691 /* int u1^j1..ud^jd W(u) du.
 | 
| 
 | 
  4692    Used for local log-linear density estimation.
 | 
| 
 | 
  4693    Assume all j_i are even.
 | 
| 
 | 
  4694    Also in some bandwidth selection.
 | 
| 
 | 
  4695 */
 | 
| 
 | 
  4696 double wint(d,j,nj,ker)
 | 
| 
 | 
  4697 int d, *j, nj, ker;
 | 
| 
 | 
  4698 { double I, z;
 | 
| 
 | 
  4699   int k, dj;
 | 
| 
 | 
  4700   dj = d;
 | 
| 
 | 
  4701   for (k=0; k<nj; k++) dj += j[k];
 | 
| 
 | 
  4702   switch(ker) /* int_0^1 u^(dj-1) W(u)du  */
 | 
| 
 | 
  4703   { case WRECT: I = 1.0/dj; break;
 | 
| 
 | 
  4704     case WEPAN: I = 2.0/(dj*(dj+2)); break;
 | 
| 
 | 
  4705     case WBISQ: I = 8.0/(dj*(dj+2)*(dj+4)); break;
 | 
| 
 | 
  4706     case WTCUB: I = 162.0/(dj*(dj+3)*(dj+6)*(dj+9)); break;
 | 
| 
 | 
  4707     case WTRWT: I = 48.0/(dj*(dj+2)*(dj+4)*(dj+6)); break;
 | 
| 
 | 
  4708     case WTRIA: I = 1.0/(dj*(dj+1)); break;
 | 
| 
 | 
  4709     case WQUQU: I = 384.0/(dj*(dj+2)*(dj+4)*(dj+6)*(dj+8)); break;
 | 
| 
 | 
  4710     case W6CUB: I = 524880.0/(dj*(dj+3)*(dj+6)*(dj+9)*(dj+12)*(dj+15)*(dj+18)); break;
 | 
| 
 | 
  4711     case WGAUS: switch(d)
 | 
| 
 | 
  4712                 { case 1: I = S2PI/GFACT; break;
 | 
| 
 | 
  4713                   case 2: I = 2*PI/(GFACT*GFACT); break;
 | 
| 
 | 
  4714                   default: I = exp(d*log(S2PI/GFACT)); /* for nj=0 */
 | 
| 
 | 
  4715                 }
 | 
| 
 | 
  4716                 for (k=0; k<nj; k++) /* deliberate drop */
 | 
| 
 | 
  4717                   switch(j[k])
 | 
| 
 | 
  4718                   { case 4: I *= 3.0/(GFACT*GFACT);
 | 
| 
 | 
  4719                     case 2: I /= GFACT*GFACT;
 | 
| 
 | 
  4720                   }
 | 
| 
 | 
  4721                 return(I);
 | 
| 
 | 
  4722     case WEXPL: I = factorial(dj-1)/ipower(EFACT,dj); break;
 | 
| 
 | 
  4723     default: LERR(("Unknown kernel %d in exacint",ker));
 | 
| 
 | 
  4724   }
 | 
| 
 | 
  4725   if ((d==1) && (nj==0)) return(2*I); /* common case quick */
 | 
| 
 | 
  4726   z = (d-nj)*LOGPI/2-mut_lgammai(dj);
 | 
| 
 | 
  4727   for (k=0; k<nj; k++) z += mut_lgammai(j[k]+1);
 | 
| 
 | 
  4728   return(2*I*exp(z));
 | 
| 
 | 
  4729 }
 | 
| 
 | 
  4730 
 | 
| 
 | 
  4731 /* taylor series expansion of weight function around x.
 | 
| 
 | 
  4732    0 and 1 are common arguments, so are worth programming
 | 
| 
 | 
  4733    as special cases.
 | 
| 
 | 
  4734    Used in density estimation.
 | 
| 
 | 
  4735 */
 | 
| 
 | 
  4736 int wtaylor(f,x,ker)
 | 
| 
 | 
  4737 double *f, x;
 | 
| 
 | 
  4738 int ker;
 | 
| 
 | 
  4739 { double v;
 | 
| 
 | 
  4740   switch(ker)
 | 
| 
 | 
  4741   { case WRECT:
 | 
| 
 | 
  4742       f[0] = 1.0;
 | 
| 
 | 
  4743       return(1);
 | 
| 
 | 
  4744     case WEPAN:
 | 
| 
 | 
  4745       f[0] = 1-x*x; f[1] = -2*x; f[2] = -1;
 | 
| 
 | 
  4746       return(3);
 | 
| 
 | 
  4747     case WBISQ:
 | 
| 
 | 
  4748       v = 1-x*x;
 | 
| 
 | 
  4749       f[0] = v*v;   f[1] = -4*x*v; f[2] = 4-6*v;
 | 
| 
 | 
  4750       f[3] = 4*x;   f[4] = 1;
 | 
| 
 | 
  4751       return(5);
 | 
| 
 | 
  4752     case WTCUB:
 | 
| 
 | 
  4753       if (x==1.0)
 | 
| 
 | 
  4754       { f[0] = f[1] = f[2] = 0; f[3] = -27; f[4] = -81; f[5] = -108;
 | 
| 
 | 
  4755         f[6] = -81; f[7] = -36; f[8] = -9; f[9] = -1; return(10); }
 | 
| 
 | 
  4756       if (x==0.0)
 | 
| 
 | 
  4757       { f[1] = f[2] = f[4] = f[5] = f[7] = f[8] = 0;
 | 
| 
 | 
  4758         f[0] = 1; f[3] = -3; f[6] = 3; f[9] = -1; return(10); }
 | 
| 
 | 
  4759       v = 1-x*x*x;
 | 
| 
 | 
  4760       f[0] = v*v*v; f[1] = -9*v*v*x*x; f[2] = x*v*(27-36*v);
 | 
| 
 | 
  4761       f[3] = -27+v*(108-84*v);         f[4] = -3*x*x*(27-42*v);
 | 
| 
 | 
  4762       f[5] = x*(-108+126*v);           f[6] = -81+84*v;
 | 
| 
 | 
  4763       f[7] = -36*x*x; f[8] = -9*x;     f[9] = -1;
 | 
| 
 | 
  4764       return(10);
 | 
| 
 | 
  4765     case WTRWT:
 | 
| 
 | 
  4766       v = 1-x*x;
 | 
| 
 | 
  4767       f[0] = v*v*v; f[1] = -6*x*v*v; f[2] = v*(12-15*v);
 | 
| 
 | 
  4768       f[3] = x*(20*v-8); f[4] = 15*v-12; f[5] = -6; f[6] = -1;
 | 
| 
 | 
  4769       return(7);
 | 
| 
 | 
  4770     case WTRIA:
 | 
| 
 | 
  4771       f[0] = 1-x; f[1] = -1;
 | 
| 
 | 
  4772       return(2);
 | 
| 
 | 
  4773     case WQUQU:
 | 
| 
 | 
  4774       v = 1-x*x;
 | 
| 
 | 
  4775       f[0] = v*v*v*v; f[1] = -8*x*v*v*v; f[2] = v*v*(24-28*v);
 | 
| 
 | 
  4776       f[3] = v*x*(56*v-32); f[4] = (70*v-80)*v+16; f[5] = x*(32-56*v);
 | 
| 
 | 
  4777       f[6] = 24-28*v; f[7] = 8*x; f[8] = 1;
 | 
| 
 | 
  4778       return(9);
 | 
| 
 | 
  4779     case W6CUB:
 | 
| 
 | 
  4780       v = 1-x*x*x;
 | 
| 
 | 
  4781       f[0] = v*v*v*v*v*v;
 | 
| 
 | 
  4782       f[1] = -18*x*x*v*v*v*v*v;
 | 
| 
 | 
  4783       f[2] = x*v*v*v*v*(135-153*v);
 | 
| 
 | 
  4784       f[3] = v*v*v*(-540+v*(1350-816*v));
 | 
| 
 | 
  4785       f[4] = x*x*v*v*(1215-v*(4050-v*3060));
 | 
| 
 | 
  4786       f[5] = x*v*(-1458+v*(9234+v*(-16254+v*8568)));
 | 
| 
 | 
  4787       f[6] = 729-v*(10206-v*(35154-v*(44226-v*18564)));
 | 
| 
 | 
  4788       f[7] = x*x*(4374-v*(30132-v*(56862-v*31824)));
 | 
| 
 | 
  4789       f[8] = x*(12393-v*(61479-v*(92664-v*43758)));
 | 
| 
 | 
  4790       f[9] = 21870-v*(89100-v*(115830-v*48620));
 | 
| 
 | 
  4791       f[10]= x*x*(26730-v*(69498-v*43758));
 | 
| 
 | 
  4792       f[11]= x*(23814-v*(55458-v*31824));
 | 
| 
 | 
  4793       f[12]= 15849-v*(34398-v*18564);
 | 
| 
 | 
  4794       f[13]= x*x*(7938-8568*v);
 | 
| 
 | 
  4795       f[14]= x*(2970-3060*v);
 | 
| 
 | 
  4796       f[15]= 810-816*v;
 | 
| 
 | 
  4797       f[16]= 153*x*x;
 | 
| 
 | 
  4798       f[17]= 18*x;
 | 
| 
 | 
  4799       f[18]= 1;
 | 
| 
 | 
  4800       return(19);
 | 
| 
 | 
  4801   }
 | 
| 
 | 
  4802   LERR(("Invalid kernel %d in wtaylor",ker));
 | 
| 
 | 
  4803   return(0);
 | 
| 
 | 
  4804 }
 | 
| 
 | 
  4805 
 | 
| 
 | 
  4806 /* convolution int W(x)W(x+v)dx.
 | 
| 
 | 
  4807    used in kde bandwidth selection.
 | 
| 
 | 
  4808 */
 | 
| 
 | 
  4809 double Wconv(v,ker)
 | 
| 
 | 
  4810 double v;
 | 
| 
 | 
  4811 int ker;
 | 
| 
 | 
  4812 { double v2;
 | 
| 
 | 
  4813   switch(ker)
 | 
| 
 | 
  4814   { case WGAUS: return(SQRPI/GFACT*exp(-SQR(GFACT*v)/4));
 | 
| 
 | 
  4815     case WRECT:
 | 
| 
 | 
  4816       v = fabs(v);
 | 
| 
 | 
  4817       if (v>2) return(0.0);
 | 
| 
 | 
  4818       return(2-v);
 | 
| 
 | 
  4819     case WEPAN:
 | 
| 
 | 
  4820       v = fabs(v);
 | 
| 
 | 
  4821       if (v>2) return(0.0);
 | 
| 
 | 
  4822       return((2-v)*(16+v*(8-v*(16-v*(2+v))))/30);
 | 
| 
 | 
  4823     case WBISQ:
 | 
| 
 | 
  4824       v = fabs(v);
 | 
| 
 | 
  4825       if (v>2) return(0.0);
 | 
| 
 | 
  4826       v2 = 2-v;
 | 
| 
 | 
  4827       return(v2*v2*v2*v2*v2*(16+v*(40+v*(36+v*(10+v))))/630);
 | 
| 
 | 
  4828   }
 | 
| 
 | 
  4829   LERR(("Wconv not implemented for kernel %d",ker));
 | 
| 
 | 
  4830   return(0.0);
 | 
| 
 | 
  4831 }
 | 
| 
 | 
  4832 
 | 
| 
 | 
  4833 /* derivative of Wconv.
 | 
| 
 | 
  4834    1/v d/dv int W(x)W(x+v)dx
 | 
| 
 | 
  4835    used in kde bandwidth selection.
 | 
| 
 | 
  4836 */
 | 
| 
 | 
  4837 double Wconv1(v,ker)
 | 
| 
 | 
  4838 double v;
 | 
| 
 | 
  4839 int ker;
 | 
| 
 | 
  4840 { double v2;
 | 
| 
 | 
  4841   v = fabs(v);
 | 
| 
 | 
  4842   switch(ker)
 | 
| 
 | 
  4843   { case WGAUS: return(-0.5*SQRPI*GFACT*exp(-SQR(GFACT*v)/4));
 | 
| 
 | 
  4844     case WRECT:
 | 
| 
 | 
  4845       if (v>2) return(0.0);
 | 
| 
 | 
  4846       return(1.0);
 | 
| 
 | 
  4847     case WEPAN:
 | 
| 
 | 
  4848       if (v>2) return(0.0);
 | 
| 
 | 
  4849       return((-16+v*(12-v*v))/6);
 | 
| 
 | 
  4850     case WBISQ:
 | 
| 
 | 
  4851       if (v>2) return(0.0);
 | 
| 
 | 
  4852       v2 = 2-v;
 | 
| 
 | 
  4853       return(-v2*v2*v2*v2*(32+v*(64+v*(24+v*3)))/210);
 | 
| 
 | 
  4854   }
 | 
| 
 | 
  4855   LERR(("Wconv1 not implemented for kernel %d",ker));
 | 
| 
 | 
  4856   return(0.0);
 | 
| 
 | 
  4857 }
 | 
| 
 | 
  4858 
 | 
| 
 | 
  4859 /* 4th derivative of Wconv.
 | 
| 
 | 
  4860    used in kde bandwidth selection (BCV, SJPI, GKK)
 | 
| 
 | 
  4861 */
 | 
| 
 | 
  4862 double Wconv4(v,ker)
 | 
| 
 | 
  4863 double v;
 | 
| 
 | 
  4864 int ker;
 | 
| 
 | 
  4865 { double gv;
 | 
| 
 | 
  4866   switch(ker)
 | 
| 
 | 
  4867   { case WGAUS:
 | 
| 
 | 
  4868       gv = GFACT*v;
 | 
| 
 | 
  4869       return(exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*(12-gv*gv*(12-gv*gv))*SQRPI/16);
 | 
| 
 | 
  4870   }
 | 
| 
 | 
  4871   LERR(("Wconv4 not implemented for kernel %d",ker));
 | 
| 
 | 
  4872   return(0.0);
 | 
| 
 | 
  4873 }
 | 
| 
 | 
  4874 
 | 
| 
 | 
  4875 /* 5th derivative of Wconv.
 | 
| 
 | 
  4876    used in kde bandwidth selection (BCV method only)
 | 
| 
 | 
  4877 */
 | 
| 
 | 
  4878 double Wconv5(v,ker) /* (d/dv)^5 int W(x)W(x+v)dx */
 | 
| 
 | 
  4879 double v;
 | 
| 
 | 
  4880 int ker;
 | 
| 
 | 
  4881 { double gv;
 | 
| 
 | 
  4882   switch(ker)
 | 
| 
 | 
  4883   { case WGAUS:
 | 
| 
 | 
  4884       gv = GFACT*v;
 | 
| 
 | 
  4885       return(-exp(-SQR(gv)/4)*GFACT*GFACT*GFACT*GFACT*gv*(60-gv*gv*(20-gv*gv))*SQRPI/32);
 | 
| 
 | 
  4886   }
 | 
| 
 | 
  4887   LERR(("Wconv5 not implemented for kernel %d",ker));
 | 
| 
 | 
  4888   return(0.0);
 | 
| 
 | 
  4889 }
 | 
| 
 | 
  4890 
 | 
| 
 | 
  4891 /* 6th derivative of Wconv.
 | 
| 
 | 
  4892    used in kde bandwidth selection (SJPI)
 | 
| 
 | 
  4893 */
 | 
| 
 | 
  4894 double Wconv6(v,ker)
 | 
| 
 | 
  4895 double v;
 | 
| 
 | 
  4896 int ker;
 | 
| 
 | 
  4897 { double gv, z;
 | 
| 
 | 
  4898   switch(ker)
 | 
| 
 | 
  4899   { case WGAUS:
 | 
| 
 | 
  4900       gv = GFACT*v;
 | 
| 
 | 
  4901       gv = gv*gv;
 | 
| 
 | 
  4902       z = exp(-gv/4)*(-120+gv*(180-gv*(30-gv)))*0.02769459142;
 | 
| 
 | 
  4903       gv = GFACT*GFACT;
 | 
| 
 | 
  4904       return(z*gv*gv*GFACT);
 | 
| 
 | 
  4905   }
 | 
| 
 | 
  4906   LERR(("Wconv6 not implemented for kernel %d",ker));
 | 
| 
 | 
  4907   return(0.0);
 | 
| 
 | 
  4908 }
 | 
| 
 | 
  4909 
 | 
| 
 | 
  4910 /* int W(v)^2 dv / (int v^2 W(v) dv)^2
 | 
| 
 | 
  4911    used in some bandwidth selectors
 | 
| 
 | 
  4912 */
 | 
| 
 | 
  4913 double Wikk(ker,deg)
 | 
| 
 | 
  4914 int ker, deg;
 | 
| 
 | 
  4915 { switch(deg)
 | 
| 
 | 
  4916   { case 0:
 | 
| 
 | 
  4917     case 1: /* int W(v)^2 dv / (int v^2 W(v) dv)^2 */
 | 
| 
 | 
  4918       switch(ker)
 | 
| 
 | 
  4919       { case WRECT: return(4.5);
 | 
| 
 | 
  4920         case WEPAN: return(15.0);
 | 
| 
 | 
  4921         case WBISQ: return(35.0);
 | 
| 
 | 
  4922         case WGAUS: return(0.2820947918*GFACT*GFACT*GFACT*GFACT*GFACT);
 | 
| 
 | 
  4923         case WTCUB: return(34.152111046847892);   /* 59049 / 1729 */
 | 
| 
 | 
  4924         case WTRWT: return(66.083916083916080);   /* 9450/143 */
 | 
| 
 | 
  4925       }
 | 
| 
 | 
  4926     case 2:
 | 
| 
 | 
  4927     case 3: /* 4!^2/8*int(W1^2)/int(v^4W1)^2
 | 
| 
 | 
  4928                W1=W*(n4-v^2n2)/(n0n4-n2n2) */
 | 
| 
 | 
  4929       switch(ker)
 | 
| 
 | 
  4930       { case WRECT: return(11025.0);
 | 
| 
 | 
  4931         case WEPAN: return(39690.0);
 | 
| 
 | 
  4932         case WBISQ: return(110346.9231);
 | 
| 
 | 
  4933         case WGAUS: return(14527.43412);
 | 
| 
 | 
  4934         case WTCUB: return(126500.5904);
 | 
| 
 | 
  4935         case WTRWT: return(254371.7647);
 | 
| 
 | 
  4936       }
 | 
| 
 | 
  4937   }
 | 
| 
 | 
  4938   LERR(("Wikk not implemented for kernel %d, deg %d",ker,deg));
 | 
| 
 | 
  4939   return(0.0);
 | 
| 
 | 
  4940 }
 |