📄 meanestimateforfixedvariancegaussian.java
字号:
/* * Created on Aug 3, 2004 * */package org.placelab.particlefilter;import org.placelab.collections.UnsupportedOperationException;/** * * * This class represents a probability distrubution over potential means for a fixed-variance * Gaussian. That is, we're trying to approximate some set of data with a Gaussian, and we know * that Gaussian's variance but not its mean--the distribution represented by 'this' class lets * us keep a distrubtion over potential means. Because the distribution over means happens to * be itself Gaussian, there are actually two Gaussians involved whenever you're using this class. * * NOTE that even though we refer to the approximating distribution as having a fixed VARIANCE, * the constructors actually take in the fixed SIGMA (stdev) value. */public class MeanEstimateForFixedVarianceGaussian extends Gaussian { private double fixedSigma; // the fixed sigma of the distribution whose mean is // being approximated by this distribution /** * @param mean * @param sigma * * This constructs a new distribution. The mean and sigma given can be thought of as * priors. Larger sigmas indicate a smaller/weaker prior, and smaller sigmas indicate * a larger/stronger prior; in either case the prior is biased toward the given mean. */ public MeanEstimateForFixedVarianceGaussian(double mean, double sigma, double fixedSigma) { super(mean, sigma); this.fixedSigma = fixedSigma; } public MeanEstimateForFixedVarianceGaussian(Gaussian g, double fixedSigma) { this(g.getMean(), g.getSigma(), fixedSigma); } public MeanEstimateForFixedVarianceGaussian(MeanEstimateForFixedVarianceGaussian other) { this(other.getMean(), other.getSigma(), other.getFixedSigma()); } /* updateWithSample * **************** * This class represents a distribution over possible mean values for a separate Gaussian * distribution approximating a set of data. Given a sample from that data, this method * updates this distribution, to give a more accurate estimate of what the mean of the * fixed-variance Gaussian approximating the actual data might look like. * * Note: assumes the data is 1D. */ public void updateWithSample(double sample, double weight) { // debugging /*POSLOG*/ if (sample < 0) { System.out.println("Trouble is a brewing!\n You forgot to use positive log signal strength"); } if ((weight < 0.0) || (weight > 1.0)) throw new UnsupportedOperationException("Error: weight "+weight+" is out of range [0.0, 1.0]"); if (weight == 0) return; // no update to be done if we aren't counting this sample! double om = getMean(); // om = old mean double os = getSigma(); // os = old sigma double fs = fixedSigma; // fs = fixed sigma double numerator = ((1.0/(os*os))*om) + ((weight/(fs*fs))*sample); double denominator = (1.0/(os*os)) + (weight/(fs*fs)); setMean(numerator/denominator); double sigmaSqInv = (1.0/(os*os))+(weight/(fs*fs)); double sigmaSq = (1.0/sigmaSqInv); setSigma(Math.sqrt(sigmaSq)); } public void updateWithSample(double sample) { updateWithSample(sample, 1.0); } /* updateWithMultipleSamples * ************************* * In actuality there is a better way to update with many samples, since the mean of all the * samples is a sufficient statistic for the update, but I'm too lazy to write it out so * we're going to do it the easy way. */ public void updateWithMultipleSamples(double samples[]) { for(int i=0; i<samples.length; i++) { updateWithSample(samples[i]); } } public void updateWithMultipleSamples(double samples[], double weights[]) { if (samples.length != weights.length) throw new UnsupportedOperationException("Error: sample and weights arrays are not of the same lengths"); for(int i=0; i<samples.length; i++) { updateWithSample(samples[i], weights[i]); } } /* getLikelihoodOfSample * ********************* * THIS IS NOT THE SAME AS Gaussian.getHeightAt!!!!!!!!!! * * Gaussian.getHeightAt will return the height of this Gaussian at the location of the given sample. * This method integrates the distribution of the mean (this gaussian) with the fixed-variance * gaussian to determine the likelihood of the given sample. That is, it determines the likelihood * by "averaging" the likelihood of the sample given that the fixed-variance gaussian has mean x, where * x ranges over all possible values represented by 'this' Gaussian. */ public double getLikelihoodOfSample(double sample) { // create the fixed-variance gaussian, and place it at the peak of the mean distribution Gaussian g = new Gaussian(this.getMean(), this.getFixedSigma()); // convolve the gaussian with 'this' distribution to get the overall sample probability curve g.smoothBy(this); // determine the probability based upon the convolved, final curve return g.getHeightAt(sample); } /* Getters/setters * *************** * * */ public double getFixedSigma() { return fixedSigma; } public void setFixedSigma(double fs) { fixedSigma = fs; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -