# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.6 64-bit
#     name: python3
# ---

#Two-Period Crusonia Model
import numpy as np
import math
import matplotlib.pyplot as plt
import pandas as pd


# +
#Friday's Uility Function; The Objective Function
def u(c1, c2):
    return math.log(c1) + ß*math.log(c2) 
#The Crusonia Plant's Budget Constraint; The Constraint
def c(c1, c2):
    return c1 + c2/π


#Parameters:
ß = 0.97
π = 1.05
c0 = 100

# +
#System of Equations for the Optimal Bundle:
#I derived these equations from the revelent Euler-Lagrange equations.
A = np.array([[1+ß,0], [0,(1+ß)/(ß*π)]])
B = np.array([c0,c0])
C = np.linalg.solve(A, B)

#Optimal Choice in Periods 1 and 2:
c1 = C[0]
c2 = C[1]


#Printing the Results: 
print('Initial Mass of the Crusonia Plant:', c(c1, c2)) #This line makes sure that my results are consistent. 
print("Friday makes the best use of his Crusonia plant when he consumes", c1, "units of Crusonia today and", c2, "units of Crusonia tomorrow.")
print("Friday's Total Utility:", u(c1, c2))

# +
#Computing Friday's income


income = c0/(1+1/π)
print("Friday's income:",income)
print("The product of Friday's discount factor and his Crusonia plant's productivity:",ß*π)
print("The ratio of Friday's consumption tomorrow to his consumption today:", c2/c1)


if income > c1:
    print("Friday saves.")

if income < c1:
    print("Friday dissaves.")


if income == c1:
    print("Friday consumes his income.")


print("The ratio of Friday's income to his consumption today:", income/c1)
print("The ratio of Friday's income to his consumption tomorrow:", income/c2) #Not quite because I don't have his income tomorrow!

# +
#Graph of Friday's Consumption Across Time:
x = np.array(["Period 1", "Period 2"])
y = np.array([c1, c2])

twoperioddf = pd.DataFrame(data=np.column_stack((x,y)),columns=['Period',"Consumption"])
print(twoperioddf.to_string(index=False))

plt.bar(x,y, width=1.25)

font1 = {'family':'MS Reference Sans Serif', 'color':'darkslategray','size':14}
font2 = {'family':'MS Reference Specialty', 'color':'black','size':18}

plt.xlabel("Period of Time",  fontdict = font1)
plt.ylabel("Friday's Consumption",  fontdict = font1)
plt.title("Friday's Optimal Consumption Over Time", fontdict = font2)
plt.show()


# +
#Plotting the Budget Constraint and the Indifference Curve of the Optimal Level of Utility:

#Plotting the  Crusonia Plant's Budget Constraint:
def B(c1):
    return π*(c0 - c1) #I define Friday's consumption tomorrow as a function of his consumption today


def plot_constraint(ax, c0, π):
    c1_bc = np.array([0, c0])
    c2 = B(c1_bc)
    ax.plot(c1_bc, c2, color='#2f4b7c')
    ax.fill_between(c1_bc, 0, c2, color='#2f4b7c', alpha=0.2)
    ax.set_xlabel("Consumption Today", fontdict = font1)
    ax.set_ylabel("Consumption Tomorrow", fontdict = font1)
    return ax

fig, ax = plt.subplots()
plot_constraint(ax, c0, 1/π)


plt.axis('square')
plt.title("Friday's Economic Problem:", size=12, fontdict = font2)
plt.plot(c1, c2, 'o', color='#a05195')

plt.subplot() 
xlist = np.linspace(0, c0+100, 100) # Create 1-D arrays for x,y dimensions
ylist = np.linspace(0, c0+100, 100) 
X,Y = np.meshgrid(xlist, ylist) # Create 2-D grid xlist,ylist values
F = np.log(X) + ß*np.log(Y) - u(c1, c2)  #Implicit equation for the indifference curve
plt.contour(X, Y, F, [0], colors = '#a05195', linestyles = 'solid')

plt.show()

StarcoderdataJupyterScriptsDedupFiltered