Diff for /imach/src/imach.c between versions 1.278 and 1.279

version 1.278, 2017/07/19 14:09:02 version 1.279, 2017/07/20 13:35:01
Line 1 Line 1
 /* $Id$  /* $Id$
   $State$    $State$
   $Log$    $Log$
     Revision 1.279  2017/07/20 13:35:01  brouard
     Summary: temporary working
   
   Revision 1.278  2017/07/19 14:09:02  brouard    Revision 1.278  2017/07/19 14:09:02  brouard
   Summary: Bug for mobil_average=0 and prevforecast fixed(?)    Summary: Bug for mobil_average=0 and prevforecast fixed(?)
   
Line 2516  void powell(double p[], double **xi, int Line 2519  void powell(double p[], double **xi, int
       
   double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)    double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
   {    {
     /* Computes the prevalence limit in each live state at age x and for covariate combination ij       /**< Computes the prevalence limit in each live state at age x and for covariate combination ij 
        (and selected quantitative values in nres)       *   (and selected quantitative values in nres)
        by left multiplying the unit       *  by left multiplying the unit
        matrix by transitions matrix until convergence is reached with precision ftolpl */       *  matrix by transitions matrix until convergence is reached with precision ftolpl 
   /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1  = Wx-n Px-n ... Px-2 Px-1 I */       * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1  = Wx-n Px-n ... Px-2 Px-1 I
   /* Wx is row vector: population in state 1, population in state 2, population dead */       * Wx is row vector: population in state 1, population in state 2, population dead
   /* or prevalence in state 1, prevalence in state 2, 0 */       * or prevalence in state 1, prevalence in state 2, 0
   /* newm is the matrix after multiplications, its rows are identical at a factor */       * newm is the matrix after multiplications, its rows are identical at a factor.
   /* Initial matrix pimij */       * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
        * Output is prlim.
        * Initial matrix pimij 
        */
   /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */    /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
   /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */    /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
   /*  0,                   0                  , 1} */    /*  0,                   0                  , 1} */
Line 5772  void  concatwav(int wav[], int **dh, int Line 5778  void  concatwav(int wav[], int **dh, int
 /************ Variance ******************/  /************ Variance ******************/
  void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)   void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)
  {   {
    /* Variance of health expectancies */     /** Variance of health expectancies 
    /*  double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/      *  double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
    /* double **newm;*/      * double **newm;
    /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/      * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav) 
       */
       
    /* int movingaverage(); */     /* int movingaverage(); */
    double **dnewm,**doldm;     double **dnewm,**doldm;
Line 5783  void  concatwav(int wav[], int **dh, int Line 5790  void  concatwav(int wav[], int **dh, int
    int i, j, nhstepm, hstepm, h, nstepm ;     int i, j, nhstepm, hstepm, h, nstepm ;
    int k;     int k;
    double *xp;     double *xp;
    double **gp, **gm;  /* for var eij */     double **gp, **gm;  /**< for var eij */
    double ***gradg, ***trgradg; /*for var eij */     double ***gradg, ***trgradg; /**< for var eij */
    double **gradgp, **trgradgp; /* for var p point j */     double **gradgp, **trgradgp; /**< for var p point j */
    double *gpp, *gmp; /* for var p point j */     double *gpp, *gmp; /**< for var p point j */
    double **varppt; /* for var p point j nlstate to nlstate+ndeath */     double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
    double ***p3mat;     double ***p3mat;
    double age,agelim, hf;     double age,agelim, hf;
    /* double ***mobaverage; */     /* double ***mobaverage; */
Line 5848  void  concatwav(int wav[], int **dh, int Line 5855  void  concatwav(int wav[], int **dh, int
    /* fprintf(fichtm, "#Local time at start: %s", strstart);*/     /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
    fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");     fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");
    fprintf(fichtm,"\n<br>%s  <br>\n",digitp);     fprintf(fichtm,"\n<br>%s  <br>\n",digitp);
    /*   } */  
    varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);     varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
    pstamp(ficresvij);     pstamp(ficresvij);
    fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n#  (weighted average of eij where weights are ");     fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n#  (weighted average of eij where weights are ");
Line 5903  void  concatwav(int wav[], int **dh, int Line 5910  void  concatwav(int wav[], int **dh, int
        for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/         for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
          xp[i] = x[i] + (i==theta ?delti[theta]:0);           xp[i] = x[i] + (i==theta ?delti[theta]:0);
        }         }
                                  /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and 
           * returns into prlim .
           */              
        prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);         prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
                           
          /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
        if (popbased==1) {         if (popbased==1) {
          if(mobilav ==0){           if(mobilav ==0){
            for(i=1; i<=nlstate;i++)             for(i=1; i<=nlstate;i++)
Line 5915  void  concatwav(int wav[], int **dh, int Line 5925  void  concatwav(int wav[], int **dh, int
              prlim[i][i]=mobaverage[(int)age][i][ij];               prlim[i][i]=mobaverage[(int)age][i][ij];
          }           }
        }         }
                                  /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}_x\f$ at horizon h.
        hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);  /* Returns p3mat[i][j][h] for h=1 to nhstepm */          */                      
          hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);  /* Returns p3mat[i][j][h] for h=0 to nhstepm */
          /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}_x\f$, which are the probability
           * at horizon h in state j including mortality.
           */
        for(j=1; j<= nlstate; j++){         for(j=1; j<= nlstate; j++){
          for(h=0; h<=nhstepm; h++){           for(h=0; h<=nhstepm; h++){
            for(i=1, gp[h][j]=0.;i<=nlstate;i++)             for(i=1, gp[h][j]=0.;i<=nlstate;i++)
              gp[h][j] += prlim[i][i]*p3mat[i][j][h];               gp[h][j] += prlim[i][i]*p3mat[i][j][h];
          }           }
        }         }
        /* Next for computing probability of death (h=1 means         /* Next for computing shifted+ probability of death (h=1 means
           computed over hstepm matrices product = hstepm*stepm months)             computed over hstepm matrices product = hstepm*stepm months) 
           as a weighted average of prlim.            as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
        */         */
        for(j=nlstate+1;j<=nlstate+ndeath;j++){         for(j=nlstate+1;j<=nlstate+ndeath;j++){
          for(i=1,gpp[j]=0.; i<= nlstate; i++)           for(i=1,gpp[j]=0.; i<= nlstate; i++)
            gpp[j] += prlim[i][i]*p3mat[i][j][1];             gpp[j] += prlim[i][i]*p3mat[i][j][1];
        }             }
        /* end probability of death */         
          /* Again with minus shift */
                                                   
        for(i=1; i<=npar; i++) /* Computes gradient x - delta */         for(i=1; i<=npar; i++) /* Computes gradient x - delta */
          xp[i] = x[i] - (i==theta ?delti[theta]:0);           xp[i] = x[i] - (i==theta ?delti[theta]:0);
Line 5964  void  concatwav(int wav[], int **dh, int Line 5979  void  concatwav(int wav[], int **dh, int
          for(i=1,gmp[j]=0.; i<= nlstate; i++)           for(i=1,gmp[j]=0.; i<= nlstate; i++)
            gmp[j] += prlim[i][i]*p3mat[i][j][1];             gmp[j] += prlim[i][i]*p3mat[i][j][1];
        }             }    
        /* end probability of death */         /* end shifting computations */
                           
          /**< Computing gradient matrix at horizon h 
           */
        for(j=1; j<= nlstate; j++) /* vareij */         for(j=1; j<= nlstate; j++) /* vareij */
          for(h=0; h<=nhstepm; h++){           for(h=0; h<=nhstepm; h++){
            gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];             gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
          }           }
                                  /**< Gradient of overall mortality p.3 (or p.j) 
        for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */          */
          for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
          gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];           gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
        }         }
                                                   
      } /* End theta */       } /* End theta */
                        
        /* We got the gradient matrix for each theta and state j */                
      trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */       trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
                                   
      for(h=0; h<=nhstepm; h++) /* veij */       for(h=0; h<=nhstepm; h++) /* veij */
Line 5987  void  concatwav(int wav[], int **dh, int Line 6006  void  concatwav(int wav[], int **dh, int
      for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */       for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
        for(theta=1; theta <=npar; theta++)         for(theta=1; theta <=npar; theta++)
          trgradgp[j][theta]=gradgp[theta][j];           trgradgp[j][theta]=gradgp[theta][j];
                        /**< as well as its transposed matrix 
         */                
                                   
      hf=hstepm*stepm/YEARM;  /* Duration of hstepm expressed in year unit. */       hf=hstepm*stepm/YEARM;  /* Duration of hstepm expressed in year unit. */
      for(i=1;i<=nlstate;i++)       for(i=1;i<=nlstate;i++)
        for(j=1;j<=nlstate;j++)         for(j=1;j<=nlstate;j++)
          vareij[i][j][(int)age] =0.;           vareij[i][j][(int)age] =0.;
                   
        /* Computing trgradg by matcov by gradg at age and summing over h
         * and k (nhstepm) formula 15 of article
         * Lievre-Brouard-Heathcote
         */
        
      for(h=0;h<=nhstepm;h++){       for(h=0;h<=nhstepm;h++){
        for(k=0;k<=nhstepm;k++){         for(k=0;k<=nhstepm;k++){
          matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);           matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
Line 6004  void  concatwav(int wav[], int **dh, int Line 6029  void  concatwav(int wav[], int **dh, int
        }         }
      }       }
                                   
      /* pptj */       /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
         * p.j overall mortality formula 49 but computed directly because
         * we compute the grad (wix pijx) instead of grad (pijx),even if
         * wix is independent of theta.
         */
      matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);       matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
      matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);       matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
      for(j=nlstate+1;j<=nlstate+ndeath;j++)       for(j=nlstate+1;j<=nlstate+ndeath;j++)
Line 10941  int main(int argc, char *argv[]) Line 10970  int main(int argc, char *argv[])
       break;        break;
   }    }
   if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){    if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
     if (num_filled == 0){      if (num_filled != 1){
       printf("ERROR %d: Model should be at minimum 'model=1+age.' WITHOUT space:'%s'\n",num_filled, line);        printf("ERROR %d: Model should be at minimum 'model=1+age' %s\n",num_filled, line);
       fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' WITHOUT space:'%s'\n",num_filled, line);        fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age' %s\n",num_filled, line);
       model[0]='\0';  
       goto end;  
     } else if (num_filled != 1){  
       printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);  
       fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);  
       model[0]='\0';        model[0]='\0';
       goto end;        goto end;
     }      }
Line 10970  int main(int argc, char *argv[]) Line 10994  int main(int argc, char *argv[])
   fflush(ficlog);    fflush(ficlog);
   /* if(model[0]=='#'|| model[0]== '\0'){ */    /* if(model[0]=='#'|| model[0]== '\0'){ */
   if(model[0]=='#'){    if(model[0]=='#'){
     printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \      printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
  'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \   'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
  'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n");          \   'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n");            \
     if(mle != -1){      if(mle != -1){
       printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");        printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter vectors and subdiagonal covariance matrix.\n");
       exit(1);        exit(1);
     }      }
   }    }

Removed from v.1.278  
changed lines
  Added in v.1.279


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>