--- imach/src/imach.c 2015/09/15 17:34:58 1.201
+++ imach/src/imach.c 2015/09/22 19:45:16 1.202
@@ -1,6 +1,9 @@
-/* $Id: imach.c,v 1.201 2015/09/15 17:34:58 brouard Exp $
+/* $Id: imach.c,v 1.202 2015/09/22 19:45:16 brouard Exp $
$State: Exp $
$Log: imach.c,v $
+ Revision 1.202 2015/09/22 19:45:16 brouard
+ Summary: Adding some overall graph on contribution to likelihood. Might change
+
Revision 1.201 2015/09/15 17:34:58 brouard
Summary: 0.98r0
@@ -644,6 +647,7 @@
/* #define DEBUG */
/* #define DEBUGBRENT */
+#define DEBUGLINMIN
#define POWELL /* Instead of NLOPT */
#define POWELLF1F3 /* Skip test */
/* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
@@ -731,12 +735,12 @@ typedef struct {
#define ODIRSEPARATOR '\\'
#endif
-/* $Id: imach.c,v 1.201 2015/09/15 17:34:58 brouard Exp $ */
+/* $Id: imach.c,v 1.202 2015/09/22 19:45:16 brouard Exp $ */
/* $State: Exp $ */
#include "version.h"
char version[]=__IMACH_VERSION__;
char copyright[]="September 2015,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015";
-char fullversion[]="$Revision: 1.201 $ $Date: 2015/09/15 17:34:58 $";
+char fullversion[]="$Revision: 1.202 $ $Date: 2015/09/22 19:45:16 $";
char strstart[80];
char optionfilext[10], optionfilefiname[FILENAMELENGTH];
int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
@@ -802,7 +806,7 @@ char command[FILENAMELENGTH];
int outcmd=0;
char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
-char fileresu[FILENAMELENGTH]; /* Without r in front */
+char fileresu[FILENAMELENGTH]; /* fileres without r in front */
char filelog[FILENAMELENGTH]; /* Log file */
char filerest[FILENAMELENGTH];
char fileregp[FILENAMELENGTH];
@@ -1606,7 +1610,7 @@ void linmin(double p[], double xi[], int
nrfunc=func;
for (j=1;j<=n;j++) {
pcom[j]=p[j];
- xicom[j]=xi[j];
+ xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
}
/* axs=0.0; */
@@ -1630,6 +1634,7 @@ void linmin(double p[], double xi[], int
#ifdef DEBUGLINMIN
printf("\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
+ fprintf(ficlog,"\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
#endif
*fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
/* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
@@ -1642,6 +1647,7 @@ void linmin(double p[], double xi[], int
#endif
#ifdef DEBUGLINMIN
printf("linmin end ");
+ fprintf(ficlog,"linmin end ");
#endif
for (j=1;j<=n;j++) {
/* printf(" before xi[%d]=%12.8f", j,xi[j]); */
@@ -1653,10 +1659,14 @@ void linmin(double p[], double xi[], int
/* printf("\n"); */
#ifdef DEBUGLINMIN
printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
+ fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
for (j=1;j<=n;j++) {
- printf(" xi[%d]= %12.7f p[%d]= %12.7f",j,xi[j],j,p[j]);
- if(j % ncovmodel == 0)
+ printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
+ fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
+ if(j % ncovmodel == 0){
printf("\n");
+ fprintf(ficlog,"\n");
+ }
}
#endif
free_vector(xicom,1,n);
@@ -1691,7 +1701,7 @@ void powell(double p[], double **xi, int
xits=vector(1,n);
*fret=(*func)(p);
for (j=1;j<=n;j++) pt[j]=p[j];
- rcurr_time = time(NULL);
+ rcurr_time = time(NULL);
for (*iter=1;;++(*iter)) {
fp=(*fret); /* From former iteration or initial value */
ibig=0;
@@ -1836,7 +1846,7 @@ void powell(double p[], double **xi, int
t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
t= t- del*SQR(fp-fptt);
#endif
- directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If del was big enough we change it for a new direction */
+ directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
#ifdef DEBUG
printf("t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
@@ -1851,9 +1861,9 @@ void powell(double p[], double **xi, int
if (t < 0.0) { /* Then we use it for new direction */
#else
if (directest*t < 0.0) { /* Contradiction between both tests */
- printf("directest= %.12lf, t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
+ printf("directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
- fprintf(ficlog,"directest= %.12lf, t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
+ fprintf(ficlog,"directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
}
if (directest < 0.0) { /* Then we use it for new direction */
@@ -1861,17 +1871,23 @@ void powell(double p[], double **xi, int
#ifdef DEBUGLINMIN
printf("Before linmin in direction P%d-P0\n",n);
for (j=1;j<=n;j++) {
- printf("Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
- if(j % ncovmodel == 0)
+ printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
+ fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
+ if(j % ncovmodel == 0){
printf("\n");
+ fprintf(ficlog,"\n");
+ }
}
#endif
linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
#ifdef DEBUGLINMIN
for (j=1;j<=n;j++) {
printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
- if(j % ncovmodel == 0)
+ fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
+ if(j % ncovmodel == 0){
printf("\n");
+ fprintf(ficlog,"\n");
+ }
}
#endif
for (j=1;j<=n;j++) {
@@ -1911,7 +1927,8 @@ double **prevalim(double **prlim, int nl
/* double **matprod2(); */ /* test */
double **out, cov[NCOVMAX+1], **pmij();
double **newm;
- double agefin, delaymax=50 ; /* Max number of years to converge */
+ double agefin, delaymax=100 ; /* Max number of years to converge */
+ long int ncvyear=0, ncvloop=0;
for (ii=1;ii<=nlstate+ndeath;ii++)
for (j=1;j<=nlstate+ndeath;j++){
@@ -1921,7 +1938,9 @@ double **prevalim(double **prlim, int nl
cov[1]=1.;
/* Even if hstepm = 1, at least one multiplication by the unit matrix */
+ /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
+ ncvloop++;
newm=savm;
/* Covariates have to be included here again */
cov[2]=agefin;
@@ -1956,17 +1975,21 @@ double **prevalim(double **prlim, int nl
sumnew=0;
for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
prlim[i][j]= newm[i][j]/(1-sumnew);
- /*printf(" prevalim i=%d, j=%d, prmlim[%d][%d]=%f, agefin=%d \n", i, j, i, j, prlim[i][j],(int)agefin);*/
max=FMAX(max,prlim[i][j]);
min=FMIN(min,prlim[i][j]);
+ /* printf(" age= %d prevalim i=%d, j=%d, prmlim[%d][%d]=%f, agefin=%d max=%f min=%f\n", (int)age, i, j, i, j, prlim[i][j],(int)agefin, max, min); */
}
maxmin=max-min;
maxmax=FMAX(maxmax,maxmin);
} /* j loop */
if(maxmax < ftolpl){
+ /* printf("maxmax=%lf maxmin=%lf ncvloop=%ld, ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age-(int)agefin); */
return prlim;
}
} /* age loop */
+ printf("Warning: the stable prevalence did not converge with the required precision ftolpl=6*10^5*ftol=%g. \n\
+Earliest age to start was %d-%d=%d, ncvloop=%ld, ncvyear=%d\n\
+Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl, (int)age, (int)delaymax, (int)agefin, ncvloop, (int)age-(int)agefin);
return prlim; /* should not reach here */
}
@@ -2542,9 +2565,9 @@ double funcone( double *x)
ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
/*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
if(globpr){
- fprintf(ficresilk,"%9ld %6d %2d %2d %1d %1d %3d %11.6f %8.4f\
+ fprintf(ficresilk,"%9ld %6.1f %6d %2d %2d %2d %2d %3d %11.6f %8.4f\
%11.6f %11.6f %11.6f ", \
- num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
+ num[i], agexact, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
for(k=1,llt=0.,l=0.; k<=nlstate; k++){
llt +=ll[k]*gipmx/gsw;
@@ -2577,13 +2600,13 @@ void likelione(FILE *ficres,double p[],
if(*globpri !=0){ /* Just counts and sums, no printings */
strcpy(fileresilk,"ILK_");
- strcat(fileresilk,fileres);
+ strcat(fileresilk,fileresu);
if((ficresilk=fopen(fileresilk,"w"))==NULL) {
printf("Problem with resultfile: %s\n", fileresilk);
fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
}
fprintf(ficresilk, "#individual(line's_record) s1 s2 wave# effective_wave# number_of_matrices_product pij weight -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
- fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav ");
+ fprintf(ficresilk, "#num_i age i s1 s2 mi mw dh likeli weight 2wlli out sav ");
/* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
for(k=1; k<=nlstate; k++)
fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
@@ -2593,8 +2616,10 @@ void likelione(FILE *ficres,double p[],
*fretone=(*funcone)(p);
if(*globpri !=0){
fclose(ficresilk);
- fprintf(fichtm,"\n
File of contributions to the likelihood (if mle=1): %s
\n",subdirf(fileresilk),subdirf(fileresilk));
- fflush(fichtm);
+ fprintf(fichtm,"\n
File of contributions to the likelihood computed with initial parameters and mle >= 1. You should at least run with mle >= 1 and starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: %s
\n",subdirf(fileresilk),subdirf(fileresilk));
+ fprintf(fichtm,"
- The first 3 individuals are drawn with lines. The function drawn is -2Log(L) in log scale: %s.png
\
+",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
+ fflush(fichtm);
}
return;
}
@@ -3841,7 +3866,7 @@ void varevsij(char optionfilefiname[], d
/*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
strcat(fileresprobmorprev,digit); /* Tvar to be done */
strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
- strcat(fileresprobmorprev,fileres);
+ strcat(fileresprobmorprev,fileresu);
if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
printf("Problem with resultfile: %s\n", fileresprobmorprev);
fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
@@ -4213,13 +4238,13 @@ void varprob(char optionfilefiname[], do
fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
}
strcpy(fileresprobcov,"PROBCOV_");
- strcat(fileresprobcov,fileres);
+ strcat(fileresprobcov,fileresu);
if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
printf("Problem with resultfile: %s\n", fileresprobcov);
fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
}
strcpy(fileresprobcor,"PROBCOR_");
- strcat(fileresprobcor,fileres);
+ strcat(fileresprobcor,fileresu);
if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
printf("Problem with resultfile: %s\n", fileresprobcor);
fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
@@ -4571,7 +4596,7 @@ fprintf(fichtm," \n