Module 9 · Dependence, Regression, and Model Foundations Lesson 88 of 120
Residuals, MAE, MSE, and RMSE
Choosing an error metric that reflects the cost of mistakes.
Transcript
19 sentences · select one to jump thereCheck your understanding
Why does RMSE have the original output units while MSE does not?
Code lab
Run it yourself
The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.
/**
* Fintech Math Bootcamp · Lesson 088 of 120
* Residuals, MAE, MSE, and RMSE
* Module 09: Dependence, Regression, and Model Foundations
*
* Scenario: Choosing an error metric that reflects the cost of mistakes
* Rule: MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
*
* Try it: Why does RMSE have the original output units while MSE does not?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson088() {
const actual=[2,4,5,4,5],predicted=[2.8,3.4,4,4.6,5.2];
const residuals=actual.map((v,i)=>v-predicted[i]);
const mae=residuals.reduce((s,e)=>s+Math.abs(e),0)/residuals.length;
const mse=residuals.reduce((s,e)=>s+e*e,0)/residuals.length;
const result={residuals,mae,mse,rmse:Math.sqrt(mse)};
return result;
}
export const checkedResult = {"residuals":[-0.7999999999999998,0.6000000000000001,1,-0.5999999999999996,-0.20000000000000018],"mae":0.6399999999999999,"mse":0.47999999999999987,"rmse":0.6928203230275508};
// Run this file directly: npx tsx lessons/09-dependence-regression-and-model-foundations/088-residuals-mae-mse-and-rmse.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson088(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}"""
Fintech Math Bootcamp · Lesson 088 of 120
Residuals, MAE, MSE, and RMSE
Module 09: Dependence, Regression, and Model Foundations
Scenario: Choosing an error metric that reflects the cost of mistakes
Rule: MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
Try it: Why does RMSE have the original output units while MSE does not?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
"""
import json
import math
def lesson_088():
actual, predicted = [2, 4, 5, 4, 5], [2.8, 3.4, 4, 4.6, 5.2]
residuals = [a - p for a, p in zip(actual, predicted)]
absolute = 0
squared = 0
for e in residuals:
absolute += abs(e)
squared += e * e
mae = absolute / len(residuals)
mse = squared / len(residuals)
return {"residuals": residuals, "mae": mae, "mse": mse, "rmse": math.sqrt(mse)}
if __name__ == "__main__":
print(json.dumps(lesson_088(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}// Fintech Math Bootcamp - Lesson 088 of 120
// Residuals, MAE, MSE, and RMSE
// Module 09: Dependence, Regression, and Model Foundations
//
// Scenario: Choosing an error metric that reflects the cost of mistakes
// Rule: MAE=mean|e|; MSE=mean(e^2); RMSE=sqrt(MSE)
//
// Try it: Why does RMSE have the original output units while MSE does not?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
static Map<String, Object> lesson088() {
double[] actual = {2, 4, 5, 4, 5}, predicted = {2.8, 3.4, 4, 4.6, 5.2};
double[] residuals = new double[actual.length];
double absolute = 0, squared = 0;
for (int i = 0; i < actual.length; i++) {
double e = actual[i] - predicted[i];
residuals[i] = e;
absolute += Math.abs(e);
squared += e * e;
}
double mae = absolute / residuals.length, mse = squared / residuals.length;
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("residuals", residuals);
result.put("mae", mae);
result.put("mse", mse);
result.put("rmse", Math.sqrt(mse));
return result;
}
public static void main(String[] args) {
System.out.println(toJson(lesson088(), ""));
}
// Minimal JSON writer: two-space indent, whole numbers without a decimal point, NaN as null.
static String toJson(Object value, String indent) {
if (value == null) return "null";
if (value instanceof Boolean) return value.toString();
if (value instanceof Number) return formatNumber(((Number) value).doubleValue());
if (value instanceof String) return quote((String) value);
if (value instanceof double[]) {
List<Object> boxed = new ArrayList<Object>();
for (double d : (double[]) value) boxed.add(d);
return toJson(boxed, indent);
}
if (value instanceof Object[]) return toJson(Arrays.asList((Object[]) value), indent);
String inner = indent + " ";
StringBuilder out = new StringBuilder();
if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
if (map.isEmpty()) return "{}";
out.append("{\n");
int i = 0;
for (Map.Entry<?, ?> entry : map.entrySet()) {
out.append(inner).append(quote(entry.getKey().toString())).append(": ")
.append(toJson(entry.getValue(), inner));
out.append(++i < map.size() ? ",\n" : "\n");
}
return out.append(indent).append("}").toString();
}
List<?> list = (List<?>) value;
if (list.isEmpty()) return "[]";
out.append("[\n");
for (int i = 0; i < list.size(); i++) {
out.append(inner).append(toJson(list.get(i), inner));
out.append(i + 1 < list.size() ? ",\n" : "\n");
}
return out.append(indent).append("]").toString();
}
static String formatNumber(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) return "null";
if (x == Math.rint(x) && Math.abs(x) < 1e15) return Long.toString((long) x);
return Double.toString(x);
}
static String quote(String s) {
StringBuilder out = new StringBuilder("\"");
for (char c : s.toCharArray()) {
if (c == '"' || c == '\\') out.append('\\').append(c);
else if (c == '\n') out.append("\\n");
else if (c < 0x20) out.append(String.format("\\u%04x", (int) c));
else out.append(c);
}
return out.append('"').toString();
}
}
No browser runner for Java yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}// Fintech Math Bootcamp · Lesson 088 of 120
// Residuals, MAE, MSE, and RMSE
// Module 09: Dependence, Regression, and Model Foundations
//
// Scenario: Choosing an error metric that reflects the cost of mistakes
// Rule: MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
//
// Try it: Why does RMSE have the original output units while MSE does not?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
package main
import (
"encoding/json"
"fmt"
"math"
)
type Lesson088Result struct {
Residuals []float64 `json:"residuals"`
MAE float64 `json:"mae"`
MSE float64 `json:"mse"`
RMSE float64 `json:"rmse"`
}
func lesson088() Lesson088Result {
actual := []float64{2, 4, 5, 4, 5}
predicted := []float64{2.8, 3.4, 4, 4.6, 5.2}
residuals := make([]float64, len(actual))
absolute, squared := 0.0, 0.0
for i, v := range actual {
e := v - predicted[i]
residuals[i] = e
absolute += math.Abs(e)
squared += e * e
}
n := float64(len(residuals))
mse := squared / n
return Lesson088Result{Residuals: residuals, MAE: absolute / n, MSE: mse, RMSE: math.Sqrt(mse)}
}
func main() {
out, err := json.MarshalIndent(lesson088(), "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
No browser runner for Go yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}// Fintech Math Bootcamp · Lesson 088 of 120
// Residuals, MAE, MSE, and RMSE
// Module 09: Dependence, Regression, and Model Foundations
//
// Scenario: Choosing an error metric that reflects the cost of mistakes
// Rule: MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
//
// Try it: Why does RMSE have the original output units while MSE does not?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
// A minimal JSON value, enough to print this lesson's result.
struct Json {
enum class Kind { Null, Bool, Number, String, Array, Object };
Kind kind = Kind::Null;
bool flag = false;
double number = 0.0;
std::string text;
std::vector<std::string> keys; // object keys, parallel to items
std::vector<Json> items; // array elements or object values
Json() = default;
Json(bool value) : kind(Kind::Bool), flag(value) {}
Json(int value) : kind(Kind::Number), number(value) {}
Json(double value) : kind(Kind::Number), number(value) {}
Json(const char* value) : kind(Kind::String), text(value) {}
Json(const std::string& value) : kind(Kind::String), text(value) {}
Json(const std::vector<double>& values) : kind(Kind::Array) {
for (double v : values) items.push_back(Json(v));
}
};
Json jsonArray(const std::vector<Json>& values) {
Json array;
array.kind = Json::Kind::Array;
array.items = values;
return array;
}
Json jsonObject(const std::vector<std::pair<std::string, Json>>& fields) {
Json object;
object.kind = Json::Kind::Object;
for (const auto& field : fields) {
object.keys.push_back(field.first);
object.items.push_back(field.second);
}
return object;
}
// Shortest decimal form that reads back as the same double.
std::string formatNumber(double x) {
if (!std::isfinite(x)) return "null";
char buffer[32];
if (x == std::floor(x) && std::fabs(x) < 1e15) {
std::snprintf(buffer, sizeof buffer, "%.0f", x);
return buffer;
}
for (int precision = 1; precision <= 17; ++precision) {
std::snprintf(buffer, sizeof buffer, "%.*g", precision, x);
if (std::strtod(buffer, nullptr) == x) break;
}
return buffer;
}
std::string quote(const std::string& s) {
std::string out = "\"";
for (char c : s) {
if (c == '"' || c == '\\') { out += '\\'; out += c; }
else if (c == '\n') out += "\\n";
else out += c;
}
return out + "\"";
}
std::string toJson(const Json& value, const std::string& indent = "") {
switch (value.kind) {
case Json::Kind::Null: return "null";
case Json::Kind::Bool: return value.flag ? "true" : "false";
case Json::Kind::Number: return formatNumber(value.number);
case Json::Kind::String: return quote(value.text);
default: break;
}
const bool isObject = value.kind == Json::Kind::Object;
if (value.items.empty()) return isObject ? "{}" : "[]";
const std::string inner = indent + " ";
std::string out = isObject ? "{\n" : "[\n";
for (std::size_t i = 0; i < value.items.size(); ++i) {
out += inner;
if (isObject) out += quote(value.keys[i]) + ": ";
out += toJson(value.items[i], inner);
out += i + 1 < value.items.size() ? ",\n" : "\n";
}
return out + indent + (isObject ? "}" : "]");
}
Json lesson088() {
const std::vector<double> actual = {2, 4, 5, 4, 5}, predicted = {2.8, 3.4, 4, 4.6, 5.2};
std::vector<double> residuals;
double absolute = 0.0, squared = 0.0;
for (std::size_t i = 0; i < actual.size(); ++i) {
const double e = actual[i] - predicted[i];
residuals.push_back(e);
absolute += std::fabs(e);
squared += e * e;
}
const double n = static_cast<double>(residuals.size());
const double mae = absolute / n, mse = squared / n;
return jsonObject({
{"residuals", residuals},
{"mae", mae},
{"mse", mse},
{"rmse", std::sqrt(mse)},
});
}
int main() {
std::cout << toJson(lesson088()) << '\n';
return 0;
}
No browser runner for C++ yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}// Fintech Math Bootcamp · Lesson 088 of 120
// Residuals, MAE, MSE, and RMSE
// Module 09: Dependence, Regression, and Model Foundations
//
// Scenario: Choosing an error metric that reflects the cost of mistakes
// Rule: MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
//
// Try it: Why does RMSE have the original output units while MSE does not?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
/// A minimal JSON value, enough to print this lesson's result.
#[allow(dead_code)]
enum Json {
Null,
Bool(bool),
Num(f64),
Str(String),
Arr(Vec<Json>),
Obj(Vec<(String, Json)>),
}
#[allow(dead_code)]
impl Json {
fn obj(fields: Vec<(&str, Json)>) -> Json {
Json::Obj(fields.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
}
fn nums(values: &[f64]) -> Json {
Json::Arr(values.iter().map(|&v| Json::Num(v)).collect())
}
/// Pretty-prints with two-space indentation.
fn pretty(&self, indent: &str) -> String {
let inner = format!("{} ", indent);
match self {
Json::Null => "null".to_string(),
Json::Bool(b) => b.to_string(),
Json::Num(x) => format_number(*x),
Json::Str(s) => quote(s),
Json::Arr(items) if items.is_empty() => "[]".to_string(),
Json::Obj(fields) if fields.is_empty() => "{}".to_string(),
Json::Arr(items) => {
let body: Vec<String> = items
.iter()
.map(|v| format!("{}{}", inner, v.pretty(&inner)))
.collect();
format!("[\n{}\n{}]", body.join(",\n"), indent)
}
Json::Obj(fields) => {
let body: Vec<String> = fields
.iter()
.map(|(k, v)| format!("{}{}: {}", inner, quote(k), v.pretty(&inner)))
.collect();
format!("{{\n{}\n{}}}", body.join(",\n"), indent)
}
}
}
}
fn format_number(x: f64) -> String {
if !x.is_finite() {
"null".to_string()
} else if x == x.trunc() && x.abs() < 1e15 {
format!("{}", x as i64)
} else {
format!("{}", x)
}
}
fn quote(s: &str) -> String {
let mut out = String::from("\"");
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
c => out.push(c),
}
}
out.push('"');
out
}
fn lesson_088() -> Json {
let actual = [2.0_f64, 4.0, 5.0, 4.0, 5.0];
let predicted = [2.8_f64, 3.4, 4.0, 4.6, 5.2];
let residuals: Vec<f64> = actual.iter().zip(predicted.iter()).map(|(a, p)| a - p).collect();
let n = residuals.len() as f64;
let mae = residuals.iter().fold(0.0_f64, |s, e: &f64| s + e.abs()) / n;
let mse: f64 = residuals.iter().fold(0.0_f64, |s, e| s + e * e) / n;
Json::obj(vec![
("residuals", Json::nums(&residuals)),
("mae", Json::Num(mae)),
("mse", Json::Num(mse)),
("rmse", Json::Num(mse.sqrt())),
])
}
fn main() {
println!("{}", lesson_088().pretty(""));
}
No browser runner for Rust yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}// Fintech Math Bootcamp · Lesson 088 of 120
// Residuals, MAE, MSE, and RMSE
// Module 09: Dependence, Regression, and Model Foundations
//
// Scenario: Choosing an error metric that reflects the cost of mistakes
// Rule: MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
//
// Try it: Why does RMSE have the original output units while MSE does not?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
var options = new JsonSerializerOptions { WriteIndented = true };
Console.WriteLine(JsonSerializer.Serialize(Lesson088(), options));
static object Lesson088()
{
double[] actual = { 2, 4, 5, 4, 5 }, predicted = { 2.8, 3.4, 4, 4.6, 5.2 };
double[] residuals = actual.Select((v, i) => v - predicted[i]).ToArray();
double mae = residuals.Aggregate(0.0, (s, e) => s + Math.Abs(e)) / residuals.Length;
double mse = residuals.Aggregate(0.0, (s, e) => s + e * e) / residuals.Length;
return new
{
residuals,
mae,
mse,
rmse = Math.Sqrt(mse),
};
}
No browser runner for C# yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"residuals": [
-0.7999999999999998,
0.6000000000000001,
1,
-0.5999999999999996,
-0.20000000000000018
],
"mae": 0.6399999999999999,
"mse": 0.47999999999999987,
"rmse": 0.6928203230275508
}Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
MAE=mean|e|; MSE=mean(e²); RMSE=√MSE