package dkohl.bayes.probability.distribution;

import java.util.Vector;

import dkohl.bayes.probability.Assignment;

public class Gaussian {
    
    public static final double TWO_PI = 2 * Math.PI;
    
    private double mean;
    private double var;
    private Vector<Double> samples;
    
    public Gaussian() {
	samples = new Vector<Double>();
    }
    
    public void push(Assignment assignment) {
	double sample = Double.valueOf(assignment.getValue());
	samples.add(sample);
    }
    
    public void estimate() {
	mean = 0;
	for(Double sample : samples) {
	    mean += sample;
	}
	mean /= samples.size();
	
	var = 0;
	for(Double sample : samples) {
	    var += Math.pow(sample - mean, 2);
	}
	var /= samples.size();
	if(var == 0) {
	    var = 1;
	}
    }
    
    public double eval(Assignment assignment) {
	double sample = Double.valueOf(assignment.getValue());
	double fac = 1 / (Math.sqrt(TWO_PI * var));
	return fac * Math.exp(-0.5 * (Math.pow(sample - mean, 2) / var));
    }
 
    @Override
    public String toString() {
        return "< " + mean + ", " + Math.sqrt(var) + " >"; 
    }
    
}
