1
// VariableIndependentSingleSplitDistribution.java
3
// (c) 1999-2004 PAL Development Team
5
// This package may be distributed under the
6
// terms of the Lesser GNU General Public License (LGPL)
8
package pal.substmodel;
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
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_;
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;
29
public VariableIndependentSingleSplitDistribution(int numberOfBaseTransitionCategories) {
30
this(numberOfBaseTransitionCategories,null);
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;
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];
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];
55
throw new IllegalArgumentException("Cannot handle "+initialParameters.length+" initial parameters");
58
correctParameters(parameterStore_);
59
updateProbabilityStore();
60
System.out.println("Variable Independent probability model created:"+this);
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];
72
public int getNumberOfBaseTransitionCategories() {
73
return numberOfBaseTransitionCategories_;
75
public double[][] getDistributionInfo() {
76
return probabilityStore_;
78
private final void updateProbabilityStore() {
79
double beforeTotal = 0;
80
double afterTotal = 0;
82
for(int i = 0 ; i < numberOfBaseTransitionCategories_ ; i++) {
83
beforeTotal+=parameterStore_[i];
84
afterTotal+=parameterStore_[i+numberOfBaseTransitionCategories_];
86
for(int i = 0 ; i < numberOfBaseTransitionCategories_ ; i++) {
87
beforeNormalisedParameterStore_[i] = parameterStore_[i]/beforeTotal;
88
afterNormalisedParameterStore_[i] = parameterStore_[i+numberOfBaseTransitionCategories_]/afterTotal;
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];
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();
102
public void getParameters(double[] store, int startIndex) {
103
System.arraycopy(parameterStore_,0,store,startIndex,parameterStore_.length);
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;
118
public void getDefaultValues(double[] store, int startIndex) {
119
for( int i = 0; i<parameterStore_.length; i++ ) {
120
store[i+startIndex] = PARAMETER_DEFAULT;
123
public String toString() {
124
return "Variable Independent("+pal.misc.Utils.toString(parameterStore_)+" - > "+pal.misc.Utils.toString(getDistribution())+")";
b'\\ No newline at end of file'