Surplus Basics
Solution
class consumer:
def __init__(self,WTP):
self.WTP = WTP
def demand(self,price):
if price<=self.WTP:
return 1
else:
return 0
Let’s make an array of consumers with WTP for the values 1 through 10. Making arrays of class objects is an allowed part of coding!
consumerArray = [consumer(x) for x in range(1,11)]
Now if we wanted to see which people would buy the product at a given price, we could use another list comprehension from our array we just created and apply the function.
print([x.demand(5) for x in consumerArray])
Let’s iterate through all the possible prices and see which people will buy the product.
for price in range(1,11):
print([x.demand(price) for x in consumerArray])
Let’s find the aggregate demand at each price level.
for price in range(1,11):
print(sum([x.demand(price) for x in consumerArray]))
We can derive the demand curve from these 10 individuals like by creating an array for all of this
demandQ = []
for price in range(1,11):
demandQ.append(sum([x.demand(price) for x in consumerArray]))
And then we can plot it.
import matplotlib.pyplot as plt
prices = [x for x in range(1,11)]
plt.plot(prices,demandQ)
plt.show()
Challenge
Add a new function to our class which returns the surplus (hint: it will not be negative).