Module 2 · Financial Arithmetic, Time Value, and Returns Lesson 20 of 120
Return Annualization and Deannualization
An annual equivalent is not a prediction.
Transcript
21 sentences · select one to jump thereCheck your understanding
Is an annualized historical return a forecast?
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 020 of 120
* Return Annualization and Deannualization
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: An annual equivalent is not a prediction
* Rule: annual = (1+holdingReturn)^(1/years) − 1
*
* Try it: Is an annualized historical return a forecast?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson020() {
const holdingReturn = 0.18, years = 2;
const annual = (1 + holdingReturn) ** (1 / years) - 1;
const recovered = (1 + annual) ** years - 1;
const result = {annual, recovered};
return result;
}
export const checkedResult = {"annual":0.08627804912002146,"recovered":0.17999999999999972};
// Run this file directly: npx tsx lessons/02-financial-arithmetic-time-value-and-returns/020-return-annualization-and-deannualization.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson020(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}"""
Fintech Math Bootcamp · Lesson 020 of 120
Return Annualization and Deannualization
Module 02: Financial Arithmetic, Time Value, and Returns
Scenario: An annual equivalent is not a prediction
Rule: annual = (1+holdingReturn)^(1/years) − 1
Try it: Is an annualized historical return a forecast?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson020() -> dict:
holding_return, years = 0.18, 2
annual = (1 + holding_return) ** (1 / years) - 1
recovered = (1 + annual) ** years - 1
result = {"annual": annual, "recovered": recovered}
return result
if __name__ == "__main__":
print(json.dumps(lesson020(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}/**
* Fintech Math Bootcamp · Lesson 020 of 120
* Return Annualization and Deannualization
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: An annual equivalent is not a prediction
* Rule: annual = (1+holdingReturn)^(1/years) − 1
*
* Try it: Is an annualized historical return a forecast?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
public class Main {
static final class Annualized {
final double annual;
final double recovered;
Annualized(double annual, double recovered) {
this.annual = annual;
this.recovered = recovered;
}
}
static Annualized lesson020() {
double holdingReturn = 0.18, years = 2;
double annual = Math.pow(1 + holdingReturn, 1 / years) - 1;
double recovered = Math.pow(1 + annual, years) - 1;
Annualized result = new Annualized(annual, recovered);
return result;
}
public static void main(String[] args) {
Annualized result = lesson020();
System.out.println(object("annual", num(result.annual), "recovered", num(result.recovered)));
}
// Formats a double the way JSON.stringify does: whole numbers without ".0", null for NaN or infinity.
static String num(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) return "null";
if (x == Math.rint(x) && Math.abs(x) < 1e15) return String.valueOf((long) x);
return String.valueOf(x);
}
static String quote(String text) {
return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
// Indents an already formatted JSON value by one level.
static String nested(String json) {
return json.replace("\n", "\n ");
}
static String object(String... keysAndValues) {
if (keysAndValues.length == 0) return "{}";
StringBuilder out = new StringBuilder("{");
for (int i = 0; i < keysAndValues.length; i += 2) {
out.append(i == 0 ? "\n " : ",\n ")
.append(quote(keysAndValues[i])).append(": ").append(nested(keysAndValues[i + 1]));
}
return out.append("\n}").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
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}// Fintech Math Bootcamp · Lesson 020 of 120
// Return Annualization and Deannualization
// Module 02: Financial Arithmetic, Time Value, and Returns
//
// Scenario: An annual equivalent is not a prediction
// Rule: annual = (1+holdingReturn)^(1/years) − 1
//
// Try it: Is an annualized historical return a forecast?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
//
// Run it: go run main.go
package main
import (
"encoding/json"
"fmt"
"math"
)
type Annualized struct {
Annual float64 `json:"annual"`
Recovered float64 `json:"recovered"`
}
func lesson020() Annualized {
holdingReturn, years := 0.18, 2.0
annual := math.Pow(1+holdingReturn, 1/years) - 1
recovered := math.Pow(1+annual, years) - 1
result := Annualized{Annual: annual, Recovered: recovered}
return result
}
func main() {
printJSON(lesson020())
}
// printJSON prints a value as JSON indented with two spaces, like JSON.stringify(value, null, 2).
func printJSON(value any) {
out, err := json.MarshalIndent(value, "", " ")
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
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}/**
* Fintech Math Bootcamp · Lesson 020 of 120
* Return Annualization and Deannualization
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: An annual equivalent is not a prediction
* Rule: annual = (1+holdingReturn)^(1/years) − 1
*
* Try it: Is an annualized historical return a forecast?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: g++ -std=c++17 -o main main.cpp && ./main
*/
#include <charconv>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
// Formats a double the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
std::string num(double x) {
if (!std::isfinite(x)) return "null";
char buffer[32];
auto [end, error] = std::to_chars(buffer, buffer + sizeof buffer, x);
(void)error;
return std::string(buffer, end);
}
std::string quote(const std::string& text) {
std::string out = "\"";
for (char c : text) {
if (c == '"' || c == '\\') out += '\\';
out += c;
}
return out + "\"";
}
// Indents an already formatted JSON value by one level.
std::string nested(const std::string& json) {
std::string out;
for (char c : json) {
out += c;
if (c == '\n') out += " ";
}
return out;
}
std::string object(const std::vector<std::pair<std::string, std::string>>& fields) {
if (fields.empty()) return "{}";
std::string out = "{";
for (std::size_t i = 0; i < fields.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += quote(fields[i].first) + ": " + nested(fields[i].second);
}
return out + "\n}";
}
struct Annualized {
double annual;
double recovered;
};
Annualized lesson020() {
const double holdingReturn = 0.18, years = 2;
const double annual = std::pow(1 + holdingReturn, 1 / years) - 1;
const double recovered = std::pow(1 + annual, years) - 1;
const Annualized result{annual, recovered};
return result;
}
int main() {
const Annualized result = lesson020();
std::cout << object({{"annual", num(result.annual)}, {"recovered", num(result.recovered)}}) << "\n";
}
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
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}//! Fintech Math Bootcamp · Lesson 020 of 120
//! Return Annualization and Deannualization
//! Module 02: Financial Arithmetic, Time Value, and Returns
//!
//! Scenario: An annual equivalent is not a prediction
//! Rule: annual = (1+holdingReturn)^(1/years) − 1
//!
//! Try it: Is an annualized historical return a forecast?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
struct Annualized {
annual: f64,
recovered: f64,
}
fn lesson020() -> Annualized {
let (holding_return, years): (f64, f64) = (0.18, 2.0);
let annual = (1.0 + holding_return).powf(1.0 / years) - 1.0;
let recovered = (1.0 + annual).powf(years) - 1.0;
let result = Annualized { annual, recovered };
result
}
fn main() {
let result = lesson020();
println!(
"{}",
object(&[("annual", num(result.annual)), ("recovered", num(result.recovered))])
);
}
/// Formats a number the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
fn num(x: f64) -> String {
if x.is_finite() {
format!("{}", x)
} else {
"null".to_string()
}
}
fn quote(text: &str) -> String {
format!("\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""))
}
/// Indents an already formatted JSON value by one level.
fn nested(json: &str) -> String {
json.replace('\n', "\n ")
}
fn object(fields: &[(&str, String)]) -> String {
if fields.is_empty() {
return "{}".to_string();
}
let lines: Vec<String> = fields
.iter()
.map(|(key, value)| format!(" {}: {}", quote(key), nested(value)))
.collect();
format!("{{\n{}\n}}", lines.join(",\n"))
}
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
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}/**
* Fintech Math Bootcamp · Lesson 020 of 120
* Return Annualization and Deannualization
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: An annual equivalent is not a prediction
* Rule: annual = (1+holdingReturn)^(1/years) − 1
*
* Try it: Is an annualized historical return a forecast?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/annualization-deannualization/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: dotnet run (inside a console project that holds this Program.cs)
*/
using System.Text.Json;
var jsonOptions = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Console.WriteLine(JsonSerializer.Serialize(Lesson020(), jsonOptions));
static Annualized Lesson020()
{
double holdingReturn = 0.18, years = 2;
var annual = Math.Pow(1 + holdingReturn, 1 / years) - 1;
var recovered = Math.Pow(1 + annual, years) - 1;
var result = new Annualized(annual, recovered);
return result;
}
record Annualized(double Annual, double Recovered);
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
{
"annual": 0.08627804912002146,
"recovered": 0.17999999999999972
}Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
annual = (1+holdingReturn)^(1/years) − 1