網頁

2018年7月25日 星期三

Use python to help you do your economics homework─linear demand-supply model

    Actually, I wrote the static game code first, after I finished that, I thought to myself that I can solve the problems from economics textbook with codes. This one is actually quite simple, you use math to find a general solution, then use the program to do the calculation for you, feed the information to get the answer.
For example:
D: Q=a-bp
S: Q=c+dp
D=S: a-bp=c+dp
p=(a-c)/(b+d)

I write the analyses of equilibrium of demand and supply, and specific tax, that's it. If there is any question you want me to solve with code, let me know in the comment below.

    I assume you already installed python, if not, just google it. Copy and paste the code directly on the Shell then press Enter, or open a new file, copy and paste the code on to it then press F5. The shell can only set one function at a time.
    The name for the variable can be defined by languages other than English, but the important commands(for instance: for, def, range, input, if, else...etc) are in English, use English to define variables so you don't have to switch language.
def ds():
    print("D=S:a-bp=c+dp")
    a=float(input('a='))
    b=float(input('b='))
    c=float(input('c='))
    d=float(input('d='))
    p=(a-c)/(b+d)
    q=a-b*p
    print('equilibrium price and quantity')
    return p,q

def tax():
    print("Pd=Ps+t")
    a=float(input('intercept of the demand line'))
    b=float(input('slope of the demand line(absolute value)'))
    c=float(input('intercept of the supply line'))
    d=float(input('slope of the supply line'))
    t=float(input('tax'))
    p1=(a-c)/(b+d)
    ps=(a-c-b*t)/(b+d)
    pd=ps+t
    q2=a-b*pd
    eq=(round(pd,2),round(q2,2))
    q=round(pd-p1,2)
    w=round((pd-p1)/t,2)
    e=round(p1-ps,2)
    r=round((p1-ps)/t,2)
    print('equilibrium price and quantity after tax:')
    print(eq)
    print('Taxes beared by consumer:')
    print((q,w))
    print('Taxes beared by producer:')
    print((e,r))

    I guess this one will only intrigue the people from the economics department, the theoretical stuff can be found in the microeconomics textbooks, so I only talk about the code. If you are interested in the economic theories, I will write about the traditional theories in textbooks, stay tuned.

沒有留言:

張貼留言