com.davidbracewell.math.distribution.Multinomial.java Source code

Java tutorial

Introduction

Here is the source code for com.davidbracewell.math.distribution.Multinomial.java

Source

/*
 * (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 com.google.common.base.Preconditions;
import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.Well19937c;
import org.apache.mahout.math.map.OpenIntDoubleHashMap;

import java.util.Objects;

/**
 * <p>
 * A Multinomial probability distribution to determine the probability of a
 * discrete event.
 * </p>
 *
 * @author David B. Bracewell
 */
public class Multinomial extends Distribution {

    private static final long serialVersionUID = 2307484424728502704L;
    protected final OpenIntDoubleHashMap probabilities;
    protected final RandomGenerator random = new Well19937c();

    /**
     * Default Constructor
     *
     * @param probabilities The probability of each of the discrete events
     * @param defaultProb   The default probability
     */
    public Multinomial(OpenIntDoubleHashMap probabilities, double defaultProb) {
        super(defaultProb);
        this.probabilities = Preconditions.checkNotNull(probabilities);
        this.probabilities.trimToSize();
    }

    @Override
    public double probability(double x) {
        if (probabilities.containsKey((int) x)) {
            return probabilities.get((int) x);
        }
        return defaultProbability;
    }

    @Override
    public String toString() {
        return "Multinomial{" + probabilities + "}";
    }

    @Override
    public int hashCode() {
        return Objects.hash(probabilities, defaultProbability);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Multinomial other = (Multinomial) obj;
        return Objects.equals(this.probabilities, other.probabilities)
                && Objects.equals(this.defaultProbability, other.defaultProbability);
    }

    @Override
    public double sample() {
        double v = random.nextDouble();
        double total = 0;
        int[] keys = probabilities.keys().elements();
        for (int i : keys) {
            total += probabilities.get(i);
            if (v < total) {
                return i;
            }
        }
        return probabilities.get(keys[keys.length - 1]);
    }

}//END OF Multinomial