Module 2 · Financial Arithmetic, Time Value, and Returns Lesson 15 of 120
Cash-Flow Timelines and Net Present Value
A project whose future receipts must repay its initial cost.
Transcript
22 sentences · select one to jump thereCheck your understanding
Why is the initial −1,000 not discounted?
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 015 of 120
* Cash-Flow Timelines and Net Present Value
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: A project whose future receipts must repay its initial cost
* Rule: NPV = Σ CFₜ/(1+r)ᵗ
*
* Try it: Why is the initial −1,000 not discounted?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson015() {
const cashFlows = [-1000, 600, 600];
const rate = 0.10;
const contributions = cashFlows.map((cf, t) =>
cf / (1 + rate) ** t);
const result = contributions.reduce((a, x) => a + x, 0);
return result;
}
export const checkedResult = 41.32231404958662;
// Run this file directly: npx tsx lessons/02-financial-arithmetic-time-value-and-returns/015-cash-flow-timelines-and-net-present-value.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson015(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
41.32231404958662"""
Fintech Math Bootcamp · Lesson 015 of 120
Cash-Flow Timelines and Net Present Value
Module 02: Financial Arithmetic, Time Value, and Returns
Scenario: A project whose future receipts must repay its initial cost
Rule: NPV = Σ CFₜ/(1+r)ᵗ
Try it: Why is the initial −1,000 not discounted?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson015() -> float:
cash_flows = [-1000, 600, 600]
rate = 0.10
contributions = [cf / (1 + rate) ** t for t, cf in enumerate(cash_flows)]
result = sum(contributions)
return result
if __name__ == "__main__":
print(json.dumps(lesson015(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
41.32231404958662/**
* Fintech Math Bootcamp · Lesson 015 of 120
* Cash-Flow Timelines and Net Present Value
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: A project whose future receipts must repay its initial cost
* Rule: NPV = Σ CFₜ/(1+r)ᵗ
*
* Try it: Why is the initial −1,000 not discounted?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
import java.util.Arrays;
public class Main {
static double lesson015() {
double[] cashFlows = {-1000, 600, 600};
double rate = 0.10;
double[] contributions = new double[cashFlows.length];
for (int t = 0; t < cashFlows.length; t++) {
contributions[t] = cashFlows[t] / Math.pow(1 + rate, t);
}
double result = Arrays.stream(contributions).reduce(0, (a, x) -> a + x);
return result;
}
public static void main(String[] args) {
System.out.println(num(lesson015()));
}
// 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);
}
}
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
41.32231404958662// Fintech Math Bootcamp · Lesson 015 of 120
// Cash-Flow Timelines and Net Present Value
// Module 02: Financial Arithmetic, Time Value, and Returns
//
// Scenario: A project whose future receipts must repay its initial cost
// Rule: NPV = Σ CFₜ/(1+r)ᵗ
//
// Try it: Why is the initial −1,000 not discounted?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
// 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"
)
func lesson015() float64 {
cashFlows := []float64{-1000, 600, 600}
rate := 0.10
contributions := make([]float64, len(cashFlows))
for t, cf := range cashFlows {
contributions[t] = cf / math.Pow(1+rate, float64(t))
}
result := 0.0
for _, x := range contributions {
result += x
}
return result
}
func main() {
printJSON(lesson015())
}
// 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
41.32231404958662/**
* Fintech Math Bootcamp · Lesson 015 of 120
* Cash-Flow Timelines and Net Present Value
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: A project whose future receipts must repay its initial cost
* Rule: NPV = Σ CFₜ/(1+r)ᵗ
*
* Try it: Why is the initial −1,000 not discounted?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
* 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 <numeric>
#include <string>
#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);
}
double lesson015() {
const std::vector<double> cashFlows{-1000, 600, 600};
const double rate = 0.10;
std::vector<double> contributions;
for (std::size_t t = 0; t < cashFlows.size(); ++t) {
contributions.push_back(cashFlows[t] / std::pow(1 + rate, static_cast<double>(t)));
}
const double result = std::accumulate(contributions.begin(), contributions.end(), 0.0);
return result;
}
int main() {
std::cout << num(lesson015()) << "\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
41.32231404958662//! Fintech Math Bootcamp · Lesson 015 of 120
//! Cash-Flow Timelines and Net Present Value
//! Module 02: Financial Arithmetic, Time Value, and Returns
//!
//! Scenario: A project whose future receipts must repay its initial cost
//! Rule: NPV = Σ CFₜ/(1+r)ᵗ
//!
//! Try it: Why is the initial −1,000 not discounted?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
fn lesson015() -> f64 {
let cash_flows = [-1000.0, 600.0, 600.0];
let rate: f64 = 0.10;
let contributions: Vec<f64> = cash_flows
.iter()
.enumerate()
.map(|(t, &cf)| cf / (1.0 + rate).powf(t as f64))
.collect();
let result = contributions.iter().fold(0.0, |a, &x| a + x);
result
}
fn main() {
println!("{}", num(lesson015()));
}
/// 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()
}
}
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
41.32231404958662/**
* Fintech Math Bootcamp · Lesson 015 of 120
* Cash-Flow Timelines and Net Present Value
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: A project whose future receipts must repay its initial cost
* Rule: NPV = Σ CFₜ/(1+r)ᵗ
*
* Try it: Why is the initial −1,000 not discounted?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/cashflow-npv/
* 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;
Console.WriteLine(JsonSerializer.Serialize(Lesson015()));
static double Lesson015()
{
double[] cashFlows = { -1000, 600, 600 };
const double rate = 0.10;
var contributions = cashFlows.Select((cf, t) => cf / Math.Pow(1 + rate, t));
var result = contributions.Aggregate(0.0, (a, x) => a + x);
return result;
}
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
41.32231404958662Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
NPV = Σ CFₜ/(1+r)ᵗ