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

gov.sandia.cognition.learning.algorithm.ensemble.AveragingEnsemble Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:            AveragingEnsemble.java
 * Authors:         Justin Basilico
 * Project:         Cognitive Foundry
 * 
 * Copyright 2011 Cognitive Foundry. All rights reserved.
 */

package gov.sandia.cognition.learning.algorithm.ensemble;

import gov.sandia.cognition.evaluator.Evaluator;
import gov.sandia.cognition.learning.function.regression.Regressor;
import java.util.ArrayList;
import java.util.List;

/**
 * An ensemble for regression functions that averages together the output value
 * of each ensemble member to get the final output.
 *
 * @param   
 *      The type of input the ensemble can take. Passed to each ensemble
 *      member to produce an output.
 * @param   
 *      The type of members of this ensemble.
 * @author  Justin Basilico
 * @since   3.3.3
 */
public class AveragingEnsemble>
    extends AbstractUnweightedEnsemble
    implements Regressor
{

    /**
     * Creates a new, empty {@code AdditiveEnsemble}.
     */
    public AveragingEnsemble()
    {
        this(new ArrayList());
    }

    /**
     * Creates a new {@code AdditiveEnsemble} with the given
     *
     * @param   members
     *      The list of ensemble members.
     */
    public AveragingEnsemble(
        final List members)
    {
        super(members);
    }

    @Override
    public Double evaluate(
        final InputType input)
    {
        // Return the input evaluated as a double.
        return this.evaluateAsDouble(input);
    }

    @Override
    public double evaluateAsDouble(
        final InputType input)
    {
        // Compute the sum and the count of non-zero vailest.
        double sum = 0.0;
        int count = 0;
        for (MemberType member : this.getMembers())
        {
            // Compute the estimate of the member.
            final Number value = member.evaluate(input);

            if (value != null)
            {
                sum += value.doubleValue();
                count++;
            }
            // else - The member had no value.
        }

        // Return the average.
        if (count <= 0)
        {
            return 0.0;
        }
        else
        {
            return sum / count;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy