Created
September 17, 2023 22:59
-
-
Save debpu06/7558b2e930be0920fc888a6ed584ad60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Starting point | |
class State: | |
def __init__(self, name, pop,s_intercept=0,d_intercept=100): | |
self.name = name | |
self.population = pop | |
#supply function parameters | |
self.supply_intercept=s_intercept | |
self.supply_slope=1 | |
#demand function parameters | |
self.demand_intercept=d_intercept | |
self.demand_slope=-1 | |
def supply(self, price): | |
'''Determine quantity supplied at a given price.''' | |
qs = self.supply_intercept + self.supply_slope * price | |
return qs | |
def demand(self,price): | |
'''Determine quantity demanded at a given price.''' | |
qd = self.demand_intercept + self.demand_slope * price | |
return qd | |
def __add__(self, other): | |
Agg_pop = self.population+other.population # Combine the population for this state (Self) and the other | |
Agg_s_intercept = self.supply_intercept + other.supply_intercept # combine s intercept | |
Agg_d_intercept = self.demand_intercept + other.demand_intercept # combine d intercept | |
return State('Aggregate',Agg_pop,Agg_s_intercept,Agg_d_intercept) # return the new state | |
def __radd__(self,other): | |
return other+self.population | |
def plot(self): | |
'''Plot''' | |
p = np.linspace(0,100,11) | |
plt.plot(self.supply(p),p,'-r',label='supply') | |
plt.plot(self.demand(p),p,'-b',label='demand') | |
plt.xlabel('Quantity') | |
plt.ylabel('Price') | |
plt.legend() | |
plt.show() | |
def q1(): | |
State1 = State('Indiana',6.73,10,40) | |
State2 = State("Illinois",12.67,40,80) | |
Agg = State1 + State2 # store the state from adding State1 and State2 into Agg | |
print(Agg) | |
fig1 = plt.figure() | |
State1.plot() | |
fig2 = plt.figure() | |
State2.plot() | |
fig3 = plt.figure() | |
Agg.plot() | |
pp = PdfPages(outputfolder + '.pdf') | |
figs = [fig1, fig2, fig3] | |
for fig in figs: | |
fig.savefig(pp, format='pdf') | |
pp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment