/* * LoadingsIndependenceOperator.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.inference.operators; import dr.inference.distribution.DistributionLikelihood; import dr.inference.model.LatentFactorModel; import dr.inference.model.MatrixParameterInterface; import dr.inference.model.Parameter; import dr.math.MathUtils; import dr.math.distributions.MultivariateNormalDistribution; import dr.math.distributions.NormalDistribution; import dr.math.matrixAlgebra.CholeskyDecomposition; import dr.math.matrixAlgebra.IllegalDimension; import dr.math.matrixAlgebra.SymmetricMatrix; import java.util.ArrayList; import java.util.ListIterator; /** * Created with IntelliJ IDEA. * User: max * Date: 5/23/14 * Time: 2:23 PM * To change this template use File | Settings | File Templates. */ public class LoadingsIndependenceOperator extends AbstractAdaptableOperator { NormalDistribution prior; LatentFactorModel LFM; ArrayList precisionArray; ArrayList meanMidArray; ArrayList meanArray; boolean randomScan; double scaleFactor; double priorPrecision; double priorMeanPrecision; public LoadingsIndependenceOperator(LatentFactorModel LFM, DistributionLikelihood prior, double weight, boolean randomScan, double scaleFactor, AdaptationMode mode) { super(mode); setWeight(weight); this.scaleFactor = scaleFactor; this.prior = (NormalDistribution) prior.getDistribution(); this.LFM = LFM; precisionArray = new ArrayList(); double[][] temp; this.randomScan = randomScan; meanArray = new ArrayList(); meanMidArray = new ArrayList(); double[] tempMean; if (!randomScan) { for (int i = 0; i < LFM.getFactorDimension(); i++) { temp = new double[i + 1][i + 1]; precisionArray.add(temp); } for (int i = 0; i < LFM.getFactorDimension(); i++) { tempMean = new double[i + 1]; meanArray.add(tempMean); } for (int i = 0; i < LFM.getFactorDimension(); i++) { tempMean = new double[i + 1]; meanMidArray.add(tempMean); } } else { for (int i = 0; i < LFM.getFactorDimension(); i++) { temp = new double[LFM.getFactorDimension() - i][LFM.getFactorDimension() - i]; precisionArray.add(temp); } for (int i = 0; i < LFM.getFactorDimension(); i++) { tempMean = new double[LFM.getFactorDimension() - i]; meanArray.add(tempMean); } for (int i = 0; i < LFM.getFactorDimension(); i++) { tempMean = new double[LFM.getFactorDimension() - i]; meanMidArray.add(tempMean); } } // vectorProductAnswer=new MatrixParameter[LFM.getLoadings().getRowDimension()]; // for (int i = 0; i currentPrecision, ListIterator currentMidMean, ListIterator currentMean) { double[] draws = null; double[][] precision = null; double[][] variance; double[] midMean = null; double[] mean = null; double[][] cholesky = null; if (currentPrecision.hasNext()) { precision = currentPrecision.next(); } if (currentMidMean.hasNext()) { midMean = currentMidMean.next(); } if (currentMean.hasNext()) { mean = currentMean.next(); } getPrecision(i, precision); variance = (new SymmetricMatrix(precision)).inverse().toComponents(); try { cholesky = new CholeskyDecomposition(variance).getL(); } catch (IllegalDimension illegalDimension) { illegalDimension.printStackTrace(); } getMean(i, variance, midMean, mean); draws = MultivariateNormalDistribution.nextMultivariateNormalCholesky(mean, cholesky, scaleFactor); // if(i 0) { copy(i, draws); // LFM.computeResiduals(); } } else { copy(i, draws); // LFM.computeResiduals(); } // copy(i, draws); } // @Override // public int getStepCount() { // return 0; //To change body of implemented methods use File | Settings | File Templates. // } @Override public String getOperatorName() { return "loadingsGibbsOperator"; //To change body of implemented methods use File | Settings | File Templates. } @Override public double doOperation() { int size = LFM.getLoadings().getColumnDimension(); if (!randomScan) { ListIterator currentPrecision = precisionArray.listIterator(); ListIterator currentMidMean = meanMidArray.listIterator(); ListIterator currentMean = meanArray.listIterator(); for (int i = 0; i < size; i++) { drawI(i, currentPrecision, currentMidMean, currentMean); } LFM.getLoadings().fireParameterChangedEvent(); } else { int i = MathUtils.nextInt(LFM.getLoadings().getColumnDimension()); ListIterator currentPrecision; ListIterator currentMidMean; ListIterator currentMean; if (i < LFM.getFactorDimension()) { currentPrecision = precisionArray.listIterator(LFM.getFactorDimension() - i - 1); currentMidMean = meanMidArray.listIterator(LFM.getFactorDimension() - i - 1); currentMean = meanArray.listIterator(LFM.getFactorDimension() - i - 1); } else { currentPrecision = precisionArray.listIterator(); currentMidMean = meanMidArray.listIterator(); currentMean = meanArray.listIterator(); } drawI(i, currentPrecision, currentMidMean, currentMean); LFM.getLoadings().fireParameterChangedEvent(); } return 0; } public double getAdaptableParameter() { return Math.log(scaleFactor); } @Override public void setAdaptableParameter(double value) { scaleFactor = Math.exp(value); } @Override public double getRawParameter() { return scaleFactor; } public String getAdaptableParameterName() { return "scaleFactor"; } }