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

maths.errorfunctions.MSEVectorFunction Maven / Gradle / Ivy

The newest version!
package maths.errorfunctions;

import datastructs.I2DDataSet;
import datastructs.IVector;
import datasets.VectorDouble;
import maths.functions.IRegularizerFunction;
import maths.functions.IVectorRealFunction;

/**
 * The Mean Square Error or MSE is defined as
 * MSE = 1/N Sum_{i = 1}^N (y_i - \hat{y}_i)^2
 *
 * The \hat{y} value is modeled after the IVectorRealFunction passed
 * to the object when instantiated
 */
public class MSEVectorFunction implements IVectorErrorRealFunction {

    /**
     * Constructor
     */
    public MSEVectorFunction(IVectorRealFunction> hypothesis ){

        this.hypothesis = hypothesis;
        this.regularizerFunction = null;
    }

    /**
     * Constructor
     */
    public MSEVectorFunction(IVectorRealFunction> hypothesis, IRegularizerFunction regularizerFunction ){

        this.hypothesis = hypothesis;
        this.regularizerFunction = regularizerFunction;
    }

    /**
     * Evaluate the error function using the given data, labels
     * @param data
     * @param labels
     * @return
     */
    @Override
    public  double evaluate(DataSetType data, VectorDouble labels){

        if(data.m() != labels.size()){
            throw new IllegalArgumentException("Invalid number of data points and labels vector size");
        }

        double result = 0.0;

        for(int rowIdx=0; rowIdx VectorDouble gradients(DataSetType data, VectorDouble labels){


        VectorDouble gradients = new VectorDouble(this.hypothesis.numCoeffs(), 0.0);

        for(int rowIdx=0; rowIdx hypothesisGrads = this.hypothesis.coeffGradients(row);

            for(int coeff=0; coeff> hypothesis;
    IRegularizerFunction regularizerFunction;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy