<jupyter_start><jupyter_text>Macro HW1 First we import packages that will be used later.<jupyter_code>%matplotlib inline
from scipy import stats
import numpy as np
from scipy import stats
import math
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Question (a):Under usual regularity, $$\alpha_1 \rightarrow \frac{cov(c,y)}{var(y)}=\frac{var(p)}{var(p)+var(\xi)}$$Therefore, if farmer has higher transitory variance, ($var(\xi)$), its estimated coefficient will be lower than others. Of course, it also depends on the level of $var(p)$.For the following simulation, we simply assum all random variable are normal distributed with mean 0 as the coefficient only depends on second moments of $y$ and $c$.We define a function that gives income and consumption of a group of 50 people under the "permanent income hypothesis".<jupyter_code>def income(sigma_p,sigma_e):
    p=sigma_p*np.random.randn(50)
    e=sigma_e*np.random.randn(50)
    c=p
    y=p+e
    return (y,c)<jupyter_output><empty_output><jupyter_text>Nothing new, just the homework requirement.<jupyter_code>(sigma_p_nonfarm,sigma_e_nonfarm)=(math.sqrt(0.01),math.sqrt(0.02))<jupyter_output><empty_output><jupyter_text>The core for question (a). Here it construct the economy with two groups equipped with different income variances and then plot the regression (not easy to adjust the graph format).<jupyter_code>def esti(a,b):

    (sigma_p_farm,sigma_e_farm)=(math.sqrt(a)*sigma_p_nonfarm,math.sqrt(b)*sigma_e_nonfarm)
    famer=income(sigma_p_farm,sigma_e_farm)
    nonfamer=income(sigma_p_nonfarm,sigma_e_nonfarm)
    
    x,y=famer
    fig, ax = plt.subplots()
    slope, intercept, r_value, p_value, std_err= stats.linregress(x,y)
    
    ax.plot(x, slope * x +intercept, color='red', label='farmer reg')    
    ax.scatter(x, y, color='blue', label='farmer')
    x,y=nonfamer
    #fig, ax = plt.subplots()
    slope2, intercept2, r_value, p_value, std_err= stats.linregress(x,y)
    ax.plot(x, slope2 * x +intercept2, color='green', label='nonfarmer reg')
    ax.scatter(x, y, color='purple', label='nonfarmer')
    ax.plot(x,   x , color='yellow', label='45% line')
    ax.legend(loc='upper left')
    plt.xlabel('income')
    plt.ylabel('consumption')
    plt.show()
    
    print "farmer's estimated function is {a}+({b})*x\nnon-farmer is {a2}+({b2})*x".format(
        b=slope,a=intercept,b2=slope2,a2=intercept2 )
def senario(n):
    if n==1:
        a=1
        b=3
    if n==2:
        a=10
        b=3
    if n==3:
        a=0.1
        b=3
    esti(a,b)<jupyter_output><empty_output><jupyter_text>Question a (i)<jupyter_code>senario(1)<jupyter_output><empty_output><jupyter_text>Question a (ii)<jupyter_code>senario(2)<jupyter_output><empty_output><jupyter_text>Question a (iii)<jupyter_code>senario(3)<jupyter_output><empty_output><jupyter_text>According to formula, we expect to see $\alpha_1$ is close to $\frac{1}{7}=0.14, \frac{5}{8}=0.625, \frac{1}{61}=0.016$ for case (i), (ii), (iii).What we have seen for the estimation are $0.156, 0.68, 0.026$.$\alpha_1$ indeed increases as $var(\xi)$ decreases more relatively to $var(p)$. Question (b):Under Permanent Income Hypothesis, saving rate is supposed to be negative correlated with income growth as income is serial independent, which mean high income (high saving rate) for this period implies low income growth because expected next period's income does not change.We simply generate two periods income and consumption and plot it.<jupyter_code>def income_2periods():
    p=np.random.randn(50)
    e=np.random.randn(50)
    e2=np.random.randn(50)
    c=p
    c2=p
    y=p+e
    y2=p+e2
    return (y,c,y2,c2)
def saving_growth():
    y,c,y2,c2=income_2periods()
    s=(y-c)/y
    g=(y2-y)/y
    slope, intercept, r_value, p_value, std_err= stats.linregress(s,g)
    fig, ax = plt.subplots()
    
    ax.plot(s, slope * s +intercept, color='green', label='g vs s regression')
    ax.scatter(s, g, color='purple', label='g vs s')
    ax.legend(loc='right left')
    plt.xlabel('saving rate')
    plt.ylabel('growth rate')
    plt.show()
    print "The regression of g against s is {a}+({b})*x".format(b=slope,a=intercept)
saving_growth()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13647114474929045, 'fraction_numerical': 0.021050141911069062, 'mean_word_length': 4.707152496626181, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '556559', 'license': 'Apache-2.0', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1529, 'n_words': 380, 'path': 'for_ipython/Homwork.ipynb', 'repo_name': 'wldtw2008/tqdb', 'size': '4228'}