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

matvec::Model Class Reference

#include <model.h>

Inheritance diagram for matvec::Model:

matvec::Minimizer matvec::GLMM List of all members.

Detailed Description

a generalized linear mixed model

term[t].classi =
'F'a fixed term, including intercept
'C'a covariate term
'R'a regular random term
'P'a random term associated with a pedigree

  factor_struct[i].classi = 'F', a fixed factor
                          = 'I', intercept
                          = 'C', covariate factor
                          = 'P', a factor associated with a pedigree

  weightinverse  = 0, no inverse is needed (default)
                 = 1, inverse is needed
 * 

See also:
Data

Definition at line 88 of file model.h.

Public Types

enum  ModelType { bad_model, fixed_model, mixed_model, reg_model }

Public Methods

 Model (void)
 Model (const std::string &modelspecs)
 Model (const Model &M)
virtual ~Model (void)
int display (void) const
virtual int prepare_data (const std::string &solver="ysmp")
unsigned nonzero (void) const
unsigned mmesize (void) const
unsigned ntrait (void) const
unsigned nterm (void) const
unsigned totalnvc (void) const
void trait_effect_level (const unsigned mmeaddr, std::string &retval, int info_flag=1)
void initialize (void)
void copyfrom (const Model &M)
void release (void)
void nonzero (const unsigned nz)
void prior_dist (const std::string &termname, Pedigree &P, GeneticDist *D)
void prior_dist (const std::string &termname, GeneticDist *D)
void DGSamplerSetup (unsigned numLoci, Data *D)
void DGSampler (unsigned numOfSamples, unsigned numOfSL, unsigned numOfHaplo, unsigned numOfSLCas)
void RSamplerPrior_dist (const std::string &termname, Pedigree &P, Data *D, GeneticDist *G)
 method to specify the prior distributions for each terms in the model for which the R-sampler is invoked; it also sets up the structures needed by the peeling process.

void RSampler (RSamplerParms rsParms)
void RSampler (std::string fileName)
void RSamplerInitialDG (const std::string &samplerType="genotypic", const std::string &whatToCompute="probDescHaplo")
 method to generate the initial descent graph to be used by pop_graph

void RSampler (unsigned pedBlockSize, unsigned numLoci, unsigned numOfSamples, const std::string &samplerType, const std::string &howToSample, const std::string &samplerUsed, const std::string &whatToCompute)
void RSamplerGibbs (unsigned pedBlockSize, unsigned numLoci, unsigned numOfSamples, const std::string &samplerType="genotypic", const std::string &whatToCompute="genotypeProb")
 method to sample ordered genotypes using the Gibbs sampler to sample across loci ( the first sample is obtained by sequential imputation)

void RSamplerMH (unsigned pedBlockSize, unsigned numLoci, unsigned numOfSamples, const std::string &samplerType="genotypic", const std::string &whatToCompute="genotypeProb")
 method to sample ordered genotypes using the Metropolis Hastings algorithm to accept samples proposed by sequential imputation.

void RSamplerMH (unsigned numLoci, unsigned numOfSamples, const std::string &samplerType="allelic", const std::string &whatToCompute="genotypeProb")
void RSamplerGibbsMH (unsigned pedBlockSize, unsigned numLoci, unsigned numOfSamples, unsigned stepGibbsMH, const std::string &samplerType="genotypic", const std::string &whatToCompute="genotypeProb")
Matrix< double > getIBDMatrix (void)
void variance (const std::string &termname, const doubleMatrix &v)
void variance (const std::string &termname, Pedigree &P, const doubleMatrix &v)
void variance (const std::string &termname, const double v00,...)
void variance (const std::string &termname, Pedigree &P, const double v00,...)
void VarLink (const std::string &termname, const std::string &termname2)
void covariance (const std::string &termname, const std::string &termname2, const doubleMatrix &v)
void covariance (const std::string &termname, const std::string &termname2, const double v00,...)
void weight (const std::string &wtname, const int r=0)
void covariate (const std::string &factorname)
int equation (const std::string &modelspecs)
void fitdata (Data &D)
void info (std::ostream &stream) const
void var2vec (Vector< double > &x)
void vec2var (const Vector< double > &x)
void release_mme (void)
void vce_gibbs_sampler (const unsigned nvc, Vector< double > &vc, const Vector< double > &ratio, const Vector< unsigned > &nlevel, const double yy, double &ee, Vector< double > &uAu, Vector< double > &bu, Vector< double > &wspace, const Vector< double > &zz)
void update_mme (const Vector< double > &ratio, const Vector< double > &oldratio, const Vector< double > &zz)
void compute_xbzu (Vector< double > &bu)
Vector< double > get_solutions (const std::string &termname)
void save (const std::string &fname, const int io_mode=std::ios::out)
void sampleW (Vector< double > &sol, Vector< double > &newrhs)
void vce_emreml_single_trait (const int miter=1000, const int intive=1, const double stoptol=1.0e-5)
void vce_emreml_multi_trait (const int miter=1000, const int intive=1, const double stoptol=1.0e-5)
void uAu_trCA (Vector< double > &tsol, Vector< double > &uAu, Vector< double > &trca, Vector< double > &ivect, Vector< double > &invect, const Vector< double > &ratio, const Vector< double > &zz)
Vector< double > * blup (const std::string &method="ysmp", double stopval=0.001, double relax=1.0, unsigned mxiter=100000)
void blup_pccg (double stopval=1.0e-8, unsigned int mxiter=100000)
Vector< double > * blue (const std::string &method="ysmp", double stopval=0.001, double relax=1.0, unsigned mxiter=100000)
Vector< double > * get_blupsol (const std::string &method="ysmp", double stopval=0.001, double relax=1.0, unsigned mxiter=100000)
Vector< double > * vce_emreml (const int miter=1000, const int intive=1, const double stoptol=1.0e-4)
Vector< double > * vce_gibbs (const unsigned warmup=2000, const unsigned gibbslen=5000)
Vector< double > * vce_dfreml (const std::string &method="powell", int maxiter=500000, double funtol=1.0e-5)
Vector< double > * genotypic_value_peeling (void)
Vector< double > * genotypic_value_gibbs (const unsigned warmup=30, const unsigned gibbslen=1000)
Vector< double > * genotypic_value_cat (const unsigned warmup, const unsigned gibbslen)
double genotype_dist_gibbs (const unsigned warmup=30, const unsigned gibbslen=1000)
void genotype_dist_peeling (const int prtlevel=1, const int estifreq=1)
double restricted_log_likelihood (void)
double log_likelihood_peeling (const unsigned maxit=3)
double log_likelihood_gibbs (const unsigned wup=30, const unsigned glen=1000)
double minfun (const Vector< double > &x, const int n)
double minfun_peeling (const Vector< double > &x, const int n)
double minfun_vce (const Vector< double > &x, const int n)
double estimate_peeling (void)
double ml_estimate_peeling (void)
double estimate (const Vector< double > &Kp)
Vector< double > estimate (const doubleMatrix &Kp)
Vector< double > estimate (const std::string &termname, const doubleMatrix &Kp)
void estimate (const double **kpme, const unsigned nr, const unsigned nc, double *kpb)
doubleMatrix CovMat (doubleMatrix &Kp)
doubleMatrix CovMat (const std::string &termname, doubleMatrix &Kp)
virtual double contrast (const Vector< double > &Kp, const double m=0.0)
virtual double contrast (const doubleMatrix &Kp, const Vector< double > &M)
virtual double contrast (const doubleMatrix &Kp)
virtual double contrast (const std::string &termname, const doubleMatrix &Kp, const Vector< double > &M)
virtual double contrast (const double **kpme, const int nr, const int nc, const double *M=0, const int prt_flag=1, double **result=0)
void anova (double &ssm, double &sse, unsigned &rank_x, int &dfe)
void lsmeans (const std::string &termnames, const std::string &filename="", const int io_mode=std::ios::out, const int savekp_flag=0)
virtual SparseMatrixsetup_mme (Vector< double > *rhs=0)
SparseMatrixmmec (void)
Vector< double > * rhs (void)
double multipoint (int maxit)
void multipoint_setup (void)
void multipoint_setup (Fpmm &F, char map_fun='H', int map_par=2)
void multipoint_tables (void)
void multipoint_QTL_table (void)
void multipoint_Rec_table (void)
void multipoint_Rec_recalc (double distance)
double MapF (double dist, int qr=0)
double minfun_multipoint (const Vector< double > &x, const int n)
double ProbGamete (Vector< int > value, int nloci)
void multipoint_profile_distance (int method, int maxit, double Min, double Max, double step_size, int Print, const std::string &fname)
double multipoint_ml_estimate (const int method=0, int niter=0, double tol=1.0e-16, double epsilon=1.0e-8, int printlevel=0)
void multipoint_estimate_Ve (double Initial, double Max, double Min)
void multipoint_estimate_Distance (double Initial, double Max, double Min)
void multipoint_estimate_Qmeans (Vector< double > Initial, Vector< double > Max, Vector< double > Min, char mtype='G')
void multipoint_estimate_Qfreq (double Initial, double Max, double Min)
void multipoint_genot_probs (void)
void multipoint_compute_Rec_table (void)
double multipoint_mixture_approx (int maxit=0, double P_var=1.0)
void multipoint_display_parms (void)
void multipoint_estimate_PolyV (double Initial, double Max, double Min)
unsigned get_popsize (void) const
void descent_graph_peeling_initialisation (void)
Vector< int > get_gamete_vector (unsigned index)
void calc_gamete_prob (void)
void map (void)
double graph_log_likelihood_peeling (const unsigned maxiter)
std::string label (const std::string &termname, const unsigned i)
void min_prtlevel (const int pl)
double praxis (Vector< double > &x, const int n, int &maxfun, const double tol=1.0e-16, const double epsilon=1.0e-8, const int pl=2)

Public Attributes

unsigned num_ped
unsigned act_numtrait
unsigned data_static
RSamplerParms myRSamplerParms
ModelType type
Populationpop
Datadata
Matrix< double > * rve
doubleMatrix residual_var
Vector< double > lng0vec
TermList term
int Q_pos
int maxit
int map_parm
char map_function
double new_distance
doubleMatrix RecoTable
int N_Parm_List
Vector< Parm_ElemParm_List
Vector< double > map_distance
doubleMatrix gamete_prob_table
doubleMatrix Cr_table

Protected Methods

virtual void setup_ww (Vector< double > *rhs)
void add_Ig (int t, Vector< double > *x_p=0)
void add_Ig_diag (int t, double **M)
void add_Ag (int t, Vector< double > *x_p=0)
void add_Ag_diag (int t, double **M)
virtual void add_G_1 (void)
virtual void add_G_1_diag (double **M)
void add_G_1_single_trait (const Vector< double > &ratio)
double setup_ww_single_trait (const Vector< double > &ratio, const unsigned pt, const unsigned ibeg, Vector< double > *zz)
void compute_rhs_diag_mme (double **M)
void mme_times (Vector< double > &x)
int build_model_term (const std::string &modelspecs)
void inverse_residual_var (void)
void assign_id_xact (const Vector< bool > &)
void hashxact (const Vector< bool > &)
void re_hash_data (Vector< bool > &)
virtual void save_pos_val (Vector< bool > &, const std::string &solver="ysmp")
void input_pos_val (std::istream &modelfile)
void input_pos_val_iod (std::vector< pos_val_node >::iterator it)
void get_lms_kp (const unsigned termth, const unsigned lvlth, const unsigned yth, Vector< double > &kp, const int *lsmtablevec)
void out_lsmeans_to_stream (std::ostream &ofs, const unsigned nlsm, const Matrix< int > &lsmtable, const int savekp=0)

Protected Attributes

std::vector< pos_val_nodepos_val_vector
std::string modelfname
std::string weightname
std::string modelstring
std::string * traitname
std::string modelstringstr
char * modelstr
int modelpcount
unsigned numtrait
unsigned numterm
unsigned numfact
unsigned maxorder
unsigned numrec
unsigned numcol
unsigned popsize
unsigned numobs
unsigned hmmesize
unsigned non_zero
unsigned max_nz
unsigned npattern
unsigned ntermGdist
unsigned * rawpos
unsigned * kvec
unsigned numgroup
int weightinverse
int data_prepared
int pop_created
int categorical
std::vector< std::string > pattern
std::set< std::string > pattern_tree
UnknownDistunknown_prior
FieldStructfactor_struct
FieldStruct ** trait_struct
HashTablexact_htable
HashTable ** idlist
Vector< double > lnr0vec
Vector< double > blupsol
Vector< double > rellrhs
Vector< double > diag_mme
Vector< double > mme_times_res
double yry
unsigned nfunk_in_dfreml
unsigned dfreml_called
double reml_value
std::string dfreml_method
SparseMatrix hmmec
Vector< int > base_effect
Vector< int > corr_link
Vector< int > pos_vec
Vector< Vector< int > > trait_map
Vector< int > nt_vec
Vector< int > var_link
unsigned * pos_term
double * xval_term
double * trait_vec
unsigned * rec_indid
int minfun_indx


Member Enumeration Documentation

enum matvec::Model::ModelType
 

Enumeration values:
bad_model 
fixed_model 
mixed_model 
reg_model 

Definition at line 173 of file model.h.


Constructor & Destructor Documentation

matvec::Model::Model void    [inline]
 

Definition at line 184 of file model.h.

References initialize().

00184 {initialize();}

matvec::Model::Model const std::string &    modelspecs [inline]
 

Definition at line 185 of file model.h.

References equation(), and initialize().

00185 {initialize(); equation(modelspecs);}

matvec::Model::Model const Model &    M [inline]
 

Definition at line 186 of file model.h.

References copyfrom(), and initialize().

00186 :Minimizer() {initialize(); copyfrom(M);}

virtual matvec::Model::~Model void    [inline, virtual]
 

Definition at line 187 of file model.h.

References release().

00187 {release();}


Member Function Documentation

void matvec::Model::add_Ag int    t,
Vector< double > *    x_p = 0
[protected]
 

Add the inverse of additive relationship matrix to mixed model quation.

Definition at line 474 of file model_blup.cpp.

References matvec::Matrix< T >::begin(), matvec::Matrix< double >::begin(), matvec::Vector< T >::begin(), matvec::Vector< double >::begin(), matvec::Session::epsilon, matvec::Individual::father_id(), matvec::Individual::father_inbcoef(), matvec::getlambda(), matvec::ginverse1(), hmmec, matvec::Individual::id(), matvec::SparseMatrix::insert(), lng0vec, matvec::Population::member(), mme_times_res, matvec::Individual::mother_id(), matvec::Individual::mother_inbcoef(), nt_vec, numgroup, pop, popsize, matvec::SESSION, term, and var_link.

Referenced by add_G_1(), and mme_times().

00475 {
00476    unsigned t1, t2, k, i, j, ii, jj, iii, jjj;
00477    double dii,val,value;
00478    double *x,*r;
00479    r = mme_times_res.begin() - 1;
00480    if (x_p) x = x_p->begin() - 1;
00481 
00482    double **var = 0;
00483    doubleMatrix *tmp = term[var_link[t]].prior->var_matrix();
00484    if (!tmp) throw exception("Model::add_Ag():  you probably forgot to use M.variance(...)\n"
00485             "  to set variance for each random effect\n");
00486    var = tmp->begin();
00487    int nt=nt_vec[var_link[t]];
00488    Matrix<double> rvarg(nt,nt);
00489    for (i=0; i<nt;i++) for (j=0; j<nt; j++) rvarg[i][j]= var[i][j];
00490    ginverse1(rvarg.begin(),nt,lng0vec[t],1,SESSION.epsilon);
00491 
00492    Matrix<double> lambda(3,3);
00493    getlambda(lambda.begin(),3);
00494    unsigned startaddr = term[t].start + 1;
00495    unsigned asd[3];
00496    Individual *I;
00497    unsigned nanim=popsize-numgroup;
00498    double f1,f2;
00499    for (k=0; k<nanim; k++) {
00500       I = pop->member(k);
00501       //  cout << popsize << " " << " "<<nanim << " " << k << I << "\n";
00502       //cout << I->id() << " " << I->father_id() << " " << I->mother_id() << "\n";
00503       asd[0] = I->id(); asd[1] = I->father_id(); asd[2] = I->mother_id();
00504       f1= I->father_inbcoef();
00505       f2= I->mother_inbcoef();
00506       
00507       if(asd[1]> nanim) f1=-1.;
00508       if(asd[2]> nanim) f2=-1.;
00509       dii = 4.0/(2.0 -f1 -f2);
00510 
00511       for (i=0; i<3; i++) {
00512          if (asd[i] == 0) continue;
00513          ii = startaddr + (asd[i]-1)*nt;
00514          for (j=0; j<3; j++) {
00515             if (asd[j] == 0) continue;
00516             jj = startaddr + (asd[j]-1)*nt;
00517             val = lambda[i][j]* dii;
00518             for (t1=0; t1<nt; t1++) {
00519                iii = ii + t1;
00520                for (t2=0; t2<nt; t2++) {
00521                   jjj = jj + t2;
00522                   if (jjj>=iii) {
00523                      value = val*rvarg[t1][t2];
00524                      if (x_p) {  // iteration on data
00525                         r[iii] += value*x[jjj];
00526                         if (jjj > iii) r[jjj] += value*x[iii];
00527                      } else {
00528                         hmmec.insert(iii,jjj,value);
00529                      }
00530                   }
00531                }
00532             }
00533          }
00534       }
00535    }
00536 }

void matvec::Model::add_Ag_diag int    t,
double **    M
[protected]
 

add the diagonals of inverse of additive relationship for pccg

Definition at line 542 of file model_blup.cpp.

References matvec::Matrix< T >::begin(), matvec::Matrix< double >::begin(), matvec::Session::epsilon, matvec::Individual::father_id(), matvec::Individual::father_inbcoef(), matvec::getlambda(), matvec::ginverse1(), matvec::Individual::id(), matvec::lidx(), lng0vec, matvec::Population::member(), matvec::Individual::mother_id(), matvec::Individual::mother_inbcoef(), nt_vec, numgroup, pop, popsize, matvec::SESSION, term, and var_link.

Referenced by add_G_1_diag().

00543 {
00544    unsigned t1, t2,i,j,k,kk,rec;
00545    double dii,val,value;
00546    double **var = 0;
00547    doubleMatrix *tmp = term[var_link[t]].prior->var_matrix();
00548    if (!tmp) throw exception("Model::add_Ag():  you probably forgot to use M.variance(...)\n"
00549             "  to set variance for each random effect\n");
00550    var = tmp->begin();
00551    int nt=nt_vec[var_link[t]];
00552    Matrix<double> rvarg(nt,nt);
00553    for (i=0; i<nt;i++) for (j=0; j<nt; j++) rvarg[i][j]= var[i][j];
00554    ginverse1(rvarg.begin(),nt,lng0vec[t],1,SESSION.epsilon);
00555 
00556    Matrix<double> lambda(3,3);
00557    getlambda(lambda.begin(),3);
00558    unsigned startaddr = term[t].start + 1;
00559    unsigned asd[3];
00560    Individual *I;
00561    unsigned nanim=popsize-numgroup;
00562    double f1,f2;
00563 
00564    for (k=0,i=0; i<t; ++i) k += term[i].nlevel();
00565    for (rec=0; rec<nanim; rec++) {
00566      I = pop->member(rec);
00567      asd[0] = I->id(); asd[1] = I->father_id(); asd[2] = I->mother_id();
00568      f1= I->father_inbcoef();
00569      f2= I->mother_inbcoef();
00570       
00571      if(asd[1]> nanim) f1=-1.;
00572      if(asd[2]> nanim) f2=-1.;
00573      dii = 4.0/(2.0 -f1 -f2);
00574 
00575      for (i=0; i<3; i++) {
00576        if (asd[i] == 0) continue;
00577        kk = k + asd[i] - 1;
00578        val = lambda[i][i]* dii;
00579        for (t1=0; t1<nt; ++t1) {
00580            for (t2=t1; t2<nt; ++t2) {
00581                M[kk][lidx(t2,t1,nt)+1] += val*rvarg[t1][t2];
00582            }
00583        }
00584      }
00585    }
00586 }

void matvec::Model::add_G_1 void    [protected, virtual]
 

Add G-inverse

Definition at line 279 of file model_blup.cpp.

References add_Ag(), add_Ig(), numterm, and term.

Referenced by setup_mme().

00280 {
00281   int done_ped=0;
00282    for (int t=0; t<numterm; t++) {
00283       if (term[t].classi() == 'P') {
00284          add_Ag(t);
00285          //hmmec.display(12,24,12,24);
00286       }
00287       else if (term[t].classi() == 'R') {
00288          add_Ig(t);
00289       }
00290    }
00291    //hmmec.display(12,24,12,24);
00292 }

void matvec::Model::add_G_1_diag double **    M [protected, virtual]
 

add diagonals of G-inverse for pccg

Definition at line 297 of file model_blup.cpp.

References add_Ag_diag(), add_Ig_diag(), numterm, and term.

Referenced by compute_rhs_diag_mme().

00298 {
00299   int done_ped=0;
00300    for (int t=0; t<numterm; t++) {
00301       if (term[t].classi() == 'P') {
00302          add_Ag_diag(t,M);
00303          //hmmec.display(12,24,12,24);
00304       }
00305       else if (term[t].classi() == 'R') {
00306          add_Ig_diag(t,M);
00307       }
00308    }
00309 }

void matvec::Model::add_G_1_single_trait const Vector< double > &    ratio [protected]
 

Definition at line 332 of file model_vce.cpp.

References matvec::Matrix< T >::begin(), matvec::Individual::father_id(), matvec::Individual::father_inbcoef(), matvec::getlambda(), hmmec, matvec::Individual::id(), matvec::SparseMatrix::insert(), matvec::Individual::mother_id(), matvec::Individual::mother_inbcoef(), numterm, pop, matvec::Population::popmember, and term.

Referenced by setup_ww_single_trait(), and vce_emreml_multi_trait().

00333 {
00334    unsigned i,j,ii,jj,t,k,rec,nl,startaddr;
00335    double val,rr,xval;
00336    for (k=0,t=0; t<numterm; t++) {
00337       startaddr = term[t].startaddr();
00338       nl = term[t].nlevel();
00339       if (term[t].classi() == 'P') {
00340          Individual *I;
00341          unsigned asd[3];
00342          val = ratio[k++];
00343          Matrix<double> lambda(3,3);
00344          getlambda(lambda.begin(),3);
00345          for (rec=0; rec<nl; rec++) {
00346             I = pop->popmember[rec];
00347             asd[0]=I->id(); asd[1]=I->father_id(); asd[2] = I->mother_id();
00348             rr = val*4.0/(2.0-I->father_inbcoef()- I->mother_inbcoef());
00349             for (i=0; i<3; i++) {
00350                if (asd[i] != 0) {
00351                   ii = startaddr + asd[i];
00352                   for (j=0; j<3; j++) {
00353                      if (asd[j] != 0) {
00354                         jj = startaddr + asd[j];
00355                         if (jj >= ii) {
00356                            xval = lambda[i][j]*rr;
00357                            hmmec.insert(ii,jj,xval);
00358                         }
00359                      }
00360                   }
00361                }
00362             }
00363          }
00364       }
00365       else if (term[t].classi() == 'R') {
00366          val = ratio[k++];
00367          for (ii=startaddr+1,i=0; i<nl; i++,ii++) hmmec.insert(ii,ii,val);
00368       }
00369    }
00370 }

void matvec::Model::add_Ig int    t,
Vector< double > *    x_p = 0
[protected]
 

Definition at line 399 of file model_blup.cpp.

References matvec::Matrix< T >::begin(), matvec::Matrix< double >::begin(), matvec::Vector< T >::begin(), matvec::Vector< double >::begin(), matvec::Session::epsilon, matvec::ginverse1(), hmmec, matvec::SparseMatrix::insert(), lng0vec, mme_times_res, nt_vec, matvec::SESSION, term, and var_link.

Referenced by add_G_1(), and mme_times().

00400 {
00401    unsigned k,i,ii,jj,nt;
00402    int t1,t2;
00403    double *x,*r,value;
00404    r = mme_times_res.begin() - 1;
00405    if (x_p) x = x_p->begin() - 1;
00406 
00407    double **v = 0;
00408    doubleMatrix *tmp = term[var_link[t]].prior->var_matrix();
00409    if (!tmp) throw exception("Model::add_Ig():  you probably forgot to use M.variance(...)\n"
00410             "  to set variance for each random effect\n");
00411    nt=nt_vec[var_link[t]];
00412    v = tmp->begin();
00413    Matrix<double> rv(nt,nt);
00414    for (t1=0; t1<nt; t1++) {
00415       for(t2=0; t2<nt; t2++) rv[t1][t2]=v[t1][t2];
00416    }
00417    ginverse1(rv.begin(),nt,lng0vec[t],1,SESSION.epsilon);
00418 
00419    unsigned na = term[t].nlevel();
00420    unsigned startaddr = term[t].start + 1;
00421    for (k=0; k<na; k++) {
00422       i = startaddr + k*nt;
00423       for (t1=0; t1<nt; t1++) {
00424          ii = i + t1;
00425          for (t2=0; t2<nt; t2++) {
00426             jj = i + t2;
00427             if (jj>=ii) {
00428                value = rv[t1][t2];
00429                if(x_p) {// iteration on data
00430                   r[ii] += value*x[jj];
00431                   if (jj > ii) r[jj] += value*x[ii];                  
00432                } else {
00433                   hmmec.insert(ii,jj,value);
00434                }
00435             }
00436          }
00437       }
00438    }
00439 }

void matvec::Model::add_Ig_diag int    t,
double **    M
[protected]
 

Definition at line 441 of file model_blup.cpp.

References matvec::Matrix< T >::begin(), matvec::Matrix< double >::begin(), matvec::Session::epsilon, matvec::ginverse1(), matvec::lidx(), lng0vec, nt_vec, matvec::SESSION, term, and var_link.

Referenced by add_G_1_diag().

00442 {
00443   unsigned rec,k,i,ii,jj,kk,nt;
00444   int t1,t2;
00445   double **v = 0;
00446   doubleMatrix *tmp = term[var_link[t]].prior->var_matrix();
00447   if (!tmp) throw exception("Model::add_Ig():  you probably forgot to use M.variance(...)\n"
00448                             "  to set variance for each random effect\n");
00449   nt=nt_vec[var_link[t]];
00450   v = tmp->begin();
00451   Matrix<double> rv(nt,nt);
00452   for (t1=0; t1<nt; t1++) {
00453     for(t2=0; t2<nt; t2++) rv[t1][t2]=v[t1][t2];
00454   }
00455   ginverse1(rv.begin(),nt,lng0vec[t],1,SESSION.epsilon);
00456 
00457   unsigned na = term[t].nlevel();
00458   for (k=0,i=0; i<t; ++i) k += term[i].nlevel();
00459   for (rec=0; rec<na; rec++) {
00460     kk = k + rec;
00461     for (t1=0; t1<nt; ++t1) {
00462        for (t2=t1; t2<nt; ++t2) {
00463           M[kk][lidx(t2,t1,nt)+1] += rv[t1][t2];
00464        }
00465     }
00466   }
00467 }

void matvec::Model::anova double &    ssm,
double &    sse,
unsigned &    rank_x,
int &    dfe
 

Definition at line 426 of file model_hypo.cpp.

References blupsol, hmmec, matvec::Vector< double >::inner_product(), matvec::SparseMatrix::logdet(), numgroup, numterm, numtrait, rellrhs, residual_var, term, and yry.

00427 {
00428    ////////////////////////////////////////////////////////
00429    // calculates sums of squares for single trait model
00430    //  the rank of the random part:
00431    ////////////////////////////////////////////////////////
00432    unsigned rank_mme=0,rank_z=0;
00433    char cl;
00434    for (unsigned i=0; i<numterm; i++) {
00435       cl = term[i].classi();
00436       if (cl == 'P' || cl == 'R') rank_z += term[i].nlevel();
00437    }
00438    rank_z = (rank_z - numgroup)*numtrait;
00439    hmmec.logdet(&rank_mme);
00440    double ve = residual_var[0][0];
00441    double sst = yry*ve;
00442    ssm = blupsol.inner_product(rellrhs)*ve;
00443    sse = sst - ssm;
00444    rank_x = rank_mme - rank_z;
00445    dfe = static_cast<int>(numobs) - static_cast<int>(rank_x);
00446 }

void matvec::Model::assign_id_xact const Vector< bool > &    [protected]
 

Definition at line 967 of file model.cpp.

References data, hashxact(), matvec::Data::num_rows(), numterm, matvec::HashTable::resize(), matvec::HashTable::size(), term, and xact_htable.

Referenced by prepare_data().

00968 {
00969    // **********************************************************
00970    // get sequantial integer ids for interation term in model
00971    // ***********************************************************
00972    if (!data) return;
00973    unsigned nord, nrow_in_data = data->num_rows();
00974 
00975    if (xact_htable) {
00976      delete [] xact_htable;
00977      xact_htable = 0;
00978    }
00979    if(numterm>0){
00980      xact_htable = new HashTable [numterm];
00981    }
00982    else {
00983      xact_htable = 0;
00984    }
00985    int t;
00986    for (t=0; t<numterm; t++) {
00987       if (term[t].order() > 1) {
00988          xact_htable[t].resize(nrow_in_data,term[t].order()*sizeof(unsigned));
00989       }
00990    }
00991    hashxact(record_legality);
00992    for (t=0;t<numterm;t++) {
00993       nord = term[t].order();
00994       if (nord>1) {
00995          term[t].numlevel = xact_htable[t].size();
00996          xact_htable[t].resize(term[t].nlevel(),nord*sizeof(unsigned));
00997       }
00998    }
00999    hashxact(record_legality);
01000 }

Vector< double > * matvec::Model::blue const std::string &    method = "ysmp",
double    stopval = 0.001,
double    relax = 1.0,
unsigned    mxiter = 100000
 

Return BLUE solution for a fixed model.

Parameters:
method  choices are ioc,iod,direct
stopval  stop criteria for solver ioc and iod
relax  relaxation factor for solver ioc
mxiter  maximum number of iterations allowed for solver ioc,ios

Definition at line 238 of file model_blup.cpp.

References blup(), fixed_model, and type.

00240 {
00241    Vector<double> *retval = 0;
00242    if (type != fixed_model) throw exception("Model::blue(): it must be a fixed model");
00243    retval = blup(method,stopval,relax,mxiter);
00244    return retval;
00245 }

Vector< double > * matvec::Model::blup const std::string &    method = "ysmp",
double    stopval = 0.001,
double    relax = 1.0,
unsigned    mxiter = 100000
 

Return the BLUP solution.

Parameters:
method  choices are ioc,iod,direct
stopval  stop criteria for solver ioc and iod
relax  relaxation factor for solver ioc
mxiter  maximum number of iterations allowed for solver ioc,ios

Definition at line 206 of file model_blup.cpp.

References bad_model, blup_pccg(), blupsol, hmmec, matvec::SparseMatrix::nz(), rellrhs, setup_mme(), matvec::SparseMatrix::solve(), and type.

Referenced by blue(), matvec::GLMM::glim(), and lsmeans().

00208 {
00209    Vector<double> *retval = 0;
00210    if (type == bad_model) throw exception("Model::blup(): bad model");
00211    if (method == "iod") {
00212      blup_pccg(stopval,mxiter);
00213      return &blupsol;
00214    }
00215    if (hmmec.nz() == 0) {
00216       try {
00217          setup_mme(&rellrhs);
00218       } catch (exception &er) {
00219          throw;
00220       }
00221    }
00222    if (hmmec.solve(blupsol,rellrhs,method,relax,stopval,mxiter) == 1) {
00223       retval = &blupsol;
00224       //hmmec.close();
00225    }
00226    // note that after BLUP, HMME remains intack
00227    //   hmmec.resize(0,0);
00228    return retval;
00229 }

void matvec::Model::blup_pccg double    tol = 1.0e-8,
unsigned int    mxiter = 100000
 

Compute blup by iteration on data using preconditioned conjugate gradient see J.Anim.Sci (2001) 79:1166-1172

Definition at line 618 of file model_blup.cpp.

References bad_model, matvec::Vector< double >::begin(), matvec::Vector< T >::begin(), blupsol, matvec::Vector< T >::clear(), matvec::Vector< double >::clear(), compute_rhs_diag_mme(), data_prepared, fixed_model, matvec::ginverse2(), hmmesize, matvec::doubleMatrix::identity(), matvec::Vector< T >::inner_product(), inverse_residual_var(), mixed_model, mme_times(), mme_times_res, nt_vec, matvec::Matrix< double >::num_rows(), numterm, numtrait, matvec::preconditioning(), prepare_data(), rellrhs, matvec::Vector< T >::reserve(), matvec::Vector< double >::reserve(), residual_var, matvec::Vector< double >::resize(), term, and type.

Referenced by blup().

00618                                                  {
00619 
00620   if (type == bad_model) throw exception("Model::blup_pccg: bad model");
00621   if (!data_prepared) {
00622     if (!prepare_data("iod")) {
00623       type = bad_model;
00624       throw exception("Model::blup_pccg: bad model");
00625     }
00626   }
00627   if (type == mixed_model) {
00628     if (residual_var.num_rows() != numtrait) {
00629       type = bad_model;
00630       throw exception("Model::blup_pccg: residual variance has not yet assigned");
00631     }
00632   }
00633   else if (type == fixed_model) {
00634     if (residual_var.num_rows() != numtrait) {
00635       residual_var.identity(numtrait,numtrait);
00636     }
00637   }
00638   else {
00639     throw exception("Model::setup_mme(): inappropriate model");
00640   }
00641 
00642   inverse_residual_var();
00643   blupsol.resize(hmmesize,0.0);
00644   rellrhs.reserve(hmmesize);
00645   unsigned i,j,k;
00646   unsigned n;
00647 
00648   unsigned nt = (numtrait+1)*numtrait/2;
00649   for (n=0,i=0; i<numterm; ++i) n += term[i].nlevel();
00650 
00651   double **M;
00652   M = new(nothrow) double *[n];
00653   for (i=0; i<n; ++i) {
00654     M[i] = new(nothrow) double[nt+1];     //M[i][0] is reserved to store the size
00655     memset(&(M[i][1]),'\0',sizeof(double)*nt);
00656   }
00657 
00658   k = 0;
00659   for (i=0; i<numterm; ++i) {
00660     nt = nt_vec[i];
00661     for (j=0; j<term[i].nlevel(); ++j)  M[k++][0] = nt;
00662   }
00663   compute_rhs_diag_mme(M);
00664 
00665   for (i=0; i<n; ++i) {
00666     //        nt = (unsigned)M[i][0];
00667     //        nt = nt*(nt+1)/2;
00668     //       for (j=1; j<=nt; ++j) cout << " " <<  M[i][j];
00669     //        cout << "\n";
00670      ginverse2(&(M[i][1]),(unsigned)M[i][0],1.0e-15);
00671   }
00672   mme_times_res.reserve(hmmesize);
00673   blupsol.resize(hmmesize);
00674   Vector<double> r;
00675   r = rellrhs;
00676 
00677   Vector<double> d;
00678   d.reserve(hmmesize);
00679   /////////  d = M*r ///////////////
00680   preconditioning(d.begin(),(const double **)M,r.begin(),n);
00681 
00682   double delta_new, delta_old, convcr = 1.0;
00683   unsigned iter = 0;
00684   double alpha,beta;
00685   delta_new = r.inner_product(d);
00686   double delta_0 = delta_new*tol*tol;
00687   bool done = false;
00688   while (!done){
00689     iter++;
00690     std::cout <<"iteration: " << iter <<std::endl;
00691      /////// q = Ad /////////
00692     mme_times(d);// the result is stored in mme_times_res
00693     alpha = delta_new/(d.inner_product(mme_times_res));
00694     blupsol += alpha*d;
00695     if(n%50){
00696        //////// r = r - alpha*q //////////
00697       r -= alpha*mme_times_res; 
00698     }
00699     else{
00700          ///// r = b - Ax /////////
00701       mme_times(blupsol);
00702       r = rellrhs - mme_times_res;
00703     }
00704     /////////  q = M*r ///////////////
00705     preconditioning(mme_times_res.begin(),(const double **)M,r.begin(),n);
00706 
00707     delta_old = delta_new;
00708     delta_new = r.inner_product(mme_times_res);
00709     beta = delta_new/delta_old;
00710     d = mme_times_res + beta*d;
00711 
00712     if (delta_new > delta_0 && iter < maxiter){
00713       done = false;
00714     }
00715     else if (delta_new <= delta_0){
00716       ///// r = b - Ax /////////
00717       mme_times(blupsol);
00718       r = rellrhs - mme_times_res;
00719 
00720       /////////  q = M*r ///////////////
00721       preconditioning(mme_times_res.begin(),(const double **)M,r.begin(),n);
00722 
00723       delta_new = r.inner_product(mme_times_res);
00724       if(delta_new > delta_0 && iter < maxiter){
00725         done = false;
00726       }
00727       else {
00728         done = true;
00729       }
00730     }
00731     else {
00732       done = true;
00733     }
00734   }
00735   //  std::cout <<"r = "<<r;
00736    mme_times_res.clear();
00737    r.clear();
00738 }

int matvec::Model::build_model_term const std::string &    modelspecs [protected]
 

Definition at line 2158 of file model.cpp.

References base_effect, categorical, matvec::check_ptr(), matvec::FieldStruct::classi(), corr_link, factor_struct, matvec::fieldindex(), matvec::FieldStruct::name(), matvec::nest_xaction(), nt_vec, numfact, numterm, numtrait, pos_vec, matvec::Vector< Vector< int > >::reserve(), matvec::Vector< int >::reserve(), matvec::Vector< Vector< int > >::resize(), matvec::Vector< int >::resize(), matvec::TermList::resize(), matvec::sort(), matvec::split(), term, trait_map, trait_struct, traitname, unknown_prior, and var_link.

Referenced by equation().

02159 {
02160    int retval = 1;
02161    unsigned i,j,k;
02162 
02163    std::string mspec = modelspec;  // modelspec:  y1 = A  B + A (B), y2 = B  + X*B
02164    nest_xaction(mspec);           // now mspec:  y1 = A  B + A**B , y2 = B  + X*B
02165 
02166    std::string sep("=+,* ");
02167    std::string TMP = mspec;
02168    ////////   TMP.unique("=+,* ");                // TMP ->y1 y2 A B X
02169    std::vector<std::string> tmpvec;
02170    std::vector<std::string>::iterator it;
02171    std::string::size_type begidx;
02172    split(TMP,sep,&tmpvec);
02173 
02174    sort(tmpvec.begin(),tmpvec.end());
02175    it = unique(tmpvec.begin(),tmpvec.end());
02176    tmpvec.erase(it,tmpvec.end());   
02177 //    for(it = tmpvec.begin(); it != tmpvec.end(); it++){
02178 //      std::cout << *it << endl;
02179 //    }
02180 
02181    categorical = 0;
02182    numfact =  tmpvec.size();
02183    if(factor_struct){
02184      delete [] factor_struct;
02185      factor_struct = 0;
02186    }
02187    if(numfact>0){
02188      factor_struct = new FieldStruct [numfact];
02189    }
02190    else {
02191      factor_struct = 0;
02192    }
02193    check_ptr(factor_struct);
02194    for (i=0; i<numfact; i++) factor_struct[i].name(tmpvec[i]);
02195 
02196    TMP = mspec;
02197    numtrait = 0;
02198    sep = "=";
02199    begidx = TMP.find(sep,0);
02200    while(begidx != std::string::npos) {
02201       numtrait++;
02202       begidx = TMP.find(sep,begidx+1);
02203    }
02204    if (numtrait == 0) {
02205       std::cerr << "Model::build_model_term(): there is no trait in the model\n";
02206       return 0;
02207    }
02208    if(trait_struct){
02209      delete [] trait_struct;
02210      trait_struct = 0;
02211    }
02212    if(numtrait>0){
02213      trait_struct = new FieldStruct* [numtrait];
02214    }
02215    else {
02216      trait_struct = 0;
02217    }
02218    check_ptr(trait_struct);
02219    if(traitname){
02220      delete [] traitname;
02221      traitname = 0;
02222    }
02223    if(numtrait>0){
02224      traitname = new std::string [numtrait];
02225    }
02226    else {
02227      traitname = 0;
02228    }
02229    check_ptr(traitname);
02230    std::vector<std::string> model_trait;
02231    std::vector<std::string> trtnm;
02232    sep = ",";
02233    split(TMP,sep,&model_trait);
02234    if (numtrait !=  model_trait.size()) throw exception("Model::build_model_term(): model for each trait is not separated properly");
02235 
02236    sep = " +";
02237    unsigned n = 0;
02238    for (i=0; i<numtrait; i++) {
02239      // trait name should be before "="
02240      begidx = model_trait[i].find("=");
02241      // bbbtrtnmbb is the trait name with possible surrounding blanks
02242      std::string bbbtrtnmbbb = model_trait[i].substr(0,begidx);
02243      // getting rid of the surrounding blanks
02244      split(bbbtrtnmbbb,sep,&trtnm);
02245      traitname[i]  = trtnm[i];
02246      model_trait[i]  = model_trait[i].substr(begidx+1);
02247      n += split(model_trait[i],sep);  /// n += model_trait[i].ntoken("+ ");
02248      k = fieldindex(traitname[i],factor_struct,numfact);
02249      if (k >= 0 && k < numfact) {
02250        factor_struct[k].classi('T');
02251        trait_struct[i] = &(factor_struct[k]);
02252      }
02253      else {
02254        throw exception("Model::build_model_term(): you have probably found a bug!");
02255      }
02256    }
02257    TermList T(n,numtrait);
02258    tmpvec.clear();
02259    sep = " +";
02260    for (numterm=0,i=0; i<numtrait; i++) {
02261       tmpvec.clear();
02262       n = split(model_trait[i],sep,&tmpvec);  //   tmpvec = model_trait[i].split(n,"+ ");
02263       for (k=0; k<n; k++) {
02264          for (j=0; j<numterm; j++) {
02265             if (T[j].match(tmpvec[k],"* ",factor_struct)) break;
02266          }
02267          if (j==numterm) {
02268             T[numterm++].input(tmpvec[k].c_str(),factor_struct,numfact);
02269          }
02270          T[j].trait[i]= 1;
02271       }
02272    }
02273 
02274    term.resize(numterm,numtrait);
02275    if (unknown_prior) {
02276      delete [] unknown_prior;                    // note that unknown_prior[i]
02277      unknown_prior = 0;
02278    }
02279    if(numterm>0){
02280      unknown_prior = new UnknownDist[numterm];     // needs to resize(numtrait)
02281    }
02282    else {
02283      unknown_prior = 0;
02284    }
02285    check_ptr(unknown_prior);
02286    nt_vec.resize(numterm,numtrait);
02287    base_effect.reserve(numterm);
02288    corr_link.resize(numterm,-1);
02289    pos_vec.resize(numterm);
02290    trait_map.reserve(numterm);
02291    var_link.reserve(numterm);
02292    
02293 
02294    for (i=0; i<numterm; i++) {
02295      base_effect[i]=i;
02296      var_link[i]=i;
02297       term[i] = T[i];
02298       term[i].prior = &(unknown_prior[i]);
02299       trait_map[i].resize(numtrait,i);
02300    }
02301    return retval;
02302 }

void matvec::Model::calc_gamete_prob void   
 

void matvec::Model::compute_rhs_diag_mme double **    M [protected]
 

Compute rhs and diagonals of mme

Definition at line 742 of file model_blup.cpp.

References add_G_1_diag(), matvec::Vector< double >::begin(), hmmesize, input_pos_val_iod(), matvec::lidx(), nt_vec, numterm, numtrait, pos_val_vector, rellrhs, rve, and term.

Referenced by blup_pccg().

00742                                            {
00743   unsigned i,j,k,kk,ii,jj,t1,t2,tt,rec;
00744   std::vector<pos_val_node>::iterator it;
00745   double **vep,val,vy,xval;
00746   memset(rellrhs.begin(),'\0',sizeof(double)*hmmesize);
00747   double *rhstmp = rellrhs.begin()-1;
00748   Matrix<double> ve(numtrait,numtrait);
00749 
00750   for (it=pos_val_vector.begin(); it!=pos_val_vector.end(); it++) {
00751     if(!it->pos_term.empty())  {
00752       input_pos_val_iod(it);
00753     }
00754     else {
00755       continue;
00756     }
00757 
00758     vep = rve[it->pos_term[numterm]].begin();
00759     val = it->xval_term[numterm];        // val = weight variable
00760     for (t1=0; t1<numtrait; t1++) {
00761       for (t2=0; t2<numtrait; t2++) ve[t1][t2] = val*vep[t1][t2];
00762     }
00763 
00764     for (t1=0; t1<numtrait; t1++) {
00765       vy = 0.0;
00766       for(t2=0; t2<numtrait; t2++) vy += ve[t1][t2]*it->trait_vec[t2];
00767       for (i=0; i<numterm; i++) {
00768         ii = term[i].addr[t1];
00769         if (ii == 0) continue;
00770         k = (ii-1)/numtrait;
00771         xval = it->xval_term[i];
00772         for (t2=t1; t2<numtrait; t2++) {
00773            jj = term[i].addr[t2];
00774            if (jj) {
00775               val = xval* ve[t1][t2];
00776               M[k][lidx(t2,t1,nt_vec[i])+1] += val*xval;
00777            }
00778         }
00779         rhstmp[ii] += vy*xval;
00780       }
00781     }
00782   }
00783 
00784   add_G_1_diag(M);
00785 }

void matvec::Model::compute_xbzu Vector< double > &    bu
 

Definition at line 204 of file model_gibbs.cpp.

Referenced by genotype_dist_gibbs(), genotype_dist_peeling(), genotypic_value_gibbs(), genotypic_value_peeling(), graph_log_likelihood_peeling(), and log_likelihood_peeling().

00205 {
00206    unsigned i,ii,rec;
00207    double val;
00208    double *sol = bu.begin()-1;
00209    // for gcc-3.2 replaced 
00210    // std::istrstream modelfile(modelstr, modelpcount);
00211    // with 
00212    std::istringstream modelfile(modelstringstr);
00213    if (!modelfile) throw exception("Model::compute_xbzu(): cannot open file");
00214    for (rec=0; rec<numrec; rec++) {
00215       input_pos_val(modelfile);
00216       val = 0.0;
00217       for (i=0; i<numterm; i++) {
00218          ii = term[i].addr[0];          // only one trait allowed now
00219          if (ii) val += sol[pos_term[i]]*xval_term[i];
00220       }
00221       ii = rec_indid[rec];
00222       if (ii > 0) pop->popmember[ii-1]->xbzu(val);
00223    }
00224    //BRS modelfile.close();
00225 }

double matvec::Model::contrast const double **    kpme,
const int    nr,
const int    nc,
const double *    M = 0,
const int    prt_flag = 1,
double **    result = 0
[virtual]
 

Definition at line 276 of file model_hypo.cpp.

References matvec::warning().

00278 {
00279    ////////////////////////////////////////////////////
00280    // H0:  K'*b = M
00281    // trailing zeros in K' can be omitted
00282    // REMINDER: blupsol & rellrhs remain intact
00283    //
00284    // if M=NULL, then it acts as a vector of zeros.
00285    // meanp must be declared as double meanp[nr][4]
00286    /////////////////////////////////////////////////////
00287    if (!kpme) {
00288       warning("Model::contrast(kp): kp is null, thus it is ignored");
00289       return 0.0;
00290    }
00291    if (!get_blupsol()) throw exception("Model::contrast(): bad model");
00292    if (nc > hmmesize) throw exception("Model::contrast(): size not conformable");
00293    int est=1;
00294    doubleMatrix kvk(nr,nr);
00295    doubleMatrix kvkori(nr,nr);
00296    Vector<double> kpdiff(nr);
00297    Vector<double> kpb_m(nr);
00298    Vector<double> xy(hmmesize);
00299    Vector<double> tmpsol(hmmesize);
00300    double *sol, kpd;
00301    const double *kp;
00302    unsigned i,j,k;
00303    for (i=0; i<nr; i++) {
00304       if (nc < hmmesize) {
00305          memset(xy.begin(),'\0',sizeof(double)*hmmesize);
00306          memcpy(xy.begin(),kpme[i],sizeof(double)*nc);
00307          if (!hmmec.solve(tmpsol,xy,"ysmp")) return 0.0;
00308       }
00309       else {
00310          if (!hmmec.solve(tmpsol.begin(),kpme[i],"ysmp")) return 0.0;
00311       }
00312       hmmec.mv(tmpsol,xy);
00313       kp = kpme[i]; sol = xy.begin();
00314       for (kpd=0.0,j=0; j<nc; j++) {
00315          kpd = std::max(kpd,fabs(*kp - *sol));
00316          kp++;
00317          sol++;
00318       }
00319       for (j=nc; j<hmmesize; j++) {
00320          kpd = std::max(kpd,fabs(*sol));
00321          sol++;
00322       }
00323       kpdiff[i] = kpd;
00324       for (k=0; k<nr; k++) {
00325          kp = kpme[k]; sol = tmpsol.begin();
00326          for (kpd=0.0,j=0; j<nc; j++) kpd += *kp++ * *sol++;
00327          kvk[k][i] = kpd;
00328       }
00329    }
00330    kvkori = kvk;
00331    kvk.ginv1(&k);
00332    if (k < nr) throw exception("Model::contrast(K'): K'V{-1}K is singular. Possible reason:\n"
00333             " (1) K is non-estimable, and (2) K isn't of full column rank");
00334    // K'b-M
00335    for (i=0; i<nr; i++) kpb_m[i] = blupsol.inner_product(kpme[i]);
00336    if (M) for (i=0; i<nr; i++) kpb_m[i] -= M[i];
00337    double quq = kvk.quadratic(kpb_m,kpb_m);       // (K'b-m)'(K'(X'V-1X)-K)-1(K'b-m)
00338    double ssm,sse;
00339    unsigned rank_x;
00340    int dfe = 0;
00341    anova(ssm,sse,rank_x,dfe);
00342    double res_var_est = sse/dfe;
00343    int degen = 0;
00344    int errcode = 0;
00345    double f_stat=0.0, prob=0.0;
00346    double sigma_e = residual_var[0][0];
00347    int chisq = 0;
00348    if (chisq) {
00349       f_stat = quq;
00350       prob = ChiSquare_cdf(f_stat,static_cast<double>(nr),0.0,errcode);
00351    }
00352    else {
00353       if (dfe <= 0 || sse <= SESSION.epsilon) {
00354          warning("Model::contrast(): either dfe or sse is zero");
00355          degen = 1;
00356       }
00357       else {
00358          f_stat = (quq*sigma_e/res_var_est)/nr;
00359          prob = F_cdf(f_stat,static_cast<double>(nr),static_cast<double>(dfe),0.0,errcode);
00360       }
00361       //   in univariate cases only: the estimated variance of estimator K'B
00362       if (!degen)  kvkori *= res_var_est/sigma_e;
00363    }
00364    std::string raw_data_code;
00365    double p_value,var,tcal=0.0;
00366    int W = SESSION.output_precision;
00367    std::cout.precision(W);
00368    if (prt_flag) {
00369       std::cout << "\n            RESULTS FROM CONTRAST(S)\n";
00370       std::cout << " ----------------------------------------------------------\n";
00371       std::cout << "  Contrast MME_addr    K_coef   Raw_data_code\n";
00372       std::cout << "  ---------------------------------------------\n";
00373    }
00374    for (i=0; i<nr; i++) {
00375       if (prt_flag) {
00376          kp = kpme[i];
00377          for (j=0; j<nc; j++) {
00378             if (kp[j] == 0.0) continue;
00379             trait_effect_level(j,raw_data_code);
00380             std::cout << std::setw(4) << i+1 <<std::setw(11) << j+1 << std::setw(13) << kp[j]
00381                  << "   " << raw_data_code << "\n";
00382          }
00383       }
00384       kpd = kpdiff[i];
00385       if (result) {
00386          result[i][0] = kpb_m[i];
00387          result[i][3] = kpd;
00388       }
00389       var = kvkori[i][i];
00390       if (var <= 0.0) throw exception(" Model::contrast(): you have probably found a bug!");
00391       var = std::sqrt(var);
00392       tcal = fabs(kpb_m[i]/var);
00393       p_value = 2.0*(1.0-t_cdf(tcal,static_cast<double>(dfe),0.0));
00394       if (result) {
00395          result[i][1] = var;
00396          result[i][2] = p_value;
00397       }
00398       if (!prt_flag) continue;
00399       if (kpd > 1.0e-5) {
00400          est = 0;
00401          std::cout << "            **** NON-ESTIMABLE ****\n\n";
00402       }
00403       else {
00404          if (kpd > 1.0e-10) {
00405              std::cout << " ***WARNING***: it may or mayn't be estimable\n";
00406              std::cout << "       max(k'*ginv(mme)*mme - k') = " << kpd << "\n";
00407          }
00408          std::cout << "          estimated value (K'b-M) = " << kpb_m[i]
00409               << " +- " << var << "\n";
00410          std::cout << "             Prob(|t| > " << tcal << ") = "
00411               <<  p_value << " (p_value)\n";
00412          if (i != nr-1) std::cout << "\n";
00413       }
00414    }
00415    if (prt_flag) {
00416       std::cout << " ----------------------------------------------------------\n";
00417       if (nr > 1 && est) {
00418          std::cout << "   joint hypothesis test H: K'b = M\n";
00419          std::cout << "   Prob(F > " <<  f_stat << ") = "
00420               <<  1.0-prob << " (p_value)\n";
00421       }
00422    }
00423    return 1.0-prob;   // p_value
00424 }

double matvec::Model::contrast const std::string &    termname,
const doubleMatrix   Kp,
const Vector< double > &    M
[virtual]
 

Reimplemented in matvec::GLMM.

Definition at line 227 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), matvec::Matrix< T >::begin(), matvec::Matrix< double >::begin(), contrast(), factor_struct, get_blupsol(), hmmesize, matvec::TermList::index(), matvec::Matrix< double >::num_cols(), matvec::Matrix< double >::num_rows(), numtrait, matvec::Vector< T >::size(), term, and matvec::warning().

00228 {
00229    if (!(Kp.begin())) {
00230       warning("Model::contrast(kp): kp is null, thus it is ignored");
00231       return 0.0;
00232    }
00233 
00234    double retval = 0.0;
00235    if (!get_blupsol()) throw exception("Model::contrast(): bad model");
00236 
00237    int k = term.index(termname,factor_struct);
00238    if (k < 0) throw exception("Model::contrast(): no such term in the model");
00239 
00240    unsigned nr = Kp.num_rows();
00241    unsigned nc = Kp.num_cols();
00242    if (M.size() != nr) throw exception("Model::contrast(): unconformable size");
00243    unsigned i,startaddr;
00244    if (nc <= term[k].nlevel()*numtrait) {
00245       startaddr = term[k].startaddr();
00246       Matrix<double> fullsize_Kp(nr,hmmesize);
00247       for (i=0; i<nr; i++) {
00248          memcpy(&(fullsize_Kp[i][startaddr]),Kp[i],sizeof(double)*nc);
00249       }
00250       contrast((const double **)fullsize_Kp.begin(),nr,hmmesize,(const double*)M.begin());
00251    }
00252    else {
00253       throw exception("Model::contrast(): too many columns in Kp");
00254    }
00255    return retval;
00256 }

double matvec::Model::contrast const doubleMatrix   Kp [virtual]
 

Reimplemented in matvec::GLMM.

Definition at line 207 of file model_hypo.cpp.

References matvec::Matrix< double >::begin(), contrast(), matvec::Matrix< double >::num_cols(), and matvec::Matrix< double >::num_rows().

00208 {
00209    ///////////////////////////////////////////
00210    // trailing zeros in K' can be omitted
00211    ///////////////////////////////////////////
00212 
00213    return contrast((const double **)Kp.begin(), Kp.num_rows(), Kp.num_cols());
00214 }

double matvec::Model::contrast const doubleMatrix   Kp,
const Vector< double > &    M
[virtual]
 

Reimplemented in matvec::GLMM.

Definition at line 216 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), matvec::Matrix< double >::begin(), contrast(), matvec::Matrix< double >::num_cols(), matvec::Matrix< double >::num_rows(), and matvec::Vector< T >::size().

00217 {
00218    ///////////////////////////////////////////
00219    // trailing zeros in K' can be omitted
00220    ///////////////////////////////////////////
00221 
00222    int nr = Kp.num_rows();
00223    if (M.size() != nr ) throw exception(" Model::contrast(): size is incomformable");
00224    return contrast((const double **)Kp.begin(),nr,Kp.num_cols(),M.begin());
00225 }

double matvec::Model::contrast const Vector< double > &    Kp,
const double    m = 0.0
[virtual]
 

Reimplemented in matvec::GLMM.

Definition at line 181 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), and matvec::Vector< T >::size().

Referenced by contrast(), and out_lsmeans_to_stream().

00182 {
00183    ///////////////////////////////////////////
00184    // trailing zeros in K' can be omitted
00185    ///////////////////////////////////////////
00186 
00187    double retval, **kpme = new double *[1];
00188    kpme[0] = Kp.begin();
00189    unsigned nc = Kp.size();
00190    double* M = 0;
00191    if (m != 0.0) {
00192       M = new double [1];
00193       M[0] = m;
00194    }
00195    retval = contrast((const double **)kpme,1,nc,M);
00196    if(kpme){
00197      delete [] kpme;
00198      kpme = 0;
00199    }
00200    if (M) {
00201      delete [] M;
00202      M = 0;
00203    }
00204    return retval;
00205 }

void matvec::Model::copyfrom const Model &    M
 

Definition at line 83 of file model.cpp.

References blupsol, matvec::check_ptr(), matvec::UnknownDist::copyfrom(), data, data_prepared, dfreml_called, factor_struct, matvec::fieldindex(), matvec::Data::hashtable, hmmec, hmmesize, idlist, initialize(), kvec, lng0vec, lnr0vec, max_nz, maxorder, modelfname, modelstring, matvec::GeneticDist::name(), nfunk_in_dfreml, non_zero, npattern, ntermGdist, num_ped, numcol, numfact, numgroup, numobs, numrec, numterm, numtrait, pattern, pattern_tree, pop, pop_created, popsize, pos_term, matvec::ModelTerm::prior, rawpos, rec_indid, rellrhs, reml_value, matvec::Matrix< double >::reserve(), residual_var, matvec::SparseMatrix::resize(), rve, term, matvec::TermList::termlist, trait_struct, trait_vec, traitname, type, unknown_prior, weightinverse, weightname, xact_htable, xval_term, and yry.

Referenced by Model().

00084 {
00085    initialize();
00086    num_ped        = M.num_ped;
00087    yry            = M.yry;
00088    data           = M.data;
00089    term           = M.term;
00090    max_nz         = M.max_nz;
00091    numrec         = M.numrec;
00092    numobs         = M.numobs;
00093    numcol         = M.numcol;
00094    max_nz         = M.max_nz;
00095    popsize        = M.popsize;
00096    numterm        = M.numterm;
00097    numfact        = M.numfact;
00098    lng0vec        = M.lng0vec;
00099    lnr0vec        = M.lnr0vec;
00100    blupsol        = M.blupsol;
00101    rellrhs        = M.rellrhs;
00102    popsize        = M.popsize;
00103    hmmesize       = M.hmmesize;
00104    numtrait       = M.numtrait;
00105    npattern       = M.npattern;
00106    hmmesize       = M.hmmesize;
00107    numgroup       = M.numgroup;
00108    maxorder       = M.maxorder;
00109    non_zero       = M.non_zero;
00110    rec_indid      = M.rec_indid;
00111    type     = M.type;
00112    modelfname     = M.modelfname;
00113    weightname     = M.weightname;
00114    ntermGdist     = M.ntermGdist;
00115    modelstring    = M.modelstring;
00116    pattern_tree   = M.pattern_tree;
00117    residual_var   = M.residual_var;
00118    data_prepared  = M.data_prepared;
00119    weightinverse  = M.weightinverse;
00120    nfunk_in_dfreml= M.nfunk_in_dfreml;
00121    reml_value     = M.reml_value;
00122    dfreml_called  = M.dfreml_called;
00123    hmmec.resize(hmmesize,max_nz);
00124 
00125    int i,k;
00126    // pos_term, xval_term, trait_vec are working spaces
00127    if (pos_term) {delete [] pos_term; pos_term = 0;}
00128    if (M.pos_term) pos_term = new unsigned [numterm+1];
00129    if (xval_term) {delete [] xval_term; xval_term = 0;}
00130    if (M.xval_term) xval_term = new double [numterm+1];
00131    if (trait_vec) {delete [] trait_vec;  trait_vec= 0;}
00132    if (M.trait_vec) trait_vec = new double [numterm+1];
00133 
00134    if (unknown_prior) {
00135       delete [] unknown_prior;unknown_prior = 0;
00136    }
00137    if (M.unknown_prior) {
00138      if(numterm>0){
00139        unknown_prior = new UnknownDist[numterm];
00140      }
00141      else {
00142        unknown_prior = 0;
00143      }
00144       for (i=0; i<numterm; i++) {
00145          unknown_prior[i].copyfrom(M.unknown_prior[i]);
00146          if (strcmp(M.term.termlist[i].prior->name(),"UnknownDist") == 0) {
00147             term.termlist[i].prior = &(unknown_prior[i]);
00148          }
00149          else {
00150             term.termlist[i].prior = M.term.termlist[i].prior;
00151          }
00152       }
00153    }
00154    if (traitname) {delete [] traitname; traitname = 0;}
00155    if (M.traitname) {
00156      if(numtrait>0){
00157        traitname = new std::string [numtrait];
00158      }
00159      else {
00160        traitname = 0;
00161      }
00162       for (i=0; i<numtrait; i++) traitname[i] = M.traitname[i];
00163    }
00164    if (kvec) {delete [] kvec; kvec = 0;}
00165    if (M.kvec) {
00166      if(numtrait>0){
00167        kvec = new unsigned [npattern];
00168      }
00169      else {
00170        kvec = 0;
00171      }
00172       for (i=0; i<npattern; i++) kvec[i] = M.kvec[i];
00173    }
00174    if (rawpos) {delete [] rawpos; rawpos = 0;}
00175    if (M.rawpos) {
00176      if(maxorder>0){
00177        rawpos = new unsigned [maxorder];  // this is just a working space
00178      }
00179      else {
00180        rawpos = 0;
00181      }
00182    }
00183    pattern.resize(npattern);
00184    std::set<std::string>::iterator pos;
00185    for (i=0, pos=pattern_tree.begin(); pos != pattern_tree.end(); ++pos,++i) {
00186       pattern[i] = *pos;
00187    }
00188    pop_created = M.pop_created;
00189    if (M.pop_created) {
00190       pop = new Population(*(M.pop));
00191    }
00192    else {
00193       pop = M.pop;
00194    }
00195    if (rve) {
00196      delete [] rve;
00197      rve = 0;
00198    }
00199    if (M.rve) {
00200      if(npattern>0){
00201        rve = new Matrix<double> [npattern];
00202      }
00203      else {
00204        rve = 0;
00205      }
00206       check_ptr(rve);
00207       for (i=0; i<npattern; i++) rve[i].reserve(numtrait,numtrait);
00208    }
00209    if (factor_struct) {
00210       delete [] factor_struct; factor_struct = 0;
00211    }
00212    if (M.factor_struct) {
00213      if(numfact>0){
00214        factor_struct = new FieldStruct [numfact];
00215      }
00216      else {
00217        factor_struct = 0;
00218      }
00219       for (i=0; i<numfact; i++) factor_struct[i] = M.factor_struct[i];
00220    }
00221    if (trait_struct) {
00222       delete [] trait_struct; trait_struct = 0;
00223    }
00224    if (M.trait_struct) {
00225      if(numtrait>0){
00226        trait_struct = new FieldStruct* [numtrait];
00227      }
00228      else {
00229        trait_struct = 0;
00230      }
00231       for (i=0; i<numtrait; i++) {
00232          k = fieldindex(traitname[i],factor_struct,numfact);
00233          if (k >= 0 && k < numfact) {
00234             trait_struct[i] = &(factor_struct[k]);
00235          }
00236          else {
00237             throw exception("Model::copyfrom(): you have probably found a bug!");
00238          }
00239       }
00240    }
00241    if (xact_htable) {delete [] xact_htable; xact_htable = 0;}
00242    if (M.xact_htable) {
00243      if(numterm>0){
00244        xact_htable = new HashTable [numterm];
00245      }
00246      else {
00247        xact_htable = 0;
00248      }
00249       for (i=0; i<numterm; i++) xact_htable[i] = M.xact_htable[i];
00250    }
00251    if (idlist) {delete [] idlist; idlist = 0;}
00252    if (M.idlist) {
00253      if(numcol>0){
00254        idlist = new HashTable* [numcol];
00255      }
00256      else {
00257        idlist = 0;
00258      }
00259       check_ptr(idlist);
00260       if (data) for (i=0; i<numcol; i++) idlist[i] = data->hashtable[i];
00261    }
00262 }

void matvec::Model::covariance const std::string &    termname,
const std::string &    termname2,
const double    v00,
...   
 

Definition at line 2059 of file model.cpp.

02063                                  :
02064   // *
02065   // *    M.variance("animal",1.0,...)
02066   // *********************************************************************
02067 {
02068    if (numtrait<1) {
02069       std::cerr << "Model::variance(args): no trait(dependent-variable) in the model\n";
02070       return;
02071    }
02072    doubleMatrix var(numtrait,numtrait);
02073    int t1,t2;
02074    va_list param_pt;                       // an object param_pt
02075    va_start(param_pt,v00);                // call the setup macro
02076    var[0][0] = v00;
02077    for (t2=1; t2<numtrait; t2++) var[0][t2] = va_arg(param_pt,double);
02078 
02079    for (t1=1; t1<numtrait; t1++) for (t2=0; t2<numtrait; t2++) {
02080       var[t1][t2] = va_arg(param_pt,double);
02081    }
02082    va_end(param_pt);
02083    covariance(termname,termname2,var);
02084 }

void matvec::Model::covariance const std::string &    termname,
const std::string &    termname2,
const doubleMatrix   v
 

Definition at line 1947 of file model.cpp.

References matvec::Matrix< double >::clear(), matvec::Matrix< double >::copy(), matvec::Matrix< double >::me, matvec::Vector< T >::reserve(), and matvec::Vector< T >::resize().

01952 {
01953    if (!factor_struct) {
01954       std::cerr << "Model::covariance(args): no equation(s) in the model\n";
01955       return;
01956    }
01957    if (v.num_rows() != v.num_cols() || v.num_rows() != numtrait) {
01958       std::cerr << "Model::covariance(): invalid variance matrix size\n";
01959       return;
01960    }
01961    int i,k,k2;
01962    k = term.index(termname,factor_struct);
01963 
01964    k2 = term.index(termname2,factor_struct);
01965 
01966    if( k < 0 ){
01967       std::cerr << "Model::covariance(..): not in model: " << termname << "\n";
01968       return;
01969    }
01970    if( k2 < 0 ){
01971       std::cerr << "Model::covariance(..): not in model: " << termname2 << "\n";
01972       return;
01973    }
01974    int bek=base_effect[k];
01975    if(term[bek].classi() != 'R' && term[bek].classi() != 'P'){
01976       std::cerr << "Model::covariance(..): not a random effect: " << termname << "\n";
01977       return;
01978    }
01979    int bek2=base_effect[k2];
01980    if(term[bek2].classi() != 'R' && term[bek2].classi() != 'P'){
01981       std::cerr << "Model::covariance(..): not a random effect: " << termname2 << "\n";
01982       return;
01983    }
01984    if(term[bek].classi() != term[bek2].classi() ){
01985      std::cerr << "Model::covariance(..): different types of random effects: \n";
01986       return;
01987    }
01988 
01989 
01990    int be;
01991    if(base_effect[k]==base_effect[k2]){  
01992      be=base_effect[k];
01993    }
01994    else {
01995      be=base_effect[k];
01996      int be2=base_effect[k2];
01997      if(be2 <be) {
01998        int tmp;
01999        tmp=k;
02000        k=k2;
02001        k2=tmp;
02002        tmp=be;
02003        be=be2;
02004        be2=tmp;
02005      }
02006      base_effect[k2]=be;
02007      doubleMatrix orig1,orig2;
02008      orig1.copy(*term[be].prior->var_matrix());
02009      orig2.copy(*term[be2].prior->var_matrix());
02010      int newsize=nt_vec[be]+nt_vec[be2];
02011      Vector<int> orig=trait_map[be];
02012      trait_map[be].reserve(newsize);
02013      int ii=0;
02014      for(int i=0;i<nt_vec[be];i++,ii++) trait_map[be][ii]=orig[i];
02015      for(int i=0;i<nt_vec[be2];i++,ii++) trait_map[be][ii]=trait_map[be2][i];
02016      term[be].prior->var_matrix()->resize(newsize,newsize);
02017 
02018      for(int i=0;i<nt_vec[be];i++){
02019        for(int j=0;j<nt_vec[be];j++){
02020          term[be].prior->var_matrix()->me[i][j]=orig1[i][j];
02021        }
02022      }
02023 
02024      for(int i=0;i<nt_vec[be2];i++){
02025        for(int j=0;j<nt_vec[be2];j++){
02026          term[be].prior->var_matrix()->me[nt_vec[be]+i][nt_vec[be]+j]=orig2[i][j];
02027        }
02028      }
02029      nt_vec[be]=newsize;
02030      term[be2].prior->var_matrix()->clear();
02031      nt_vec[be2]=0;
02032      term[be2].classi('F');
02033      int last_eff=be;
02034      for(;corr_link[last_eff] != -1;)last_eff=corr_link[last_eff];
02035      corr_link[last_eff]=be2;
02036      int last_pos=pos_vec[last_eff];
02037      for(;corr_link[last_eff] != -1;){
02038        last_eff=corr_link[last_eff];
02039        pos_vec[last_eff]=pos_vec[last_eff]+last_pos+1;
02040      }
02041    }
02042 
02043    int srow,scol;
02044    srow=pos_vec[k]*numtrait;
02045    scol=pos_vec[k2]*numtrait;
02046    for(int irow=0;irow<numtrait;irow++){
02047      for(int jcol=0;jcol<numtrait;jcol++){
02048        term[be].prior->var_matrix()->me[srow+irow][scol+jcol]=v[irow][jcol];
02049        term[be].prior->var_matrix()->me[scol+jcol][srow+irow]=v[irow][jcol];
02050      }
02051    }      
02052 
02053 
02054    //   std::cout << "Cov " << *term[be].prior->var_matrix();
02055    
02056 
02057 }

void matvec::Model::covariate const std::string &    factorname
 

Definition at line 2097 of file model.cpp.

References matvec::FieldStruct::classi(), data, factor_struct, matvec::fieldindex(), numfact, matvec::split(), and term.

02098 {
02099   // *********************************************************************
02100   //  it specifies a list of effect (factor) as covariates.
02101   //  note that an effect is not a model-term, which consists of effects,
02102   //  instead an effect is associated with a data column. It is required
02103   //  that the name of an effect must be the same as the associated
02104   //  data-column.
02105   // **********************************************************************
02106    if (!factor_struct) {
02107       std::cerr << "Model::covariate(): no equation(s) in the model\n";
02108       return;
02109    }
02110 
02111    int j,k,t;
02112    unsigned i,nc;
02113    std::vector<std::string> efflist;
02114    nc = split(covariate_names," ,",&efflist);  // efflist = covariate_names.split(nc,", ");
02115 
02116    for (i=0; i<nc; i++) {
02117       k = fieldindex(efflist[i],factor_struct,numfact);
02118       if (k < 0) throw exception("Model::covariate(): not in the model, thus it's ignored");
02119        // need to check if classi is changed
02120       if (factor_struct[k].classi() != 'F') data= 0;
02121       factor_struct[k].classi('C');
02122 
02123       ////////////////////////////////////////////////////////////////////
02124       // if a term contains at least one covariate, then its classi = 'C'
02125       // else leave it alone
02126       //////////////////////////////////////////////////////////////////
02127       for (t=0; t<numterm; t++) {
02128          j = term[t].partial_match(efflist[i].c_str(),factor_struct);
02129          if (j >=0 ) {
02130             term[t].classi('C');
02131             break;
02132          }
02133       }
02134    }
02135 }

doubleMatrix matvec::Model::CovMat const std::string &    termname,
doubleMatrix   Kp
 

Definition at line 128 of file model_hypo.cpp.

References CovMat(), factor_struct, get_blupsol(), hmmesize, matvec::TermList::index(), matvec::Matrix< double >::num_cols(), matvec::Matrix< double >::num_rows(), numtrait, and term.

00128                                                                     {
00129    doubleMatrix retval;
00130    if (!get_blupsol()) throw exception("Model::CovMat(): bad model");
00131 
00132    int k = term.index(termname,factor_struct);
00133    if (k < 0) throw exception("Model::CovMat(): no such term in the model");
00134 
00135    unsigned nr = Kp.num_rows();
00136    unsigned nc = Kp.num_cols();
00137    unsigned i,startaddr;
00138    if (nc <= term[k].nlevel()*numtrait) {
00139       startaddr = term[k].startaddr();
00140       doubleMatrix fullsize_Kp(nr,hmmesize);
00141       for (i=0; i<nr; i++) {
00142          memcpy(&(fullsize_Kp[i][startaddr]),Kp[i], sizeof(double)*nc);
00143       }
00144       retval=CovMat(fullsize_Kp);
00145    }
00146    else {
00147       throw exception("Model::CovMat(): too many columns in Kp");
00148    }
00149    return retval;
00150 }

doubleMatrix matvec::Model::CovMat doubleMatrix   Kp
 

Definition at line 69 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), matvec::Matrix< double >::begin(), get_blupsol(), hmmec, hmmesize, matvec::Matrix< double >::num_cols(), matvec::Matrix< double >::num_rows(), and matvec::SparseMatrix::solve().

Referenced by CovMat().

00069                                           {
00070   unsigned nr=Kp.num_rows();
00071   unsigned nc = Kp.num_cols();
00072   double *kp,*sol;
00073   doubleMatrix KvK(nr,nr);
00074   if (!get_blupsol("ysmp")) throw exception("Model::CovMat(): bad model");
00075   if (nc > hmmesize) throw exception("Model::CovMat(): size not conformable");
00076   
00077   Vector<double> xy(hmmesize);
00078   Vector<double> tmpsol(hmmesize);
00079   double kpd,**kpme=Kp.begin();
00080   unsigned j;
00081   for (unsigned i=0; i<nr; i++) {
00082     if (nc < hmmesize) {
00083       memset(xy.begin(),'\0',sizeof(double)*hmmesize);
00084       memcpy(xy.begin(),kpme[i],sizeof(double)*nc);
00085       if (!hmmec.solve(tmpsol,xy,"ysmp"))  throw exception("Model::CovMat(): hmmec.solve failed");
00086     }
00087     else {
00088       if (!hmmec.solve(tmpsol.begin(),kpme[i],"ysmp"))  throw exception("Model::CovMat(): hmmec.solve failed");
00089     }
00090     for (unsigned k=0; k<nr; k++) {
00091       kp = Kp[k]; sol = tmpsol.begin();
00092       for (kpd=0.0,j=0; j<nc; j++) kpd += *kp++ * *sol++;
00093       KvK[k][i] = kpd;
00094     }
00095   }
00096   return(KvK);
00097 }

void matvec::Model::descent_graph_peeling_initialisation void   
 

void matvec::Model::DGSampler unsigned    numOfSamples,
unsigned    numOfSL,
unsigned    numOfHaplo,
unsigned    numOfSLCas
 

Definition at line 1176 of file model.cpp.

References matvec::Individual::display_freq_haplotype(), matvec::Population::member(), matvec::Population::MH_ibd_sample_map(), pop, matvec::Population::size(), and matvec::Population::sum_descentState_map().

01176                                                                                                    {
01177   // Authors: L. Radu Totir 
01178   // (August, 2004) 
01179   // Contributors:
01180   double l_hood = 0.0;
01181   unsigned k = 0, j = 0, option = 0;
01182   unsigned k1 = numOfSL;
01183   unsigned k2 = numOfHaplo;
01184   unsigned k3 = numOfSLCas;
01185   cout<< endl << "Descent graph iterations = " << numOfSamples << endl;
01186   for(int sample=1; sample <= numOfSamples; sample++) {
01187     if(sample%5000 == 0) cout<<sample<<".";  
01188     //  cout<<endl<< "SAMPLE.................................. " << sample <<endl;  
01189     if (k >= k1 + k2 + k3)                {k = 0;}
01190     if (k < k1)                           {option = 1;}  
01191     else if ((k >= k1) && (k < k1 + k2))  {option = 2;}
01192     else if (k <= k1 + k2 + k3)           {option = 3;}
01193     k++;    
01194     j++;    
01195     
01196     l_hood = pop->MH_ibd_sample_map(l_hood,option);    
01197     pop->sum_descentState_map();  // update pdq's for all marker loci 
01198     if (j >= 100000) {     
01199       j = 0;    
01200       cout << "SI_PDQ sample = " << sample << endl;  
01201     }  
01202   }
01203   cout << endl << "Haplotypes origins mm mp pm pp / mother - father " << endl;
01204   for (int i=0; i<pop->size(); i++) {
01205     pop->member(i)->display_freq_haplotype(numOfSamples);
01206   }  
01207   cout << endl; 
01208 }

void matvec::Model::DGSamplerSetup unsigned    numLoci,
Data   D
 

Definition at line 1153 of file model.cpp.

References matvec::Population::descent_graph_setup(), matvec::Vector< unsigned >::empty(), matvec::Population::founder_allele_counter, matvec::Individual::m_counter_map, matvec::Population::member(), matvec::Individual::p_counter_map, pop, popsize, matvec::Vector< unsigned >::resize(), matvec::Individual::search_heteroz(), matvec::Individual::set_founder_alleles(), and matvec::Population::size().

01153                                                    {
01154   // Authors: L. Radu Totir 
01155   // (August, 2004) 
01156   // Contributors:
01157   pop->descent_graph_setup(D);
01158   pop->founder_allele_counter = 0;
01159   int popsize = pop->size();
01160   int zero = 0;  
01161   for (int i=0;i<popsize;i++) {  
01162     pop->member(i)->set_founder_alleles(0);
01163   }
01164   unsigned lastLocus = numLoci;
01165   for (int i=0; i<popsize; i++) {
01166     if (pop->member(i)->m_counter_map.empty()){
01167       int zero = 0;    
01168       pop->member(i)->m_counter_map.resize(numLoci,zero);
01169       pop->member(i)->p_counter_map.resize(numLoci,zero);      
01170     }
01171     pop->member(i)->search_heteroz(lastLocus);  
01172     //cout << pop->member(i)-> id() << " " << pop->member(i)-> ord_heter << endl;
01173   }       
01174 }

int matvec::Model::display void    const
 

Definition at line 324 of file model.cpp.

References modelstring.

00325 {
00326    std::cout << " model:  " << modelstring << "\n";
00327    return 1;
00328 }

int matvec::Model::equation const std::string &    modelspecs
 

Reimplemented in matvec::GLMM.

Definition at line 264 of file model.cpp.

References bad_model, build_model_term(), fixed_model, lng0vec, matvec::TermList::maxorder(), maxorder, modelstring, num_ped, numterm, rawpos, matvec::Vector< double >::resize(), term, and type.

Referenced by Model().

00265 {
00266   //******************************************************************
00267   //  take the folowing model as example to show how I process model
00268   //
00269   //     model M("y1 =  A + B + A * B,"
00270   //             "y2 =  B         X(B)"
00271   //
00272   // *****************************************************************
00273    int retval = 1;
00274    num_ped=0;
00275    if (modelspecs == "") throw exception("Model::equation(): equation is empty");
00276    modelstring = modelspecs;
00277    if (build_model_term(modelstring)) {
00278       maxorder = term.maxorder();
00279       if (rawpos) {
00280         delete [] rawpos;
00281         rawpos=0;
00282       }
00283       if(maxorder>0){
00284         rawpos = new unsigned [maxorder];  // this is just a working space
00285       }
00286       else {
00287         rawpos = 0;
00288       }
00289       lng0vec.resize(numterm);
00290    }
00291    else {
00292       modelstring = "";
00293       retval = 0;
00294    }
00295    if (retval == 1) {
00296       type = fixed_model;
00297    }
00298    else {
00299       type = bad_model;
00300    }
00301    return retval;
00302 }

void matvec::Model::estimate const double **    kpme,
const unsigned    nr,
const unsigned    nc,
double *    kpb
 

Definition at line 154 of file model_hypo.cpp.

References matvec::warning().

00155 {
00156    ///////////////////////////////////////////
00157    // trailing zeros in K' can be omitted
00158    ///////////////////////////////////////////
00159    if (!kpme) {
00160       warning("Model::estimate(kp): kp is null, thus it is ignored");
00161       return;
00162    }
00163    if (!get_blupsol("ysmp")) throw exception("Model::estimate(): bad model");
00164 
00165    Vector<double> xy;
00166    if (nc < hmmesize) xy.resize(hmmesize);
00167    for (unsigned i=0; i<nr; i++) {
00168       if (nc < hmmesize) {
00169          memcpy(xy.begin(),kpme[i],sizeof(double)*nc);
00170          kpb[i] = blupsol.inner_product(xy);
00171       }
00172       else {
00173          kpb[i] = blupsol.inner_product(kpme[i]);
00174       }
00175    }
00176 }

Vector< double > matvec::Model::estimate const std::string &    termname,
const doubleMatrix   Kp
 

Definition at line 99 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), matvec::Matrix< T >::begin(), estimate(), factor_struct, get_blupsol(), hmmesize, matvec::TermList::index(), matvec::Matrix< double >::num_cols(), matvec::Matrix< double >::num_rows(), numtrait, matvec::Vector< T >::resize(), and term.

00100 {
00101    Vector<double> retval;
00102    if (!get_blupsol()) throw exception("Model::estimate(): bad model");
00103 
00104    int k = term.index(termname,factor_struct);
00105    if (k < 0) throw exception("Model::estimate(): no such term in the model");
00106 
00107    unsigned nr = Kp.num_rows();
00108    unsigned nc = Kp.num_cols();
00109    unsigned i,startaddr;
00110    if (nc <= term[k].nlevel()*numtrait) {
00111       startaddr = term[k].startaddr();
00112       Matrix<double> fullsize_Kp(nr,hmmesize);
00113       for (i=0; i<nr; i++) {
00114          memcpy(&(fullsize_Kp[i][startaddr]),Kp[i], sizeof(double)*nc);
00115       }
00116       retval.resize(nr);
00117       estimate((const double **)fullsize_Kp.begin(),nr,hmmesize,retval.begin());
00118    }
00119    else {
00120       throw exception("Model::estimate(): too many columns in Kp");
00121    }
00122    return retval;
00123 }

Vector< double > matvec::Model::estimate const doubleMatrix   Kp
 

Definition at line 55 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), matvec::Matrix< double >::begin(), estimate(), matvec::Matrix< double >::num_cols(), and matvec::Matrix< double >::num_rows().

00056 {
00057    ///////////////////////////////////////////
00058    // trailing zeros in K' can be omitted
00059    ///////////////////////////////////////////
00060 
00061    unsigned nr = Kp.num_rows();
00062    unsigned nc = Kp.num_cols();
00063    Vector<double> kpb(nr);
00064    estimate((const double **)Kp.begin(),nr,nc,kpb.begin());
00065    return kpb;
00066 }

double matvec::Model::estimate const Vector< double > &    Kp
 

Definition at line 37 of file model_hypo.cpp.

References matvec::Vector< T >::begin(), and matvec::Vector< T >::size().

Referenced by estimate().

00038 {
00039    ///////////////////////////////////////////
00040    // trailing zeros in K' can be omitted
00041    ///////////////////////////////////////////
00042 
00043    double **kpme = new double *[1];
00044    kpme[0] = Kp.begin();
00045    unsigned nc = Kp.size();
00046    double kpb[1];
00047    estimate((const double **)kpme,1,nc,kpb);
00048    if(kpme) {
00049      delete [] kpme;
00050      kpme = 0;
00051    }
00052    return kpb[0];
00053 }

double matvec::Model::estimate_peeling void   
 

Definition at line 113 of file model_peeling.cpp.

References matvec::ChromStruct::locus.

00115 {
00116    if (type == bad_model) throw exception("Model::estimate_peeling(): bad model");
00117    double max_llhood = 0.0;
00118    if (!data_prepared) {
00119       if (!prepare_data()) {
00120          type = bad_model;
00121          return max_llhood;
00122       }
00123    }
00124 
00125    minfun_indx = 2;
00126    pop->residual_var = residual_var;
00127    pop->input_data(data);
00128    pop->build_pop_gamete();
00129    unsigned i,n,m,k,j;
00130    m = pop->tn_gamete - 1;          // because p(A1)+ p(A2) +...+ p(At) = 1
00131    n = 1 + m;                       // n = residual_var + allele_freq
00132 //   n += pop->tn_genotype;         //     + genotypic_value // uncommented in old version
00133    Vector<double> x(n);
00134 
00135    x[0] = pop->residual_var[0][0];           // residual_variance
00136 
00137    ChromStruct *chrom = pop->prior->chrom();
00138    double* ve = chrom[0].locus[0].allele_freq.begin();
00139    for (i=0; i<m; i++) x[i+1] = ve[i];         // 1 is for residual_variance
00140 
00141    // uncommented in old version
00142 //   k = 1 + m;
00143 //   const double** me = pop->prior->genotypic_val(0,0);
00144 //   for (i=0; i<tn_genotype; i++) for (j=0; j<=i; j++) x[k++] = me[i][j];
00145    int iter = 0;
00146    max_llhood = praxis(x,n,iter);
00147    minfun_indx = 0;
00148    return max_llhood;
00149 }

void matvec::Model::fitdata Data   D
 

Definition at line 304 of file model.cpp.

References bad_model, data, data_prepared, hmmec, hmmesize, max_nz, npattern, matvec::Data::num_rows(), numcol, numobs, numrec, matvec::SparseMatrix::resize(), type, and matvec::warning().

Referenced by matvec::Population::setupRSampler().

00305 {
00306    if (D.num_rows() > 0) {
00307       data = &D;
00308    }
00309    else {
00310       data = 0;
00311       type = bad_model;
00312       warning("Model::fitdata(D): Data is empty");
00313    }
00314    max_nz     = 0;
00315    numrec     = 0;
00316    numobs     = 0;
00317    numcol     = 0;
00318    npattern   = 0;
00319    hmmesize   = 0;
00320    data_prepared = 0;
00321    hmmec.resize(0,0);
00322 }

double matvec::Model::genotype_dist_gibbs const unsigned    warmup = 30,
const unsigned    gibbslen = 1000
 

Definition at line 109 of file model_gibbs.cpp.

References matvec::Vector< T >::begin(), matvec::Vector< double >::begin(), blupsol, compute_xbzu(), matvec::Population::cond_genotype_config(), data, data_prepared, matvec::Population::gibbs_iterate(), matvec::SparseMatrix::gibbs_iterate(), hmmec, hmmesize, matvec::Population::input_data(), ntermGdist, pop, prepare_data(), rellrhs, matvec::Vector< T >::reserve(), residual_var, matvec::Population::residual_var, matvec::Vector< double >::resize(), matvec::Population::resize_genotype_counter(), setup_mme(), and matvec::warning().

00110 {
00111    if (ntermGdist==0) {
00112       warning(" Model::genotype_dist_gibbs(): no GeneticDist term");
00113       return 0.0;
00114    }
00115    if (!data_prepared) prepare_data();
00116    unsigned i,it;
00117    blupsol.resize(hmmesize,0.0);
00118    Vector<double> wspace; wspace.reserve(hmmesize);
00119    pop->resize_genotype_counter();
00120    pop->residual_var = residual_var;
00121    pop->input_data(data);
00122    pop->cond_genotype_config();   //  an initial configuration
00123 
00124    if (numterm) setup_mme(&rellrhs);
00125 
00126    /////////////////////////////////////////////////////
00127    //    Gibbs Sampler warm up
00128    /////////////////////////////////////////////
00129    for (it=0; it<warmup; it++) {
00130       if (numterm) hmmec.gibbs_iterate(blupsol.begin(),rellrhs.begin(),wspace.begin());
00131       for (i=0; i<ntermGdist; i++) {
00132          compute_xbzu(blupsol);                   // to adjust trait
00133          pop->gibbs_iterate();
00134       }
00135    }
00136 
00137    /////////////////////////////////////////////////////
00138    //    samples from Gibbs Sampler now will be used
00139    ////////////////////////////////////////////////////
00140 
00141    for (it=0; it<gibbslen; it++) {
00142       if (numterm) hmmec.gibbs_iterate(blupsol.begin(),rellrhs.begin(),wspace.begin());
00143       for (i=0; i<ntermGdist; i++) {
00144          compute_xbzu(blupsol);                       // to adjust trait
00145          pop->gibbs_iterate(1);
00146       }
00147    }
00148    hmmec.resize(0,0);
00149    ntermGdist = 0;
00150    return 0.0;     // no useful value could be returned so far
00151 }

void matvec::Model::genotype_dist_peeling const int    prtlevel = 1,
const int    estifreq = 1
 

Definition at line 29 of file model_peeling.cpp.

References bad_model, blupsol, compute_xbzu(), data, data_prepared, matvec::Population::genotype_dist_peeling(), hmmec, hmmesize, matvec::Population::input_data(), ntermGdist, pop, prepare_data(), rellrhs, residual_var, matvec::Population::residual_var, matvec::SparseMatrix::resize(), matvec::Vector< double >::resize(), setup_mme(), matvec::SparseMatrix::solve(), and type.

00030 {
00031    //////////////////////////////////////////////////////////////////////////
00032    // this works for arbitrary pedigrees
00033    // based on iterative peeling algorithm: Arendonk(1989), Fernando(1993)
00034    //  prtl     = 0, print nothing
00035    //             1, default, print genotype distribution and genotypic values
00036    //  estifreq = 0, not estimate allele frequencies
00037    //             1, default, estimate allele frequencies
00038    //////////////////////////////////////////////////////////////////////////
00039    if (type == bad_model) return;
00040    if (!data_prepared) {
00041       if (!prepare_data()) {
00042          type = bad_model;
00043          return;
00044       }
00045    }
00046 
00047    pop->residual_var = residual_var;
00048    pop->input_data(data);
00049 
00050    blupsol.resize(hmmesize);
00051 
00052    if (numterm) {
00053       setup_mme(&rellrhs);
00054       hmmec.solve(blupsol,rellrhs);
00055    }
00056    if (ntermGdist) {
00057       compute_xbzu(blupsol);                   // to adjust trait
00058       pop->genotype_dist_peeling(prtl,estifreq);
00059    }
00060 
00061    hmmec.resize(0,0);
00062    ntermGdist = 0;
00063 }

Vector< double > * matvec::Model::genotypic_value_cat const unsigned    warmup,
const unsigned    gibbslen
 

Definition at line 77 of file model_gibbs.cpp.

References matvec::Vector< double >::begin(), matvec::Vector< T >::begin(), blupsol, data_prepared, matvec::SparseMatrix::gibbs_iterate(), hmmec, hmmesize, prepare_data(), rellrhs, matvec::Vector< T >::reserve(), matvec::Vector< double >::resize(), sampleW(), and setup_mme().

Referenced by genotypic_value_gibbs().

00079 {
00080    if (!data_prepared) prepare_data();
00081 
00082    blupsol.resize(hmmesize,0.0);
00083    Vector<double> tmpsol(hmmesize);
00084    Vector<double> wspace; wspace.reserve(hmmesize);
00085    setup_mme(&rellrhs);            // relrhs is used here
00086 
00087    /////////////////////////////////////////////////////
00088    //    Gibbs Sampler warm up
00089    /////////////////////////////////////////////
00090    unsigned it;
00091    for (it=0; it<warmup; it++) {
00092       sampleW(tmpsol,rellrhs);
00093       hmmec.gibbs_iterate(tmpsol.begin(),rellrhs.begin(),wspace.begin());
00094    }
00095 
00096    /////////////////////////////////////////////////////
00097    //    samples from Gibbs Sampler now will be used
00098    ////////////////////////////////////////////////////
00099    for (it=0; it<gibbslen; it++) {
00100       sampleW(tmpsol,rellrhs);
00101       hmmec.gibbs_iterate(tmpsol.begin(),rellrhs.begin(),wspace.begin());
00102       blupsol += tmpsol;
00103    }
00104    blupsol /= gibbslen;
00105    hmmec.resize(0,0);
00106    return &blupsol;
00107 }

Vector< double > * matvec::Model::genotypic_value_gibbs const unsigned    warmup = 30,
const unsigned    gibbslen = 1000
 

Definition at line 153 of file model_gibbs.cpp.

References matvec::Vector< T >::assign(), matvec::Vector< double >::begin(), matvec::Vector< T >::begin(), blupsol, compute_xbzu(), matvec::Population::cond_genotype_config(), data, data_prepared, genotypic_value_cat(), matvec::Population::gibbs_iterate(), matvec::SparseMatrix::gibbs_iterate(), hmmec, hmmesize, matvec::Population::input_data(), ntermGdist, pop, popsize, prepare_data(), rellrhs, matvec::Vector< T >::reserve(), residual_var, matvec::Population::residual_var, matvec::Vector< double >::resize(), and setup_mme().

00155 {
00156    if (categorical) return genotypic_value_cat(warmup,gibbslen);
00157    if (!data_prepared) prepare_data();
00158    unsigned i,it;
00159    if (numterm) setup_mme(&rellrhs);
00160    blupsol.resize(hmmesize + ntermGdist*popsize, 0.0);
00161    Vector<double> tmpsol(hmmesize);
00162           tmpsol.assign(0.0);
00163    Vector<double> wspace; wspace.reserve(hmmesize);
00164    double * ve = &(blupsol[hmmesize]);
00165    if (ntermGdist) {
00166       pop->residual_var = residual_var;
00167       pop->input_data(data);
00168       pop->cond_genotype_config();
00169    }
00170 
00171    /////////////////////////////////////////////////////
00172    //    Gibbs Sampler warm up
00173    /////////////////////////////////////////////
00174    for (it=0; it<warmup; it++) {
00175       if (numterm) hmmec.gibbs_iterate(tmpsol.begin(),rellrhs.begin(),wspace.begin());
00176       for (i=0; i<ntermGdist; i++) {
00177          compute_xbzu(tmpsol);                   // to adjust trait
00178          pop->gibbs_iterate();
00179       }
00180    }
00181 
00182    /////////////////////////////////////////////////////
00183    //    samples from Gibbs Sampler now will be used
00184    ////////////////////////////////////////////////////
00185 
00186    for (it=0; it<gibbslen; it++) {
00187       if (numterm) {
00188          hmmec.gibbs_iterate(tmpsol.begin(),rellrhs.begin(),wspace.begin());
00189          for (i=0; i<hmmesize; i++) blupsol[i] += tmpsol[i];
00190       }
00191       for (i=0; i<ntermGdist; i++) {
00192          compute_xbzu(tmpsol);                       // to adjust trait
00193          pop->gibbs_iterate();
00194          for (i=0; i<popsize; i++) {
00195             ve[i] += pop->popmember[i]->genotypic_val();
00196          }
00197       }
00198    }
00199    blupsol /= gibbslen;
00200    hmmec.resize(0,0);
00201    return &(blupsol);
00202 }

Vector< double > * matvec::Model::genotypic_value_peeling void   
 

Definition at line 65 of file model_peeling.cpp.

References bad_model, blupsol, compute_xbzu(), data, data_prepared, matvec::Individual::est_GV, matvec::Population::genotype_dist_peeling(), get_blupsol(), hmmec, hmmesize, matvec::Population::input_data(), ntermGdist, pop, matvec::Population::popmember, popsize, prepare_data(), matvec::Vector< double >::print(), rellrhs, residual_var, matvec::Population::residual_var, matvec::Vector< double >::resize(), setup_mme(), matvec::SparseMatrix::solve(), and type.

00066 {
00067    if (type == bad_model) return 0;
00068    if (!data_prepared) {
00069       if (!prepare_data()) {
00070          type = bad_model;
00071          return 0;
00072       }
00073    }
00074 
00075    blupsol.resize(hmmesize + ntermGdist*popsize);
00076    unsigned i;
00077 
00078    if (ntermGdist ) {
00079       pop->residual_var = residual_var;
00080       pop->input_data(data);
00081       if (numterm) {
00082          get_blupsol();
00083          compute_xbzu(blupsol);                   // to adjust trait
00084       }
00085       else {
00086          pop->genotype_dist_peeling(0);
00087          for (i=0; i<popsize; i++) blupsol[i] = pop->popmember[i]->est_GV;
00088          return  &(blupsol);
00089       }
00090    }
00091    else {
00092       setup_mme(&rellrhs);
00093       hmmec.solve(blupsol,rellrhs);
00094       return &(blupsol);
00095    }
00096 
00097    ///////////////////////////////////////////
00098    //   iterate begins now
00099    ////////////////////////////////////////////
00100 
00101    double * ve = &(blupsol[hmmesize]);
00102    unsigned niterate = 0;
00103    while (niterate++ < 4) {
00104       pop->genotype_dist_peeling(0);
00105       for (i=0; i<popsize; i++) ve[i] = pop->popmember[i]->est_GV;
00106       blupsol.print();
00107       setup_mme(&rellrhs);
00108       hmmec.solve(blupsol,rellrhs);
00109    }
00110    return &(blupsol);
00111 }

Vector< double > * matvec::Model::get_blupsol const std::string &    method = "ysmp",
double    stopval = 0.001,
double    relax = 1.0,
unsigned    mxiter = 100000
 

Return the BLUP solution

The difference between blup() and get_blupsol() is that blup() will always recompute blup again, while get_blupsol() gets whatsoever in blupsol, Of course, if it's empty then it will compute blup solution

See also:
blup,blue

Definition at line 257 of file model_blup.cpp.

References bad_model, blupsol, hmmec, matvec::SparseMatrix::nz(), rellrhs, setup_mme(), matvec::SparseMatrix::solve(), and type.

Referenced by contrast(), matvec::GLMM::contrast(), CovMat(), estimate(), genotypic_value_peeling(), and get_solutions().

00259 {
00260    Vector<double> *retval = 0;
00261    if (type == bad_model) throw exception("Model::blup(): bad model");
00262    if (hmmec.nz() == 0) {
00263       setup_mme(&rellrhs);
00264       if (type == bad_model) throw exception("Model::blup(): bad model");
00265       if (hmmec.solve(blupsol,rellrhs,method,relax,stopval,mxiter)==1) {
00266          retval = &blupsol;
00267          //hmmec.close();
00268       }
00269    }
00270    else {
00271       retval = &blupsol;
00272    }
00273    return retval;
00274 }

Vector<int> matvec::Model::get_gamete_vector unsigned    index
 

void matvec::Model::get_lms_kp const unsigned    termth,
const unsigned    lvlth,
const unsigned    yth,
Vector< double > &    kp,
const int *    lsmtablevec
[protected]
 

Definition at line 448 of file model_hypo.cpp.

References matvec::SparseMatrix::a(), matvec::Vector< T >::assign(), matvec::Vector< T >::begin(), matvec::Session::epsilon, hmmec, hmmesize, matvec::SparseMatrix::ia(), matvec::SparseMatrix::ja(), numterm, numtrait, matvec::SESSION, and term.

Referenced by out_lsmeans_to_stream().

00450 {
00451    int i,j,k,h;
00452    unsigned l,t,jb,n,nh,nl,term_start,term_nlevel,endaddr;
00453    int mcell;
00454 
00455    k = term[termid].startaddr() + lvlth*numtrait + yth;
00456    int *ia = hmmec.ia();
00457    int *ja = hmmec.ja();
00458    double *a = hmmec.a();
00459    kp.assign(0.0);
00460    double *kpve = kp.begin();
00461    kpve--;
00462    mcell = 0;
00463    k++;
00464    endaddr = ia[k+1];
00465    for (j=ia[k]; j<endaddr; j++) {
00466       if (ja[j] != k) continue;
00467       if (a[j] <= SESSION.epsilon) {
00468          mcell = 1;
00469          return;
00470       }
00471       else {
00472           break;
00473       }
00474    }
00475 
00476    ////////////////////////////////////////////////////////////////
00477    // set  element kp[i] = 1.0 if MME[k][i] is non-zero element
00478    //
00479    ////////////////////////////////////////////////////////////////
00480    for (j=ia[k]; j<endaddr; j++) kpve[ja[j]] = 1.0;
00481    for (i=1; i<=hmmesize; i++) {
00482       endaddr = ia[i+1];
00483       for (j=ia[i]; j<endaddr; j++) if (ja[j] == k) kpve[i] = 1.0;
00484    }
00485 
00486    for (t=0; t<numterm; t++) {
00487       if (!(term[t].classi() == 'F' || term[t].classi() == 'C')) continue;
00488       if (lsmtablevec[t] >= 1) continue;
00489       term_start = term[t].startaddr() + 1;
00490       term_nlevel = term[t].nlevel();
00491       l = term_start + yth;
00492       for (i=0; i<term_nlevel; i++) {
00493          for (n=ia[l+1], j=ia[l]; j<n; j++) {
00494             if (ja[j] == l) {
00495                if (fabs(a[j]) > SESSION.epsilon) kpve[l] = 1.0;
00496             }
00497          }
00498          l += numtrait;
00499       }
00500    }
00501 
00502    ////////////////////////////////////////////////////////////////////
00503    // if term[i] is random, then set all elements kp[j] for term[i]
00504    //    to be zeros,
00505    // else set elements kp[j] for non-relevent trait to be zero
00506    ////////////////////////////////////////////////////////////////
00507    for (i=0; i<numterm; i++) {
00508       term_start = term[i].startaddr() + 1;
00509       term_nlevel = term[i].nlevel();
00510       if (term[i].classi() == 'F' || term[i].classi() == 'C') {
00511          for (t=0; t<numtrait; t++) {
00512             if (t != yth) {
00513                l = term_start;
00514                for ( j=0; j<term_nlevel; j++) {
00515                   kpve[l+t] = 0.0;
00516                   l +=  numtrait;
00517                }
00518             }
00519          }
00520       }
00521       else {
00522          memset(&(kpve[term_start]),'\0',numtrait*sizeof(double)*term_nlevel);
00523       }
00524    }
00525 
00526    double dx,xn;
00527    DataNode dnode;
00528    for (j=0; j<numterm; j++) {
00529       if (lsmtablevec[j] == 2) continue;   // j = lsm_term
00530       term_nlevel = term[j].nlevel();
00531       term_start = term[j].startaddr() + 1;
00532       dx = 1.0;
00533       if (term[j].classi() == 'C') {
00534          k = factor_struct[term[j].factorindx[0]].index();
00535          dnode = data->datasheet[k].mean(0);
00536          if (dnode.missing) throw exception("get_lms_kp(): no valid values in the covariate column");
00537          dx = dnode.double_val();
00538       }
00539       if (!(term[j].classi() == 'F' || term[j].classi() == 'C')) continue;
00540       if (lsmtablevec[j] == 1) {        // j is related to lsm_term
00541          n = term_start + term_nlevel*numtrait;
00542          for (xn=0.0,k=term_start; k<n; k++) xn += kpve[k];
00543          if (xn >= SESSION.epsilon*10) {
00544             for (k=term_start; k<n; k++) kpve[k] *= dx/xn;
00545          }
00546       }
00547       else {
00548          if (term[j].order() == 1) {
00549             n = term_start + term_nlevel*numtrait;
00550             for (xn=0.0, k=term_start; k<n; k++) xn += kpve[k];
00551             if (xn >= SESSION.epsilon*10) {
00552                for (k=term_start; k<n; k++) kpve[k] *= dx/xn;
00553             }
00554          }
00555          else {
00556             nl = factor_struct[term[j].factorindx[0]].nlevel();
00557             nh = term_nlevel/nl;
00558             for (k=0; k<nh; k++) {
00559                jb = term_start + k;
00560                for (xn=0.0,h=0; h<nl; h++) {
00561                   xn += kpve[jb];
00562                   jb += nh*numtrait;
00563                }
00564                if (xn < SESSION.epsilon*10) continue;
00565                jb = term_start + k;
00566                for (h=0; h<nl; h++) {
00567                   kpve[jb] *= dx/(xn*nh);
00568                   jb += nh*numtrait;
00569                }
00570             }
00571          }
00572       }
00573    }
00574 }

unsigned matvec::Model::get_popsize void    const [inline]
 

Definition at line 339 of file model.h.

References popsize.

00339 {return popsize;};

Vector< double > matvec::Model::get_solutions const std::string &    termname
 

Definition at line 2390 of file model.cpp.

References blupsol, factor_struct, get_blupsol(), matvec::TermList::index(), numtrait, matvec::Vector< T >::resize(), and term.

02391 {
02392   // Authors: Liviu R. Totir and Rohan L. Fernando (November, 2002) 
02393   // Contributors: 
02394   Vector<double> retval;
02395   if (!get_blupsol()) throw exception("Model::get_solutions(): get_blupsol() failed");
02396   
02397   int k = term.index(termname,factor_struct);
02398   if (k < 0) throw exception("Model::get_solutions(): no such term in the model");
02399   int nlevels = term[k].nlevel()*numtrait;
02400   unsigned i,startaddr;
02401   startaddr = term[k].startaddr();
02402   retval.resize(nlevels);
02403   for (i=0; i<nlevels; i++) {
02404     retval[i] = blupsol[startaddr+i];
02405   }
02406   return retval;
02407 }

matvec::Matrix< double > matvec::Model::getIBDMatrix void   
 

Definition at line 1713 of file model.cpp.

References pop, matvec::Population::popmember, popsize, matvec::Population::popsize, matvec::Matrix< T >::resize(), and matvec::Matrix< T >::transpose().

Referenced by RSamplerGibbs(), RSamplerGibbsMH(), and RSamplerMH().

01713                                             {
01714   // Author: L. Radu Totir
01715   // (July, 2004) 
01716   // Contributors: 
01717   matvec::Matrix<double> geneticValVec;
01718   matvec::Matrix<double> CovMatrix;
01719   CovMatrix.resize(pop->popsize,pop->popsize,0.0);
01720   matvec::Vector<double> qtlMu;
01721   qtlMu.resize(4,0.0);
01722   qtlMu[0]=-1.41421,qtlMu[3]=1.41421;
01723   geneticValVec.resize(pop->popsize,1,0.0);
01724   for (unsigned t=0;t<pop->popsize;t++){
01725     // cout << (pop->popmember[t])->id() << "   " 
01726     //  << (pop->popmember[t])->genotNodeVector[1].acceptedGenotypeState 
01727     //      << endl;
01728     int geno = (pop->popmember[t])->genotNodeVector[1].acceptedGenotypeState;
01729     geneticValVec[t][0] = qtlMu[geno];
01730   }
01731   CovMatrix =geneticValVec*geneticValVec.transpose();
01732   return CovMatrix;
01733 }

double matvec::Model::graph_log_likelihood_peeling const unsigned    maxiter
 

Definition at line 1155 of file model_peeling.cpp.

References matvec::Population::analysis_type, bad_model, matvec::Vector< double >::begin(), blupsol, compute_xbzu(), data, data_prepared, matvec::Individual::est_GV, matvec::SparseMatrix::factorization(), matvec::Population::genotype_dist_peeling(), hmmec, hmmesize, matvec::SparseMatrix::info_vec, matvec::Population::input_data(), max_nz, ntermGdist, numobs, numterm, pop, matvec::Population::popmember, popsize, prepare_data(), matvec::SparseMatrix::q(), rellrhs, matvec::SparseMatrix::reorder(), matvec::Population::residual_var, residual_var, matvec::Vector< double >::resize(), matvec::SparseMatrix::resize(), setup_ww_single_trait(), matvec::SparseMatrix::solve(), term, matvec::Population::tn_qtl, totalnvc(), type, var2vec(), and matvec::Individual::xbzu_val.

01156 {
01157   if (ntermGdist == 0) {
01158     throw exception("Model::graph_log_likelihood_peeling(): ntermGdist == 0");
01159     return 0.0;
01160   }
01161 
01162   if (type == bad_model) {
01163     throw exception("Model::graph_log_likelihood_peeling(): bad model");
01164     return 0.0;
01165   }
01166   if (!data_prepared) {
01167     if (!prepare_data()) {
01168       type = bad_model;
01169       return 0.0;
01170     }
01171   }
01172 
01173   int pt,i;
01174   Individual *I;
01175   double sigma_mu,retval = 0.0;
01176   double *sol, rr;
01177   unsigned nl,rank_mme,startaddr,iter = 0;
01178   pop->analysis_type = "not-multi";
01179   pop->tn_qtl=4;
01180   if (numterm) {
01181     retval += numobs*std::log(residual_var[0][0]);
01182     hmmec.resize(hmmesize,max_nz);
01183     rellrhs.resize(hmmesize);
01184     blupsol.resize(hmmesize);
01185 
01186     pop->genotype_dist_peeling(0);
01187     for (i=0; i<popsize; i++) {
01188       I = pop->popmember[i];
01189       I->xbzu_val = I->est_GV;  
01190     }
01191     unsigned nvc = totalnvc() - 1;              // number of ratio's
01192     Vector<double> zz(popsize);
01193     Vector<double> ratio(nvc);
01194     Vector<double> vc(nvc+1);
01195     
01196     var2vec(vc);
01197     for (i=0; i<nvc; i++) ratio[i] = vc[nvc]/vc[i];
01198     for (pt=-1,i=0; i<numterm; i++) {
01199       if (term[i].classi() == 'P') {
01200         pt = i;
01201         startaddr =  term[pt].start + 1;
01202         nl = term[pt].nlevel();
01203         break;
01204       }
01205     }
01206     
01207     setup_ww_single_trait(ratio,pt,startaddr,&zz);
01208     hmmec.reorder();
01209     rank_mme = hmmec.factorization(5);
01210     
01211     hmmec.solve(blupsol,rellrhs,"ysmp1");
01212     retval += hmmec.info_vec[0]- rank_mme*std::log(residual_var[0][0]);
01213 
01214     if (pt >= 0) {
01215       sol = &(blupsol[startaddr-1]);
01216       for (rr=0.0,i=0; i<nl; i++) {
01217         rr += (*sol * zz[i] * *sol);  sol++;
01218       }
01219       sigma_mu = *(term[pt].prior->var_matrix())[0][0];
01220       retval += nl*std::log(sigma_mu);
01221       sigma_mu = hmmec.q(blupsol.begin(),blupsol.begin(),startaddr,startaddr+nl-1)
01222         - rr;
01223       retval += std::log(sigma_mu);
01224     }
01225     compute_xbzu(blupsol);                   // to adjust trait
01226   }
01227   retval *= -0.5;
01228   pop->residual_var = residual_var;
01229   pop->input_data(data);
01230   //  retval += pop->graph_log_likelihood_peeling(maxiter);
01231   return retval;
01232 }

void matvec::Model::hashxact const Vector< bool > &    [protected]
 

Definition at line 1002 of file model.cpp.

References matvec::Field::classi(), data, matvec::Data::datasheet, matvec::DataNode::double_val(), factor_struct, matvec::HashTable::get_id(), idlist, matvec::TermList::index(), matvec::Vector< T >::initialize(), matvec::HashTable::insert(), matvec::Data::num_rows(), numcol, numterm, rawpos, matvec::Data::row(), term, matvec::DataNode::unsigned_val(), and xact_htable.

Referenced by assign_id_xact().

01003 {
01004    if (!data) return;
01005    char CL;
01006    int i,j,t,k,nord;
01007    int *tempval = new int;
01008    Vector<unsigned> pos_datacol(numcol);
01009    DataNode* recd; 
01010    if(numcol>0){
01011      recd = new DataNode [numcol];
01012    }
01013    else {
01014      recd = 0;
01015    }
01016    unsigned nrow_in_data = data->num_rows();
01017 
01018    for (i=0; i<nrow_in_data; i++) {
01019       if (record_legality[i]) continue;
01020       data->row(i,recd);
01021       pos_datacol.initialize(numcol,0);
01022       for (t=0; t<numterm; t++) {
01023          nord = term[t].order();
01024          if (nord > 1) {
01025             for (j=0; j<nord; j++) {
01026                k = factor_struct[term[t].factorindx[j]].index();
01027                if (pos_datacol[k] == 0) {
01028                   CL = data->datasheet[k].classi();
01029                   if (CL == 'F') {
01030                      *tempval = static_cast<int>(recd[k].double_val());
01031                      pos_datacol[k] = idlist[k]->get_id(tempval);
01032                   }
01033                   else if (CL == 'U' ||CL == 'P') {
01034                      pos_datacol[k] = recd[k].unsigned_val();
01035                   }
01036                   else if (CL =='C') {
01037                      pos_datacol[k] = 1;
01038                   }
01039                }
01040                rawpos[j] = pos_datacol[k];
01041             }
01042             xact_htable[t].insert(rawpos);
01043          }
01044       }
01045    }
01046    if (recd) {
01047      delete [] recd;
01048      recd = 0;
01049    }
01050    if(tempval){
01051      delete tempval;
01052      tempval = 0;
01053    }
01054 }

void matvec::Model::info std::ostream &    stream const
 

Definition at line 1017 of file model_vce.cpp.

References bad_model, matvec::ModelTerm::classi(), dfreml_method, factor_struct, matvec::ModelTerm::factorindx, hmmec, hmmesize, matvec::FieldStruct::mean(), matvec::FieldStruct::name(), nfunk_in_dfreml, matvec::FieldStruct::nlevel(), numterm, numtrait, matvec::SparseMatrix::nz(), matvec::Session::output_precision, matvec::Matrix< double >::print(), matvec::ModelTerm::prior, reml_value, residual_var, matvec::SESSION, matvec::FieldStruct::std(), term, trait_struct, type, and matvec::GeneticDist::var_matrix().

Referenced by matvec::funk(), lsmeans(), save(), vce_dfreml(), vce_emreml_multi_trait(), vce_emreml_single_trait(), and vce_gibbs().

01018 {
01019    if (type == bad_model) throw exception("Model::info(stream): model is too bad");
01020    stream.precision(SESSION.output_precision);
01021    stream <<"\n             some extra information in the model\n";
01022    stream << " --------------------------------------------------------\n";
01023    if (dfreml_called) {
01024       stream << "optimizor used in dfreml = " << dfreml_method <<"\n";
01025       stream <<"# of function evaluations used in dfreml = "
01026              << nfunk_in_dfreml<<"\n";
01027       stream << "maximum log restricted likelihood in dfreml = "
01028              <<  reml_value <<"\n";
01029    }
01030    else {
01031       ;
01032    }
01033    const ModelTerm *T;
01034    int i;
01035    for (i=0; i<numterm; i++) {
01036       T = &(term(i));
01037       if (T->classi() == 'R' || T->classi() == 'P') {
01038          stream << " variance for "
01039               << factor_struct[T->factorindx[0]].name() << " = \n";
01040          T->prior->var_matrix()->print(stream);
01041       }
01042    }
01043    stream << " residual variance =\n" << residual_var;
01044 
01045    if (data_prepared) {
01046       stream << "\n";
01047       stream << " MME dimension   : " << hmmesize << "\n";
01048       stream << " non-zeros in MME: " << hmmec.nz() << "\n\n";
01049 
01050       stream << "       basic statistics for dependent variables\n";
01051       stream << "  -----------------------------------------------------\n";
01052       stream << " trait-name              n          mean         std\n";
01053       unsigned n;
01054       for (i=0; i<numtrait; i++) {
01055          n = trait_struct[i]->nlevel();
01056          stream << " " << std::setw(16) << trait_struct[i]->name()
01057                 << " " << std::setw(10) << n
01058                 << " " << std::setw(12) << trait_struct[i]->mean();
01059          if (n==1) {
01060             stream << " " << std::setw(12) << "." << "\n";
01061          }
01062          else {
01063             stream<< " " << std::setw(12) << trait_struct[i]->std() << "\n";
01064          }
01065       }
01066    }
01067    stream << " --------------------------------------------------------\n";
01068 }

void matvec::Model::initialize void   
 

Definition at line 35 of file model.cpp.

References bad_model, data, data_prepared, dfreml_called, factor_struct, hmmesize, idlist, kvec, max_nz, maxorder, modelpcount, modelstr, nfunk_in_dfreml, non_zero, npattern, ntermGdist, num_ped, numcol, numfact, numgroup, numobs, numrec, numterm, numtrait, pop, pop_created, popsize, pos_term, rawpos, rec_indid, reml_value, rve, trait_struct, trait_vec, traitname, type, unknown_prior, weightinverse, xact_htable, and xval_term.

Referenced by copyfrom(), and Model().

00036 {
00037    num_ped        = 0;
00038    max_nz         = 0;
00039    numrec         = 0;
00040    numobs         = 0;
00041    numcol         = 0;
00042    numterm        = 0;
00043    popsize        = 0;
00044    numfact        = 0;
00045    maxorder       = 0;
00046    numtrait       = 0;
00047    npattern       = 0;
00048    hmmesize       = 0;
00049    numgroup       = 0;
00050    non_zero       = 0;
00051    type           = bad_model;    // it becomes good mode after having equations - was model_type
00052    ntermGdist     = 0;
00053    pop_created    = 0;
00054    data_prepared  = 0;
00055    weightinverse  = 0;
00056    modelpcount    =0;
00057    modelstr       = 0;
00058 
00059    pop            = 0;
00060    rve            = 0;
00061    data           = 0;
00062    kvec           = 0;
00063    rawpos         = 0;
00064    idlist         = 0;
00065    pos_term       = 0;
00066    xval_term      = 0;
00067    traitname      = 0;
00068    trait_vec      = 0;
00069    rec_indid      = 0;
00070    xact_htable    = 0;
00071    factor_struct  = 0;
00072    trait_struct   = 0;
00073    unknown_prior  = 0;
00074 
00075    // **********************************************
00076    //  information variables for Model::info(stream)
00077    // **********************************************
00078    nfunk_in_dfreml = 0;
00079    reml_value  = 0.0;
00080    dfreml_called = 0;
00081 }

void matvec::Model::input_pos_val std::istream &    modelfile [protected]
 

Input position and value for each term in the model

Note that term[k].addr[t] starts from 1 rather than from 0

Definition at line 367 of file model_blup.cpp.

References base_effect, nt_vec, numterm, numtrait, pos_term, term, trait_vec, and xval_term.

Referenced by matvec::GLMM::residual(), sampleW(), setup_ww(), matvec::GLMM::setup_ww(), setup_ww_single_trait(), and matvec::GLMM::SSQCP().

00368 {
00369    int t,k,nt;
00370    unsigned startaddr;
00371 
00372    modelfile.read((char *)pos_term, (numterm+1)*sizeof(unsigned));
00373    modelfile.read((char *)xval_term, (numterm+1)*sizeof(double));
00374    modelfile.read((char *)trait_vec, numtrait*sizeof(double));
00375 
00376    for (k=0; k<numterm; k++) {
00377      nt=nt_vec[base_effect[k]];
00378       startaddr = term[k].start + 1 +(pos_term[k]-1)*nt ;
00379       for (t=0; t<numtrait; t++) {
00380         if (term[k].trait[t]) term[k].addr[t] = startaddr + t;
00381         else                  term[k].addr[t] = 0;
00382       }
00383    }
00384 }

void matvec::Model::input_pos_val_iod std::vector< pos_val_node >::iterator    it [protected]
 

Definition at line 385 of file model_blup.cpp.

References base_effect, nt_vec, numterm, numtrait, and term.

Referenced by compute_rhs_diag_mme(), and mme_times().

00386 {
00387    int t,k,nt;
00388    unsigned startaddr;
00389    for (k=0; k<numterm; k++) {
00390      nt=nt_vec[base_effect[k]];
00391       startaddr = term[k].start + 1 + (it->pos_term[k]-1)*nt ;
00392       for (t=0; t<numtrait; t++) {
00393         if (term[k].trait[t]) term[k].addr[t] = startaddr + t;
00394         else                  term[k].addr[t] = 0;
00395       }
00396    }
00397 }

void matvec::Model::inverse_residual_var void    [protected]
 

Compute the inverse of residual variance matrix

Definition at line 594 of file model_blup.cpp.

References matvec::Matrix< double >::begin(), matvec::Session::epsilon, matvec::ginverse1(), lnr0vec, npattern, numtrait, pattern, residual_var, rve, and matvec::SESSION.

Referenced by blup_pccg(), matvec::GLMM::residual(), and setup_mme().

00595 {
00596    unsigned i,t,t1,t2;
00597    double **ve;
00598    for (i=0; i<npattern; i++) {
00599       ve = rve[i].begin();
00600       for (t1=0; t1<numtrait; t1++) for (t2=0; t2<numtrait; t2++) {
00601          ve[t1][t2] = residual_var[t1][t2];
00602       }
00603       for (t=0; t<numtrait; t++) {
00604          if (pattern[i][t]=='0') {
00605             for (t1=0; t1<numtrait; t1++) ve[t1][t] = 0.0;  // cross col t
00606             for (t2=0; t2<numtrait; t2++) ve[t][t2] = 0.0;  // cross row t
00607          }
00608       }
00609       ginverse1(ve,numtrait,lnr0vec[i],1,SESSION.epsilon);
00610    }
00611 }

std::string matvec::Model::label const std::string &    termname,
const unsigned    i
 

Definition at line 260 of file model_hypo.cpp.

References factor_struct, matvec::TermList::index(), term, and trait_effect_level().

00260                                                                 {
00261   std::string rawcode;
00262   if(termname==""){
00263     trait_effect_level(i,rawcode);
00264   }
00265   else{
00266     int k = term.index(termname,factor_struct);
00267     if (k < 0) throw exception("Model::label(): no such term in the model");
00268     unsigned startaddr = term[k].startaddr();
00269     // cout << startaddr+i << std::endl;
00270     trait_effect_level(startaddr+i,rawcode);
00271   }
00272   return(rawcode);
00273 }

double matvec::Model::log_likelihood_gibbs const unsigned    wup = 30,
const unsigned    glen = 1000
 

Definition at line 71 of file model_gibbs.cpp.

References matvec::warning().

00072 {
00073    warning("Model.log_likelihood_gibbs(): not inplemented yet");
00074    return 0;
00075 }

double matvec::Model::log_likelihood_peeling const unsigned    maxit = 3
 

Definition at line 195 of file model_peeling.cpp.

References matvec::Population::analysis_type, bad_model, matvec::Vector< double >::begin(), blupsol, compute_xbzu(), data, data_prepared, matvec::Individual::est_GV, matvec::SparseMatrix::factorization(), matvec::Population::genotype_dist_peeling(), hmmec, hmmesize, matvec::SparseMatrix::info_vec, matvec::Population::input_data(), matvec::Population::log_likelihood_peeling(), max_nz, ntermGdist, numobs, numterm, pop, matvec::Population::popmember, popsize, prepare_data(), matvec::SparseMatrix::q(), rellrhs, matvec::SparseMatrix::reorder(), matvec::Population::residual_var, residual_var, matvec::Vector< double >::resize(), matvec::SparseMatrix::resize(), restricted_log_likelihood(), setup_ww_single_trait(), matvec::SparseMatrix::solve(), term, matvec::Population::tn_qtl, totalnvc(), type, var2vec(), and matvec::Individual::xbzu_val.

00196 {
00197    if (type == bad_model) throw exception("Model::log_likelihood_peeling(): bad model");
00198    if (ntermGdist == 0) {
00199       return restricted_log_likelihood();
00200    }
00201 
00202    if (!data_prepared) {
00203       if (!prepare_data()) {
00204          type = bad_model;
00205          return 0.0;
00206       }
00207    }
00208 
00209    int pt,i;
00210    Individual *I;
00211    double sigma_mu,retval = 0.0;
00212    double *sol, rr;
00213    unsigned nl,rank_mme,startaddr,iter = 0;
00214    pop->analysis_type = "not-multi";
00215   pop->tn_qtl=4;
00216    if (numterm) {
00217       retval += numobs * std::log(residual_var[0][0]);
00218       hmmec.resize(hmmesize,max_nz);
00219       rellrhs.resize(hmmesize);
00220       blupsol.resize(hmmesize);
00221 
00222       pop->genotype_dist_peeling(0);
00223       for (i=0; i<popsize; i++) {
00224          I = pop->popmember[i];
00225          I->xbzu_val = I->est_GV;
00226       }
00227       unsigned nvc = totalnvc() - 1;              // number of ratio's
00228       Vector<double> zz(popsize);
00229       Vector<double> ratio(nvc);
00230       Vector<double> vc(nvc+1);
00231 
00232       var2vec(vc);
00233       for (i=0; i<nvc; i++) ratio[i] = vc[nvc]/vc[i];
00234       for (pt=-1,i=0; i<numterm; i++) {
00235          if (term[i].classi() == 'P') {
00236             pt = i;
00237             startaddr =  term[pt].start + 1;
00238             nl = term[pt].nlevel();
00239             break;
00240          }
00241       }
00242 
00243       setup_ww_single_trait(ratio,pt,startaddr,&zz);
00244       hmmec.reorder();
00245       rank_mme = hmmec.factorization(5);
00246 
00247       hmmec.solve(blupsol,rellrhs,"ysmp1");
00248       retval += hmmec.info_vec[0]- rank_mme * std::log(residual_var[0][0]);
00249 
00250       if (pt >= 0) {
00251          sol = &(blupsol[startaddr-1]);
00252          for (rr=0.0,i=0; i<nl; i++) {
00253             rr += (*sol * zz[i] * *sol);  sol++;
00254          }
00255          sigma_mu = *(term[pt].prior->var_matrix())[0][0];
00256          retval += nl * std::log(sigma_mu);
00257          sigma_mu = hmmec.q(blupsol.begin(),blupsol.begin(),startaddr,startaddr+nl-1)
00258                        - rr;
00259          retval += std::log(sigma_mu);
00260       }
00261       compute_xbzu(blupsol);                   // to adjust trait
00262    }
00263    retval *= -0.5;
00264    pop->residual_var = residual_var;
00265    pop->input_data(data);
00266    retval += pop->log_likelihood_peeling(maxiter);
00267    return retval;
00268 }

void matvec::Model::lsmeans const std::string &    termnames,
const std::string &    filename = "",
const int    io_mode = std::ios::out,
const int    savekp_flag = 0
 

Definition at line 576 of file model_hypo.cpp.

References blup(), factor_struct, matvec::TermList::index(), info(), numterm, out_lsmeans_to_stream(), term, and matvec::warning().

00578 {
00579    ///////////////////////////////////////////////////////////////////////
00580    // currently not working together with group-model
00581    // A*B works, but not A * B, we will fix it later
00582    //
00583    // REMINDER: blupsol & rellrhs remain intact
00584    ///////////////////////////////////////////////////////////////////////
00585 
00586    //   lsm_terms.split(nlsm," ");
00587    std::string sep(" ");
00588    std::vector<std::string> tmpstr;
00589    std::string::size_type begidx,endidx;
00590    begidx = termnames.find_first_not_of(sep);
00591    while(begidx != std::string::npos) {
00592       endidx = termnames.find_first_of(sep,begidx);
00593       if (endidx == std::string::npos) endidx = termnames.length();
00594       tmpstr.push_back(termnames.substr(begidx,endidx - begidx));
00595       begidx = termnames.find_first_not_of(sep,endidx);
00596    }
00597 
00598    unsigned nlsm = tmpstr.size();
00599    if (nlsm == 0)  {
00600       warning("Model::lsmeans(termnames): termnames is empty");
00601       return;
00602    }
00603    if (!blup("ysmp")) throw exception("Model::lsmeans(): bad model");
00604 
00605    Matrix<int> lsmtable(nlsm,numterm+1);
00606    unsigned term_nlevel,i,j,t;
00607    unsigned nord = 0;
00608    int k,kk;
00609    sep = "*";
00610    std::vector<std::string> effectnames;
00611    for (term_nlevel = 0, i=0; i<nlsm; i++) {
00612       k = term.index(tmpstr[i],factor_struct);
00613       if (k >= 0) {
00614          if (term[k].classi() != 'F') {
00615             warning("Model::lsmeans(%s): only fixed(discrete) term allowed",
00616                     tmpstr[i].c_str());
00617             lsmtable[i][numterm] = -1; // lsm_term is discarded
00618             continue;
00619          }
00620       }
00621       else {
00622          warning("Model::lsmeans(%s): no such term in the model, it's ignored",
00623                tmpstr[i].c_str());
00624          lsmtable[i][numterm] = -1;   // lsm_term can't be found in the model
00625          continue;
00626       }
00627       lsmtable[i][k] = 2;          // term k is the lsmeans term
00628       lsmtable[i][numterm] = k;
00629       term_nlevel += term[k].nlevel();
00630       effectnames.clear();
00631       ////////  effectnames = tmpstr[i].split(nord,"*");
00632       begidx = tmpstr[i].find_first_not_of(sep);
00633       while(begidx != std::string::npos) {
00634          endidx = tmpstr[i].find_first_of(sep,begidx);
00635          if (endidx == std::string::npos) endidx = tmpstr[i].length();
00636          effectnames.push_back(tmpstr[i].substr(begidx,endidx - begidx));
00637          begidx = tmpstr[i].find_first_not_of(sep,endidx);
00638       }
00639       nord = effectnames.size();
00640       for (t=0; t<numterm; t++) {
00641          if (term[t].order() > 2) {
00642             lsmtable[i][numterm] = -1;
00643             throw exception("Model::lsmeans(args): can't handle high order interaction");
00644          }
00645          if (lsmtable[i][t] == 2) continue;
00646          for (j=0; j<nord; j++) {
00647             kk = term[t].partial_match(effectnames[j],factor_struct);
00648             if (kk>=0) break;
00649          }
00650          if (j<nord) lsmtable[i][t] = 1;
00651       }
00652    }
00653    if (filename == "") {
00654      std::ofstream ofs;
00655      ofs.open(filename.c_str(),(OpenModeType)io_mode);
00656      if (!ofs) throw exception("Model::lsmeans(): cann't open or already exist");
00657      out_lsmeans_to_stream(ofs,nlsm,lsmtable,savekp_flag);
00658      info(ofs);
00659      ofs.close();
00660    }
00661    else {
00662         out_lsmeans_to_stream(std::cout,nlsm,lsmtable,savekp_flag);
00663    }
00664 }

void matvec::Model::map void   
 

double matvec::Model::MapF double    dist,
int    qr = 0
 

Definition at line 527 of file model_peeling.cpp.

References map_function, and map_parm.

Referenced by matvec::Population::buildRecombinationMatrix(), matvec::Population::descent_graph_setup(), and multipoint_compute_Rec_table().

00527                                        {
00528     // Computes different map functions that are multilocus feasible:
00529     // Model::map_method M is Morgans
00530     // Model::map_method H (default) is Haldanes
00531     // Model::map_method B Binomial with extra_parm
00532     // qr is a flag
00533     //    0 means theta is a recombination rate
00534     //    1 means theta is a distance in Morgans no centiMorgrans
00535 
00536    double temp;
00537    if ((map_function=='M') || (map_function=='m')) {
00538      //Morgan's function  recombination rate equals distance
00539      return theta;
00540     }
00541    else if ((map_function=='B') || (map_function=='b')) {
00542      // Binomial Method
00543         if (qr) {
00544             temp=std::pow((1.0-2.0*theta),(1.0/map_parm));
00545             return (0.5*map_parm*(1.0-temp));
00546             }
00547         else {
00548             if (theta < map_parm/2.0) {
00549                temp=(1.0-2.0*theta/map_parm);
00550                return 0.5*(1.0-(std::pow(temp,map_parm)));
00551                }
00552             else
00553                return 0.5;
00554             }
00555         }
00556    else  {
00557      // Haldane's function if method not set
00558         if (qr) {
00559            if (theta < 0.5) {
00560               temp= (-0.5 * std::log(1.0-2.0*theta));
00561               return temp;
00562               }
00563            else
00564               return 5.0;
00565             }
00566         else {
00567             temp= (0.5*(1.0 - std::exp(-2.0*theta)));
00568             return temp;
00569            }
00570      }
00571 }

void matvec::Minimizer::min_prtlevel const int    pl [inline, inherited]
 

Definition at line 50 of file minimizer.h.

References matvec::Minimizer::prtlevel.

00050 {prtlevel = pl;}

double matvec::Model::minfun const Vector< double > &    x,
const int    n
[virtual]
 

Implements matvec::Minimizer.

Definition at line 2622 of file model.cpp.

References matvec::Minimizer::minfun_indx, minfun_multipoint(), minfun_peeling(), minfun_vce(), and matvec::warning().

02623 {
02624    double llhood = 0.0;
02625    switch (minfun_indx) {
02626       case 1:                //  dfreml vce,   Model_vce.cc
02627          llhood = minfun_vce(x,n);
02628          break;
02629       case 2:                // MLE peeling,  Model_peeling
02630          llhood = minfun_peeling(x,n);
02631          break;
02632 // BRS
02633       case 3:   // Multipoint 
02634          llhood = minfun_multipoint(x,n);
02635          break;
02636 // BRS
02637       default:
02638          warning("Model::Model::minfun(): unknown minfun_indx: %d",minfun_indx);
02639    }
02640    return llhood;
02641 }

double matvec::Model::minfun_multipoint const Vector< double > &    x,
const int    n
 

Definition at line 770 of file model_peeling.cpp.

References matvec::Population::analysis_type, matvec::GeneticDist::chrom(), matvec::ChromStruct::locus, maxit, matvec::Population::mean_for_genotype, matvec::Population::multi_m_log_likelihood_peeling(), matvec::Population::multipoint_likelihood(), multipoint_QTL_table(), multipoint_Rec_recalc(), N_Parm_List, new_distance, Parm_List, pop, matvec::Population::prior, and matvec::Population::Q_freq.

Referenced by minfun().

00770                                                                     {
00771   int i;
00772   // std::cout << "entered minfun_multipoint " << std::endl;
00773   // setup values from praxis into correct places
00774   for (i=0; i < N_Parm_List; i++) {
00775     *(Parm_List[i].Value) = x[i];
00776     //    std::cout << " Parameter " << i << " is  " << x[i] << std::endl;
00777     if ((x[i] > Parm_List[i].Max) || (x[i] < Parm_List[i].Min)) {
00778       std::cout << " Parameter " << i << " is out of bounds  " << x[i] << " Max is " << Parm_List[i].Max;
00779       std::cout << " Min is " << Parm_List[i].Min << std::endl;
00780       return 1.0e+25;
00781     }
00782   }
00783     // First need to find the new location and position of  the QTL and recompute QTL table
00784   // std::cout << "New dist " << new_distance; // << "RecoTable Before change " << RecoTable;
00785     multipoint_Rec_recalc(new_distance);
00786     //  std::cout << "After change " << RecoTable;
00787     //   std::cout << " POP Q FREQS " << pop->Q_freq;
00788     // Recompute QTL genotype frequencies
00789     double QTL_freq= pop->prior->chrom()[0].locus[0].allele_freq[0];
00790     pop->Q_freq(1) = QTL_freq*QTL_freq;
00791     pop->Q_freq(2) = QTL_freq*(1-QTL_freq);
00792     pop->Q_freq(3) = QTL_freq*(1-QTL_freq);
00793     pop->Q_freq(4) = (1-QTL_freq)*(1-QTL_freq);
00794   // End of old Stuff
00795 
00796   multipoint_QTL_table();
00797 int N_means=5;
00798       // Find which means was used
00799  for (i=1; i <= N_Parm_List; i++) {
00800       // std::cout << "Start:" <<Parm_List[i].Name << ":End" << endl;
00801      if (Parm_List[i].Name == "Mean_Qq   ") {
00802         N_means= N_means-2;
00803        }
00804      if (Parm_List[i].Name == "Dom_Qq    ") {
00805        N_means--;
00806       }
00807       if (Parm_List[i].Name == "Mean_QQ   ") {
00808          N_means--;
00809         }
00810 }
00811      //Dominance Means
00812       if (N_means == 3) {
00813           if (pop->mean_for_genotype[0] > pop->mean_for_genotype[3]){
00814              if (pop->mean_for_genotype[1] > pop->mean_for_genotype[0]){
00815                  pop->mean_for_genotype[1] = pop->mean_for_genotype[0];
00816                  }
00817              else if (pop->mean_for_genotype[3] > pop->mean_for_genotype[1]){
00818                  pop->mean_for_genotype[1] = pop->mean_for_genotype[3];
00819                  }
00820            }
00821            else {
00822                if (pop->mean_for_genotype[1] > pop->mean_for_genotype[3]){
00823                    pop->mean_for_genotype[1] = pop->mean_for_genotype[3];
00824                    }
00825                 else if (pop->mean_for_genotype[0] > pop->mean_for_genotype[1]){
00826                    pop->mean_for_genotype[1] = pop->mean_for_genotype[0];
00827                    }
00828             }
00829         }
00830         //Additive means
00831       else if (N_means == 4) {
00832              (pop->mean_for_genotype[1]) = 0.5*((pop->mean_for_genotype[0])+ (pop->mean_for_genotype[3]));
00833             }
00834         //Same means 
00835       else if (N_means == 5) {
00836              (pop->mean_for_genotype[1]) = (pop->mean_for_genotype[0]);
00837              (pop->mean_for_genotype[3]) = (pop->mean_for_genotype[0]);
00838       }
00839 // General model don't do anything!
00840 
00841       //Force heterozygotes to have same mean
00842         (pop->mean_for_genotype[2]) = (pop->mean_for_genotype[1]);
00843 
00844 
00845       //            cout << "mean 0 = " << pop->mean_for_genotype[0] << endl;
00846       //            cout << "mean 1 = " << pop->mean_for_genotype[1] << endl;
00847       //            cout << "mean 2 = " << pop->mean_for_genotype[2] << endl;
00848       //            cout << "mean 3 = " << pop->mean_for_genotype[3] << endl;
00849          
00850   //  std::cout << "parameters set " << std::endl;
00851   double log_llhood;
00852   if (pop->analysis_type == "multipoint") {
00853 //      std::cout << "doing " << pop->analysis_type << " multipoint" << std::endl;
00854  log_llhood= -1.0*pop->multipoint_likelihood(maxit);
00855 }
00856    else {
00857 //      std::cout << "doing " << pop->analysis_type << " multipoint" << std::endl;
00858  log_llhood= -1.0*pop->multi_m_log_likelihood_peeling(maxit);
00859 }
00860 //  std::cout << "log likelihood value (its negative) = " << std::setprecision(14);
00861 //  std::cout << log_llhood;
00862 //  for (i=0; i < N_Parm_List; i++)
00863 //    std::cout << "(" <<  i  << ")" << " = " << x[i] << "  ";
00864 //  exit(1);
00865   return log_llhood;
00866 }

double matvec::Model::minfun_peeling const Vector< double > &    x,
const int    n
 

Definition at line 151 of file model_peeling.cpp.

References matvec::Genome::chromosome, matvec::Chromosome::freq(), matvec::Population::log_likelihood_peeling(), matvec::Population::mean_for_genotype, pop, matvec::Population::pop_gamete, matvec::Population::residual_var, matvec::Population::tn_gamete, and matvec::Population::tn_qtl.

Referenced by minfun().

00152 {
00153    // requirement: build_pop_gamete() must have been called
00154    int i,m = pop->tn_gamete - 1;      // because p(A1)+ p(A2) +...+ p(At) = 1
00155    double p,pm;
00156 
00157    //////////////////////////////////////////////////////////////
00158    // constraints for minimization
00159    //       (0)  residual_variance >= 0;
00160    //       (1)  0.0 <= p(Ai) <= 1
00161    //////////////////////////////////////////////////////////////
00162    if (x[0] < 0.0) return 1.0e+25;
00163    for (pm=1.0, i=0; i<m; i++) {
00164       pm -= p = x[1+i];
00165       if (p>1.0 || p<0.0) return 1.0e+25;
00166    }
00167    if (pm>1.0 || pm < 0.0 ) return 1.0e+25;
00168 
00169    //////////////////////////////////////////////////////////////
00170    // now put back new values in x into appropriate places
00171    //////////////////////////////////////////////////////////////
00172 
00173 //RLF
00174    /*
00175 
00176    pop->residual_var.me[0][0] = x[0];
00177    for (i=0; i<m; i++) pop->pop_gamete[0].chromosome[i].freq(x[i+1]);
00178    pop->pop_gamete[0].chromosome[m].freq(pm);
00179 //   pop->prior->genotypic_val(1,1,&x[1+m]);  // get genotypic_values from x
00180    */
00181    pop->residual_var[0][0] = x[0];
00182    pop->pop_gamete[0].chromosome[0].freq(x[1]);
00183    pop->pop_gamete[0].chromosome[1].freq(x[2]);
00184    pop->pop_gamete[0].chromosome[2].freq(pm);
00185    pop->mean_for_genotype[0]  = x[3];
00186    pop->mean_for_genotype[1]  = x[4];
00187    pop->mean_for_genotype[2]  = x[5];
00188     pop->tn_qtl=4;
00189 //RLF
00190    double log_llhood = -1.0*pop->log_likelihood_peeling();
00191 // std::cout << "log likelihood value (its negative) = " << log_llhood << std::endl;
00192    return log_llhood;
00193 }

double matvec::Model::minfun_vce const Vector< double > &    x,
const int    n
 

Definition at line 143 of file model_vce.cpp.

References numterm, matvec::doubleMatrix::psd(), residual_var, restricted_log_likelihood(), term, and vec2var().

Referenced by minfun().

00144 {
00145   ////////////////////////////////////////////////////////////////////////
00146   // it returns negative restricted log likelihood.
00147   //   (it is for minimization program)
00148   //
00149   // x contains upper triangular part of variance matrices for residual
00150   // and other random terms in the model.
00151   // the hiden order in x is :
00152   //    residual--randtermlist[0]--randtermlist[1] .....
00153   // the order of randtermlist[i] is based on "random = A B A*B" given by user
00154   // user is totally responsible for size of vector x.
00155   // size of x == (1+nrandterm)*numtrait*(numtrait+1)/2
00156   //
00157   //  Important Notice:
00158   //  because restricted_log_likelihood(D) will be called sequentially
00159   //  under the same MME structure.
00160   //  hmmec is kept after restricted_log_likelihood(D) call.
00161   //  Call release_mme() to release hmmec from memory
00162   ///////////////////////////////////////////////////////////////////////////
00163 
00164    double mlhood;
00165    int i;
00166    vec2var(x);
00167    if (!residual_var.psd()) return (1.0e30);
00168    for (mlhood=0.0,i=0; i<numterm; i++) {
00169       if (term[i].classi() == 'R' || term[i].classi() == 'P') {
00170          if (!(term[i].prior->var_matrix()->psd())) {
00171             mlhood =1.0e30 + 1.0e10*(i+1);
00172             break;
00173          }
00174       }
00175    }
00176    if (mlhood == 0.0) mlhood = -1.0*restricted_log_likelihood();
00177    return ( mlhood );
00178 }

double matvec::Model::ml_estimate_peeling void   
 

void matvec::Model::mme_times Vector< double > &    x [protected]
 

Compute mme*x without storing mme The result is stored in mme_times_res

Definition at line 791 of file model_blup.cpp.

References add_Ag(), add_Ig(), matvec::Vector< T >::begin(), matvec::Vector< double >::begin(), hmmesize, input_pos_val_iod(), mme_times_res, numterm, numtrait, pos_val_vector, rve, and term.

Referenced by blup_pccg().

00792 {
00793    std::vector<pos_val_node>::iterator it;
00794    unsigned i,j,ii,jj,t1,t2;
00795    double vy,xval,val,value;
00796 
00797    double *r = mme_times_res.begin();
00798    memset(r,'\0',sizeof(double)*hmmesize);
00799    r--;
00800    double *xp = x.begin() - 1;
00801 
00802    double **vep;
00803    Matrix<double> ve(numtrait,numtrait);
00804    for (it=pos_val_vector.begin(); it!=pos_val_vector.end(); it++) {
00805      if(!it->pos_term.empty()){
00806        input_pos_val_iod(it);
00807      }
00808      else{
00809        continue;
00810      }
00811      if (ntermGdist) {
00812        throw exception("Model::mme_times: something does not seem to be compatible with iod");
00813      }
00814      vep = rve[it->pos_term[numterm]].begin();
00815      val = it->xval_term[numterm];        // val = weight variable
00816      for (t1=0; t1<numtrait; t1++) {
00817        for (t2=0; t2<numtrait; t2++) ve[t1][t2] = val*vep[t1][t2];
00818      }
00819 
00820      for (t1=0; t1<numtrait; t1++) {
00821        for (i=0; i<numterm; i++) {
00822          ii = term[i].addr[t1];
00823          if (ii == 0) continue;
00824          xval = it->xval_term[i];
00825          for (t2=0; t2<numtrait; t2++) {
00826            val = xval * ve[t1][t2];
00827            for (j=i; j<numterm; j++) {
00828                jj = term[j].addr[t2];
00829                if (jj >= ii) {
00830                   value = val*it->xval_term[j];
00831                   r[ii] += value*xp[jj];
00832                   if (jj > ii) r[jj] += value*xp[ii];
00833                }
00834            }
00835          }
00836        }
00837      }
00838    }
00839 
00840    for (int t=0; t<numterm; t++) {
00841      if (term[t].classi() == 'P') {
00842        add_Ag(t,&x);
00843      }
00844      else if (term[t].classi() == 'R') {
00845        add_Ig(t,&x);
00846      }
00847    }
00848 
00849 }

SparseMatrix* matvec::Model::mmec void    [inline]
 

Definition at line 304 of file model.h.

Referenced by matvec::GLMM::glim().

00304 {return &hmmec;}

unsigned matvec::Model::mmesize void    const [inline]
 

Definition at line 192 of file model.h.

References hmmesize.

00192 {return hmmesize;}

double matvec::Model::multipoint int    maxit
 

Definition at line 322 of file model_peeling.cpp.

References matvec::Population::analysis_type, matvec::Population::F, matvec::Population::multipoint_init_parm(), matvec::Population::multipoint_likelihood(), and pop.

00322                                     {
00323   double llh;
00324 
00325   pop->analysis_type = "multipoint";
00326   // This is a bit excessive as means we have two versions of Farg when doing FPMM
00327   Fpmm Farg(0,0.5,1.0); // setup for 0 polygenic loci.
00328   if (pop->F == 0) {
00329     // Need to setup F as not using FPMM stuff
00330     pop->multipoint_init_parm(Farg);
00331   }
00332 
00333   llh=pop->multipoint_likelihood(maxiter);
00334   //  std::cout << "QTL pos " << Q_pos << std::endl;
00335   std::cout << "Natural log llh= " << std::setprecision(10) << llh << " Log10 llh = " << std::setprecision(10) << (std::log10(std::exp(1.0))*llh) << std::endl;
00336   /*
00337     multipoint_Rec_recalc(6);
00338       multipoint_QTL_table();
00339  llh=pop->multipoint_likelihood(maxit);
00340  //  std::cout << "QTL pos " << Q_pos << std::endl;
00341   std::cout << "Natural log llh= " << std::setprecision(10) << llh << " Log10 llh = " << std::setprecision(10) << (log10(exp(1.0))*llh) << std::endl;
00342   */
00343 return llh;
00344 }

void matvec::Model::multipoint_compute_Rec_table void   
 

Definition at line 594 of file model_peeling.cpp.

References MapF(), matvec::Population::n_markerLoci, pop, Q_pos, and RecoTable.

Referenced by multipoint_Rec_recalc(), and multipoint_Rec_table().

00594                                          {
00595   int nloci=pop->n_markerLoci+1, i,j;
00596   double temp;
00597   int Q_start = Q_pos;
00598 
00599   for(i=Q_start; i<nloci-1; i++) {
00600     if(RecoTable[i][i] > RecoTable[i+1][i+1]) {
00601       temp = RecoTable[i][i];
00602       RecoTable[i][i] = RecoTable[i+1][i+1];
00603       RecoTable[i+1][i+1] = temp;
00604       Q_pos++;
00605     }
00606     else {
00607       break;
00608     }
00609   }
00610 
00611   for(i=Q_start; i>0; i--) {
00612     if(RecoTable[i][i] < RecoTable[i-1][i-1]) {
00613       temp = RecoTable[i][i];
00614       RecoTable[i][i] = RecoTable[i-1][i-1];
00615       RecoTable[i-1][i-1] = temp;
00616       Q_pos--;
00617     }
00618     else {
00619       break;
00620     }
00621   }
00622 
00623   for (i=0; i < nloci-1; i++){
00624     for (j=i+1; j < nloci; j++){
00625       RecoTable[i][j]=MapF(RecoTable[j][j]-RecoTable[i][i]);
00626       RecoTable[j][i]=1.0-RecoTable[i][j];
00627       //  std::cout << "i " << i << " j " << j << " Reco " << RecoTable[i][j] << " RT[j] " << RecoTable[j][j] << " RT[i] " << RecoTable[i][i] << std::endl;
00628     }
00629   }
00630   //  std::cout << RecoTable;
00631 }

void matvec::Model::multipoint_display_parms void   
 

Definition at line 1079 of file model_peeling.cpp.

References matvec::Population::analysis_type, N_Parm_List, matvec::Population::P_ndim, Parm_List, and pop.

01079                                          {
01080   int nl=0;
01081   if (pop->analysis_type == "multipoint") {
01082     if (pop->P_ndim > 1) {
01083       nl= static_cast<int>(0.5*(pop->P_ndim-1));
01084       std::cout << "P_dim" << pop->P_ndim << " nl = " << nl << std::endl;
01085       std::cout << "Multipoint with FPMM with ";
01086       if (nl >1) {
01087         std::cout << nl << " loci." << std::endl;
01088       }
01089       else {
01090         std::cout << "1 locus." << std::endl;
01091       }
01092     }
01093     else
01094       std::cout << "Simple Multipoint" << std::endl;
01095   }
01096   else if (pop->analysis_type== "multipoint_m") {
01097     std::cout << "Multipoint with the PAP style mixture approximation" << std::endl;
01098   }
01099   else
01100     std::cout << "what did you run?" << std::endl;
01101   
01102   //Now pretty print the final values
01103   if (N_Parm_List > 0) {
01104     std::cout << " Parameter    Final Value " << std::endl;
01105     int i;
01106     for (i=1; i <= N_Parm_List; i++) {
01107       if (Parm_List(i).Name != "Distance  ") 
01108         std::cout <<  Parm_List(i).Name << "       " <<  *Parm_List(i).Value << std::endl;
01109       else 
01110         std::cout <<  Parm_List(i).Name << "       " <<  *Parm_List(i).Value-2.0 << std::endl;
01111     }
01112   }
01113 }

void matvec::Model::multipoint_estimate_Distance double    Initial,
double    Max,
double    Min
 

Definition at line 895 of file model_peeling.cpp.

References multipoint_QTL_table(), multipoint_Rec_recalc(), matvec::Population::n_markerLoci, N_Parm_List, new_distance, Parm_List, pop, RecoTable, and matvec::Vector< Parm_Elem >::resize().

00895                                                                                {
00896   double T_dist,tmp;
00897   if (N_Parm_List == 0) {
00898     Parm_List.resize(7);
00899   }
00900   if (Min > Max) {
00901     tmp=Min;
00902     Min=Max;
00903     Max=tmp;
00904   }
00905   Initial +=2.0;
00906   Max +=2.0;
00907   Min +=2.0;
00908   T_dist = 2.0+RecoTable[pop->n_markerLoci][pop->n_markerLoci];
00909   // CHECK parameter bounds;
00910   if (Max > T_dist)
00911     Max=T_dist;
00912   if (Min < 0.0)
00913     Min=0.0;
00914   if (Initial > Max)
00915     Initial=Max;
00916   if (Initial < Min)
00917     Initial=Min;
00918   new_distance=Initial;
00919   N_Parm_List++;
00920   Parm_List(N_Parm_List).Value = &new_distance;
00921   Parm_List(N_Parm_List).Max = Max;
00922   Parm_List(N_Parm_List).Min = Min;
00923   Parm_List(N_Parm_List).Name = "Distance  ";
00924   // Need to recompute tables for the new starting location
00925   multipoint_Rec_recalc(new_distance);
00926   multipoint_QTL_table();
00927   // std::cout << T_dist << " min " << Min << " Initial " << Initial << " Max " << Max << std::endl;
00928   //  std::cout << RecoTable ;
00929   // exit(1);
00930   }

void matvec::Model::multipoint_estimate_PolyV double    Initial,
double    Max,
double    Min
 

Definition at line 1115 of file model_peeling.cpp.

References matvec::Population::F, N_Parm_List, Parm_List, pop, matvec::Population::residual_var, matvec::Vector< Parm_Elem >::resize(), and matvec::Fpmm::var.

01115                                                                             {
01116     // Maximize the polygenic variance under the mixture model approximation
01117     // not used by any other method!
01118             
01119 double tmp;
01120 if (N_Parm_List == 0) {
01121              Parm_List.resize(7); // Parm_list is empty therefore resize it
01122                  }
01123 if (Min < 0.0)
01124             Min=0.0;
01125 if (Min > Max) {
01126             tmp=Min;
01127                 Min=Max;
01128                     Max=tmp;
01129                         }
01130 if (Initial > Max)
01131             Initial=Max;
01132 if (Initial < Min)
01133             Initial=Min;
01134 pop->residual_var[0][0]=Initial;
01135 N_Parm_List++;
01136 
01137 Parm_List(N_Parm_List).Value = &(pop->F->var);
01138 Parm_List(N_Parm_List).Max = Max;
01139 Parm_List(N_Parm_List).Min = Min;
01140 Parm_List(N_Parm_List).Name = "Poly_Var  ";
01141 }

void matvec::Model::multipoint_estimate_Qfreq double    Initial,
double    Max,
double    Min
 

Definition at line 1030 of file model_peeling.cpp.

References matvec::GeneticDist::chrom(), matvec::ChromStruct::locus, N_Parm_List, Parm_List, pop, matvec::Population::prior, and matvec::Vector< Parm_Elem >::resize().

01031 {
01032   double QTL_allele_freq =Initial,tmp;
01033   if (N_Parm_List == 0) {
01034     Parm_List.resize(7);
01035   }
01036   if (Max > 1.0)
01037     Max=1.0;
01038   if (Min < 0.0)
01039     Min=0.0;
01040   if (Min > Max) {
01041     tmp=Min;
01042     Min=Max;
01043     Max=tmp;
01044   }
01045   if (Initial > Max)
01046     Initial=Max;
01047   if (Initial < Min)
01048     Initial=Min;
01049    N_Parm_List++;
01050    Parm_List(N_Parm_List).Value = &(pop->prior->chrom()[0].locus[0].allele_freq[0]);
01051    Parm_List(N_Parm_List).Max = Max;
01052    Parm_List(N_Parm_List).Min = Min;
01053    Parm_List(N_Parm_List).Name = "QTL_Freq  ";
01054   }

void matvec::Model::multipoint_estimate_Qmeans Vector< double >    Initial,
Vector< double >    Max,
Vector< double >    Min,
char    mtype = 'G'
 

Definition at line 932 of file model_peeling.cpp.

References matvec::Population::mean_for_genotype, min, N_Parm_List, Parm_List, pop, and matvec::Vector< Parm_Elem >::resize().

00932                                                                                                                  {
00933      // This maximizes the genotypic means for the genotypes qq, Qq and QQ
00934      // only two alleles at QTL supported
00935      // Currently force same heterozygotes mean_Qq is set the same as mean_qQ
00936      // Future flag maybe added to allow different heterozygote means
00937      // Current types
00938      // S is same mean : mean qq == mean Qq == mean QQ
00939      // A is additive model : mean Qq == average of (mean qq + mean QQ)
00940      // D is dominance model: mean qq <= mean Qq <= mean QQ
00941      // G or other value has unrestricted means so that overdominance is possible
00942 
00943 double tmp;
00944 int i, j=0;
00945 if (N_Parm_List == 0) {
00946             Parm_List.resize(7); // Parm_list is empty therefore resize it
00947                 }
00948 // Need to check if any other type of QTL mean maximization have been defined
00949  for (i=1; i <= N_Parm_List; i++) {
00950  //      cout << "Name " << Parm_List(i).Name << endl;
00951      if (Parm_List(i).Name == "Mean_qq   ") {
00952         j=1;
00953         }
00954   }
00955   if (j) {
00956           std::cout << "Another estimate Qmeans has been previously defined" << std::endl;
00957      throw exception("Model::estimate_Qmeans: has been previously defined");
00958      }
00959   else {
00960      N_Parm_List++;
00961      // For qq genotype
00962      pop->mean_for_genotype[0] =  Initial[0];
00963      Parm_List(N_Parm_List).Max = Max[0];
00964      Parm_List(N_Parm_List).Min = Min[0];
00965      Parm_List(N_Parm_List).Value = &(pop->mean_for_genotype[0]);
00966      Parm_List(N_Parm_List).Name = "Mean_qq   ";
00967      if ((mtype == 'S') || (mtype=='s')){ // Same means
00968          (pop->mean_for_genotype[1]) = (pop->mean_for_genotype[0]);
00969          (pop->mean_for_genotype[2]) = (pop->mean_for_genotype[0]);
00970          (pop->mean_for_genotype[3]) = (pop->mean_for_genotype[0]);
00971          }
00972      else {
00973      // For QQ genotype
00974           N_Parm_List++;
00975           pop->mean_for_genotype[3] =  Initial[2];
00976           Parm_List(N_Parm_List).Max = Max[2];
00977           Parm_List(N_Parm_List).Min = Min[2];
00978           Parm_List(N_Parm_List).Value = &(pop->mean_for_genotype[3]);
00979           Parm_List(N_Parm_List).Name = "Mean_QQ   ";
00980           if ((mtype=='A') || (mtype=='a') ) { // Additive
00981              (pop->mean_for_genotype[1]) = 0.5*((pop->mean_for_genotype[0])+(pop->mean_for_genotype[3]));
00982              (pop->mean_for_genotype[2]) = (pop->mean_for_genotype[1]);
00983              }
00984           else { // set heterozygotes to the same value depending on model
00985              N_Parm_List++;
00986              if ((mtype == 'D')|| (mtype == 'd')) {// Dominance model
00987                 Parm_List(N_Parm_List).Name = "Dom_Qq    ";
00988                 // Now check that the Qq means fall within the bounds of QQ and qq means
00989                 tmp= (double) std::max(Max[0],Max[2]);
00990                 if (Max[1] > tmp)
00991                     Max[1]=tmp;
00992                 tmp=std::min(Min[0],Min[2]);
00993                 if (Min[1] < tmp)
00994                    Min[1]=tmp;
00995                 tmp= (double) std::max(Initial[0],Initial[2]);
00996                 if (Initial[1] > tmp)
00997                     Initial[1]=tmp;
00998                 tmp=std::min(Initial[0],Initial[2]);
00999                 if (Initial[1] < tmp)
01000                     Initial[1]=tmp;
01001                 pop->mean_for_genotype[1] =  Initial[1];
01002                 Parm_List(N_Parm_List).Max = Max[1];
01003                 Parm_List(N_Parm_List).Min = Min[1];
01004                 Parm_List(N_Parm_List).Value = &(pop->mean_for_genotype[1]);
01005                 (pop->mean_for_genotype[2]) = (pop->mean_for_genotype[1]);
01006                 }
01007            else {  // General model
01008               Parm_List(N_Parm_List).Name = "Mean_Qq   ";
01009               pop->mean_for_genotype[1] =  Initial[1];
01010               Parm_List(N_Parm_List).Max = Max[1];
01011               Parm_List(N_Parm_List).Min = Min[1];
01012               Parm_List(N_Parm_List).Value = &(pop->mean_for_genotype[1]);
01013               (pop->mean_for_genotype[2]) = (pop->mean_for_genotype[1]);
01014               }
01015            pop->mean_for_genotype[2] =  Initial[1];
01016           (pop->mean_for_genotype[2]) = (pop->mean_for_genotype[1]);
01017           }
01018 
01019       }
01020  }
01021   /*
01022    * for (i=1; i <= N_Parm_List; i++) {
01023    *          cout << "Name " << Parm_List(i).Name <<  " Value= " << &Parm_List(i).Value
01024    *          << std::endl;
01025    *          }
01026    *          */
01027 }

void matvec::Model::multipoint_estimate_Ve double    Initial,
double    Max,
double    Min
 

Definition at line 869 of file model_peeling.cpp.

References matvec::Matrix< double >::assign(), N_Parm_List, Parm_List, pop, matvec::Population::residual_var, and matvec::Vector< Parm_Elem >::resize().

00869                                                                          {
00870   double tmp;
00871    if (N_Parm_List == 0) {
00872     Parm_List.resize(7);
00873   }
00874   if (Min < 0.0)
00875     Min=0.0;
00876   if (Min > Max) {
00877     tmp=Min;
00878     Min=Max;
00879     Max=tmp;
00880   }
00881   if (Initial > Max)
00882     Initial=Max;
00883   if (Initial < Min)
00884     Initial=Min;
00885   pop->residual_var.assign(Initial);
00886   N_Parm_List++;
00887 
00888   Parm_List(N_Parm_List).Value = &(pop->residual_var(1,1));
00889   Parm_List(N_Parm_List).Max = Max;
00890   Parm_List(N_Parm_List).Min = Min;
00891   Parm_List(N_Parm_List).Name = "Res_Var   ";
00892   //  std::cout << " value " <<  *Parm_List(N_Parm_List).Value << " Min " <<  Parm_List(N_Parm_List).Min << " Max " <<  Parm_List(N_Parm_List).Max << " name " <<  Parm_List(N_Parm_List).Name << std::endl;
00893 }

void matvec::Model::multipoint_genot_probs void   
 

Definition at line 345 of file model_peeling.cpp.

References matvec::Population::analysis_type, matvec::Population::build_nufamily(), matvec::Individual::initial_posterior(), matvec::Population::multi_geno_dist_peeling(), pop, matvec::Population::popmember, and matvec::Population::popsize.

00346 {
00347   int i;
00348   Individual *I;
00349   std::cout << "computing genot_probs" << std::endl;
00350   for (i=0;i<pop->popsize; i++){
00351     I = pop->popmember[i];
00352     //I->initial_anterior(genotype_freq.ve,tn_genotype);
00353     I->initial_posterior(3);
00354   }
00355   pop->analysis_type = "multipoint";
00356   pop->build_nufamily();
00357   pop->multi_geno_dist_peeling(0);
00358 }

double matvec::Model::multipoint_mixture_approx int    maxit = 0,
double    P_var = 1.0
 

Definition at line 1056 of file model_peeling.cpp.

References matvec::Population::analysis_type, matvec::Population::multi_m_log_likelihood_peeling(), matvec::Population::multipoint_init_parm(), pop, and Q_pos.

01056                                                                 {
01057 
01058   double llh;
01059   pop->analysis_type = "multipoint_m";
01060   std::cout << "lets do multipoint with the mixture approximation!" << std::endl;
01061   Fpmm Farg(0,0.5,P_var); // setup for 0 polygenic loci.
01062   // Need to setup F as not using FPMM stuff
01063   pop->multipoint_init_parm(Farg);
01064 
01065   llh = pop->multi_m_log_likelihood_peeling(maxiter);
01066     std::cout << "QTL pos " << Q_pos << std::endl;
01067   std::cout << "Natural log llh= " << std::setprecision(10) << llh << " Log10 llh = "
01068        << std::setprecision(10) << (std::log10(std::exp(1.0))*llh) << std::endl;
01069   /*
01070       multipoint_Rec_recalc(14);
01071       multipoint_QTL_table();
01072   llh = pop->multi_m_log_likelihood_peeling(maxiter);
01073     std::cout << "QTL pos " << Q_pos << std::endl;
01074   std::cout << "Natural log llh= " << std::setprecision(10) << llh << " Log10 llh = " << std::setprecision(10) << (log10(exp(1.0))*llh) << std::endl;
01075   */
01076   return llh;
01077 }

double matvec::Model::multipoint_ml_estimate const int    method = 0,
int    niter = 0,
double    tol = 1.0e-16,
double    epsilon = 1.0e-8,
int    printlevel = 0
 

Definition at line 722 of file model_peeling.cpp.

References matvec::Population::analysis_type, bad_model, data_prepared, maxit, matvec::Minimizer::minfun_indx, N_Parm_List, Parm_List, pop, matvec::Minimizer::praxis(), prepare_data(), and type.

00723 {
00724       /* 
00725        * tol  is the tolerance allowed for the precision of the
00726        *      solution. praxis returns if the criterion
00727        *      2 * ||x[k]-x[k-1]|| <= sqrt(macheps) * ||x[k]|| + tol
00728        *      is fulfilled more than ktm times.
00729        *      the default value 1.0e-16
00730        *      tol is usually equal to  EPISILON*EPISILON
00731        *
00732        * printlevel controls praxis printing output:
00733        *      0 -> no output
00734        *      1 -> print only starting and final values
00735        *      2 -> detailed map of the minimization process
00736        *      3 -> print also eigenvalues and vectors of the
00737        *           search directions
00738        *      the default value is 0
00739        *                                                  */
00740    if (type == bad_model) throw exception("Model::ml_estimate_peeling(): bad model");
00741   // Setting up to run praxis to maximise the llh
00742   if (method == 0)
00743       pop->analysis_type = "multipoint";
00744  else
00745     pop->analysis_type = "multipoint_m";
00746   maxit=niter;
00747   unsigned n;
00748    double max_llhood = 0.0;
00749 
00750    if (!data_prepared) {
00751       if (!prepare_data()) {
00752          type = bad_model;
00753          return max_llhood;
00754       }
00755    }
00756 
00757    minfun_indx = 3;
00758    //  std::cout << "get x " << N_Parm_List << std::endl;
00759    Vector<double> x(N_Parm_List);
00760    for (n=0; n < N_Parm_List; n++)
00761      x[n]= *(Parm_List[n].Value);
00762    int iter = 0;
00763    //  std::cout << "Go to praxis " << std::endl;
00764    max_llhood = praxis(x,n,iter, tol, epsilon, printlevel);
00765    minfun_indx = 0;
00766 
00767    return max_llhood;
00768 }

void matvec::Model::multipoint_profile_distance int    method,
int    maxit,
double    Min,
double    Max,
double    step_size,
int    Print,
const std::string &    fname
 

Definition at line 641 of file model_peeling.cpp.

References matvec::Population::analysis_type, matvec::Population::F, matvec::Population::multi_m_log_likelihood_peeling(), matvec::Population::multipoint_init_parm(), matvec::Population::multipoint_likelihood(), multipoint_QTL_table(), multipoint_Rec_recalc(), matvec::Population::n_markerLoci, matvec::Population::P_ndim, pop, and matvec::Matrix< double >::resize().

00641                                                                                                                                             {
00642 
00643   int i, nsteps;
00644   double distance=0.0, T_dist, temp=0.0;
00645   doubleMatrix Results;
00646 
00647   std::ofstream Outfile (fname.c_str(), std::ios::app);
00648     if (!Outfile) throw exception("Cannot open requested filename");
00649     
00650   Fpmm Farg(0,0.5,1.0); // setup for 0 polygenic loci.
00651   if (pop->F == 0) {
00652     // Need to setup F as not using FPMM stuff
00653     pop->multipoint_init_parm(Farg);
00654   }
00655   std::cout << "Nloci " << pop->n_markerLoci << std::endl;
00656   T_dist = Max-Min;
00657   // T_dist = 2.0+RecoTable[pop->n_markerLoci][pop->n_markerLoci];
00658    temp= (T_dist/step_size);
00659   nsteps= (int) (std::ceil(temp+1.0));
00660   std::cout  << "Total dist " << T_dist << " With steps " << nsteps << " Of size " << step_size << std::endl;
00661   Results.resize(nsteps,2);
00662   distance = Min -step_size + 2.0;
00663   //  std::cout << distance << std::endl;
00664 
00665   if (method==0) {
00666    pop->analysis_type = "multipoint";
00667        if (pop->P_ndim > 1) { 
00668             std::cout << "Method used : multipoint FPMM" << std::endl;
00669         }
00670        else {
00671             std::cout << "Method used : Simple multipoint" << std::endl;
00672        }
00673        for (i=0; i < nsteps; i++) { 
00674             distance += (step_size);
00675             Results[i][0] = (distance-2.0);
00676        // Find the new location and position of  the QTL and recompute QTL table
00677             multipoint_Rec_recalc(distance);
00678             multipoint_QTL_table();
00679             temp=(pop->multipoint_likelihood(maxiter));
00680             Results[i][1]=temp;
00681         } 
00682 
00683        if (Print==1) {
00684           for (i=0; i < nsteps; i++) { 
00685                Outfile <<  std::setw(10) << std::setprecision(6) << Results[i][0];
00686                Outfile << " " << std::setw(20) << std::setprecision(15);
00687                Outfile <<  Results[i][1] << std::endl; 
00688           } 
00689      } 
00690   } 
00691   else if (method==1) { 
00692         pop->analysis_type = "multipoint_m";
00693         std::cout << "Method used : multipoint Mixture approx" << std::endl;
00694         for (i=0; i < nsteps; i++) { 
00695             distance += (step_size);
00696             Results[i][0] = (distance-2.0);
00697 // Find the new location and position of  the QTL and recompute QTL table
00698             multipoint_Rec_recalc(distance);
00699             multipoint_QTL_table();
00700             temp = pop->multi_m_log_likelihood_peeling(maxiter);
00701             Results[i][1]=temp; 
00702         } 
00703    if (Print) { 
00704        for (i=0; i < nsteps; i++) {
00705                Outfile <<  std::setw(10) << std::setprecision(6) << Results[i][0];
00706                Outfile << " " << std::setw(20) << std::setprecision(15);
00707                Outfile <<  Results[i][1] << std::endl; 
00708             } 
00709    } 
00710   }
00711   else {
00712         std::cout << "Unknown method for multipoint_profile_distance" << std::endl;
00713   }
00714                                  
00715 
00716   if (Print==0) {
00717     std::cout  << Results << std::endl;
00718   }
00719   Outfile.close();
00720 }

void matvec::Model::multipoint_QTL_table void   
 

Definition at line 438 of file model_peeling.cpp.

References matvec::Population::n_markerLoci, pop, ProbGamete(), Q_pos, RecoTable, and matvec::Population::switch_table_prob.

Referenced by minfun_multipoint(), multipoint_estimate_Distance(), multipoint_profile_distance(), and multipoint_setup().

00438                                  {
00439 /* Try to create  the marker and qtl gametic event */
00440 
00441  int current, row, EL, TE,i,j,Nel,pow2,pow3;
00442   int eindex,sindex,etemp,stemp,eval,sval,locus;
00443   double epow,spow,count=0.0;
00444   double diff;
00445 
00446   pow2= (int) (1 << pop->n_markerLoci); // pow(2,pop->n_markerLoci));
00447   pow3= (int) (pow((double) 3,(int)pop->n_markerLoci));
00448 
00449   int qindex;
00450   doubleMatrix QT(pow3,3);
00451   Vector<int> gamete((pop->n_markerLoci)+1);
00452 
00453   for(eindex=0; eindex < pow3; eindex++) {
00454     etemp=eindex;
00455     for (locus=(pop->n_markerLoci-1); locus > -1; locus--){
00456       epow=pow((double) 3,(int) locus);
00457       eval=(int) (etemp/epow);
00458       etemp -= ((int) (epow*eval));
00459       if (((pop->n_markerLoci) - 1 - locus) < (Q_pos))
00460