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.
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.
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.
19
* Copyright (C) 2008 Illinois Institute of Technology
22
package weka.classifiers.bayes.blr;
24
import weka.classifiers.bayes.BayesianLogisticRegression;
25
import weka.core.Instance;
26
import weka.core.Instances;
27
import weka.core.RevisionUtils;
30
* Implementation of the Gaussian Prior update function based on modified
31
* CLG Algorithm (CLG-Lasso) with a certain Trust Region Update based
34
* @author Navendu Garg(gargnav@iit.edu)
35
* @version $Revision: 1.2 $
37
public class LaplacePriorImpl
40
/** for serialization. */
41
private static final long serialVersionUID = 2353576123257012607L;
43
Instances m_Instances;
45
double Hyperparameter;
51
* Update function specific to Laplace Prior.
53
public double update(int j, Instances instances, double beta,
54
double hyperparameter, double[] r, double deltaV) {
58
m_Instances = instances;
60
Hyperparameter = hyperparameter;
66
DeltaUpdate = laplaceUpdate(j, sign);
68
if (DeltaUpdate <= 0.0) { // positive direction failed.
70
DeltaUpdate = laplaceUpdate(j, sign);
72
if (DeltaUpdate >= 0.0) {
77
sign = Beta / Math.abs(Beta);
78
DeltaUpdate = laplaceUpdate(j, sign);
79
change = Beta + DeltaUpdate;
80
change = change / Math.abs(change);
83
DeltaUpdate = 0 - Beta;
91
* This is the CLG-lasso update function described in the
94
* @TechReport{blrtext04,
95
*author = {Alexander Genkin and David D. Lewis and David Madigan},
96
*title = {Large-scale bayesian logistic regression for text categorization},
97
*institution = {DIMACS},
99
*url = "http://www.stat.rutgers.edu/~madigan/PAPERS/shortFat-v3a.pdf",
104
* @return double value
106
public double laplaceUpdate(int j, double sign) {
108
double numerator = 0.0;
109
double denominator = 0.0;
113
for (int i = 0; i < m_Instances.numInstances(); i++) {
114
instance = m_Instances.instance(i);
116
if (instance.value(j) != 0) {
117
numerator += (instance.value(j) * BayesianLogisticRegression.classSgn(instance.classValue()) * (1.0 / (1.0 +
119
denominator += (instance.value(j) * instance.value(j) * BayesianLogisticRegression.bigF(R[i],
120
Delta * instance.value(j)));
124
numerator -= (Math.sqrt(2.0 / Hyperparameter) * sign);
126
if (denominator != 0.0) {
127
value = numerator / denominator;
134
* Computes the log-likelihood values using the implementation in the Prior class.
137
* @param hyperparameter
139
public void computeLogLikelihood(double[] betas, Instances instances) {
140
//Basic implementation done in the prior class.
141
super.computelogLikelihood(betas, instances);
145
* This function computes the penalty term specific to Laplacian distribution.
147
* @param hyperparameters
149
public void computePenalty(double[] betas, double[] hyperparameters) {
154
for (int j = 0; j < betas.length; j++) {
155
lambda = Math.sqrt(hyperparameters[j]);
156
penalty += (Math.log(2) - Math.log(lambda) +
157
(lambda * Math.abs(betas[j])));
160
penalty = 0 - penalty;
164
* Returns the revision string.
166
* @return the revision
168
public String getRevision() {
169
return RevisionUtils.extract("$Revision: 1.2 $");