Python  

How to Find Laspeyre’s Index Number in Python

Table of Contents

  • Introduction

  • What Is Laspeyre’s Index Number?

  • Why It Matters in Economic Analysis

  • Real-World Scenario: Measuring Inflation in Renewable Energy Component Costs

  • Step-by-Step Calculation

  • Complete Implementation with Test Cases

  • Best Practices and Common Mistakes

  • Conclusion

Introduction

When tracking inflation or cost changes over time, not all items are created equal. Some matter more than others—like how a spike in solar panel prices affects a green energy startup far more than a change in office stapler costs. This is where Laspeyre’s Index Number shines.

Unlike simple averages, Laspeyre’s method uses base-period quantities as weights, making it ideal for measuring how the cost of a fixed basket of goods changes over time. In this article, we’ll break down the concept, implement it in clean Python code, and apply it to a timely real-world challenge: tracking cost inflation in renewable energy supply chains.

What Is Laspeyre’s Index Number?

Laspeyre’s Index measures the change in the total cost of purchasing the same basket of goods (based on base-period quantities) at current prices versus base prices.

Formula

qw

Where

  • p0 : Prices in base period

  • p1 : Prices in the current period

  • q0 : Quantities in base period (used as weights)

An index of 100 = no change. 115 = 15% cost increase for the same basket.

Why It Matters in Economic Analysis

Governments use Laspeyre’s Index to compute early versions of the Consumer Price Index (CPI). Businesses use it to assess input cost inflation. Because it fixes quantities to the base period, it answers a critical question: “How much more would it cost today to buy what we bought last year?”

This makes it perfect for budgeting, contract renegotiations, and supply chain risk assessment.

Real-World Scenario: Measuring Inflation in Renewable Energy Component Costs

You’re the procurement lead at a solar farm developer. In 2023 (base year), your standard project used:

  • 500 solar panels

  • 20 inverters

  • 1000 meters of cabling

  • 5 mounting structures

Now in 2024, you want to know: “How much more expensive is it to build the exact same solar farm today?”

Since your design hasn’t changed, the quantities remain fixed—making Laspeyre’s Index the ideal tool. You collect current prices and compare them to 2023 prices, weighted by last year’s quantities.

Step-by-Step Calculation

  1. Multiply each base-year quantity by the current price → total current cost of the old basket

  2. Multiply each base-year quantity by base-year price → original cost

  3. Divide the current total by the original total

  4. Multiply by 100 → Laspeyre’s Index

Complete Implementation with Test Cases

PlantUML Diagram
from typing import List
import unittest

def laspeyres_index(
    base_prices: List[float],
    current_prices: List[float],
    base_quantities: List[float]
) -> float:
    """
    Calculate Laspeyre's Price Index Number.
    
    Args:
        base_prices: Prices in base period (p0)
        current_prices: Prices in current period (p1)
        base_quantities: Quantities in base period (q0) — used as weights
        
    Returns:
        Laspeyre's Index (e.g., 108.3 means 8.3% cost increase)
        
    Raises:
        ValueError: If inputs are invalid (empty, unequal length, or zero denominator)
    """
    if not (base_prices and current_prices and base_quantities):
        raise ValueError("All input lists must be non-empty")
    
    if not (len(base_prices) == len(current_prices) == len(base_quantities)):
        raise ValueError("All lists must have the same length")
    
    numerator = sum(p1 * q0 for p1, q0 in zip(current_prices, base_quantities))
    denominator = sum(p0 * q0 for p0, q0 in zip(base_prices, base_quantities))
    
    if denominator == 0:
        raise ValueError("Total base-period expenditure cannot be zero")
    
    index = (numerator / denominator) * 100
    return round(index, 2)

class TestLaspeyresIndex(unittest.TestCase):
    
    def test_solar_farm_scenario(self):
        # 2023 (base) prices in USD
        base_p = [300, 1200, 2.5, 800]       # panel, inverter, cable/m, structure
        # 2024 (current) prices
        curr_p = [330, 1250, 2.8, 820]
        # Quantities per project (2023 design)
        qty = [500, 20, 1000, 5]
        
        index = laspeyres_index(base_p, curr_p, qty)
        # Expected value is 109.09, corrected from 106.80
        self.assertAlmostEqual(index, 109.09, places=2)

    def test_no_change(self):
        prices = [10, 20, 30]
        qty = [2, 3, 1]
        index = laspeyres_index(prices, prices, qty)
        self.assertEqual(index, 100.0)

    def test_price_increase_only(self):
        base_p = [100, 50]
        curr_p = [110, 55]
        qty = [10, 20]
        # Numerator: (110*10) + (55*20) = 1100 + 1100 = 2200
        # Denominator: (100*10) + (50*20) = 1000 + 1000 = 2000
        # Index: (2200/2000) * 100 = 110.0
        index = laspeyres_index(base_p, curr_p, qty)
        self.assertEqual(index, 110.0)

    def test_error_cases(self):
        with self.assertRaises(ValueError):
            laspeyres_index([], [1], [1])
        with self.assertRaises(ValueError):
            laspeyres_index([1, 2], [3], [4, 5])
        with self.assertRaises(ValueError):
            # Tests for zero denominator: (0*1) + (0*1) = 0
            laspeyres_index([0, 0], [1, 2], [1, 1])

if __name__ == "__main__":
    # Solar farm component data
    base_prices_2023 = [300, 1200, 2.50, 800]    # USD
    current_prices_2024 = [330, 1250, 2.80, 820]
    base_quantities = [500, 20, 1000, 5]          # per project
    
    index = laspeyres_index(base_prices_2023, current_prices_2024, base_quantities)
    
    print("=== Renewable Energy Cost Inflation Tracker ===")
    print(f"Laspeyre's Index (2023 → 2024): {index}")
    
    if index > 100:
        print(f"→ Building the same solar farm now costs {index - 100:.2f}% more")
    elif index < 100:
        print(f"→ Costs decreased by {100 - index:.2f}%")
    else:
        print("→ No change in total component cost")
    
    # Run tests
    unittest.main(argv=[''], exit=False, verbosity=2)
c

Best Practices and Common Mistakes

  • Use consistent units: Prices in same currency, quantities in same units (e.g., per unit, per meter).

  • Keep quantities fixed to base period—never update them for current usage.

  • Don’t confuse with Paasche Index (which uses current quantities).

  • Validate denominator: If base expenditure is zero, the index is undefined.

  • Pair with trend analysis: Track Laspeyre’s Index monthly to spot inflationary trends early.

Conclusion

Laspeyre’s Index Number is a powerful, intuitive tool for measuring cost inflation when your consumption basket remains stable—perfect for manufacturing, construction, energy, and public policy. By anchoring weights to the base period, it answers a real business question: “What would last year’s plan cost today?” With the clean, tested implementation above, you can integrate this classic economic metric into your data pipelines in minutes—and make smarter, cost-aware decisions in an inflationary world.