| 1 | /* |
| 2 | * WishartSufficientStatistics.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.math.distributions; |
| 27 | |
| 28 | import dr.math.matrixAlgebra.Vector; |
| 29 | |
| 30 | import java.util.Arrays; |
| 31 | |
| 32 | public class WishartSufficientStatistics { |
| 33 | |
| 34 | public WishartSufficientStatistics(int dim) { |
| 35 | df = 0; |
| 36 | scaleMatrix = new double[dim * dim]; |
| 37 | } |
| 38 | |
| 39 | public WishartSufficientStatistics(int df, double[] scaleMatrix) { |
| 40 | this.df = df; |
| 41 | this.scaleMatrix = scaleMatrix; |
| 42 | } |
| 43 | |
| 44 | public WishartSufficientStatistics clone() { |
| 45 | return new WishartSufficientStatistics(this.df, scaleMatrix.clone()); |
| 46 | } |
| 47 | |
| 48 | public void copyTo(WishartSufficientStatistics destination) { |
| 49 | assert (destination != null); |
| 50 | |
| 51 | destination.df = this.df; |
| 52 | System.arraycopy(this.scaleMatrix, 0, destination.scaleMatrix, 0, this.scaleMatrix.length); |
| 53 | } |
| 54 | |
| 55 | // public WishartSufficientStatistics(int df, double[] matrix, int dim) { |
| 56 | // this(df, convertToSquare(matrix, dim)); |
| 57 | // } |
| 58 | // |
| 59 | // public WishartSufficientStatistics(int df, double[] matrix, int dim, int count) { |
| 60 | // this(df, convertToSquareAndAccumulate(matrix, dim, count)); |
| 61 | // } |
| 62 | // |
| 63 | // public WishartSufficientStatistics(double[] matrix, int dim, int count) { |
| 64 | // this(convertFromSquareAndAccumulate(matrix, dim, count)); |
| 65 | // } |
| 66 | |
| 67 | public WishartSufficientStatistics(int[] dfs, double[] matrix) { |
| 68 | this(sum(dfs), accumulateMatrix(matrix, dfs.length)); |
| 69 | } |
| 70 | |
| 71 | // private WishartSufficientStatistics(WishartSufficientStatistics copy) { |
| 72 | // this.df = copy.df; |
| 73 | // this.scaleMatrix = copy.scaleMatrix; |
| 74 | // } |
| 75 | |
| 76 | public final int getDf() { |
| 77 | return df; |
| 78 | } |
| 79 | |
| 80 | public final double[] getScaleMatrix() { |
| 81 | return scaleMatrix; |
| 82 | } |
| 83 | |
| 84 | public final void incrementDf(int n) { |
| 85 | df += n; |
| 86 | } |
| 87 | |
| 88 | public final void clear() { |
| 89 | df = 0; |
| 90 | Arrays.fill(scaleMatrix, 0.0); |
| 91 | } |
| 92 | |
| 93 | // private static double[][] convertToSquare(double[] vector, int dim) { |
| 94 | // double[][] matrix = new double[dim][dim]; |
| 95 | // int offset = 0; |
| 96 | // for (int row = 0; row < dim; ++row) { |
| 97 | // System.arraycopy(vector, offset, matrix[row], 0, dim); |
| 98 | // offset += dim; |
| 99 | // } |
| 100 | // return matrix; |
| 101 | // } |
| 102 | |
| 103 | // private static WishartSufficientStatistics convertFromSquareAndAccumulate(double[] vector, int dim, int count) { |
| 104 | // double[][] matrix = new double[dim][dim]; |
| 105 | // int df = 0; |
| 106 | // |
| 107 | // for (int c = 0; c < count; ++c) { |
| 108 | // int offset = (dim * dim + 1) * c; |
| 109 | // for (int i = 0; i < dim; ++i) { |
| 110 | // for (int j = 0; j < dim; ++j) { |
| 111 | // matrix[i][j] += vector[offset]; |
| 112 | // ++offset; |
| 113 | // } |
| 114 | // } |
| 115 | // df += vector[offset]; |
| 116 | // } |
| 117 | // return new WishartSufficientStatistics(df, matrix); |
| 118 | // } |
| 119 | |
| 120 | private static double[] accumulateMatrix(double[] matrix, final int count) { |
| 121 | if (count == 1) { |
| 122 | return matrix; |
| 123 | } else { |
| 124 | final int length = matrix.length / count; |
| 125 | double[] result = new double[length]; |
| 126 | System.arraycopy(matrix, 0, result, 0, length); |
| 127 | |
| 128 | for (int i = 1; i < count; ++i) { |
| 129 | final int offset = i * length; |
| 130 | for (int j = 0; j < length; ++j) { |
| 131 | result[j] += matrix[offset + j]; |
| 132 | } |
| 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private static int sum(int[] vector) { |
| 139 | int sum = 0; |
| 140 | for (int x : vector) { |
| 141 | sum += x; |
| 142 | } |
| 143 | return sum; |
| 144 | } |
| 145 | |
| 146 | // private static double[][] convertToSquareAndAccumulate(double[] vector, int dim, int count) { |
| 147 | // double[][] matrix = new double[dim][dim]; |
| 148 | // |
| 149 | // for (int c = 0; c < count; ++c) { |
| 150 | // int offset = dim * dim * c; |
| 151 | // for (int i = 0; i < dim; ++i) { |
| 152 | // for (int j = 0; j < dim; ++j) { |
| 153 | // matrix[i][j] += vector[offset]; |
| 154 | // ++offset; |
| 155 | // } |
| 156 | // } |
| 157 | // } |
| 158 | // return matrix; |
| 159 | // } |
| 160 | |
| 161 | public String toString() { |
| 162 | return df + " : " + new Vector(scaleMatrix).toString(); |
| 163 | } |
| 164 | |
| 165 | private int df; |
| 166 | private final double[] scaleMatrix; |
| 167 | } |