~ubuntu-branches/ubuntu/quantal/libpal-java/quantal

« back to all changes in this revision

Viewing changes to src/pal/substmodel/VariableIndependentSingleSplitDistribution.java

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2012-01-27 13:57:54 UTC
  • Revision ID: package-import@ubuntu.com-20120127135754-d63l3581f65fw9pk
Tags: upstream-1.5.1
ImportĀ upstreamĀ versionĀ 1.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// VariableIndependentSingleSplitDistribution.java
 
2
//
 
3
// (c) 1999-2004 PAL Development Team
 
4
//
 
5
// This package may be distributed under the
 
6
// terms of the Lesser GNU General Public License (LGPL)
 
7
 
 
8
package pal.substmodel;
 
9
 
 
10
/**
 
11
 * <p>Title: VariableIndependentSingleSplitDistribution </p>
 
12
 * <p>Description: Allows for a split in substitution model parameters where the class distribution before the split maybe different after the split (and the probabilities of being in a before class and an after class are independent)</p>
 
13
 * @author Matthew Goode
 
14
 */
 
15
 
 
16
public class VariableIndependentSingleSplitDistribution implements SingleSplitDistribution{
 
17
  private final double[][] probabilityStore_;
 
18
        private final double[] parameterStore_;
 
19
        private final double[] beforeNormalisedParameterStore_;
 
20
        private final double[] afterNormalisedParameterStore_;
 
21
        private final int numberOfBaseTransitionCategories_;
 
22
 
 
23
        private final static double PARAMETER_BASE = 0.00000001;
 
24
        private final static double PARAMETER_MINIMUM = 0+PARAMETER_BASE;
 
25
        private final static double PARAMETER_DEFAULT = 0.5+PARAMETER_BASE;
 
26
        private final static double PARAMETER_MAXIMUM = 1+PARAMETER_BASE;
 
27
 
 
28
 
 
29
        public VariableIndependentSingleSplitDistribution(int numberOfBaseTransitionCategories) {
 
30
                this(numberOfBaseTransitionCategories,null);
 
31
        }
 
32
        public VariableIndependentSingleSplitDistribution(int numberOfBaseTransitionCategories, double[] initialParameters) {
 
33
          this.numberOfBaseTransitionCategories_ = numberOfBaseTransitionCategories;
 
34
                this.probabilityStore_ = new double[numberOfBaseTransitionCategories][numberOfBaseTransitionCategories];
 
35
                this.parameterStore_ = new double[numberOfBaseTransitionCategories*2];
 
36
                this.afterNormalisedParameterStore_ = new double[numberOfBaseTransitionCategories];
 
37
                this.beforeNormalisedParameterStore_ = new double[numberOfBaseTransitionCategories];
 
38
                if(initialParameters==null) {
 
39
                        for( int i = 0; i<parameterStore_.length; i++ ) {
 
40
                                parameterStore_[i] = PARAMETER_DEFAULT;
 
41
                        }
 
42
                } else {
 
43
                        if(initialParameters.length == numberOfBaseTransitionCategories) {
 
44
                                //Traditional values supplied
 
45
                                for(int i = 0 ; i < numberOfBaseTransitionCategories ; i++) {
 
46
                                  parameterStore_[i] = initialParameters[i];
 
47
                                  parameterStore_[i+numberOfBaseTransitionCategories] = initialParameters[i];
 
48
                                }
 
49
                        } else if(initialParameters.length == numberOfBaseTransitionCategories*2) {
 
50
                                //Full values supplied
 
51
                                for(int i = 0 ; i < numberOfBaseTransitionCategories*2 ; i++) {
 
52
                                  parameterStore_[i] = initialParameters[i];
 
53
                                }
 
54
                        } else {
 
55
                          throw new IllegalArgumentException("Cannot handle "+initialParameters.length+" initial parameters");
 
56
                        }
 
57
                }
 
58
                correctParameters(parameterStore_);
 
59
                updateProbabilityStore();
 
60
                System.out.println("Variable Independent probability model created:"+this);
 
61
        }
 
62
        public double[] getDistribution() {
 
63
                double[] distribution = new double[numberOfBaseTransitionCategories_*numberOfBaseTransitionCategories_];
 
64
                for(int i = 0 ; i < distribution.length ; i++) {
 
65
                        final int a = i%numberOfBaseTransitionCategories_;
 
66
                        final int b = i/numberOfBaseTransitionCategories_;
 
67
                        distribution[i] = afterNormalisedParameterStore_[a]*beforeNormalisedParameterStore_[b];
 
68
                }
 
69
                return distribution;
 
70
        }
 
71
 
 
72
        public int getNumberOfBaseTransitionCategories() {
 
73
                return numberOfBaseTransitionCategories_;
 
74
        }
 
75
        public double[][] getDistributionInfo() {
 
76
                return probabilityStore_;
 
77
        }
 
78
        private final void updateProbabilityStore() {
 
79
                double beforeTotal = 0;
 
80
                double afterTotal = 0;
 
81
 
 
82
                for(int i = 0 ; i < numberOfBaseTransitionCategories_ ; i++) {
 
83
                        beforeTotal+=parameterStore_[i];
 
84
                        afterTotal+=parameterStore_[i+numberOfBaseTransitionCategories_];
 
85
                }
 
86
                for(int i = 0 ; i < numberOfBaseTransitionCategories_ ; i++) {
 
87
                  beforeNormalisedParameterStore_[i] = parameterStore_[i]/beforeTotal;
 
88
                        afterNormalisedParameterStore_[i] = parameterStore_[i+numberOfBaseTransitionCategories_]/afterTotal;
 
89
                }
 
90
                int parameterIndex =0;
 
91
                for(int before=0 ; before < numberOfBaseTransitionCategories_ ; before++) {
 
92
                        for(int after=0 ; after < numberOfBaseTransitionCategories_ ; after++) {
 
93
                                probabilityStore_[before][after] = beforeNormalisedParameterStore_[before]*afterNormalisedParameterStore_[after];
 
94
                        }
 
95
                }
 
96
        }
 
97
        public int getNumberOfParameters() { return numberOfBaseTransitionCategories_; }
 
98
        public void setParameters(double[] store, int startIndex) {
 
99
                System.arraycopy(store,startIndex,parameterStore_,0,parameterStore_.length);
 
100
                updateProbabilityStore();
 
101
        }
 
102
        public void getParameters(double[] store, int startIndex) {
 
103
                System.arraycopy(parameterStore_,0,store,startIndex,parameterStore_.length);
 
104
        }
 
105
        public double getLowerLimit(int n) { return PARAMETER_MINIMUM; }
 
106
        public double getUpperLimit(int n) { return PARAMETER_MAXIMUM; }
 
107
        private static final void correctParameters(double[] parameters) {
 
108
          for(int i = 0 ; i < parameters.length ; i++) {
 
109
                  double p = parameters[i]+PARAMETER_BASE;
 
110
                        if(p<PARAMETER_MINIMUM) {
 
111
                          p = PARAMETER_MINIMUM;
 
112
                        } else if(p>PARAMETER_MAXIMUM) {
 
113
                          p = PARAMETER_MAXIMUM;
 
114
                        }
 
115
                        parameters[i] = p;
 
116
                }
 
117
        }
 
118
        public void getDefaultValues(double[] store, int startIndex) {
 
119
                for( int i = 0; i<parameterStore_.length; i++ ) {
 
120
                        store[i+startIndex] = PARAMETER_DEFAULT;
 
121
                }
 
122
        }
 
123
        public String toString() {
 
124
                return "Variable Independent("+pal.misc.Utils.toString(parameterStore_)+" - > "+pal.misc.Utils.toString(getDistribution())+")";
 
125
        }
 
126
}
 
 
b'\\ No newline at end of file'