<jupyter_start><jupyter_text>Village consumption smoothing We simulate T periods of income for N individuals.  Each individual receives a base level of income plus an income shocks.  The income shocks can be independent or correlated.  We plot income in each household with and without consumption smoothing.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, fixed, FloatSlider
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Default parameters<jupyter_code>rho = 0   
N = 5
T = 15
V = 1
def shocks(rho=0, N=N, T=T):
    '''Returns an N by T matrix of multivariate normally distributed shocks with correlation rho'''
    mean = np.zeros(N)
    cov = np.ones((N, N), int)*rho*V
    np.fill_diagonal(cov, V)
    #print(cov)
    e = np.random.multivariate_normal(mean, cov, size=T)
    return e
def incomes(rho=0, V=V, N = N, T=T):
    '''Generates random incomes for N over T periods'''
    t = np.arange(T)   # time periods
    x0 = np.arange(10,10+N*5,5)   # average income
    e = shocks(rho=rho, N=N, T=T)*V
    X = x0 + e
    
    XT = np.sum(X, axis = 1)
    
    w = x0/np.sum(x0)
    XS = np.array([XT * wt for wt in w]).T
    
    return t, X, XS
def plot_cons(rho=0, V=1):
    #print('rho = {}'.format(rho))
    t, X, XS = incomes(rho=rho, V=V, N=N, T=T)
    fig, ax = plt.subplots(figsize=(10,8))
    ax.plot(t, X,'x-')
    ax.plot(t,XS,linestyle='dashed')
    ax.set_xlabel('time')
    ax.set_xticks(np.arange(T, step=1))
    fig.suptitle(r'$\rho = {:0.2f}$'.format(rho))
    ax.grid()
    plt.tight_layout();
interact(plot_cons, rho=(-0.25,0.99,0.05));<jupyter_output><empty_output><jupyter_text>Examples We illustrate with three different scenarios.  Remember that these are random draws so will be different everytime these are run. independent incomes<jupyter_code>plot_cons(rho=0, V=1)<jupyter_output><empty_output><jupyter_text>Correlated incomesLess opportunity for risk sharing. So consumption will tend to follow income.<jupyter_code>plot_cons(rho=0.8, V=1)<jupyter_output><empty_output><jupyter_text>Negatively correlated<jupyter_code>plot_cons(rho=-0.2, V=1)<jupyter_output><empty_output><jupyter_text>Unfinished explorations<jupyter_code>import statsmodels.api as sm
import pandas as pd
t, X, XS = incomes(rho=rho, V=V, N=N, T=T)
X.sum(axis=1)
XC = XS.reshape((N*T,1))
XI = X.reshape((N*T,1))
model = sm.OLS(XC,XI)
results = model.fit()
results.params<jupyter_output><empty_output><jupyter_text>Another way to draw from a multivariate normal<jupyter_code>from scipy.spatial.distance import cdist
import scipy.linalg as spla
X = np.random.normal(size=(5,1))
K = np.exp(-cdist(X,X,"sqeuclidean")) +1e-6* np.eye(X.shape[0])
mean = np.array(np.arange(10,35,5))
mean
def multivariate_normal_sampler(mean, covariance, n_samples=1):
    L = spla.cholesky(covariance)
    Z = np.random.normal(size=(n_samples, covariance.shape[0]))
    return Z.dot(L) + mean
multivariate_normal_sampler(mean, K, 10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1378162450066578, 'fraction_numerical': 0.019973368841544607, 'mean_word_length': 4.823643410852713, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '402651', 'license': 'MIT', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1128, 'n_words': 273, 'path': 'notebooks/micro/Village_sharing.ipynb', 'repo_name': 'jhconning/simpleTeach', 'size': '3004'}