/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.fit.functions;


import org.netlib.blas.DAXPY;

import com.approximatrix.draco.fit.RunLS;
import com.approximatrix.draco.math.VectorSupport;

public class LogitLikelihood extends SimpleLikelihood implements LikelihoodFunction {

	private static final String FUNCTION_NAME = "Logit";
	
	public LogitLikelihood() {
		// Dummy
	}

	public double evaluateFunction(double[] parameters, double[] variables) {
		double exponential = calculateExponential(parameters,variables);
		return exponential/(1.0+exponential);
	}
	
	/** Never called... */
	public double evaluateLikelihood(double[] parameters, 
									 double[][] independent, 
									 double[] dependent) {
		double L = 0.0;
		
		for(int i=0;i<dependent.length;i++) {
			double[] row = new double[independent[i].length];
			for(int j=0;j<independent[i].length;j++)
				row[j] = independent[i][j];
			
			L += (dependent[i]*calculateLinearTerm(parameters,row) - 
			     Math.log(1.0+calculateExponential(parameters,row)));
		}
		
		return L;
	}
	
	private double calculateExponential(double[] parameters, double[] variables) {
		return Math.exp(calculateLinearTerm(parameters,variables));
	}
	
	public String getLikelihoodType() {
		return FUNCTION_NAME;
	}
	
	protected double evaluatePDF(double[] parameters, double[] variables) {
		double exponential = calculateExponential(parameters,variables);
		return exponential/((1.0+exponential)*(1.0+exponential));
	}
	
}
