Esempio n. 1
0
/**
 *   Prépare et appelle une ACP sur les summary stats
 */
void cal_acp() {
    long double **matstat, *pca_statobs;
    //float *stat_obs;
    enregC enr;
    int *numscen, k, bidon;
    resACPC rACP;
    //header.calstatobs(statobsfilename);
    //stat_obs = header.stat_obs; 
    cout << "header.nstat=" << header.nstat << "\n";
    for (int i = 0; i < header.nstat; i++) cout << header.stat_obs[i] << "\n";
    cout << "apres read_statobs\n";
    int nparamax = 0;
    for (int i = 0; i < rt.nscen; i++) if (rt.nparam[i] > nparamax) nparamax = rt.nparam[i];
    //cout<<nparamax<<"\n";
    enr.param = vector<float>(nparamax);
    enr.stat = vector<float>(rt.nstat);
    if (nacp > rt.nrec) nacp = rt.nrec;
    matstat = new long double*[nacp];
    numscen = new int [nacp];
    pca_statobs = new long double[rt.nstat];
    rt.openfile2();
    for (int p = 0; p < nacp; p++) {
        bidon = rt.readrecord(&enr);
        if (bidon == 0) {
            matstat[p] = new long double[rt.nstat];
            numscen[p] = enr.numscen;
            for (int j = 0; j < rt.nstat; j++) matstat[p][j] = enr.stat[j];
        }
    }
    rt.closefile();
    cout << "apres la lecture des " << nacp << " enregistrements\n";
    if (header.reference) {
        long double** matstat0;
        int nacp0 = 0, *numscen0;
        matstat0 = new long double*[nacp];
        numscen0 = new int [nacp];
        for (int p = 0; p < nacp; p++) {
            if (matstat[p][0] <= 1.0) {
                matstat0[nacp0] = new long double[rt.nstat];
                numscen0[nacp0] = numscen[p];
                for (int j = 0; j < rt.nstat; j++) matstat0[nacp0][j] = matstat[p][j];
                nacp0++;
            }
        }
        nacp = nacp0;
        for (int p = 0; p < nacp; p++) {
            numscen[p] = numscen0[p];
            for (int j = 0; j < rt.nstat; j++) matstat[p][j] = matstat0[p][j];
        }
    }
    iprog += 1;
    fprog.open(progressfilename.c_str());
    fprog << iprog << "   " << nprog << "\n";
    fprog.close();
    cout << "avant ACP\n";
    rACP = ACP(nacp, rt.nstat, matstat, 1.0, 0);
    cout << "apres ACP  path =" << path << "\n";
    for (int j = 0; j < rACP.nlambda; j++) {
        pca_statobs[j] = 0.0;
        for (k = 0; k < rt.nstat; k++) if (rACP.sd[k] > 0.0) pca_statobs[j] += (header.stat_obs[k] - rACP.moy[k]) / rACP.sd[k] * rACP.vectprop[k][j];
    }
    string nomfiACP;
    nomfiACP = path + ident + "_ACP.txt";
    //strcpy(nomfiACP,path);
    //strcat(nomfiACP,ident);
    //strcat(nomfiACP,"_ACP.txt");
    cout << nomfiACP << "\n";
    ofstream f1;
    f1.open(nomfiACP.c_str());
    f1 << setiosflags(ios::fixed) << nacp << " " << rACP.nlambda;
    f1 << setprecision(3);
    for (int i = 0; i < rACP.nlambda; i++) f1 << " " << (rACP.lambda[i] / rACP.slambda);
    f1 << "\n";
    f1 << "0";
    for (int i = 0; i < rACP.nlambda; i++) f1 << " " << pca_statobs[i];
    f1 << "\n";
    for (int i = 0; i < nacp; i++) {
        f1 << numscen[i];
        for (int j = 0; j < rACP.nlambda; j++) f1 << " " << rACP.princomp[i][j];
        f1 << "\n";
    }
    f1.close();
    iprog += 1;
    fprog.open(progressfilename.c_str());
    fprog << iprog << "   " << nprog << "\n";
    fprog.close();
}
Esempio n. 2
0
/**
 *  Effectue les calculs qui localisent summary stat par summary stat le jeu de données observées par rapport à la table de référence
 */
void cal_loc() {
    long double** qobs;
    cout << "debut de cal_loc\n";
    //float *stat_obs,
    float diff;
    int scen, **avant, **apres, **egal, nparamax = 0, npartpos = 0, bidon;
    enregC enr;
    string** star;
    //header.calstatobs(statobsfilename);
    //stat_obs = header.stat_obs;  //cout<<"apres read_statobs\n";
    for (int i = 0; i < rt.nscen; i++) if (rt.nparam[i] > nparamax) nparamax = rt.nparam[i];
    cout << nparamax << "\n";
    enr.param = vector<float>(nparamax);
    enr.stat = vector<float>(rt.nstat);
    qobs = new long double*[rt.nscen];
    for (int i = 0; i < rt.nscen; i++) qobs[i] = new long double[rt.nstat];
    star = new string*[rt.nscen];
    for (int i = 0; i < rt.nscen; i++) star[i] = new string[rt.nstat];
    avant = new int*[rt.nscen];
    for (int i = 0; i < rt.nscen; i++) {
        avant[i] = new int[rt.nstat];
        for (int j = 0; j < rt.nstat; j++) avant[i][j] = 0;
    }
    apres = new int*[rt.nscen];
    for (int i = 0; i < rt.nscen; i++) {
        apres[i] = new int[rt.nstat];
        for (int j = 0; j < rt.nstat; j++) apres[i][j] = 0;
    }
    egal = new int*[rt.nscen];
    for (int i = 0; i < rt.nscen; i++) {
        egal[i] = new int[rt.nstat];
        for (int j = 0; j < rt.nstat; j++) egal[i][j] = 0;
    }
    rt.openfile2();
    cout << "avant la lecture des enregistrements  header.reference = " << header.reference << "\n";
    for (int p = 0; p < rt.nrec; p++) {
        if (not header.reference) {
            bidon = rt.readrecord(&enr);
            if (bidon != 0) cout << "probleme dans la lecture de la table de référence n=" << p << "\n";
        } else {
            do {
                bidon = rt.readrecord(&enr);
            } while ((bidon == 0)and (enr.stat[0] > 1.0));
        }
        npartpos++;
        scen = enr.numscen - 1;
        for (int j = 0; j < rt.nstat; j++) {
            diff = header.stat_obs[j] - enr.stat[j];
            if (diff > 0.001) avant[scen][j]++;
            else if (diff < -0.001) apres[scen][j]++; else egal[scen][j]++;
        }
    }
    rt.closefile();
    cout << "apres la lecture des " << rt.nrec << " enregistrements\n";
    for (int j = 0; j < rt.nstat; j++) {
        for (int i = 0; i < rt.nscen; i++) {
            qobs[i][j] = (long double)(avant[i][j] + apres[i][j] + egal[i][j]);
            if (qobs[i][j] > 0.0) qobs[i][j] = (0.5 * (long double)egal[i][j] + (long double)avant[i][j]) / qobs[i][j]; else qobs[i][j] = -1;
            star[i][j] = "      ";
            if ((qobs[i][j] > 0.95)or (qobs[i][j] < 0.05)) star[i][j] = " (*)  ";
            if ((qobs[i][j] > 0.99)or (qobs[i][j] < 0.01)) star[i][j] = " (**) ";
            if ((qobs[i][j] > 0.999)or (qobs[i][j] < 0.001)) star[i][j] = " (***)";
        }
        cout << setiosflags(ios::left) << setw(15) << header.statname[j] << "    (" << setiosflags(ios::fixed) << setw(8) << setprecision(4) << header.stat_obs[j] << ")   ";
        for (int i = 0; i < rt.nscen; i++) cout << setiosflags(ios::fixed) << setw(8) << setprecision(4) << qobs[i][j] << star[i][j] << "  ";
        cout << "\n";
    }
    string nomfiloc;
    nomfiloc = path + ident + "_locate.txt";
    //strcpy(nomfiloc,path);
    //strcat(nomfiloc,ident);
    //strcat(nomfiloc,"_locate.txt");
    cout << nomfiloc << "\n";
    time_t rawtime;
    struct tm* timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    ofstream f12(nomfiloc.c_str(), ios::out);
    f12 << "DIYABC :                   PRIOR CHECKING                         " << asctime(timeinfo) << "\n";
    f12 << "Data file                     : " << header.datafilename << "\n";
    f12 << "Reference table               : " << rt.filename << "\n";
    f12 << "Number of simulated data sets : " << rt.nrec << "\n";
    if (header.reference) f12 << "Number of simulated data sets with positive weight : " << npartpos << "\n";
    f12 << "\n";
    f12 << "Values indicate for each summary statistics the proportion \nof simulated data sets which have a value below the observed one\n";
    f12 << " Summary           observed";
    for (int i = 0; i < rt.nscen; i++) f12 << "    scenario   ";
    f12 << "\n";
    f12 << "statistics           value ";
    for (int i = 0; i < rt.nscen; i++) f12 << "      " << setw(3) << i + 1 << "      ";
    f12 << "\n";
    for (int j = 0; j < rt.nstat; j++) {
        f12 << setiosflags(ios::left) << setw(15) << header.statname[j] << "    (" << setiosflags(ios::fixed) << setw(8) << setprecision(4) << header.stat_obs[j] << ")   ";
        for (int i = 0; i < rt.nscen; i++) {
            f12 << setiosflags(ios::fixed) << setw(6) << setprecision(4) << qobs[i][j] << star[i][j] << "   ";
        }
        f12 << "\n";
    }
    f12.close();
    iprog += 1;
    fprog.open(progressfilename.c_str());
    fprog << iprog << "   " << nprog << "\n";
    fprog.close();
    cout << iprog << "  " << nprog << "\n";
}
Esempio n. 3
0
    void domodchec(string opt,int seed){
        int nstatOK, nphistarOK, iprog,nprog;
        int nrec = 0,nsel = 0, ns,nrecpos = 0,newsspart = 0,npv,npvmax,nss,nsr,newrefpart,*numscen,nparamax,bidon;
        string s,s0,s1,snewstat;
        vector<string> ss, ss1;
        //float  *stat_obs;
        bool usestats,firsttime,dopca = false,doloc = false, newstat=false;
        long double** matC, **phistar, **phistarcompo, **phistarscaled;

        progressfilename = path + ident + "_progress.txt";
		scurfile = path + "first_records_of_the_reference_table_"+ ident +".txt";
        cout<<"debut domodchec  options : "<<opt<<"\n";
		original=true;composite=false;scaled=false;
        splitwords(opt,";",ss); ns = ss.size();
        numtransf=3;
        for (int i=0;i<ns;i++) { cout<<ss[i]<<"\n";
            s0=ss[i].substr(0,2);
            s1=ss[i].substr(2);
            if (s0=="s:") {
                splitwords(s1,",",ss1); rt.nscenchoisi = ss1.size();
                if (rt.nscenchoisi>1) rt.nscenchoisi=1;
                rt.scenchoisi = new int[rt.nscenchoisi];
                for (int j=0;j<rt.nscenchoisi;j++) rt.scenchoisi[j] = atoi(ss1[j].c_str());
                nrecpos=rt.nrecscen[rt.scenchoisi[0]-1];
                cout <<"scenario choisi : "<<rt.scenchoisi[0]<<"\n";
                rt.scenteste = rt.scenchoisi[0];
            } else if (s0=="n:") {
                nrec=atoi(s1.c_str());
                if(nrec>nrecpos) nrec=nrecpos;
                cout<<"nombre total de jeux de données considérés (pour le scénario choisi )= "<<nrec<<"\n";
            } else if (s0=="m:") {
                nsel=atoi(s1.c_str());
                cout<<"nombre de jeux de données considérés pour la régression locale = "<<nsel<<"\n";
            } else if (s0=="t:") {
                numtransf=atoi(s1.c_str());
                switch (numtransf) {
                  case 1 : cout <<" pas de transformation des paramètres\n";break;
                  case 2 : cout <<" transformation log des paramètres\n";break;
                  case 3 : cout <<" transformation logit des paramètres\n";break;
                  case 4 : cout <<" transformation log(tg) des paramètres\n";break;
                }
            } else if (s0=="v:") {
                cout<<""<< "\n";
                snewstat=s1;
                newstat = (s1.length()>0);
            } else if (s0=="q:") {
                newsspart=atoi(s1.c_str());
                cout<<"nombre de particules à simuler à partir du posterior = "<<newsspart<<"\n";
            } else if (s0=="a:") {
                dopca=(s1.find("p")!=string::npos);
                doloc=(s1.find("l")!=string::npos);
                if (dopca) cout <<"Perform ACP  ";
                if ((s1=="pl")or(s1=="lp")) cout <<"et ";
                if (doloc) cout<<"locate  ";
                cout<< "\n";

            }
        }
        nprog=newsspart+100;
        if ((newstat)and(dopca)) nprog += scenario.size()*10000;
        iprog=10;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
        nstatOK = rt.cal_varstat();                       cout<<"apres cal_varstat   nstatOK="<<nstatOK<<"\n";
        //header.calstatobs(statobsfilename);
		//stat_obs = header.stat_obs;  cout<<"apres read_statobs\n";
        cout<<"nrec="<<nrec<<"     nsel="<<nsel<<"\n";
        rt.alloue_enrsel(nsel);
        rt.cal_dist(nrec,nsel,&header.stat_obs[0],false,false);                  cout<<"apres cal_dist\n";
        iprog+=40;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
        det_numpar();
		cout<<"apres det_numpar\n";
        rempli_mat(nsel,&header.stat_obs[0]);                        cout<<"apres rempli_mat\n";
		if (not deltanul) matC = cal_matC(nsel); 
        recalparamO(nsel);                                 cout<<"apres recalparam\n";
		if (not deltanul) {
			rempli_parsim(nsel,nparamcom);            			cout<<"apres rempli_parsim(O)\n";
			local_regression(nsel,nparamcom,matC);              cout<<"apres local_regression\n";
		}
		iprog+=20;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
		phistar = new long double* [nsel];
		for (int i=0;i<nsel;i++) phistar[i] = new long double[nparamcom];
        if (not deltanul) calphistarO(nsel,phistar);else copyphistar(nsel,nparamcom,phistar); cout<<"apres calphistar\n";
        det_nomparam();
        phistarcompo = new long double*[nsel];phistarscaled = new long double*[nsel];
        for (int i=0;i<nsel;i++) {phistarcompo[i] = new long double[0];phistarscaled[i] = new long double[0];}
		savephistar(nsel,path,ident,phistar,phistarcompo,phistarscaled);                     cout<<"apres savephistar\n";
        iprog+=20;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
        phistarOK = new long double*[nsel];
        for (int i=0;i<nsel;i++) phistarOK[i] = new long double[rt.nparam[rt.scenteste-1]];
		cout<<"scenario[rt.scenteste-1].nparam = "<<scenario[rt.scenteste-1].nparam<<"\n";
		nphistarOK=detphistarOK(nsel,phistar);               cout << "apres detphistarOK  nphistarOK="<<nphistarOK<<"\n";
		//for (int i=0;i<nsel;i++) delete [] phistar[i];delete phistar;
		
        cout//<<"naparamcom="<<nparamcom<<"   nparcompo="<<nparcompo<<"   nenr="<<nenr
            << "   nphistarOK="<< nphistarOK<<"   nstat="<<header.nstat<<"\n";
        //cout <<"DEBUG: j'arrête là." << endl; exit(1);
        if(nphistarOK < 100){
        	cout << "Not enough suitable particles ("<<nphistarOK<<")to perform model checking. Stopping computations." << endl;
        	exit(1);
        }
        npv = rt.nparam[rt.scenteste-1];
		npvmax = npv; for (int i=0;i<(int)scenario.size();i++) if (npvmax<rt.nparam[i]) npvmax=rt.nparam[i];
		cout<<"npv="<<npv<<"    nenr="<<nenr<<"   nstat="<<header.nstat<<"\n";
		cout<<"npvmax = "<<npvmax<<"\n";
		//delete []enreg;
        nss=0;
        firsttime=true;
        cout<<"ns="<<ns<<"\n";
        //cout<<phistarOK[0][0]<<"\n";
        if (newstat) usestats = resetstats(snewstat); if (not usestats) cout<<"problème dans la construction des nouvelles stats\n";
        cout<<"header.nstat = "<<header.nstat<<"\n";
        enreg = new enregC[nenr];
		cout<<"apres new enregC[nenr]\n";
        for (int p=0;p<nenr;p++) {
            enreg[p].stat = vector <float>(header.nstat);
            enreg[p].param = vector <float>(npvmax);
            enreg[p].numscen = rt.scenteste;
        }
        cout<<"apres dimensionnement des enreg nstat="<<header.nstat<<"   nparam="<<npv<<"   nenr="<<nenr<<"\n";
        ssphistar = new long double*[newsspart];
        for (int i=0;i<newsspart;i++) ssphistar[i] = new long double[header.nstat];
        cout<<"newstat ="<<newstat<<"   newsspart="<<newsspart<<"     nenr="<<nenr<<"\n";
		if (nenr>newsspart) nenr=newsspart;
        while (nss<newsspart) {
			if (debuglevel==2) cout<<"avant dosimulphistar\n";
            ps.dosimulphistar(nenr,false,multithread,firsttime,rt.scenteste,seed,nphistarOK);
			if (debuglevel==2) cout<<"apres dosimulphistar\n";
            for (int i=0;i<nenr;i++) {
                for (int j=0;j<header.nstat;j++) ssphistar[i+nss][j]=enreg[i].stat[j];
                for (int j=0;j<header.nstat;j++) cout<<ssphistar[i+nss][j]<<"   ";cout<<"\n";
            }
            firsttime=false;
            nss+=nenr;
            iprog+=nenr;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
            //cout<<nss<<"\n";
        }
        if (newstat) {
			//cout<<"\n\n\nAVANT CALSTATOBS\n";
			header.calstatobs(statobsfilename);/*stat_obs = header.stat_obs;*/
			//cout<<"\nAPRES CALSTATOBS\n\n\n";
		}
        if (doloc) call_loc(newsspart,nrec,nsel,ssphistar,&header.stat_obs[0]);
        if (dopca) {
            if (newstat) {
                header.readHeader(headerfilename);cout<<"apres readHeader scenario.size()= "<<scenario.size()<<"\n";
                usestats = resetstats(snewstat);
                newrefpart= 10000; cout<<"newrefparts="<<newrefpart<<"\n";
                numscen = new int[newrefpart];
                ssref = new long double*[newrefpart];
                for (int i=0;i<newrefpart;i++) ssref[i] = new long double[header.nstat];
                nsr=0;
                firsttime=true;
				cout<<"avant le while (nsr<newrefpart)\n";
                while (nsr<newrefpart) {
                    ps.dosimultabref(nenr,false,multithread,firsttime,0,seed,3);
                    for (int i=0;i<nenr;i++) {
                      numscen[i+nsr] = enreg[i].numscen;
                      for (int j=0;j<header.nstat;j++) ssref[i+nsr][j]=enreg[i].stat[j];
                      if (debuglevel==2) {for (int j=0;j<header.nstat;j++) cout<<ssref[i+nsr][j]<<"   ";cout<<"\n";}
                    }
                    firsttime=false;
                    nsr+=nenr;
                    iprog+=nenr;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
                    cout<<nsr<<"\n";
                }
            } else {
                rt.openfile2();
                enregC enr;
                nparamax=0;for (int i=0;i<rt.nscen;i++)  if (rt.nparam[i]>nparamax) nparamax=rt.nparam[i];
                enr.stat = vector <float>(rt.nstat);
                enr.param = vector <float>(nparamax);
                newrefpart= scenario.size()*10000; cout<<"newrefparts="<<newrefpart<<"\n";
                numscen = new int[newrefpart];
                ssref = new long double*[newrefpart];
                for (int i=0;i<newrefpart;i++) ssref[i] = new long double[header.nstat];
                nsr=0;
                while (nsr<newrefpart) {
                    bidon = rt.readrecord(&enr); if (bidon!=0) cout<<"probleme à la lecture du reftable\n";
                    numscen[nsr] = enr.numscen;
                    for (int j=0;j<rt.nstat;j++) ssref[nsr][j]=enr.stat[j];
                    nsr++;
                }
            }
            call_acp(newrefpart,newsspart,header.nstat,numscen,ssref,ssphistar,&header.stat_obs[0]);
        }
        iprog+=10;fprog.open(progressfilename.c_str());fprog<<iprog<<"   "<<nprog<<"\n";fprog.close();
    }