##############################################################################
# SimTrade blog post: The greenium: why do investors accept a lower yield
#                     on green bonds?
#
# Author: Aarichsun BATLISH (Indian Institute of Technology Kanpur)
# Date: November 2026
#
# Description:
# This code computes the yield (internal rate of return, IRR) of a standard
# bond and of a green bond from their prices and cash flows, and the greenium,
# defined as the yield of the green bond minus the yield of a comparable
# conventional bond. It reproduces the numbers of Table 1 of the post for
# India's first sovereign green bonds (auction of 25 January 2023).
#
# Assumptions:
# - Face value of 100 rupees, coupons paid twice a year
# - The green bonds were issued at par, so their coupon equals their yield
# - Without the green label, the government would have borrowed at the yield
#   of the comparable conventional bond
#
# Source of the data: Business Standard (26 January 2023)
##############################################################################

import numpy as np


##############################################################################
# 1. Functions
##############################################################################

# Cash flows of a bond per 100 of face value: a coupon every period and the
# face value repaid with the last coupon
def bond_cash_flows(coupon_rate, maturity, frequency=2):
    n = maturity * frequency
    cash_flows = np.full(n, 100 * coupon_rate / frequency)
    cash_flows[-1] += 100
    times = np.arange(1, n + 1)
    return times, cash_flows


# Price of a bond: present value of its cash flows discounted at the yield
def bond_price(coupon_rate, yield_rate, maturity, frequency=2):
    times, cash_flows = bond_cash_flows(coupon_rate, maturity, frequency)
    return np.sum(cash_flows / (1 + yield_rate / frequency) ** times)


# Yield of a bond: the IRR, i.e. the discount rate that makes the present value
# of the cash flows equal to the market price. It is found by bisection: the
# price decreases with the yield, so we keep the half of the interval that
# contains the solution until the interval is very small
def bond_yield(price, coupon_rate, maturity, frequency=2, low=0.0, high=0.5):
    for _ in range(200):
        middle = (low + high) / 2
        if bond_price(coupon_rate, middle, maturity, frequency) > price:
            low = middle
        else:
            high = middle
    return (low + high) / 2


# Modified duration: percentage change in the price for a 1% change in yield
def modified_duration(coupon_rate, yield_rate, maturity, frequency=2):
    times, cash_flows = bond_cash_flows(coupon_rate, maturity, frequency)
    pv = cash_flows / (1 + yield_rate / frequency) ** times
    macaulay = np.sum(pv * times / frequency) / np.sum(pv)
    return macaulay / (1 + yield_rate / frequency)


##############################################################################
# 2. A simple example: a standard bond and a green bond
##############################################################################

# Two 10-year bonds of the same issuer with a 7.29% coupon. Investors pay 100
# for the green bond but only 99.58 for the conventional bond
coupon, maturity = 0.0729, 10
price_green, price_standard = 100.00, 99.58

yield_green = bond_yield(price_green, coupon, maturity)
yield_standard = bond_yield(price_standard, coupon, maturity)

print("Example: two 10-year bonds with a coupon of 7.29%")
print("Yield (IRR) of the standard bond: %.2f%%" % (100 * yield_standard))
print("Yield (IRR) of the green bond:    %.2f%%" % (100 * yield_green))
print("Greenium: %.0f bp" % (1e4 * (yield_green - yield_standard)))
print()


##############################################################################
# 3. India's first sovereign green bonds (Table 1 of the post)
##############################################################################

# Data: name, coupon (= yield at issuance) of the green bond, yield of the
# comparable conventional bond, maturity in years, amount issued in crore
bonds = [
    ("5-year green bond", 0.0710, 0.0715, 5, 4000),
    ("10-year green bond", 0.0729, 0.0735, 10, 4000),
]

total_saving = 0
for name, coupon_green, yield_conventional, maturity, amount in bonds:
    # The green bond is issued at par: its yield (IRR) at a price of 100 is its coupon
    yield_green = bond_yield(100, coupon_green, maturity)

    # Greenium in basis points (negative: lower yield for the green bond)
    greenium = 1e4 * (yield_green - yield_conventional)

    # Price of the same cash flows at the yield of the conventional bond:
    # the difference with 100 is the extra price paid by the green investor
    price_conventional = bond_price(coupon_green, yield_conventional, maturity)
    extra_price = 100 - price_conventional

    # Interest saved by the government each year and over the life of the bond
    # (a simple sum, not discounted)
    saving_per_year = amount * (yield_conventional - yield_green)
    saving_total = saving_per_year * maturity
    total_saving += saving_total

    print(name)
    print("  Yield at issuance of the green bond:       %.2f%%" % (100 * yield_green))
    print("  Yield of the comparable conventional bond: %.2f%%" % (100 * yield_conventional))
    print("  Greenium:                                  %.0f bp" % greenium)
    print("  Extra price paid per 100 of face value:    %.2f rupees" % extra_price)
    print("  Modified duration:                         %.2f" % modified_duration(coupon_green, yield_conventional, maturity))
    print("  Interest saved per year:                   %.1f crore" % saving_per_year)
    print("  Interest saved over the life of the bond:  %.0f crore" % saving_total)
    print()

print("Total interest saved by the government: %.0f crore" % total_saving)
