source: src/main/java/weka/classifiers/bayes/blr/Prior.java @ 14

Last change on this file since 14 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.2 KB
Line 
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 *    Prior.java
19 *    Copyright (C) 2008 Illinois Institute of Technology
20 *
21 */
22
23package weka.classifiers.bayes.blr;
24
25import weka.classifiers.bayes.BayesianLogisticRegression;
26import weka.core.Instance;
27import weka.core.Instances;
28import weka.core.RevisionHandler;
29
30import java.io.Serializable;
31
32/**
33 * This is an interface to plug various priors into
34 * the Bayesian Logistic Regression Model.
35 *
36 * @version $Revision: 1.2 $
37 * @author Navendu Garg (gargnav@iit.edu)
38 */
39public abstract class Prior
40  implements Serializable, RevisionHandler {
41 
42  protected Instances m_Instances;
43  protected double Beta = 0.0;
44  protected double Hyperparameter = 0.0;
45  protected double DeltaUpdate;
46  protected double[] R;
47  protected double Delta = 0.0;
48  protected double log_posterior = 0.0;
49  protected double log_likelihood = 0.0;
50  protected double penalty = 0.0;
51
52  /**
53   * Interface for the update functions for different types of
54   * priors.
55   *
56   */
57  public double update(int j, Instances instances, double beta,
58    double hyperparameter, double[] r, double deltaV) {
59    return 0.0;
60  }
61
62  /**
63   * Function computes the log-likelihood value:
64   * -sum{1 to n}{ln(1+exp(-Beta*x(i)*y(i))}
65   * @param betas
66   * @param instances
67   */
68  public void computelogLikelihood(double[] betas, Instances instances) {
69    Instance instance;
70    log_likelihood = 0.0;
71
72    for (int i = 0; i < instances.numInstances(); i++) {
73      instance = instances.instance(i);
74
75      double log_row = 0.0;
76
77      for (int j = 0; j < instance.numAttributes(); j++) {
78        if (instance.value(j) != 0.0) {
79          log_row += (betas[j] * instance.value(j) * instance.value(j));
80        }
81      }
82
83      log_row = log_row * BayesianLogisticRegression.classSgn(instance.classValue());
84      log_likelihood += Math.log(1.0 + Math.exp(0.0 - log_row));
85    }
86
87    log_likelihood = 0 - log_likelihood;
88  }
89
90  /**
91   * Skeleton function to compute penalty terms.
92   * @param betas
93   * @param hyperparameters
94   */
95  public void computePenalty(double[] betas, double[] hyperparameters) {
96    //implement specific penalties in the prior implmentation.
97  }
98
99  /**
100   *
101   * @return log-likelihood value.
102   */
103  public double getLoglikelihood() {
104    return log_likelihood;
105  }
106
107  /**
108   *
109   * @return regularized log posterior value.
110   */
111  public double getLogPosterior() {
112    log_posterior = log_likelihood + penalty;
113
114    return log_posterior;
115  }
116
117  /**
118   *
119   * @return penalty term.
120   */
121  public double getPenalty() {
122    return penalty;
123  }
124}
Note: See TracBrowser for help on using the repository browser.