Created
April 20, 2026 16:24
-
-
Save 0187773933/e181329910111228bbcca9470969a18e to your computer and use it in GitHub Desktop.
Technology Adoption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import requests | |
| import pandas as pd | |
| import numpy as np | |
| from io import StringIO | |
| URL = "https://ourworldindata.org/grapher/technology-adoption-by-households-in-the-united-states.csv" | |
| print("Downloading dataset...") | |
| resp = requests.get(URL) | |
| resp.raise_for_status() | |
| df = pd.read_csv(StringIO(resp.text)) | |
| # convert to long if needed | |
| if "Entity" not in df.columns: | |
| df = df.melt(id_vars=["Year"], var_name="Entity", value_name="Value") | |
| df = df.rename(columns={ | |
| "Technology Diffusion (Comin and Hobijn (2004) and others)": "Value" | |
| }) | |
| results = [] | |
| for entity, sub in df.groupby("Entity"): | |
| sub = sub.dropna().sort_values("Year") | |
| if len(sub) < 5: | |
| continue | |
| t = sub["Year"].values | |
| y = sub["Value"].values | |
| # normalize (optional) | |
| if np.max(y) < 20: | |
| continue | |
| # compute slope (dy/dt) | |
| dy = np.gradient(y, t) | |
| # smooth a bit to avoid noise spikes | |
| dy_smooth = pd.Series(dy).rolling(3, center=True).mean().to_numpy() | |
| if np.all(np.isnan(dy_smooth)): | |
| continue | |
| idx = np.nanargmax(dy_smooth) | |
| midpoint_year = t[idx] | |
| slope = dy_smooth[idx] | |
| results.append((entity, midpoint_year, slope)) | |
| results.sort(key=lambda x: x[1]) | |
| print("\n=== MIDPOINT YEARS (max slope) ===\n") | |
| for entity, year, slope in results: | |
| print(f"{entity:35s} midpoint ≈ {year:.0f} (slope={slope:.2f})") | |
| print("\nDone.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment