Java tutorial
/* * (c) 2005 David B. Bracewell * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.davidbracewell.math.distribution; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import java.util.Objects; /** * <p> * Calculates the probability of a REAL value using a Normal Distribution that * was built from a Continuous Estimator. The default probability is 0 and * default log probability is log2(Double.MIN_VALUE). * </p> * * @author David B. Bracewell */ public class NormalDistribution extends Distribution { private static final long serialVersionUID = 4801944404621040636L; private final double mean; private final double stddev; private final double var; private final RandomGenerator random = new Well19937c(); private static final double TWO_PI = 2d * Math.PI; /** * Default Constructor * * @param mean The mean of the distribution * @param stddev The standard deviation of the distribution */ public NormalDistribution(double mean, double stddev) { super(0d); this.mean = mean; this.stddev = stddev; this.var = Math.pow(stddev, 2); } @Override public double probability(double x) { return 1d / Math.sqrt(TWO_PI * var) * Math.exp(-Math.pow(x - mean, 2) / (2 * var)); } @Override public String toString() { return "NormalDistribution [mean=" + mean + ", stddev=" + stddev + "]"; } @Override public double logProbability(double x) { return Math.log(probability(x)); } @Override public int hashCode() { return Objects.hash(mean, stddev); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } final NormalDistribution other = (NormalDistribution) obj; return Objects.equals(this.mean, other.mean) && Objects.equals(this.stddev, other.stddev); } @Override public double sample() { return 0; } /** * Gets mean. * * @return the mean */ public double getMean() { return mean; } /** * Gets stddev. * * @return the stddev */ public double getStandardDeviation() { return stddev; } /** * Gets var. * * @return the var */ public double getVar() { return var; } }//END OF NormalDistribution