All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.enterprisemath.math.prediction.CategoryPredictionResult Maven / Gradle / Ivy

The newest version!
package com.enterprisemath.math.prediction;

import com.enterprisemath.utils.DomainUtils;
import com.enterprisemath.utils.ValidationUtils;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * Definition of result for category prediction.
 *
 * @author radek.hecl
 */
public class CategoryPredictionResult {

    /**
     * Builder class.
     */
    public static class Builder {

        /**
         * Predictions.
         * This is map where key is category and value is probability that tested object falls into that category.
         */
        private Map predictions = new HashMap();

        /**
         * Sets predictions.
         * This is map where key is category and value is probability that tested object falls into that category.
         *
         * @param predictions predictions
         * @return this instance
         */
        public Builder setPredictions(Map predictions) {
            this.predictions = DomainUtils.softCopyMap(predictions);
            return this;
        }

        /**
         * Adds prediction. Key is category and value is probability for that category.
         *
         * @param category category
         * @param probabilty probability, must be in interval [0, 1]
         * @return this instance
         */
        public Builder addPrediction(String category, double probabilty) {
            this.predictions.put(category, probabilty);
            return this;
        }

        /**
         * Builds the result object.
         *
         * @return created object
         */
        public CategoryPredictionResult build() {
            return new CategoryPredictionResult(this);
        }
    }

    /**
     * Predictions.
     * This is map where key is category and value is probability that tested object falls into that category.
     */
    private Map predictions;

    /**
     * Creates new instance.
     *
     * @param builder builder object
     */
    public CategoryPredictionResult(Builder builder) {
        predictions = DomainUtils.softCopyUnmodifiableMap(builder.predictions);
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotNullMap(predictions, "predictions cannot have null element");
    }

    /**
     * Returns whole predictions.
     * This is map where key is category and value is probability that tested object falls into that category.
     *
     * @return predictions
     */
    public Map getPredictions() {
        return predictions;
    }

    /**
     * Returns predicted category. If there is no category which satisfies
     * the required confidence, then returns null. Fundamental meaning for null
     * is that system simply doesn't know.
     *
     * @param confidence required confidence, must be in interval [0, 1]
     * @return predicted category or null if system doesn't know
     */
    public String getPredictedCategory(double confidence) {
        String res = null;
        double maxProb = 0;
        for (String category : predictions.keySet()) {
            if (predictions.get(category) > maxProb) {
                maxProb = predictions.get(category);
                if (maxProb >= confidence) {
                    res = category;
                }
            }
        }
        return res;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy