50 minReal data snapshotExecuted reading edition

Distinguish CPI, inflation and real values

Rebase an observed price index, reconcile annual changes and deflate a stated hypothetical nominal amount.

Economics & finance Β· Lesson 02πŸ”‹ Offline core analysis⏱ 50 minEmbedded real dataπŸ“¦ numpy Β· pandas Β· matplotlib Β· IPython

Goal and how to run

This lesson assumes basic Python knowledge. Read each method and caption before changing parameters. AI exercises are optional; no AI account is needed for the core workflow. Data lives in notebook metadata, so there is no companion data download. Keep the notebook saved in the active working folder.

Your goal is to understand the research question, execute transparent calculations, check the results, and state the limits of the conclusion.

Setup and parameters

In [1] Β· Python
from pathlib import Path
import base64, gzip, hashlib, json, platform
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import display, HTML
from IPython.display import display as _display
def display(value):
    if isinstance(value,pd.DataFrame):
        table=value.to_html(max_rows=12,escape=True)
        style="<style>.vibeit-readable-table{max-width:100%;overflow-x:auto}.vibeit-readable-table table{width:max-content;max-width:none;border-collapse:collapse}.vibeit-readable-table td,.vibeit-readable-table th{white-space:nowrap!important;word-break:normal!important;overflow-wrap:normal!important}</style>"
        return _display(HTML(style+'<div class="vibeit-readable-table">'+table+'</div>'))
    return _display(value)
RECIPE_ID='ec02-inflation'
OUTPUT_DIR=Path.cwd()/"results"/RECIPE_ID
plt.rcParams.update({"figure.dpi":120,"font.size":10,"axes.spines.top":False,"axes.spines.right":False,
                      "axes.prop_cycle":plt.cycler(color=["#18785f","#416aa6","#bc6541","#9974a5"])})
print("Python:",platform.python_version(),"; NumPy:",np.__version__,"; Pandas:",pd.__version__)
print("Working folder:",Path.cwd())
print("Core data mode: embedded snapshot; no network requests")
Python: 3.13.13 ; NumPy: 2.5.3 ; Pandas: 3.0.6
Working folder: /Users/zhiluo/Developer/vibeit-cookbook/cookbook/downloads
Core data mode: embedded snapshot; no network requests

Load the embedded snapshot

In [2] Β· Python
def load_snapshot(recipe_id, expected_sha256, snapshot_key):
    """Read embedded data from this notebook, including a renamed copy."""
    for path in sorted(Path.cwd().glob("*.ipynb")):
        try:
            notebook = json.loads(path.read_text(encoding="utf-8"))
            metadata = notebook.get("metadata", {}).get("vibeit_cookbook", {})
            blob = metadata.get("snapshots", {}).get(snapshot_key)
            if metadata.get("recipe_id") != recipe_id or not blob:
                continue
            if blob.get("sha256") != expected_sha256:
                continue
            raw = gzip.decompress(base64.b64decode(blob["gzip_base64"], validate=True))
            if hashlib.sha256(raw).hexdigest() != expected_sha256:
                raise ValueError("Embedded snapshot checksum mismatch: " + snapshot_key)
            return json.loads(raw)
        except (OSError, json.JSONDecodeError):
            continue
    raise FileNotFoundError(
        "Save/open the complete .ipynb in the active working folder. "
        "The embedded snapshot could not be located for " + recipe_id)

SNAPSHOT_HASHES={'worldbank': 'd45779250297fb9371748b85b81321b2ae88f93ecf70cfb88256647a68e54376'}
def snapshot(key):
    return load_snapshot(RECIPE_ID,SNAPSHOT_HASHES[key],key)
print("Embedded snapshots:",list(SNAPSHOT_HASHES))
Embedded snapshots: ['worldbank']

Read the method functions

In [3] Β· Python
def worldbank_panel(payload):
    """Preserve missing observations; official economy metadata excludes aggregates."""
    economies=pd.DataFrame(payload['economies']).set_index('iso3')
    observations=pd.DataFrame(payload['observations'])
    observations=observations[observations.iso3.isin(economies.index)].copy()
    if observations.duplicated(['iso3','year','indicator']).any():raise ValueError('Duplicate economy/year/indicator observations')
    observations['value']=pd.to_numeric(observations.value,errors='raise')
    wide=observations.pivot(index=['iso3','year'],columns='indicator',values='value').sort_index()
    return wide,economies,observations

print("Method ready: worldbank_panel")
Method ready: worldbank_panel

Data and model boundary

World Bank records retain definitions, missing observations and reference years. The six economies are declared examples, not a random global sample. Country averages, price-basket conversions and Gini indices describe different quantities; survey concepts and coverage can differ.

Steps

1. Declare the price index and hypothetical amount

Use US CPI and rebase its observed 2010 value. The constant nominal 1000 USD is a teaching input, not invented wage or household-income data.

In [4] Β· Python
wb=snapshot("worldbank");panel,economies,observations=worldbank_panel(wb)
countries=["CHN","USA","DEU","BRA","IND","ZAF"]
assert set(countries)<=set(economies.index)
print("Economy/year records:",len(panel),"; official aggregates excluded; missing values preserved")
display(economies.loc[countries,["name","region","income_group"]])
cpi=panel.loc["USA","FP.CPI.TOTL"].reindex(range(2010,2025))
reported=panel.loc["USA","FP.CPI.TOTL.ZG"].reindex(cpi.index)
assert cpi.loc[2010]>0
rebased=100*cpi/cpi.loc[2010];nominal=1000.0
fig,ax=plt.subplots(figsize=(8,4));ax.plot(rebased.index,rebased,"o-")
ax.set(xlabel="Indicator year",ylabel="Rebased CPI (2010=100)",title="Price-index level, not a rate");plt.tight_layout();plt.show()
Economy/year records: 3255 ; official aggregates excluded; missing values preserved
name region income_group
iso3
CHN China East Asia & Pacific Upper middle income
USA United States North America High income
DEU Germany Europe & Central Asia High income
BRA Brazil Latin America & Caribbean Upper middle income
IND India South Asia Lower middle income
ZAF South Africa Sub-Saharan Africa Upper middle income
Computed figure from this code cell

2. Reconcile calculated and published rates

Use adjacent observed years. Differences can reflect source revision and rounding; expose them rather than force agreement. Differences of rates are percentage points.

In [5] Β· Python
computed=100*cpi.pct_change(fill_method=None)
fig,ax=plt.subplots(figsize=(8,4));ax.plot(computed.index,computed,"o-",label="From frozen CPI")
ax.plot(reported.index,reported,"x--",label="Published inflation")
ax.set(xlabel="Indicator year",ylabel="Annual change (%)",title="Rates and source reconciliation");ax.legend();plt.tight_layout();plt.show()
comparison=pd.DataFrame({"computed_percent":computed,"reported_percent":reported,"difference_percentage_points":computed-reported})
display(comparison.tail(7))
Computed figure from this code cell
computed_percent reported_percent difference_percentage_points
year
2018 2.442583 2.442583 9.325873e-15
2019 1.812210 1.812210 3.563816e-13
2020 1.233584 1.233584 -5.031531e-13
2021 4.697859 4.697859 -3.730349e-14
2022 8.002800 8.002800 6.235013e-13
2023 4.116338 4.116338 -2.149392e-13
2024 2.949525 2.949525 -2.593481e-13

3. Convert to a CPI-based real amount

Divide nominal USD by rebased CPI/100. The result uses a broad source price basket, not personalized living costs or investment profits.

In [6] Β· Python
real=nominal*100/rebased
fig,ax=plt.subplots(figsize=(8,4));ax.plot(cpi.index,np.full(len(cpi),nominal),label="Hypothetical nominal USD")
ax.plot(real.index,real,"o-",label="CPI-based 2010-price amount")
ax.set(xlabel="Indicator year",ylabel="USD / 2010-price USD",title="Stated hypothetical purchasing-power example");ax.legend();plt.tight_layout();plt.show()
Computed figure from this code cell

4. Check rebasing and export assumptions

Nominal and real values match at the base year. Preserve original CPI and its derived index; the fixed nominal example does not measure income change.

In [7] Β· Python
assert np.isclose(rebased.loc[2010],100) and np.isclose(real.loc[2010],nominal) and pd.isna(computed.loc[2010])
OUTPUT_DIR.mkdir(parents=True,exist_ok=True)
pd.DataFrame({"source_CPI":cpi,"rebased_2010_100":rebased,"hypothetical_nominal_USD":nominal,"CPI_based_2010_price_USD":real}).to_csv(OUTPUT_DIR/"price-and-real-amount.csv")
comparison.to_csv(OUTPUT_DIR/"inflation-reconciliation.csv")
print("Rebasing checked; maximum discrepancy (percentage points):",float(comparison.difference_percentage_points.abs().max()))
Rebasing checked; maximum discrepancy (percentage points): 6.235012506294879e-13

Exercises and reference explanation

  1. Distinguish an index and an inflation rate.
  2. Why is the money amount hypothetical?

Reference explanation. A rate compares adjacent levels; the money amount is a stated input, not observed income.

Keep a copy before changing parameters. Compare old and new figures and record which inputs, calculations and conclusions changed.

Optional AI coding exercises

Prompt 1

Using NumPy/Pandas/Matplotlib, rebase to 2020 and relabel the real-value unit.

Prompt 2

With the same packages, compare one additional declared economy with its own observed CPI.

Human acceptance. No missing values are filled, differences use percentage points and base real/nominal values match.

Review the edited source, then manually run affected cells and subsequent checks. Ask the assistant to explain the purpose, packages and data provenance of its changes, and reconcile that explanation with actual output.

Optional online extension

In [8] Β· Python
RUN_ONLINE_UPDATE=False
if RUN_ONLINE_UPDATE:
    import requests
    try:
        response=requests.get('https://api.worldbank.org/v2/indicator/NY.GDP.PCAP.KD?format=json',timeout=20)
        response.raise_for_status()
        print("Source:",response.url)
        print(response.text[:700])
    except requests.RequestException as error:
        print("Online extension unavailable:",type(error).__name__,str(error)[:160])
else:
    print("Online extension skipped; embedded core data unchanged")
Online extension skipped; embedded core data unchanged

Sources, snapshots and next steps

  • World Bank economy metadata β€” World Bank CC BY 4.0 with additional terms; retain original indicator attribution; retrieved 2026-10-09. SHA-256 c9acf3932785191a649cbc04c3056af4cf64a236e5c3b49d49be25ddb0440bce. Preserve economy IDs, region and income-group metadata; exclude aggregates in student analyses.

  • World Bank NY.GDP.PCAP.KD β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 59a9efaf20ab359c7832f24da45dda2282511c0eaf927d044583f47d08e02cda. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata NY.GDP.PCAP.KD β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 46689488cd435920647264ede1bfb2d4b51b09800fb36f3d3b1422a0b11ea49d. Preserve indicator definition, source organization and source note.

  • World Bank NY.GDP.MKTP.KD β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 466f4e3176b509613f3df99acffce89b5f1ea14472c376dd10d8b8f67f6172b0. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata NY.GDP.MKTP.KD β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 bc4bc0ffec1755410f52b1091c6b1f46917c7417977102350d3d1102b4659216. Preserve indicator definition, source organization and source note.

  • World Bank SP.POP.TOTL β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 7ecf8d749dbddd87a269f2019e6d42ed32875c57565cf5c5af097c6e6c9af528. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata SP.POP.TOTL β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 2f8491efc00bf2c0995112ff8e87aba69e5e9d22d0880b22f7e3a4ec757ba05c. Preserve indicator definition, source organization and source note.

  • World Bank SP.DYN.LE00.IN β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 d8b5c6bd7fecaf8cee0e422444719ba1f42ec70996308bc74afd14ee48799055. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata SP.DYN.LE00.IN β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 a39cb6fddfbbb953053690366243dffe77a266c4deae79c46ce5a06da9f5fc31. Preserve indicator definition, source organization and source note.

  • World Bank FP.CPI.TOTL β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 55c3b71ad4dd8c92c15fa7dc2b502a3d60f26cf47ae9a24e384bc2ff6108ff56. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata FP.CPI.TOTL β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 13b1e3c4b2b72f7fb3a630b4dde37f0e68e6d5e78dc06b2437dfea0c0b93af46. Preserve indicator definition, source organization and source note.

  • World Bank FP.CPI.TOTL.ZG β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 849b6a88a84a7efdeb92d89e8db1db36f86543e28ff8eb645e0feaa8c634def7. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata FP.CPI.TOTL.ZG β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 df9371a3b6c01e55a3f28991e62e1b1ebbe0cd2a7da94456be9938c7a2962233. Preserve indicator definition, source organization and source note.

  • World Bank SI.POV.GINI β€” World Bank CC BY 4.0 with additional terms; original sources retained in indicator metadata; retrieved 2026-10-09. SHA-256 4919413d3b9ef06b7445c3fd42d47b8a09e9836bbab7be84e4993099ffdc7ab2. Retain official observations including null values and footnotes, 2010–2024; no interpolation; no aggregate-country mixing.

  • World Bank indicator metadata SI.POV.GINI β€” World Bank CC BY 4.0 with additional terms; retrieved 2026-10-09. SHA-256 e377bf82b38024748153880ff90c0106a0ef0fd8cb04761146cc60a2609cab3d. Preserve indicator definition, source organization and source note.

Next steps. Transfer these methods to your own public or authorized data while retaining input checks, recorded parameters and result validation. Document the new experimental design and method limits; this example does not validate a new experiment. Full data licenses and generation instructions accompany the cookbook.