]> henry.ined.fr Git - .git/commitdiff
Check memory allocation through valgrind on Linux and libnjamd .
authorN. Brouard <brouard@ined.fr>
Fri, 2 May 2003 18:51:41 +0000 (18:51 +0000)
committerN. Brouard <brouard@ined.fr>
Fri, 2 May 2003 18:51:41 +0000 (18:51 +0000)
So it is free of malloc bugs now!
Version 0.95

src/imach.c

index b3357c4094ba54e2e677516e8834196c9d69f705..e0f121e49692c5010df869c29308c37b5f7ef2d0 100644 (file)
   It is copyrighted identically to a GNU software product, ie programme and
   software can be distributed freely for non commercial use. Latest version
   can be accessed at http://euroreves.ined.fr/imach .
+
+  Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
+  or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
+  
   **********************************************************************/
+/*
+  main
+  read parameterfile
+  read datafile
+  concatwav
+  if (mle >= 1)
+    mlikeli
+  print results files
+  if mle==1 
+     computes hessian
+  read end of parameter file: agemin, agemax, bage, fage, estepm
+      begin-prev-date,...
+  open gnuplot file
+  open html file
+  stable prevalence
+   for age prevalim()
+  h Pij x
+  variance of p varprob
+  forecasting if prevfcast==1 prevforecast call prevalence()
+  health expectancies
+  Variance-covariance of DFLE
+  prevalence()
+   movingaverage()
+  varevsij() 
+  if popbased==1 varevsij(,popbased)
+  total life expectancies
+  Variance of stable prevalence
+ end
+*/
+
+
+
  
 #include <math.h>
 #include <stdio.h>
 #define ODIRSEPARATOR '\\'
 #endif
 
-char version[80]="Imach version 0.94, February 2003, INED-EUROREVES ";
+char version[80]="Imach version 0.95, February 2003, INED-EUROREVES ";
 int erreur; /* Error number */
 int nvar;
 int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov;
@@ -188,15 +224,9 @@ static     int split( char *path, char *dirc, char *name, char *ext, char *finame )
   if ( ss == NULL ) {                  /* no directory, so use current */
     /*if(strrchr(path, ODIRSEPARATOR )==NULL)
       printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
-#if    defined(__bsd__)                /* get current working directory */
-    extern char        *getwd( );
-
-    if ( getwd( dirc ) == NULL ) {
-#else
-    extern char        *getcwd( );
-
+    /* get current working directory */
+    /*    extern  char* getcwd ( char *buf , int len);*/
     if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
-#endif
       return( GLOCK_ERROR_GETCWD );
     }
     strcpy( name, path );              /* we've got it */
@@ -365,6 +395,8 @@ double **matrix(long nrl, long nrh, long ncl, long nch)
 
   for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
   return m;
+  /* print *(*(m+1)+70) ou print m[1][70]; print m+1 or print &(m[1]) 
+   */
 }
 
 /*************************free matrix ************************/
@@ -404,7 +436,10 @@ double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
     for (j=ncl+1; j<=nch; j++) 
       m[i][j]=m[i][j-1]+nlay;
   }
-  return m;
+  return m; 
+  /*  gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
+           &(m[i][j][k]) <=> *((*(m+i) + j)+k)
+  */
 }
 
 /*************************free ma3x ************************/
@@ -1150,7 +1185,7 @@ double func( double *x)
 void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
 {
   int i,j, iter;
-  double **xi,*delti;
+  double **xi;
   double fret;
   xi=matrix(1,npar,1,npar);
   for (i=1;i<=npar;i++)
@@ -1423,7 +1458,7 @@ void lubksb(double **a, int n, int *indx, double b[])
 } 
 
 /************ Frequencies ********************/
-void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2)
+void  freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2)
 {  /* Some frequencies */
   
   int i, m, jk, k1,i1, j1, bool, z1,z2,j;
@@ -1435,8 +1470,7 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
   char fileresp[FILENAMELENGTH];
   
   pp=vector(1,nlstate);
-  prop=matrix(1,nlstate,agemin,agemax+3);
-  probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
+  prop=matrix(1,nlstate,iagemin,iagemax+3);
   strcpy(fileresp,"p");
   strcat(fileresp,fileres);
   if((ficresp=fopen(fileresp,"w"))==NULL) {
@@ -1444,7 +1478,7 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
     fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
     exit(0);
   }
-  freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
+  freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);
   j1=0;
   
   j=cptcoveff;
@@ -1459,11 +1493,11 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
        scanf("%d", i);*/
       for (i=-1; i<=nlstate+ndeath; i++)  
        for (jk=-1; jk<=nlstate+ndeath; jk++)  
-         for(m=agemin; m <= agemax+3; m++)
+         for(m=iagemin; m <= iagemax+3; m++)
            freq[i][jk][m]=0;
 
     for (i=1; i<=nlstate; i++)  
-      for(m=agemin; m <= agemax+3; m++)
+      for(m=iagemin; m <= iagemax+3; m++)
        prop[i][m]=0;
       
       dateintsum=0;
@@ -1479,15 +1513,15 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
          for(m=firstpass; m<=lastpass; m++){
            k2=anint[m][i]+(mint[m][i]/12.);
            if ((k2>=dateprev1) && (k2<=dateprev2)) {
-             if(agev[m][i]==0) agev[m][i]=agemax+1;
-             if(agev[m][i]==1) agev[m][i]=agemax+2;
+             if(agev[m][i]==0) agev[m][i]=iagemax+1;
+             if(agev[m][i]==1) agev[m][i]=iagemax+2;
              if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];
              if (m<lastpass) {
                freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
-               freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i];
+               freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i];
              }
              
-             if ((agev[m][i]>1) && (agev[m][i]< (agemax+3))) {
+             if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3))) {
                dateintsum=dateintsum+k2;
                k2cpt++;
              }
@@ -1507,8 +1541,8 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
        fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
       fprintf(ficresp, "\n");
       
-      for(i=(int)agemin; i <= (int)agemax+3; i++){
-       if(i==(int)agemax+3){
+      for(i=iagemin; i <= iagemax+3; i++){
+       if(i==iagemax+3){
          fprintf(ficlog,"Total");
        }else{
          if(first==1){
@@ -1554,7 +1588,7 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
              printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
            fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
          }
-         if( i <= (int) agemax){
+         if( i <= iagemax){
            if(pos>=1.e-5){
              fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
              probs[i][jk][j1]= pp[jk]/pos;
@@ -1572,7 +1606,7 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
              printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
              fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
            }
-       if(i <= (int) agemax)
+       if(i <= iagemax)
          fprintf(ficresp,"\n");
        if(first==1)
          printf("Others in log...\n");
@@ -1583,14 +1617,14 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
   dateintmean=dateintsum/k2cpt; 
  
   fclose(ficresp);
-  free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
+  free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);
   free_vector(pp,1,nlstate);
-  free_matrix(prop,1,nlstate,(int) agemin,(int) agemax+3);
+  free_matrix(prop,1,nlstate,iagemin, iagemax+3);
   /* End of Freq */
 }
 
 /************ Prevalence ********************/
-void prevalence(int agemin, float agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)
+void prevalence(double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)
 {  
   /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
      in each health status at the date of interview (if between dateprev1 and dateprev2).
@@ -1602,10 +1636,13 @@ void prevalence(int agemin, float agemax, int **s, double **agev, int nlstate, i
   double *pp, **prop;
   double pos,posprop; 
   double  y2; /* in fractional years */
+  int iagemin, iagemax;
 
-  pp=vector(1,nlstate);
-  prop=matrix(1,nlstate,agemin,agemax+3); 
-  freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
+  iagemin= (int) agemin;
+  iagemax= (int) agemax;
+  /*pp=vector(1,nlstate);*/
+  prop=matrix(1,nlstate,iagemin,iagemax+3); 
+  /*  freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
   j1=0;
   
   j=cptcoveff;
@@ -1616,8 +1653,8 @@ void prevalence(int agemin, float agemax, int **s, double **agev, int nlstate, i
       j1++;
       
       for (i=1; i<=nlstate; i++)  
-       for(m=agemin; m <= agemax+3; m++)
-         prop[i][m]=0;
+       for(m=iagemin; m <= iagemax+3; m++)
+         prop[i][m]=0.0;
      
       for (i=1; i<=imx; i++) { /* Each individual */
        bool=1;
@@ -1630,38 +1667,39 @@ void prevalence(int agemin, float agemax, int **s, double **agev, int nlstate, i
          for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/
            y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
            if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
-             if(agev[m][i]==0) agev[m][i]=agemax+1;
-             if(agev[m][i]==1) agev[m][i]=agemax+2;
-             if (s[m][i]>0 && s[m][i]<=nlstate) {
-               prop[s[m][i]][(int)agev[m][i]] += weight[i];
-               prop[s[m][i]][(int)(agemax+3)] += weight[i];
-             }
+             if(agev[m][i]==0) agev[m][i]=iagemax+1;
+             if(agev[m][i]==1) agev[m][i]=iagemax+2;
+             if((int)agev[m][i] <iagemin || (int)agev[m][i] >iagemax+3) printf("Error on individual =%d agev[m][i]=%f m=%d\n",i, agev[m][i],m); 
+             if (s[m][i]>0 && s[m][i]<=nlstate) { 
+               /*if(i>4620) printf(" i=%d m=%d s[m][i]=%d (int)agev[m][i]=%d weight[i]=%f prop=%f\n",i,m,s[m][i],(int)agev[m][m],weight[i],prop[s[m][i]][(int)agev[m][i]]);*/
+               prop[s[m][i]][(int)agev[m][i]] += weight[i];
+               prop[s[m][i]][iagemax+3] += weight[i]; 
+             } 
            }
          } /* end selection of waves */
        }
       }
-      for(i=(int)agemin; i <= (int)agemax+3; i++){ 
+      for(i=iagemin; i <= iagemax+3; i++){  
        
-       for(jk=1,posprop=0; jk <=nlstate ; jk++) {
-         posprop += prop[jk][i];
-       }
-       
-       for(jk=1; jk <=nlstate ; jk++){    
-         if( i <= (int) agemax){
-           if(posprop>=1.e-5){
-            probs[i][jk][j1]= prop[jk][i]/posprop;
-           }
-         }
-       }/* end jk */
-      }/* end i */
+       for(jk=1,posprop=0; jk <=nlstate ; jk++) { 
+         posprop += prop[jk][i]; 
+       } 
+
+       for(jk=1; jk <=nlstate ; jk++){     
+         if( i <=  iagemax){ 
+           if(posprop>=1.e-5){ 
+             probs[i][jk][j1]= prop[jk][i]/posprop;
+           } 
+         } 
+       }/* end jk */ 
+      }/* end i */ 
     } /* end i1 */
   } /* end k1 */
-
   
-  free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
-  free_vector(pp,1,nlstate);
-  free_matrix(prop,1,nlstate,(int) agemin,(int) agemax+3);
-}  /* End of Freq */
+  /*  free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
+  /*free_vector(pp,1,nlstate);*/
+  free_matrix(prop,1,nlstate, iagemin,iagemax+3);
+}  /* End of prevalence */
 
 /************* Waves Concatenation ***************/
 
@@ -1853,10 +1891,10 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
   double ***gradg, ***trgradg;
   int theta;
 
-  varhe=ma3x(1,nlstate*2,1,nlstate*2,(int) bage, (int) fage);
+  varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
   xp=vector(1,npar);
-  dnewm=matrix(1,nlstate*2,1,npar);
-  doldm=matrix(1,nlstate*2,1,nlstate*2);
+  dnewm=matrix(1,nlstate*nlstate,1,npar);
+  doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
   
   fprintf(ficreseij,"# Health expectancies\n");
   fprintf(ficreseij,"# Age");
@@ -1902,9 +1940,9 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
     /* if (stepm >= YEARM) hstepm=1;*/
     nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
     p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
-    gradg=ma3x(0,nhstepm,1,npar,1,nlstate*2);
-    gp=matrix(0,nhstepm,1,nlstate*2);
-    gm=matrix(0,nhstepm,1,nlstate*2);
+    gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
+    gp=matrix(0,nhstepm,1,nlstate*nlstate);
+    gm=matrix(0,nhstepm,1,nlstate*nlstate);
 
     /* Computed by stepm unit matrices, product of hstepm matrices, stored
        in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
@@ -1945,7 +1983,7 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
          }
        }
       }
-      for(j=1; j<= nlstate*2; j++)
+      for(j=1; j<= nlstate*nlstate; j++)
        for(h=0; h<=nhstepm-1; h++){
          gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
        }
@@ -1953,26 +1991,26 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
    
 /* End theta */
 
-     trgradg =ma3x(0,nhstepm,1,nlstate*2,1,npar);
+     trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
 
      for(h=0; h<=nhstepm-1; h++)
-      for(j=1; j<=nlstate*2;j++)
+      for(j=1; j<=nlstate*nlstate;j++)
        for(theta=1; theta <=npar; theta++)
          trgradg[h][j][theta]=gradg[h][theta][j];
      
 
-     for(i=1;i<=nlstate*2;i++)
-      for(j=1;j<=nlstate*2;j++)
+     for(i=1;i<=nlstate*nlstate;i++)
+      for(j=1;j<=nlstate*nlstate;j++)
        varhe[i][j][(int)age] =0.;
 
      printf("%d|",(int)age);fflush(stdout);
      fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
      for(h=0;h<=nhstepm-1;h++){
       for(k=0;k<=nhstepm-1;k++){
-       matprod2(dnewm,trgradg[h],1,nlstate*2,1,npar,1,npar,matcov);
-       matprod2(doldm,dnewm,1,nlstate*2,1,npar,1,nlstate*2,gradg[k]);
-       for(i=1;i<=nlstate*2;i++)
-         for(j=1;j<=nlstate*2;j++)
+       matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
+       matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
+       for(i=1;i<=nlstate*nlstate;i++)
+         for(j=1;j<=nlstate*nlstate;j++)
            varhe[i][j][(int)age] += doldm[i][j]*hf*hf;
       }
     }
@@ -1995,19 +2033,19 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
       }
     fprintf(ficreseij,"\n");
    
-    free_matrix(gm,0,nhstepm,1,nlstate*2);
-    free_matrix(gp,0,nhstepm,1,nlstate*2);
-    free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*2);
-    free_ma3x(trgradg,0,nhstepm,1,nlstate*2,1,npar);
+    free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
+    free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
+    free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
+    free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
     free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
   }
   printf("\n");
   fprintf(ficlog,"\n");
 
   free_vector(xp,1,npar);
-  free_matrix(dnewm,1,nlstate*2,1,npar);
-  free_matrix(doldm,1,nlstate*2,1,nlstate*2);
-  free_ma3x(varhe,1,nlstate*2,1,nlstate*2,(int) bage, (int)fage);
+  free_matrix(dnewm,1,nlstate*nlstate,1,npar);
+  free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
+  free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
 }
 
 /************ Variance ******************/
@@ -2542,7 +2580,7 @@ void varprob(char optionfilefiname[], double **matcov, double x[], double delti[
     
        for(theta=1; theta <=npar; theta++){
          for(i=1; i<=npar; i++)
-           xp[i] = x[i] + (i==theta ?delti[theta]:0);
+           xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
          
          pmij(pmmij,cov,ncovmodel,xp,nlstate);
          
@@ -2555,7 +2593,7 @@ void varprob(char optionfilefiname[], double **matcov, double x[], double delti[
          }
          
          for(i=1; i<=npar; i++)
-           xp[i] = x[i] - (i==theta ?delti[theta]:0);
+           xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
     
          pmij(pmmij,cov,ncovmodel,xp,nlstate);
          k=0;
@@ -2567,7 +2605,7 @@ void varprob(char optionfilefiname[], double **matcov, double x[], double delti[
          }
      
          for(i=1; i<= (nlstate)*(nlstate+ndeath); i++) 
-           gradg[theta][i]=(gp[i]-gm[i])/2./delti[theta];  
+           gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];  
        }
 
        for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
@@ -3077,8 +3115,11 @@ prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, doubl
 
   stepsize=(int) (stepm+YEARM-1)/YEARM;
   if (stepm<=12) stepsize=1;
-  
-  hstepm=1;
+  if(estepm < stepm){
+    printf ("Problem %d lower than %d\n",estepm, stepm);
+  }
+  else  hstepm=estepm;   
+
   hstepm=hstepm/stepm; 
   yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp  and
                                fractional in yp1 */
@@ -3111,7 +3152,7 @@ prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, doubl
           fprintf(ficresf," p%d%d",i,j);
        fprintf(ficresf," p.%d",j);
       }
-      for (yearp=0; yearp<=(anproj2-anproj1);yearp++) { 
+      for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { 
        fprintf(ficresf,"\n");
        fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);   
 
@@ -3323,7 +3364,7 @@ populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double
 int main(int argc, char *argv[])
 {
   int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
-  int i,j, k, n=MAXN,iter,m,size,cptcode, cptcod;
+  int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
   double agedeb, agefin,hf;
   double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
 
@@ -3345,7 +3386,8 @@ int main(int argc, char *argv[])
   int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
   int mobilav=0,popforecast=0;
   int hstepm, nhstepm;
-  double jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,jpyram, mpyram,anpyram,jpyram1, mpyram1,anpyram1;
+  double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
+  double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
 
   double bage, fage, age, agelim, agebase;
   double ftolpl=FTOL;
@@ -3360,7 +3402,7 @@ int main(int argc, char *argv[])
   double **varpl; /* Variances of prevalence limits by age */
   double *epj, vepp;
   double kk1, kk2;
-  double dateprev1, dateprev2,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2;
+  double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
 
   char *alph[]={"a","a","b","c","d","e"}, str[4];
 
@@ -3507,7 +3549,7 @@ int main(int argc, char *argv[])
   ungetc(c,ficpar);
 
   delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
-  delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */
+  /* delti=vector(1,npar); *//* Scale of each paramater (output from hesscov) */
   for(i=1; i <=nlstate; i++){
     for(j=1; j <=nlstate+ndeath-1; j++){
       fscanf(ficpar,"%1d%1d",&i1,&j1);
@@ -3524,6 +3566,9 @@ int main(int argc, char *argv[])
     }
   }
   delti=delti3[1][1];
+
+
+  /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
   
   /* Reads comments: lines beginning with '#' */
   while((c=getc(ficpar))=='#' && c!= EOF){
@@ -4038,6 +4083,7 @@ int main(int argc, char *argv[])
   fprintf(ficparo,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);
   fprintf(ficres,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);
 
+  probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
   freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
 
   /*------------ gnuplot -------------*/
@@ -4071,7 +4117,7 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
  - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n
  - Log file of the run: <a href=\"%s\">%s</a><br>\n
  - Gnuplot file name: <a href=\"%s\">%s</a></ul>\n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,filelog,filelog,optionfilegnuplot,optionfilegnuplot);
-  fclose(fichtm);
+   fclose(fichtm);
 
   printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);
  
@@ -4197,7 +4243,7 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
     }
   }
 
-  varprob(optionfilefiname, matcov, p, delti, nlstate, (int) bage, (int) fage,k,Tvar,nbcode, ncodemax);
+  varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax);
 
   fclose(ficrespij);
 
@@ -4205,15 +4251,15 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
   /*---------- Forecasting ------------------*/
   /*if((stepm == 1) && (strcmp(model,".")==0)){*/
   if(prevfcast==1){
-    if(stepm ==1){
+    /*    if(stepm ==1){*/
       prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
-      if (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);
-    } 
-    else{
-      erreur=108;
-      printf("Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model);
-      fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model);
-    }
+      /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
+/*      }  */
+/*      else{ */
+/*        erreur=108; */
+/*        printf("Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
+/*        fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
+/*      } */
   }
   
 
@@ -4247,7 +4293,11 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
   printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
   fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
 
-  prevalence(ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
+  /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
+  prevalence(agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
+  /*  printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d,  mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\
+ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
+  */
 
   if (mobilav!=0) {
     mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
@@ -4374,10 +4424,13 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
   
   free_matrix(covar,0,NCOVMAX,1,n);
   free_matrix(matcov,1,npar,1,npar);
-  free_vector(delti,1,npar);
+  /*free_vector(delti,1,npar);*/
+  free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel); 
   free_matrix(agev,1,maxwav,1,imx);
   free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
   if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+  free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
+
   free_ivector(ncodemax,1,8);
   free_ivector(Tvar,1,15);
   free_ivector(Tprod,1,15);
@@ -4385,9 +4438,8 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
   free_ivector(Tage,1,15);
   free_ivector(Tcode,1,100);
 
-  fprintf(fichtm,"\n</body>");
-  fclose(fichtm);
-  fclose(ficgp);
+  /*  fclose(fichtm);*/
+  /*  fclose(ficgp);*/ /* ALready done */
   
 
   if(erreur >0){