<jupyter_start><jupyter_text>[](https://mybinder.org/v2/gh/NumEconCopenhagen/NumEconNotebooks/master?urlpath=lab/tree/micro/04_Substitution_income_and_wealth_effects.ipynb)  Setup<jupyter_code>%matplotlib inline
%load_ext autoreload
%autoreload 2

import numpy as np

import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import ipywidgets as widgets

import numecon.micro.consumption as consumption<jupyter_output><empty_output><jupyter_text>Static: Decomposition with exogenous income The budget set is $$C(p_1,p_2,I) = \{(x_1,x_2) \in \mathbb{R}_{+}^2 \,\, | \,\, p_1 x_1 + p_2 x_2 \leq I\}$$We normalize with $p_2 = 1$ and consider a change in $p_1$ to $p_1^{\prime}$.<jupyter_code>consumer = consumption.ConsumerClass(preferences='cobb_douglas')
fig,ax = consumer.figure()
A,B,C = consumer.plot_decomposition_exogenous(ax,p1_old=1,p1_new=2,p2=1)

print(f'A = ({A[0]:5.2f},{A[1]:5.2f})')
print(f'B = ({B[0]:5.2f},{B[1]:5.2f})')
print(f'C = ({C[0]:5.2f},{C[1]:5.2f})')<jupyter_output>A = ( 5.00, 5.00)
B = ( 3.54, 7.07)
C = ( 2.50, 5.00)<jupyter_text>Static: Decomposition with endogenous income The budget set is $$C(p_1,p_2,e_1,e_2) = \{(x_1,x_2) \in \mathbb{R}_{+}^2 \,\, | \,\, p_1 x_1 + p_2 x_2 \leq p_1 e_1 + p_2 e_2\}$$We normalize with $p_2 = 1$ and consider a change in $p_1$ to $p_1^{\prime}$.<jupyter_code>consumer = consumption.ConsumerClass(preferences='cobb_douglas')
fig,ax = consumer.figure()
A,B,C1,C2 = consumer.plot_decomposition_endogenous(ax,p1_old=1,p1_new=2,p2=1,e1=6,e2=4)

print(f'A  = ({A[0]:5.2f},{A[1]:5.2f})')
print(f'B  = ({B[0]:5.2f},{B[1]:5.2f})')
print(f'C1 = ({C1[0]:5.2f},{C1[1]:5.2f})')
print(f'C2 = ({C2[0]:5.2f},{C2[1]:5.2f})')<jupyter_output>A  = ( 5.00, 5.00)
B  = ( 3.54, 7.07)
C1 = ( 2.50, 5.00)
C2 = ( 4.00, 8.00)<jupyter_text>Interactive: Cobb-Douglas<jupyter_code>consumption.interactive_slutsky_exogenous(preferences='cobb_douglas')
consumption.interactive_slutsky_endogenous(preferences='cobb_douglas')<jupyter_output><empty_output><jupyter_text>Interactive: CES<jupyter_code>consumption.interactive_slutsky_exogenous(preferences='ces')
consumption.interactive_slutsky_endogenous(preferences='ces')<jupyter_output><empty_output><jupyter_text>Deadweight loss  Integration<jupyter_code># a. calculations
x = np.linspace(1e-8,10,1000)
y = 1/x
I = x <= 1

# a. figure
fig = plt.figure(figsize=(6,4),dpi=100)
ax = fig.add_subplot(1,1,1) 
ax.plot(x,y,color='black',lw=2)
ax.fill_between(x[I],y[I],np.ones(np.sum(I)),color='black',alpha=0.3)
ax.set_xlim([0,3])
ax.set_ylim([0,5])
ax.set_xlabel('x')
ax.set_ylabel('y');<jupyter_output><empty_output><jupyter_text>Midpoint approximation of integral<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

# a. interval
x0 = 1
x1 = 2
z0 = 1.2
z1 = 1.6

# b. functions
f = lambda x: np.log(x)
f_prime = lambda x: 1/x

# c. points
x = np.linspace(1,2,1000)
y = f(x)

# d. box
width = z1-z0
midpoint = (z0+z1)/2
height = f(z0) + f_prime(z0)*0.5*width

# e. figure 
fig = plt.figure(figsize=(6,4),dpi=100)
ax = fig.add_subplot(1,1,1) 

ax.plot(x,y,lw=2,color='navy')
ax.plot([z0,z0,z1,z1],[0,height,height,0],color='firebrick')
ax.plot(x,f(z0)+f_prime(z0)*(x-z0),color='black')
ax.plot([midpoint,midpoint],[0,height],color='black',ls='--')

ax.scatter(z0,0,s=5,color='black')
ax.text(z0*0.97,0.01,'$z_0$',fontsize=10)
ax.scatter(z1,0,s=5,color='black')
ax.text(z1*1.01,0.01,'$z_1$',fontsize=10)
ax.scatter(z0,f(z0),s=5,color='black')
ax.text(z0*0.94,f(z0),'$f(z_0)$',fontsize=10)
ax.text(1.5,0.7,'$f(z_0)+f^{\\prime}(z_0)(x-z_0)$',fontsize=10)
I = (x >= z0) & (x <= z1)
ax.fill_between(x[I],y[I],color='black',alpha=0.25)

ax.set_xlim([1,2])
ax.set_ylim([0.0,0.8])

# f. box
width = z1-z0
midpoint = (z0+z1)/2
height = f(z1) - f_prime(z1)*0.5*width

# g. figure 
fig = plt.figure(figsize=(6,4),dpi=100)
ax = fig.add_subplot(1,1,1) 

ax.plot(x,y,lw=2,color='navy')
ax.plot([z0,z0,z1,z1],[0,height,height,0],color='firebrick')
ax.plot(x,f(z1)+f_prime(z1)*(x-z1),color='black')
ax.plot([midpoint,midpoint],[0,height],color='black',ls='--')

ax.scatter(z0,0,s=5,color='black')
ax.text(z0*0.97,0.01,'$z_0$',fontsize=10)
ax.scatter(z1,0,s=5,color='black')
ax.text(z1*1.01,0.01,'$z_1$',fontsize=10)
ax.scatter(z1,f(z1),s=5,color='black')
ax.text(z1*0.95,f(z1),'$f(z_1)$',fontsize=10)
ax.text(1.55,0.65,'$f(z_1)+f^{\\prime}(z_1)(x-z_1)$',fontsize=10)
I = (x >= z0) & (x <= z1)
ax.fill_between(x[I],y[I],color='black',alpha=0.25)

ax.set_xlim([1,2])
ax.set_ylim([0.0,0.8]);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22749521174718026, 'fraction_numerical': 0.07788891253458183, 'mean_word_length': 7.202443280977312, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 29, 'https://': 1, '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': '506409', 'license': 'MIT', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2308, 'n_words': 274, 'path': 'micro/04_Substitution_income_and_wealth_effects.ipynb', 'repo_name': 'Kristianuruplarsen/NumEconNotebooks', 'size': '4699'}