Debian Codegraph @

src/dr/oldevomodel/sitemodel/SampleQuantileLociRates.java

raw symbols SCIP
1 /*
2 * SampleQuantileLociRates.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 import dr.oldevomodelxml.sitemodel.SampleQuantileLociRatesParser;
31
32 /**
33 * @author Chieh-Hsi Wu
34 * Sampling relative loci rates from an underlying distribution by sampling quantiles.
35 */
36 public class SampleQuantileLociRates extends AbstractModel {
37 private CompoundParameter lociRates;
38 private Parameter rateQuantileParameter;
39 private ParametricDistributionModel distrModel;
40 private double normalizeRateTo;
41 private boolean normalize;
42 private double scaleFactor;
43 private double[] rates;
44
45
46
47 public SampleQuantileLociRates(
48 CompoundParameter lociRates,
49 Parameter rateQuantileParameter,
50 ParametricDistributionModel model,
51 boolean normalize,
52 double normalizeLociRateTo) {
53 super(SampleQuantileLociRatesParser.SAMPLE_QUANTILE_LOCI_RATES);
54
55 this.lociRates = lociRates;
56
57 this.rateQuantileParameter = rateQuantileParameter;
58 //Force the boundaries of rateCategoryParameter to match the category count
59 //Parameter.DefaultBounds bound = new Parameter.DefaultBounds(0.99, 0.01, rateQuantileParameter.getDimension());
60 //this.rateQuantileParameter.addBounds(bound);
61
62 this.distrModel = model;
63 this.normalizeRateTo = normalizeLociRateTo;
64 this.normalize = normalize;
65 rates = new double[rateQuantileParameter.getDimension()];
66 setupRates();
67 setupRelativeRates();
68
69 addModel(distrModel);
70 addVariable(this.rateQuantileParameter);
71 }
72
73 private void setupRates(){
74 for(int i = 0; i < rates.length; i++){
75 rates[i]= distrModel.quantile(rateQuantileParameter.getParameterValue(i));
76 //System.err.println("setupRates: "+rates[i]);
77 }
78 }
79
80 private void setupRates(int rateQuantileParameterIndex){
81 rates[rateQuantileParameterIndex]= distrModel.quantile(rateQuantileParameter.getParameterValue(rateQuantileParameterIndex));
82 }
83
84 public void handleModelChangedEvent(Model model, Object object, int index) {
85 if (model == distrModel) {
86 setupRates();
87 setupRelativeRates();
88 //System.out.println("speed investigation 1");
89 fireModelChanged();
90 }else if (model == rateQuantileParameter) {
91 //System.out.println("speed investigation 2");
92 setupRates(index);
93 fireModelChanged(null, index);
94 }
95 }
96
97 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
98 //System.out.println("speed investigation 3");
99 setupRates(index);
100 setupRelativeRates();
101 fireModelChanged(null, index);
102 }
103
104 protected void storeState() {
105 }
106 protected void acceptState() {
107 }
108 protected void restoreState() {
109 //setupRates();
110 }
111 private void setupRelativeRates(){
112 //System.err.println("computeFactor: "+normalize);
113 if(normalize){
114 computeFactor();
115 }
116 int lociCount = rateQuantileParameter.getDimension();
117 for(int i = 0; i < lociCount; i ++){
118 //System.err.println(rates[i]*scaleFactor);
119 lociRates.setParameterValue(i,rates[i]*scaleFactor);
120 }
121
122 }
123
124 private void computeFactor(){
125 double sumRates = 0.0;
126 int lociCount = rateQuantileParameter.getDimension();
127 for(int i = 0; i < lociCount; i++){
128 //System.err.println("computeFactor: "+rates[i]);
129 sumRates += rates[i];
130 }
131 scaleFactor = normalizeRateTo/(sumRates/lociCount);
132 }
133
134
135 }