<commit_before>/*
 * ApproximateFactorAnalysisPrecisionMatrix.java
 *
 * Copyright (c) 2002-2016 <NAME>, <NAME> and <NAME>
 *
 * 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.evomodel.continuous;

import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import dr.inference.model.*;
import dr.math.matrixAlgebra.Matrix;
import dr.math.matrixAlgebra.RobustSingularValueDecomposition;
import dr.math.matrixAlgebra.Vector;
import dr.xml.*;

/**
 * @author <NAME>
 * @author <NAME>
 *
 */

public class ApproximateFactorAnalysisPrecisionMatrix extends Parameter.Abstract implements
        MatrixParameterInterface, VariableListener {

    private double[] values;
    private double[] storedValues;

    private boolean valuesKnown;
    private boolean storedValuesKnow;

    private final MatrixParameterInterface L;
    private final Parameter gamma;
    private int dim;

    private final static boolean DEBUG = false;

    public ApproximateFactorAnalysisPrecisionMatrix(String name,
                                                    MatrixParameterInterface L,
                                                    Parameter gamma) {
        super(name);

        this.L = L;
        this.gamma = gamma;

        L.addVariableListener(this);
        gamma.addVariableListener(this);

        this.dim = L.getRowDimension();


        values = new double[dim * dim];
    }

    private void computeValues() {
        if (!valuesKnown) {
            computeValuesImp();
            valuesKnown = true;
        }
    }

    private void computeValuesImp() {
        dim = L.getRowDimension();
        double[][] matrix = new double[dim][dim];

        for (int row = 0; row < L.getRowDimension(); ++row) {
            for (int col = 0; col < L.getRowDimension(); ++col) {
                double sum = 0;
                for (int k = 0; k < L.getColumnDimension(); ++k) {
                    sum += L.getParameterValue(row, k) * L.getParameterValue(col, k);
                }
                matrix[row][col] = sum;
            }


        }
        DoubleMatrix2D LL = new DenseDoubleMatrix2D(matrix);
        RobustSingularValueDecomposition SVD = new RobustSingularValueDecomposition(LL);
        double[][] U = SVD.getU().toArray();
        double[][] V = SVD.getV().toArray();
        DoubleMatrix2D EvalsTemp= SVD.getS();

        double[] EVals = new double[EvalsTemp.rows()];
        for (int i = 0; i < EVals.length ; i++) {
            EVals[i] = EvalsTemp.get(i, i);
        }

        for (int i = 0; i < EVals.length ; i++) {
            if(Math.abs(EVals[i]) >= Math.pow(10, -10)){
                EVals[i] = 1 / EVals[i];
            }
            else
                EVals[i] = 0;
        }


        for (int i = 0; i <U.length; i++) {
            for (int j = 0; j <V.length ; j++) {
                matrix[i][j] = 0;
                for (int k = 0; k < U.length; k++) {
                    matrix[i][j] += V[i][k] * EVals[k] * U[j][k];
                }

            }

        }

        for (int row = 0; row < dim; row++) {

            matrix[row][row] += gamma.getParameterValue(row);
        }

        if (DEBUG) {
            System.err.println("mult:");
            System.err.println(new Matrix(L.getParameterAsMatrix()));
            System.err.println(new Vector(gamma.getParameterValues()) + "\n");
            System.err.println(new Matrix(matrix));
        }

//        Matrix inverse = new Matrix(matrix).inverse();
<commit_msg>Uses correct model now<commit_after>/*
 * ApproximateFactorAnalysisPrecisionMatrix.java
 *
 * Copyright (c) 2002-2016 <NAME>, <NAME> and <NAME>
 *
 * 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.evomodel.continuous;

import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import dr.inference.model.*;
import dr.math.matrixAlgebra.Matrix;
import dr.math.matrixAlgebra.RobustSingularValueDecomposition;
import dr.math.matrixAlgebra.Vector;
import dr.xml.*;

/**
 * @author <NAME>
 * @author <NAME>
 *
 */

public class ApproximateFactorAnalysisPrecisionMatrix extends Parameter.Abstract implements
        MatrixParameterInterface, VariableListener {

    private double[] values;
    private double[] storedValues;

    private boolean valuesKnown;
    private boolean storedValuesKnow;

    private final MatrixParameterInterface L;
    private final Parameter gamma;
    private int dim;

    private final static boolean DEBUG = false;

    public ApproximateFactorAnalysisPrecisionMatrix(String name,
                                                    MatrixParameterInterface L,
                                                    Parameter gamma) {
        super(name);

        this.L = L;
        this.gamma = gamma;

        L.addVariableListener(this);
        gamma.addVariableListener(this);

        this.dim = L.getRowDimension();


        values = new double[dim * dim];
    }

    private void computeValues() {
        if (!valuesKnown) {
            computeValuesImp();
            valuesKnown = true;
        }
    }

    private void computeValuesImp() {
        dim = L.getRowDimension();
        double[][] matrix = new double[dim][dim];

        for (int row = 0; row < L.getRowDimension(); ++row) {
            for (int col = 0; col < L.getRowDimension(); ++col) {
                double sum = 0;
                for (int k = 0; k < L.getColumnDimension(); ++k) {
                    sum += L.getParameterValue(row, k) * L.getParameterValue(col, k);
                }
                matrix[row][col] = sum;
            }


        }
//        DoubleMatrix2D LL = new DenseDoubleMatrix2D(matrix);
//        RobustSingularValueDecomposition SVD = new RobustSingularValueDecomposition(LL);
//        double[][] U = SVD.getU().toArray();
//        double[][] V = SVD.getV().toArray();
//        DoubleMatrix2D EvalsTemp= SVD.getS();
//
//        double[] EVals = new double[EvalsTemp.rows()];
//        for (int i = 0; i < EVals.length ; i++) {
//            EVals[i] = EvalsTemp.get(i, i);
//        }
//
//        for (int i = 0; i < EVals.length ; i++) {
//            if(Math.abs(EVals[i]) >= Math.pow(10, -10)){
//                EVals[i] = 1 / EVals[i];
//            }
//            else
//                EVals[i] = 0;
//        }
//
//
//        for (int i = 0; i <U.length; i++) {
//            for (int j = 0; j <V.length ; j++) {
//                matrix[i][j] = 0;
//                for (int k = 0; k < U.length; k++) {
//                    matrix[i][j] += V[i][k] * EVals[k] * U[j][k];
//                }
//
//            }
//
//        }

        for (int row = 0; row < dim; row++) {

            matrix[row][row] += 1 / gamma.getParameterValue(row);
        }

        if (DEBUG) {
            System.err.println("mult:");
            System.err.println(new Matrix(L.getParameterAsMatrix()));
            System.err.println(new Vector(gamma.getParameterValues()) + "\n");
            System.err.println(new Matrix(matrix));
        }

        matrix = new Matrix(matrix).inverse().toComponents();


{'fraction_non_alphanumeric': 0.11728465487735311, 'fraction_numerical': 0.009697661152310326, 'mean_word_length': 2.093154551870148, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6722219', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2426, 'n_words': 735}