Accelerating the pace of engineering and science

# loss

Regression error

## Syntax

L = loss(ens,X,Y)
L = loss(ens,X,Y,Name,Value)

## Description

L = loss(ens,X,Y) returns the mean squared error between the predictions of ens to the data in X, compared to the true responses Y.

L = loss(ens,X,Y,Name,Value) computes the error in prediction with additional options specified by one or more Name,Value pair arguments.

## Input Arguments

 ens A regression ensemble created with fitensemble, or the compact method. X A matrix of predictor values. Each column of X represents one variable, and each row represents one observation. NaN values in X are taken to be missing values. Observations with all missing values for X are not used in the calculation of loss. Y A numeric column vector with the same number of rows as X. Each entry in Y is the response to the data in the corresponding row of X. NaN values in Y are taken to be missing values. Observations with missing values for Y are not used in the calculation of loss.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

 'learners' Indices of weak learners in the ensemble ranging from 1 to ens.NumTrained. oobEdge uses only these learners for calculating loss. Default: 1:NumTrained 'lossfun' Function handle for loss function, or the string 'mse', meaning mean squared error. If you pass a function handle fun, loss calls it as `fun(Y,Yfit,W)` where Y, Yfit, and W are numeric vectors of the same length. Y is the observed response.Yfit is the predicted response.W is the observation weights. The returned value fun(Y,Yfit,W) should be a scalar. Default: 'mse' 'mode' String representing the meaning of the output L: 'ensemble' — L is a scalar value, the loss for the entire ensemble.'individual' — L is a vector with one element per trained learner.'cumulative' — L is a vector in which element J is obtained by using learners 1:J from the input list of learners. Default: 'ensemble' 'UseObsForLearner' A logical matrix of size N-by-NumTrained, where N is the number of observations in ens.X, and NumTrained is the number of weak learners. When UseObsForLearner(I,J) is true, predict uses learner J in predicting observation I. Default: true(N,NumTrained) 'weights' Numeric vector of observation weights with the same number of elements as Y. The formula for loss with weights is in Weighted Mean Squared Error. Default: ones(size(Y))

## Output Arguments

 L Weighted mean squared error of predictions. The formula for loss is in Weighted Mean Squared Error.

## Definitions

### Weighted Mean Squared Error

Let n be the number of rows of data, xj be the jth row of data, yj be the true response to xj, and let f(xj) be the response prediction of ens to xj. Let w be the vector of weights (all one by default).

First the weights are divided by their sum so they add to one: www. The mean squared error L is

$L=\sum _{j=1}^{n}{w}_{j}{\left(f\left({x}_{j}\right)-{y}_{j}\right)}^{2}.$

## Examples

Find the loss of an ensemble predictor of the carsmall data to find MPG as a function of engine displacement, horsepower, and vehicle weight:

```load carsmall
X = [Displacement Horsepower Weight];
ens = fitensemble(X,MPG,'LSBoost',100,'Tree');
L = loss(ens,X,MPG)

L =
4.3904```