# Utility Functions

The FactorGraph provides several utility functions to evaluate and compare obtained results.

#### The WLS results

The function provides the estimate obtained by the weighted least-squares (WLS) method and root mean square error (RMSE), the mean absolute error (MAE) and the weighted residual sum of squares (WRSS) error metrics evaluated according to the WLS solutions. These results can be used to compare results obtained by the GBP algorithm.

`exact = wls(gbp)`

The function returns the composite type `WeightedLeastSquares`

with fields `estimate`

, `rmse`

, `mae`

, `wrss`

. Note that results are obtained according to variables `ContinuousSystem.coefficient`

, `ContinuousSystem.observation`

and `ContinuousSystem.variance`

.

#### The GBP error metrics

The package provides the function to obtain RMSE, MAE, and WRSS error metrics of the GBP algorithm.

`evaluation = errorMetric(gbp)`

The function returns the composite type `ErrorMetric`

with fields `rmse`

, `mae`

, `wrss`

. Further, passing the composite type `WeightedLeastSquares`

, we obtained additional fields `rmseGBPWLS`

and `maeGBPWLS`

that determine the distance between the GBP estimate and WLS estimate.

`evaluation = errorMetric(gbp, exact)`

The function returns the composite type `ErrorMetricWiden`

with fields `rmse`

, `mae`

, `wrss`

, `rmseGBPWLS`

, `maeGBPWLS`

.

#### Error metrics

The root mean square error, the mean absolute error and the weighted residual sum of squares are evaluated according to:

\[ \begin{aligned} \text{rmse} = \sqrt {\cfrac{\sum_{i=1}^m \left[z_i - h_i(\hat{\mathbf x}) \right]^2}{m}}; \quad \text{mae} = \cfrac{\sum_{i=1}^m \left|z_i - h_i(\hat{\mathbf x}) \right|}{m}; \quad \text{wrss} = \sum_{i=1}^m \cfrac{\left[z_i - h_i(\hat{\mathbf x}) \right]^2}{v_i}, \end{aligned}\]

where $m$ denotes the number of observations, $z_i$ is observation value, $v_i$ is observation variance, and corresponding equation $h_i(\hat{\mathbf x})$ is evaluated at the point $\hat{\mathbf x}$ obtained using the GBP or WLS algorithm. Note, `wrss`

is the value of the objective function of the optimization problem we are solving.

Fields `rmseGBPWLS`

and `maeGBPWLS`

determine distance beetwen the GBP estimate $\hat{x}_{\text{gbp},i}$ and WLS estimate $\hat{x}_{\text{wls},i}$, where root mean square error and mean absolute error are obtained using:

\[ \begin{aligned} \text{rmse} = \sqrt {\cfrac{\sum_{i=1}^n \left[\hat{x}_{\text{wls},i} - \hat{x}_{{\text{gbp}},i}) \right]^2}{n}}; \quad \text{mae} = {\cfrac{\sum_{i=1}^n \left|\hat{x}_{\text{wls},i} - \hat{x}_{{\text{gbp}},i}) \right|}{n}}, \end{aligned}\]

where $n$ is the number of state variables.