Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

fpmm.cpp

Go to the documentation of this file.
00001 //****************************************************
00002 //  April, 1993, University of Illinois
00003 // Copyright (C) 1993, 1994 Tianlin Wang
00004 /* Copyright (C) 1994-2003 Matvec Development Team. 
00005 
00006   This program is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010   
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015     
00016   You should have received a copy of the GNU Library General Public
00017   License along with this library; if not, write to the Free
00018   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019   MA 02111-1307, USA 
00020 */
00021 
00022 #include "fpmm.h"
00023 
00024 namespace matvec {
00025 
00026 Fpmm::Fpmm(Fpmm& F){
00027  trm  = F.trm;
00028  ndim = F.ndim;
00029  genval = F.genval;
00030  genfreq = F.genfreq;
00031  freq = F.freq;
00032 }
00033 
00034 void Fpmm::setup_trm(int nl, double fq){
00035 
00036   int locus,m, f, o,m1, f1, o1;
00037   int ndimt;
00038   Vector<double> gf(3); // genotypic freqs for 1 locus
00039   double sum;
00040   Dblock trmt;  // temporary transmission matrix; used to update trm
00041   Dblock trm1;  // transmission matrix for one locus
00042  
00043   freq = 1 - fq; // to be consistent with SALP freq is the frequency of the bad allele
00044   ndim=2*nl+1;
00045 
00046   trm.resize(ndim,ndim,ndim);
00047   genfreq.resize(ndim);
00048   if (ndim == 1) {
00049     // We have no polygenic component so ndim=ncol=nrow=1 so that
00050     // trm is 1 and genfreq=1
00051     trm[0][0][0]=1.0;
00052     genfreq[0]=1.0;
00053   }
00054   else {
00055   trmt.resize(ndim,ndim,ndim);
00056   trm1.resize(3,3,3);
00057  
00058   gf(1) = (1-freq)*(1-freq);
00059   gf(2) = 2*freq*(1-freq);
00060   gf(3) = freq*freq;
00061 
00062   // transition probs; father, mother, child
00063   double A[3][3][3] = 
00064 {1.0, 0.0, 0.0,   0.5,  0.5, 0.0,    0.0, 1.0, 0.0,
00065  0.5, 0.5, 0.0,   0.25, 0.5, 0.25,   0.0, 0.5, 0.5,
00066  0.0, 1.0, 0.0,   0.0,  0.5, 0.5,    0.0,0.0,1.0};
00067 
00068 // calculating trm1
00069 
00070  for (f=0;f<3;f++){
00071     for (m=0;m<3;m++){
00072        for (o=0;o<3;o++){
00073          trm1[f][m][o] = A[f][m][o]*gf[f]*gf[m];
00074          trmt[f][m][o] = trm1[f][m][o];
00075          trm[f][m][o] = trm1[f][m][o];
00076        }
00077     }
00078  }
00079 
00080  for (locus=2; locus<=nl; locus++){
00081    // inittialize trm to zero
00082    ndimt = 2*locus + 1;
00083     for (f=0;f<ndimt;f++){
00084       for (m=0;m<ndimt;m++){
00085        for (o=0;o<ndimt;o++){
00086          trm[f][m][o] = 0.0;
00087        }
00088       }
00089     }
00090    // go through all combinations of trmt
00091     ndimt = 2*(locus-1) + 1;
00092     for (f=0;f<ndimt;f++){
00093       for (m=0;m<ndimt;m++){
00094        for (o=0;o<ndimt;o++){
00095        // make appropriate contributions for each comb. of trm1
00096          for (f1=0;f1<3;f1++){
00097            for (m1=0;m1<3;m1++){
00098              for (o1=0;o1<3;o1++){
00099                trm[f+f1][m+m1][o+o1] += trmt[f][m][o]*trm1[f1][m1][o1];
00100              }
00101            }
00102          }
00103        }
00104       }
00105     }
00106     ndimt = 2*locus + 1;
00107     for (f=0;f<ndimt;f++){
00108       for (m=0;m<ndimt;m++){
00109        for (o=0;o<ndimt;o++){
00110          trmt[f][m][o] = trm[f][m][o];
00111        }
00112       }
00113     }
00114  }
00115     for (f=0;f<ndim;f++){
00116       genfreq[f] = 0.0;
00117       for (m=0;m<ndim;m++){
00118         sum = 0.0;
00119         for (o=0;o<ndim;o++){ 
00120           sum += trm[f][m][o];
00121         }
00122         genfreq[f] += sum;
00123         for (o=0;o<ndim;o++){ 
00124           trm[f][m][o] /= sum;
00125         }
00126       }
00127     }
00128   }
00129   //  std::cout << genfreq;
00130 
00131 }
00132 
00133 void Fpmm::setup_genval(double vr){
00134   var = vr;  
00135   int gen;
00136   double mean=0.0, vart=0.0,scale;
00137 
00138   genval.resize(ndim);
00139   if (ndim == 1) {
00140     // no polygenic component
00141     genval[0]=0.0;
00142   }
00143   else {
00144     for (gen=0; gen<ndim; gen++){
00145       mean += gen*genfreq[gen];
00146     }
00147     for (gen=0; gen<ndim; gen++){
00148       vart += (gen - mean)*(gen - mean)*genfreq[gen];
00149     }
00150     scale = std::sqrt(var/vart);
00151     for (gen=0; gen<ndim; gen++){
00152       genval[gen] = (gen - mean)*scale;
00153     }
00154   }
00155 }
00156 
00157 int Fpmm::display(void){
00158   std::cout << "Fpmm Object: Polygenic Values" << std::endl;
00159   std::cout << genval << std::endl;
00160   return 1;
00161 }
00162 
00163 } ///////// end of namespace matvec
00164 

Generated on Thu Jun 16 17:13:40 2005 for Matvec by doxygen1.2.16