<jupyter_start><jupyter_text>Model analysis project For the model analysis project, we have chosen to focus on the utility maximization problem from basic consumer theory. It is stated that a rational consumer will maximize their utility based on their disposable income for a given commodity. This means that the consumer have a chosen amount of money to spend on a specific group of interchangable commodities, and therefore, must choose a quantity of each commodity within their budget. This is done based on prices of each commodity and individual preferences of the consumer.  Describing and solving utility-maximization subject to a budgetconstraint Utility-maximization is based on a consumers individual preferences, illustrated by an equation explaining utility given by interchangable goods. These preferences are subject to a budget-constraint, illustrating the chosen amount of money, the consumer has chosen to spend on the group of goods in question. The two equations together is an example of a consumerproblem. We have chosen to explain the optimization of utility based on the simple equations underneath.<jupyter_code># we start out by importing the necessary packages to both manipulate and illustrate the equations and data we have chosen.
import numpy as np
import scipy as sp
import math
from scipy import linalg
from scipy import optimize
from scipy import interpolate
import sympy as sm
import matplotlib.pyplot as plt
# we also toggle on nice printing to illustrate our equations
sm.init_printing(use_unicode=True)
# we go on to defining our variables as sympy symbols for equation handling
q1 = sm.symbols('q_1')
q2 = sm.symbols('q_2')
p1 = sm.symbols('p_1')
p2 = sm.symbols('p_2')
I = sm.symbols('I')
U = sm.symbols('U')
# Next thing is to state the utility function
utility=q1*q2/(q1+q2)
utility
# The consumers preferences are subject to the following budgetconstraint
budgcons=sm.Eq(p1*q1+p2*q2,I)
budgcons
# The indifference curve with respect to q2 is defined for later use
indiff=sm.solve(sm.Eq(q1*q2/(q1+q2),U),q2)
indiff<jupyter_output><empty_output><jupyter_text>With the model set, we can begin solving the optimization problem theoretically.<jupyter_code># Isolating q2 in the budgetconstraint for later substitutaion
q2_iso=sm.solve(budgcons,q2)
q2_iso
q1_iso=sm.solve(budgcons,q1)
q1_iso
# Substituting q2 into the utility function
util_subsq2=utility.subs(q2,q2_iso[0])
util_subsq2
util_subsq1=utility.subs(q1,q1_iso[0])
util_subsq1
# Deriving the first order condition
foc1=sm.diff(util_subsq2,q1)
foc1
foc2=sm.diff(util_subsq1,q2)
foc2
# Deriving the solution for the optimal quantity of commodity 1 based on prices of two commodities and budget.
sol1 = sm.solve(sm.Eq(foc1,0),q1)
sol1
sol2 = sm.solve(sm.Eq(foc2,0),q2)
sol2<jupyter_output><empty_output><jupyter_text>Optimization with values As an example of the utility optimization problem, we start out by defining the utility function, the budgetconstraint and the indifference curve which we found in the algebraic solutions above.<jupyter_code># We start out by defining the functions found above
# The utility function is defined by
def util(q1,q2):
    return (q1*q2)/(q1+q2)
# The budget constraint with respect to q2 is defined by
def budgcons(q1,p1,p2,I):
    return (I/p2)-(p1/p2)*q1
# The indifference function with respect to q2 is defined by
def indiff(q1,u):
    return (-u*q1)/(u-q1)<jupyter_output><empty_output><jupyter_text>As we have found the solutions for the quantities with the chosen utility function and budgetconstraint above, we can define q1 and q2 as part of the the function to find the optimal values. Furthermore, as we got two optimal solutions for each of the quantities of the two commodities, we know that the solution must be positive for each of the commoditites. Therefore we choose the solution functions that gives positive amounts.<jupyter_code># We then go on to defining the funtion for finding the optimal quantities.
def optimum(p1,p2,I):
    q1= (I*p1-I*math.sqrt(p1*p2))/(p1**2-p1*p2)
    q2= -(I*p2-I*math.sqrt(p1*p2))/(p1*p2-p2**2)
    u=util(q1,q2)
    return q1, q2, u<jupyter_output><empty_output><jupyter_text>As we need prices and a budget value to solve for the optimal quantities, we have chosen the price of q1 as 0.5 and the price of q2 as 1. Furthermore, the budget amount is set to 100.<jupyter_code># Defining the prices and the budget amount. Furthermore, defining boundaries for the visualization later on.
p1, p2 = 0.5, 1
I = 100

pmin, pmax = 0.5, 4
Imin, Imax = 10, 200
qmax = (3/4)*Imax/pmin
# We then find the optimal quantities for the commodities, given our set prices and budget amount.
optimum(0.5,1,100)
# Finally we define a function to plot the budget constraint and indifference curve for the model to show the optimal point.

def consume_plot(p1=p1, p2=p2, I=I):

    q1 = np.linspace(0.1,qmax,num=100)
    q1e, q2e, u = optimum(p1, p2 ,I)
    idfc = indiff(q1,u)
    budg = budgcons(q1, p1, p2, I)

    fig, ax = plt.subplots(figsize=(8,8))
    ax.plot(q1, budg, lw=2.5)
    ax.plot(q1, idfc, lw=2.5)
    ax.vlines(q1e,0,q2e, linestyles="dashed")
    ax.hlines(q2e,0,q1e, linestyles="dashed")
    ax.plot(q1e,q2e,'ob')
    ax.set_xlim(0, qmax)
    ax.set_ylim(0, qmax)
    ax.set_xlabel(r'$c_1$', fontsize=16)
    ax.set_ylabel('$c_2$', fontsize=16)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.grid()
    plt.show()
# Calling the function for the graph gives us a visualization of the optimal quantities of the 
# commodities, also given by our optimum function.
consume_plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0824449182658138, 'fraction_numerical': 0.03109452736318408, 'mean_word_length': 5.033226152197213, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '311765', 'license': 'MIT', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1823, 'n_words': 703, 'path': 'modelproject/modelproject.ipynb', 'repo_name': 'NumEconCopenhagen/projects-2019-whynot', 'size': '5628'}