package beast.math.distributions;


import org.apache.commons.math.distribution.ContinuousDistribution;
import org.apache.commons.math.distribution.NormalDistributionImpl;

import beast.core.Description;
import beast.core.Input;
import beast.core.parameter.RealParameter;



@Description("Normal distribution.  f(x) = frac{1}{\\sqrt{2\\pi\\sigma^2}} e^{ -\\frac{(x-\\mu)^2}{2\\sigma^2} } " +
        "If the input x is a multidimensional parameter, each of the dimensions is considered as a " +
        "separate independent component.")
public class Normal extends ParametricDistribution {
    public Input<RealParameter> meanInput = new Input<RealParameter>("mean", "mean of the normal distribution, defaults to 0");
    public Input<RealParameter> sigmaInput = new Input<RealParameter>("sigma", "variance of the normal distribution, defaults to 1");

    static org.apache.commons.math.distribution.NormalDistribution dist = new NormalDistributionImpl(0, 1);

    @Override
    public void initAndValidate() {
        refresh();
    }

    /**
     * make sure internal state is up to date *
     */
    void refresh() {
        double fMean;
        double fSigma;
        if (meanInput.get() == null) {
            fMean = 0;
        } else {
            fMean = meanInput.get().getValue();
        }
        if (sigmaInput.get() == null) {
            fSigma = 1;
        } else {
            fSigma = sigmaInput.get().getValue();
        }
        dist.setMean(fMean);
        dist.setStandardDeviation(fSigma);
    }

    @Override
    public ContinuousDistribution getDistribution() {
        refresh();
        return dist;
    }

    @Override
    public double getMean() {
        if (meanInput.get() == null) {
        	return offsetInput.get();
        } else {
        	return offsetInput.get() + meanInput.get().getValue();
        }
    }
} // class Normal

@@ -222,16 +222,50 @@
 .Input;%0A
+import beast.core.Input.Validate;%0A
 import b
@@ -299,16 +299,16 @@
 ameter;%0A
-
 %0A%0A%0A@Desc
@@ -879,16 +879,174 @@
  to 1%22);
+%0A    public Input%3CRealParameter%3E tauInput = new Input%3CRealParameter%3E(%22tau%22, %22precission of the normal distribution, defaults to 1%22, Validate.XOR, sigmaInput);
 %0A%0A    st
@@ -1528,39 +1528,76 @@
 == null) %7B%0A     
+   %09if (tauInput.get() == null) %7B%0A
        
+ %09%09
 fSigma = 1;%0A    
@@ -1592,16 +1592,112 @@
 ma = 1;%0A
+        %09%7D else %7B%0A                fSigma = Math.sqrt(1.0/tauInput.get().getValue());%0A        %09%7D%0A
         

add precission input to Normal distribution