Debian Codegraph @

src/dr/inference/distribution/LinearRegression.java

raw symbols SCIP
1 /*
2 * LinearRegression.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.inference.distribution;
27
28 import dr.inference.model.Parameter;
29
30 /**
31 * @author Marc Suchard
32 */
33 @Deprecated // GLM stuff is now in inference.glm - this is here for backwards compatibility temporarily
34 public class LinearRegression extends GeneralizedLinearModel {
35
36 private static final double normalizingConstant = -0.5 * Math.log(2 * Math.PI);
37
38 private boolean logTransform = false;
39
40 public double[] getTransformedDependentParameter() {
41 double[] y = dependentParam.getParameterValues();
42 if (logTransform) {
43 for(int i=0; i<y.length; i++)
44 y[i] = Math.log(y[i]);
45 }
46 return y;
47 }
48
49 protected double calculateLogLikelihood() {
50 double logLikelihood = 0;
51 double[] xBeta = getXBeta();
52 double[] precision = getScale();
53 double[] y = getTransformedDependentParameter();
54
55 for (int i = 0; i < N; i++) { // assumes that all observations are independent given fixed and random effects
56 if (logTransform)
57 logLikelihood -= y[i]; // Jacobian
58 logLikelihood += 0.5 * Math.log(precision[i]) - 0.5 * (y[i] - xBeta[i]) * (y[i] - xBeta[i]) * precision[i];
59
60 }
61 return N * normalizingConstant + logLikelihood;
62 }
63
64 public LinearRegression(Parameter dependentParam, boolean logTransform) { //, Parameter independentParam, DesignMatrix designMatrix) {
65 super(dependentParam); //, independentParam, designMatrix);
66 System.out.println("Constructing a linear regression model");
67 this.logTransform = logTransform;
68 }
69
70 protected double calculateLogLikelihoodAndGradient(double[] beta, double[] gradient) {
71 throw new RuntimeException("Optimization not yet implemented.");
72 }
73
74 public boolean requiresScale() {
75 return true;
76 }
77
78 protected double calculateLogLikelihood(double[] beta) {
79 throw new RuntimeException("Optimization not yet implemented.");
80 }
81
82 protected boolean confirmIndependentParameters() {
83 return true;
84 }
85 }