Module 3 · Data, Variables, Samples, and Measurement Lesson 23 of 120
Population, Sample, Census, and Sampling Frame
A sample of approved loans cannot represent all applications.
Transcript
21 sentences · select one to jump thereCheck your understanding
Can more sampling from the same incomplete frame recover E?
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 023 of 120
* Population, Sample, Census, and Sampling Frame
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A sample of approved loans cannot represent all applications
* Rule: target population → sampling frame → observed sample
*
* Try it: Can more sampling from the same incomplete frame recover E?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson023() {
const population = ["A", "B", "C", "D", "E"];
const frame = ["A", "B", "C", "D"];
const sample = ["A", "C"];
const result = {population: population.length,
frame: frame.length, sample: sample.length,
uncovered: population.filter(x => !frame.includes(x))};
return result;
}
export const checkedResult = {"population":5,"frame":4,"sample":2,"uncovered":["E"]};
// Run this file directly: npx tsx lessons/03-data-variables-samples-and-measurement/023-population-sample-census-and-sampling-frame.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson023(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}"""
Fintech Math Bootcamp · Lesson 023 of 120
Population, Sample, Census, and Sampling Frame
Module 03: Data, Variables, Samples, and Measurement
Scenario: A sample of approved loans cannot represent all applications
Rule: target population → sampling frame → observed sample
Try it: Can more sampling from the same incomplete frame recover E?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson023() -> dict:
population = ["A", "B", "C", "D", "E"]
frame = ["A", "B", "C", "D"]
sample = ["A", "C"]
result = {
"population": len(population),
"frame": len(frame),
"sample": len(sample),
"uncovered": [x for x in population if x not in frame],
}
return result
if __name__ == "__main__":
print(json.dumps(lesson023(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}/**
* Fintech Math Bootcamp · Lesson 023 of 120
* Population, Sample, Census, and Sampling Frame
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A sample of approved loans cannot represent all applications
* Rule: target population → sampling frame → observed sample
*
* Try it: Can more sampling from the same incomplete frame recover E?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
* 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.Arrays;
import java.util.List;
public class Main {
static final class Coverage {
final int population;
final int frame;
final int sample;
final List<String> uncovered;
Coverage(int population, int frame, int sample, List<String> uncovered) {
this.population = population;
this.frame = frame;
this.sample = sample;
this.uncovered = uncovered;
}
}
static Coverage lesson023() {
List<String> population = Arrays.asList("A", "B", "C", "D", "E");
List<String> frame = Arrays.asList("A", "B", "C", "D");
List<String> sample = Arrays.asList("A", "C");
List<String> uncovered = new ArrayList<String>();
for (String x : population) {
if (!frame.contains(x)) uncovered.add(x);
}
Coverage result = new Coverage(population.size(), frame.size(), sample.size(), uncovered);
return result;
}
public static void main(String[] args) {
Coverage result = lesson023();
List<String> uncovered = new ArrayList<String>();
for (String x : result.uncovered) uncovered.add(quote(x));
System.out.println(object(
"population", num(result.population),
"frame", num(result.frame),
"sample", num(result.sample),
"uncovered", array(uncovered)));
}
// 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
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}// Fintech Math Bootcamp · Lesson 023 of 120
// Population, Sample, Census, and Sampling Frame
// Module 03: Data, Variables, Samples, and Measurement
//
// Scenario: A sample of approved loans cannot represent all applications
// Rule: target population → sampling frame → observed sample
//
// Try it: Can more sampling from the same incomplete frame recover E?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
// 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"
"slices"
)
type Coverage struct {
Population int `json:"population"`
Frame int `json:"frame"`
Sample int `json:"sample"`
Uncovered []string `json:"uncovered"`
}
func lesson023() Coverage {
population := []string{"A", "B", "C", "D", "E"}
frame := []string{"A", "B", "C", "D"}
sample := []string{"A", "C"}
uncovered := []string{}
for _, x := range population {
if !slices.Contains(frame, x) {
uncovered = append(uncovered, x)
}
}
result := Coverage{
Population: len(population),
Frame: len(frame),
Sample: len(sample),
Uncovered: uncovered,
}
return result
}
func main() {
printJSON(lesson023())
}
// 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
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}/**
* Fintech Math Bootcamp · Lesson 023 of 120
* Population, Sample, Census, and Sampling Frame
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A sample of approved loans cannot represent all applications
* Rule: target population → sampling frame → observed sample
*
* Try it: Can more sampling from the same incomplete frame recover E?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
* 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 <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
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]";
}
struct Coverage {
std::size_t population;
std::size_t frame;
std::size_t sample;
std::vector<std::string> uncovered;
};
Coverage lesson023() {
const std::vector<std::string> population{"A", "B", "C", "D", "E"};
const std::vector<std::string> frame{"A", "B", "C", "D"};
const std::vector<std::string> sample{"A", "C"};
std::vector<std::string> uncovered;
for (const std::string& x : population) {
if (std::find(frame.begin(), frame.end(), x) == frame.end()) uncovered.push_back(x);
}
const Coverage result{population.size(), frame.size(), sample.size(), uncovered};
return result;
}
int main() {
const Coverage result = lesson023();
std::vector<std::string> uncovered;
for (const std::string& x : result.uncovered) uncovered.push_back(quote(x));
std::cout << object({
{"population", std::to_string(result.population)},
{"frame", std::to_string(result.frame)},
{"sample", std::to_string(result.sample)},
{"uncovered", array(uncovered)}
}) << "\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
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}//! Fintech Math Bootcamp · Lesson 023 of 120
//! Population, Sample, Census, and Sampling Frame
//! Module 03: Data, Variables, Samples, and Measurement
//!
//! Scenario: A sample of approved loans cannot represent all applications
//! Rule: target population → sampling frame → observed sample
//!
//! Try it: Can more sampling from the same incomplete frame recover E?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
struct Coverage {
population: usize,
frame: usize,
sample: usize,
uncovered: Vec<&'static str>,
}
fn lesson023() -> Coverage {
let population = ["A", "B", "C", "D", "E"];
let frame = ["A", "B", "C", "D"];
let sample = ["A", "C"];
let uncovered: Vec<&str> = population.iter().copied().filter(|x| !frame.contains(x)).collect();
let result = Coverage {
population: population.len(),
frame: frame.len(),
sample: sample.len(),
uncovered,
};
result
}
fn main() {
let result = lesson023();
let uncovered: Vec<String> = result.uncovered.iter().map(|x| quote(x)).collect();
println!(
"{}",
object(&[
("population", result.population.to_string()),
("frame", result.frame.to_string()),
("sample", result.sample.to_string()),
("uncovered", array(&uncovered)),
])
);
}
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"))
}
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
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}/**
* Fintech Math Bootcamp · Lesson 023 of 120
* Population, Sample, Census, and Sampling Frame
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A sample of approved loans cannot represent all applications
* Rule: target population → sampling frame → observed sample
*
* Try it: Can more sampling from the same incomplete frame recover E?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/population-sample-census-and-sampling-frame/
* 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(Lesson023(), jsonOptions));
static Coverage Lesson023()
{
string[] population = { "A", "B", "C", "D", "E" };
string[] frame = { "A", "B", "C", "D" };
string[] sample = { "A", "C" };
var result = new Coverage(
population.Length,
frame.Length,
sample.Length,
population.Where(x => !frame.Contains(x)).ToList());
return result;
}
record Coverage(int Population, int Frame, int Sample, List<string> Uncovered);
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
{
"population": 5,
"frame": 4,
"sample": 2,
"uncovered": [
"E"
]
}Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
target population → sampling frame → observed sample