Debian Codegraph @

src/dr/oldevomodel/sitemodel/DiscretizedLociRates.java

raw symbols SCIP
1 /*
2 * DiscretizedLociRates.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.oldevomodel.sitemodel;
27
28 import dr.inference.model.*;
29 import dr.inference.distribution.ParametricDistributionModel;
30
31 /**
32 * @author Chieh-Hsi Wu
33 *
34 * This class models the relative loci rates using the given discritized parametric distribution.
35 */
36 public class DiscretizedLociRates extends AbstractModel {
37
38 private CompoundParameter lociRates;
39 private Parameter rateCategoryParameter;
40 private ParametricDistributionModel distrModel;
41 private double normalizeRateTo;
42 private double[] rates;
43 private boolean normalize;
44 private int categoryCount;
45 private double scaleFactor;
46 private boolean completeSetup;
47
48
49 public DiscretizedLociRates(
50 CompoundParameter lociRates,
51 Parameter rateCategoryParameter,
52 ParametricDistributionModel model,
53 boolean normalize,
54 double normalizeLociRateTo,
55 int categoryCount) {
56 super("DiscretizedLociRatesModel");
57 this.lociRates = lociRates;
58
59 this.rateCategoryParameter = rateCategoryParameter;
60 //Force the boundaries of rateCategoryParameter to match the category count
61 Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, rateCategoryParameter.getDimension());
62 this.rateCategoryParameter.addBounds(bound);
63
64 this.distrModel = model;
65 this.normalizeRateTo = normalizeLociRateTo;
66 this.normalize = normalize;
67 this.categoryCount = categoryCount;
68 rates = new double[categoryCount];
69 completeSetup = true;
70 setupRates();
71
72
73 addModel(distrModel);
74 addVariable(this.rateCategoryParameter);
75
76 }
77
78 private void setupRates(){
79 if(completeSetup){
80
81 double categoryIntervalSize = 1.0/categoryCount;
82 for(int i = 0; i < categoryCount; i++){
83 rates[i]= distrModel.quantile((i+0.5)*categoryIntervalSize);
84 }
85 }
86 if(normalize){
87 computeFactor();
88 }
89
90 completeSetup = false;
91 int lociCount = rateCategoryParameter.getDimension();
92 for(int i = 0; i < lociCount; i ++){
93 lociRates.setParameterValue(i,rates[(int)rateCategoryParameter.getParameterValue(i)]*scaleFactor);
94 }
95
96 }
97 public void handleModelChangedEvent(Model model, Object object, int index) {
98 if (model == distrModel) {
99 completeSetup = true;
100 setupRates();
101 //System.out.println("speed investigation 1");
102 fireModelChanged();
103 }else if (model == rateCategoryParameter) {
104 //System.out.println("speed investigation 2");
105 setupRates();
106 fireModelChanged(null, index);
107 }
108 }
109
110 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
111 //System.out.println("speed investigation 3");
112 setupRates();
113 fireModelChanged(null, index);
114 }
115
116 protected void storeState() {
117 }
118 protected void acceptState() {
119 }
120 protected void restoreState() {
121 //setupRates();
122 }
123 private void computeFactor(){
124 double sumRates = 0.0;
125 int lociCount = rateCategoryParameter.getDimension();
126 for(int i = 0; i < lociCount; i++){
127 sumRates += rates[(int)rateCategoryParameter.getParameterValue(i)];
128 }
129 scaleFactor = normalizeRateTo/(sumRates/lociCount);
130 }
131
132 }