1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * GaussianPrior.java |
---|
19 | * Copyright (C) 2008 Illinois Institute of Technology |
---|
20 | * |
---|
21 | */ |
---|
22 | package weka.classifiers.bayes.blr; |
---|
23 | |
---|
24 | import weka.classifiers.bayes.BayesianLogisticRegression; |
---|
25 | |
---|
26 | import weka.core.Instance; |
---|
27 | import weka.core.Instances; |
---|
28 | import weka.core.RevisionUtils; |
---|
29 | |
---|
30 | /** |
---|
31 | * Implementation of the Gaussian Prior update function based on |
---|
32 | * CLG Algorithm with a certain Trust Region Update. |
---|
33 | * |
---|
34 | * The values are updated in the BayesianLogisticRegressionV variables |
---|
35 | * used by the algorithm. |
---|
36 | * |
---|
37 | * |
---|
38 | * @author Navendu Garg(gargnav@iit.edu) |
---|
39 | * @version $Revision: 1.2 $ |
---|
40 | */ |
---|
41 | public class GaussianPriorImpl |
---|
42 | extends Prior { |
---|
43 | |
---|
44 | /** for serialization. */ |
---|
45 | private static final long serialVersionUID = -2995684220141159223L; |
---|
46 | |
---|
47 | /** |
---|
48 | * Update function specific to Laplace Prior. |
---|
49 | */ |
---|
50 | public double update(int j, Instances instances, double beta, |
---|
51 | double hyperparameter, double[] r, double deltaV) { |
---|
52 | int i; |
---|
53 | double numerator = 0.0; |
---|
54 | double denominator = 0.0; |
---|
55 | double value = 0.0; |
---|
56 | Instance instance; |
---|
57 | |
---|
58 | m_Instances = instances; |
---|
59 | Beta = beta; |
---|
60 | Hyperparameter = hyperparameter; |
---|
61 | Delta = deltaV; |
---|
62 | R = r; |
---|
63 | |
---|
64 | //Compute First Derivative i.e. Numerator |
---|
65 | //Compute the Second Derivative i.e. |
---|
66 | for (i = 0; i < m_Instances.numInstances(); i++) { |
---|
67 | instance = m_Instances.instance(i); |
---|
68 | |
---|
69 | if (instance.value(j) != 0) { |
---|
70 | //Compute Numerator (Note: (0.0-1.0/(1.0+Math.exp(R[i]) |
---|
71 | numerator += ((instance.value(j) * BayesianLogisticRegression.classSgn(instance.classValue())) * (0.0 - |
---|
72 | (1.0 / (1.0 + Math.exp(R[i]))))); |
---|
73 | |
---|
74 | //Compute Denominator |
---|
75 | denominator += (instance.value(j) * instance.value(j) * BayesianLogisticRegression.bigF(R[i], |
---|
76 | Delta * Math.abs(instance.value(j)))); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | numerator += ((2.0 * Beta) / Hyperparameter); |
---|
81 | denominator += (2.0 / Hyperparameter); |
---|
82 | value = numerator / denominator; |
---|
83 | |
---|
84 | return (0 - (value)); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * This method calls the log-likelihood implemented in the Prior |
---|
89 | * abstract class. |
---|
90 | * @param betas |
---|
91 | * @param instances |
---|
92 | */ |
---|
93 | public void computeLoglikelihood(double[] betas, Instances instances) { |
---|
94 | super.computelogLikelihood(betas, instances); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * This function computes the penalty term specific to Gaussian distribution. |
---|
99 | * @param betas |
---|
100 | * @param hyperparameters |
---|
101 | */ |
---|
102 | public void computePenalty(double[] betas, double[] hyperparameters) { |
---|
103 | penalty = 0.0; |
---|
104 | |
---|
105 | for (int j = 0; j < betas.length; j++) { |
---|
106 | penalty += (Math.log(Math.sqrt(hyperparameters[j])) + |
---|
107 | (Math.log(2 * Math.PI) / 2) + |
---|
108 | ((betas[j] * betas[j]) / (2 * hyperparameters[j]))); |
---|
109 | } |
---|
110 | |
---|
111 | penalty = 0 - penalty; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Returns the revision string. |
---|
116 | * |
---|
117 | * @return the revision |
---|
118 | */ |
---|
119 | public String getRevision() { |
---|
120 | return RevisionUtils.extract("$Revision: 1.2 $"); |
---|
121 | } |
---|
122 | } |
---|