1 /*
2  * ratefree.h
3  *
4  *  Created on: Nov 3, 2014
5  *      Author: minh
6  */
7 
8 #ifndef RATEFREE_H_
9 #define RATEFREE_H_
10 
11 #include "rategamma.h"
12 
13 class RateFree: public RateGamma {
14 public:
15 	/**
16 		constructor
17 		@param ncat number of rate categories
18 		@param tree associated phylogenetic tree
19         @param opt_alg optimization algorithm (1-BFGS, 2-BFGS, EM)
20 	*/
21     RateFree(int ncat, double start_alpha, string params, bool sorted_rates, string opt_alg, PhyloTree *tree);
22 
23 	virtual ~RateFree();
24 
25     /**
26         start structure for checkpointing
27     */
28     virtual void startCheckpoint();
29 
30     /**
31         save object into the checkpoint
32     */
33     virtual void saveCheckpoint();
34 
35     /**
36         restore object from the checkpoint
37     */
38     virtual void restoreCheckpoint();
39 
40 	/**
41 		@return true if this is a Gamma model (default: false)
42 	*/
isGammaRate()43     virtual int isGammaRate() { return 0; }
44 
45 	/**
46 	 * @return model name with parameters in form of e.g. GTR{a,b,c,d,e,f}
47 	 */
48 	virtual string getNameParams();
49 
50 	/**
51 		get the proportion of sites under a specified category.
52 		@param category category ID from 0 to #category-1
53 		@return the proportion of the specified category
54 	*/
getProp(int category)55 	virtual double getProp(int category) { return prop[category]; }
56 
57 	/**
58 		the target function which needs to be optimized
59 		@param x the input vector x
60 		@return the function value at x
61 	*/
62 	virtual double targetFunk(double x[]);
63 
64 	/**
65 	 * setup the bounds for joint optimization with BFGS
66 	 */
67 	virtual void setBounds(double *lower_bound, double *upper_bound, bool *bound_check);
68 
69 	/**
70 		optimize parameters. Default is to optimize gamma shape
71 		@return the best likelihood
72 	*/
73 	virtual double optimizeParameters(double gradient_epsilon);
74 
75     /**
76         optimize rate parameters using EM algorithm
77         @return log-likelihood of optimized parameters
78     */
79     double optimizeWithEM();
80 
81 	/**
82 		return the number of dimensions
83 	*/
84 	virtual int getNDim();
85 
86 	/**
87 		write information
88 		@param out output stream
89 	*/
90 	virtual void writeInfo(ostream &out);
91 
92 	/**
93 		write parameters, used with modeltest
94 		@param out output stream
95 	*/
96 	virtual void writeParameters(ostream &out);
97 
98     /**
99         set number of rate categories
100         @param ncat #categories
101     */
102 	virtual void setNCategory(int ncat);
103 
104     /**
105         initialize from checkpoint rates and prop from rate model with #category-1
106     */
107     virtual void initFromCatMinusOne();
108 
109 	/**
110 	 * used to normal branch lengths if mean rate is not equal to 1 (e.g. FreeRate model)
111 	 * @return mean rate, default = 1
112 	 */
113 	virtual double meanRates();
114 
115 	/**
116 	 * rescale rates s.t. mean rate is equal to 1, useful for FreeRate model
117 	 * @return rescaling factor
118 	 */
119 	virtual double rescaleRates();
120 
121 protected:
122 
123 	/**
124 		this function is served for the multi-dimension optimization. It should pack the model parameters
125 		into a vector that is index from 1 (NOTE: not from 0)
126 		@param variables (OUT) vector of variables, indexed from 1
127 	*/
128 	virtual void setVariables(double *variables);
129 
130 	/**
131 		this function is served for the multi-dimension optimization. It should assign the model parameters
132 		from a vector of variables that is index from 1 (NOTE: not from 0)
133 		@param variables vector of variables, indexed from 1
134 		@return TRUE if parameters are changed, FALSE otherwise (2015-10-20)
135 	*/
136 	virtual bool getVariables(double *variables);
137 
138 	/**
139 	 * proportion of sites for each rate categories
140 	 */
141 	double *prop;
142 
143 	/** 1 to fix weights, 2 to fix both weights and rates */
144 	int fix_params;
145 
146     /** true to sort rate in increasing order, false otherwise */
147     bool sorted_rates;
148 
149     /** 0: no, 1: rates, 2: weights */
150     int optimizing_params;
151 
152     string optimize_alg;
153 
154 };
155 
156 #endif /* RATEFREE_H_ */
157