tpemartin / 112-2-programming-for-economic-modeling

MIT License
0 stars 1 forks source link

Exercise Simulate Supply Shock process #14

Open tpemartin opened 1 year ago

aryhsg commented 1 year ago
import numpy as np
from numpy.linalg import inv
class Demand:
    def __init__(self, D, h):
        self.D=D
        self.h=h
    def quantity(self, pd):
        qd=np.array((self.D@pd)+self.h)
        return qd
class Supply:
    def __init__(self, C, e):
        self.C=C
        self.e=e
    def quantity(self, ps):
        qs=np.array((self.C@ps)+self.e)
        return qs
class Market:
    def __init__(self, demand, supply, epsilon):
        self.demand = demand
        self.supply = supply
        self.memory = np.array([])
        self.ep = epsilon
    def price(self):
        pe=inv(self.supply.C-self.demand.D)@(self.demand.h-self.supply.e)
        return pe
    def quantity(self):
        qe=self.demand.D@self.price()+self.demand.h
        return qe
    def equilibrium(self):
        pe=self.price()
        qe=self.quantity()
        return pe, qe

    def supply_shocks(self, eps):
        for i in range(10):
            E = np.array([
                [eps[i, 0]],
                [eps[i, 1]]
            ])
            pe = inv(self.supply.C - self.demand.D) @ (self.demand.h - (self.supply.e + E))
            qe = self.demand.D @ pe + self.demand.h
            self.memory = np.append(self.memory, [pe, qe])
        return pe, qe

D=np.array([
    [-10,-5],
    [-1,-10]
])
h=np.array([
    [100],
    [50]
])
C=np.array([
    [10,5],
    [5,10]
])
e=np.array([
    [0],
    [0]
])
np.random.seed(2023)

epsilon = np.random.normal(0, 0.2, (10, 2))

demand = Demand(D, h)
supply = Supply(C, e)
market = Market(demand, supply, epsilon= epsilon)
market.supply_shocks(eps=market.ep)
print(market.memory)