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

MIT License
0 stars 1 forks source link

Exercise Supply and Demand Shocks #11

Open tpemartin opened 11 months ago

tricer963 commented 11 months 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):
        self.demand=demand
        self.supply=supply
    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

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

demand = Demand(D, h)
supply = Supply(C, e)
market = Market(demand, supply)

market.equilibrium()