Skip to content
Module 12 of 1290 min readBeginner

Three real analyses on Kenyan data

Replicate the bank-rates spread, the pension allocation shift, and the M-PESA growth curve — entirely in Python.

100%

Listen along

Read “Three real analyses on Kenyan data” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Load real Kenyan datasets, transform them, and produce publication-ready charts
  • 02Replicate the bank-rates spread analysis using pandas and matplotlib
  • 03Replicate the pension allocation shift analysis using groupby and plotting
  • 04Replicate the M-PESA growth-rate analysis with rolling computations

The previous 11 modules gave you the vocabulary. This one asks you to use it. Three real analyses on Kenyan data, each replicating an analysis published on this site, end-to-end in Python.

Project 1 — the bank-rates spread

Using the bankrates DataFrame (21 months of commercial bank lending and deposit rates), compute the spread (lending − deposit), plot it over time, and run a regression of the spread on a linear trend. Is the spread widening, narrowing, or stable? What does it tell you about competitive intensity in Kenyan banking?

python
bankrates['spread'] = bankrates['lending_rate'] - bankrates['deposit_rate']
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(bankrates['month'], bankrates['spread'])
ax.set_title('Lending-deposit spread, Kenyan banks')
plt.show()
# Regression: spread on a linear time trend
bankrates['t'] = range(len(bankrates))
import statsmodels.formula.api as smf
model = smf.ols('spread ~ t', data=bankrates).fit()
print(model.summary())

Project 2 — pension allocation shift

The pension DataFrame has 14 half-years of pension-industry asset allocation by asset class. Compute the share of government securities each half-year, plot the trend, and identify the inflection point. What was happening macroeconomically at that inflection?

python
# Compute share
pension['govt_share'] = pension['govt_securities'] / pension['total']
# Plot
fig, ax = plt.subplots()
ax.plot(pension['period'], pension['govt_share'])
ax.set_ylabel('Share of govt securities')
plt.show()

Project 3 — M-PESA growth rate

The mpesa DataFrame has Kenyan mobile-money volumes over time. Compute the year-over-year growth rate, plot it, and fit a linear trend. Is the growth rate decelerating? At what implied saturation level?

python
mpesa['yoy_growth'] = mpesa['volume_bn'].pct_change(periods=12)
fig, ax = plt.subplots()
ax.plot(mpesa['date'], mpesa['yoy_growth'])
ax.set_ylabel('YoY growth (%)')
plt.show()

What you've learned

If you can do these three projects without copying — load data, transform it, plot it, fit a model, interpret the output — you have working Python at an applied-economist level. The toolkit from here is mostly more libraries (scipy, scikit-learn, networkx) and more data, not more language. The language is mostly behind you.

What to do next

Pick a real question you care about. Pull data — Kenyan or otherwise. Build a notebook. Publish it. The single highest-leverage next step is doing one analysis end-to-end and writing it up. Everything else compounds from there.

Exercise

Design your first real-world Python project end-to-end. Pick one: (a) Pull and analyse the CBK weekly T-bill auction results to find the relationship between auction acceptance rate and subsequent yields. (b) Scrape Safaricom's annual M-Pesa transaction volumes and model growth rates. (c) Use Kenya NBS quarterly GDP data to build a leading-indicator model. For your chosen project: (1) Outline the steps from data acquisition to final report. (2) Identify the specific Python libraries at each step. (3) Estimate realistic time-to-completion. (4) Describe what 'publishing it' looks like as an artefact.

Key takeaways

  • Three projects, three patterns: time-series trend (banking spread), structural shift (pension allocation), growth-rate computation (M-PESA)
  • Real analysis is a pipeline: load → inspect → transform → visualise → model → interpret
  • The vocabulary from modules 1-11 is enough — most of applied work is composition, not new syntax
  • Publish your work. The single highest-leverage next step is doing one analysis end-to-end and writing it up
Loading progress…
LeadAfrikPublic Economics Hub