Python Forum
Model of supply and demand - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Model of supply and demand (/thread-35561.html)



Model of supply and demand - maeva - Nov-17-2021

Hello, can you help me figure out what is the problem in this code?
In a model of supply and demand,

the supply is:

Qs = P**a + P + 1

And the demand is:

Qd = 1/(0.5*P**2 + P)

I want to draw a sample of 1000 values for a assuming that a is a random variable which is uniformly distributed within interval [0.2,2.0]. Then I want to solve the equilibrium price for each by numerically finding the root of the excess demand function.

This is my code:
import numpy as np

a= np.linspace(0.2,2.0, num=1000)
z = 0.5  
    
    excess_demand = lambda W: 1/(z*P**2 +P)  - (P**(a) +P + 1)
    
    W, res = optimize.brentq(excess_demand, 1e-8, 1000.0, full_output=True)
    assert res.converged == True
    print(res)
    print(W) 
It appears to me an error? Can you tell me why?

This is the output:
Output:
excess_demand = lambda W: 1/(z*P**2 +P) - (P**(b) +P + 1) ^ IndentationError: unexpected indent



RE: Model of supply and demand - jefsummers - Nov-17-2021

Line 6 is indented. No need for the indentation, so it says unexpected indent


RE: Model of supply and demand - maeva - Nov-17-2021

And so what should I do?


RE: Model of supply and demand - ibreeden - Nov-18-2021

(Nov-17-2021, 06:54 PM)maeva Wrote: And so what should I do?
Read this. Especially the part about indentation.


RE: Model of supply and demand - maeva - Nov-18-2021

(Nov-18-2021, 08:50 AM)ibreeden Wrote:
(Nov-17-2021, 06:54 PM)maeva Wrote: And so what should I do?
Read this. Especially the part about indentation.

Thank you!
I wrote this code:
from scipy import optimize
import numpy as np

def main():
    a= np.linspace(0.2,2.0, num=1000)
    z = 0.5  
    
     
    excess_demand = lambda W: 1/(z*P**2 +P)  - (P**(a) +P + 1)
     
    W, res = optimize.brentq(excess_demand, 1e-8, 1000.0, full_output=True)
    assert res.converged == True
    print(res)
    print(W)
Now the problem is that P is undefined name (pyflakes E). I don't know how to solve it. Do you have any suggestions?