Module 3 · Data, Variables, Samples, and Measurement Lesson 27 of 120
Missing, Nonfinite, Censored, and Truncated Values
Unknown, nonfinite, censored, and absent from the sample.
Transcript
24 sentences · select one to jump thereCheck your understanding
Is “income above 100,000” an exact value of 100,000?
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 027 of 120
* Missing, Nonfinite, Censored, and Truncated Values
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Unknown, nonfinite, censored, and absent from the sample
* Rule: missing ≠ 0; censored ≠ truncated
*
* Try it: Is “income above 100,000” an exact value of 100,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson027() {
const values: unknown[] = [10, null, Infinity, "unknown", 20];
const accepted = values.filter((x): x is number =>
typeof x === "number" && Number.isFinite(x));
const result = {accepted, excluded: values.length - accepted.length};
return result;
}
export const checkedResult = {"accepted":[10,20],"excluded":3};
// Run this file directly: npx tsx lessons/03-data-variables-samples-and-measurement/027-missing-nonfinite-censored-and-truncated-values.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson027(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
{
"accepted": [
10,
20
],
"excluded": 3
}"""
Fintech Math Bootcamp · Lesson 027 of 120
Missing, Nonfinite, Censored, and Truncated Values
Module 03: Data, Variables, Samples, and Measurement
Scenario: Unknown, nonfinite, censored, and absent from the sample
Rule: missing ≠ 0; censored ≠ truncated
Try it: Is “income above 100,000” an exact value of 100,000?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
import math
def lesson027() -> dict:
values: list[object] = [10, None, math.inf, "unknown", 20]
accepted = [
x for x in values if type(x) in (int, float) and math.isfinite(x)
]
result = {"accepted": accepted, "excluded": len(values) - len(accepted)}
return result
if __name__ == "__main__":
print(json.dumps(lesson027(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"accepted": [
10,
20
],
"excluded": 3
}/**
* Fintech Math Bootcamp · Lesson 027 of 120
* Missing, Nonfinite, Censored, and Truncated Values
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Unknown, nonfinite, censored, and absent from the sample
* Rule: missing ≠ 0; censored ≠ truncated
*
* Try it: Is "income above 100,000" an exact value of 100,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
* 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.ArrayList;
import java.util.List;
public class Main {
static final class Screened {
final List<Double> accepted;
final int excluded;
Screened(List<Double> accepted, int excluded) {
this.accepted = accepted;
this.excluded = excluded;
}
}
static Screened lesson027() {
Object[] values = {10.0, null, Double.POSITIVE_INFINITY, "unknown", 20.0};
List<Double> accepted = new ArrayList<Double>();
for (Object x : values) {
if (x instanceof Double && Double.isFinite((Double) x)) accepted.add((Double) x);
}
Screened result = new Screened(accepted, values.length - accepted.size());
return result;
}
public static void main(String[] args) {
Screened result = lesson027();
List<String> accepted = new ArrayList<String>();
for (double x : result.accepted) accepted.add(num(x));
System.out.println(object("accepted", array(accepted), "excluded", num(result.excluded)));
}
// 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();
}
static String array(java.util.List<String> items) {
if (items.isEmpty()) return "[]";
StringBuilder out = new StringBuilder("[");
for (int i = 0; i < items.size(); i++) {
out.append(i == 0 ? "\n " : ",\n ").append(nested(items.get(i)));
}
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
{
"accepted": [
10,
20
],
"excluded": 3
}// Fintech Math Bootcamp · Lesson 027 of 120
// Missing, Nonfinite, Censored, and Truncated Values
// Module 03: Data, Variables, Samples, and Measurement
//
// Scenario: Unknown, nonfinite, censored, and absent from the sample
// Rule: missing ≠ 0; censored ≠ truncated
//
// Try it: Is “income above 100,000” an exact value of 100,000?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
// 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 Screened struct {
Accepted []float64 `json:"accepted"`
Excluded int `json:"excluded"`
}
func lesson027() Screened {
values := []any{10.0, nil, math.Inf(1), "unknown", 20.0}
accepted := []float64{}
for _, v := range values {
if x, ok := v.(float64); ok && !math.IsNaN(x) && !math.IsInf(x, 0) {
accepted = append(accepted, x)
}
}
result := Screened{Accepted: accepted, Excluded: len(values) - len(accepted)}
return result
}
func main() {
printJSON(lesson027())
}
// 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
{
"accepted": [
10,
20
],
"excluded": 3
}/**
* Fintech Math Bootcamp · Lesson 027 of 120
* Missing, Nonfinite, Censored, and Truncated Values
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Unknown, nonfinite, censored, and absent from the sample
* Rule: missing ≠ 0; censored ≠ truncated
*
* Try it: Is “income above 100,000” an exact value of 100,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
* 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 <limits>
#include <string>
#include <utility>
#include <variant>
#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}";
}
std::string array(const std::vector<std::string>& items) {
if (items.empty()) return "[]";
std::string out = "[";
for (std::size_t i = 0; i < items.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += nested(items[i]);
}
return out + "\n]";
}
std::string numbers(const std::vector<double>& values) {
std::vector<std::string> items;
for (double value : values) items.push_back(num(value));
return array(items);
}
// A raw field value: missing, a number, or text.
using Value = std::variant<std::monostate, double, std::string>;
struct Screened {
std::vector<double> accepted;
std::size_t excluded;
};
Screened lesson027() {
const std::vector<Value> values{
10.0, std::monostate{}, std::numeric_limits<double>::infinity(), std::string("unknown"), 20.0
};
std::vector<double> accepted;
for (const Value& v : values) {
const double* x = std::get_if<double>(&v);
if (x != nullptr && std::isfinite(*x)) accepted.push_back(*x);
}
const Screened result{accepted, values.size() - accepted.size()};
return result;
}
int main() {
const Screened result = lesson027();
std::cout << object({
{"accepted", numbers(result.accepted)},
{"excluded", std::to_string(result.excluded)}
}) << "\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
{
"accepted": [
10,
20
],
"excluded": 3
}//! Fintech Math Bootcamp · Lesson 027 of 120
//! Missing, Nonfinite, Censored, and Truncated Values
//! Module 03: Data, Variables, Samples, and Measurement
//!
//! Scenario: Unknown, nonfinite, censored, and absent from the sample
//! Rule: missing ≠ 0; censored ≠ truncated
//!
//! Try it: Is “income above 100,000” an exact value of 100,000?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
/// A raw field value: missing, a number, or text.
#[allow(dead_code)] // the text payload is kept for realism but never read
enum Value {
Null,
Number(f64),
Text(&'static str),
}
struct Screened {
accepted: Vec<f64>,
excluded: usize,
}
fn lesson027() -> Screened {
let values = [
Value::Number(10.0),
Value::Null,
Value::Number(f64::INFINITY),
Value::Text("unknown"),
Value::Number(20.0),
];
let accepted: Vec<f64> = values
.iter()
.filter_map(|v| match v {
Value::Number(x) if x.is_finite() => Some(*x),
_ => None,
})
.collect();
let excluded = values.len() - accepted.len();
let result = Screened { accepted, excluded };
result
}
fn main() {
let result = lesson027();
println!(
"{}",
object(&[
("accepted", numbers(&result.accepted)),
("excluded", result.excluded.to_string()),
])
);
}
/// 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"))
}
fn array(items: &[String]) -> String {
if items.is_empty() {
return "[]".to_string();
}
let lines: Vec<String> = items.iter().map(|item| format!(" {}", nested(item))).collect();
format!("[\n{}\n]", lines.join(",\n"))
}
fn numbers(values: &[f64]) -> String {
let items: Vec<String> = values.iter().map(|&value| num(value)).collect();
array(&items)
}
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
{
"accepted": [
10,
20
],
"excluded": 3
}/**
* Fintech Math Bootcamp · Lesson 027 of 120
* Missing, Nonfinite, Censored, and Truncated Values
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Unknown, nonfinite, censored, and absent from the sample
* Rule: missing ≠ 0; censored ≠ truncated
*
* Try it: Is “income above 100,000” an exact value of 100,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/missing-nonfinite-censored-and-truncated-values/
* 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(Lesson027(), jsonOptions));
static Screened Lesson027()
{
object?[] values = { 10.0, null, double.PositiveInfinity, "unknown", 20.0 };
var accepted = values.OfType<double>().Where(double.IsFinite).ToList();
var result = new Screened(accepted, values.Length - accepted.Count);
return result;
}
record Screened(List<double> Accepted, int Excluded);
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
{
"accepted": [
10,
20
],
"excluded": 3
}Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
missing ≠ 0; censored ≠ truncated