Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions .ipynb_checkpoints/questions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integration exercises\n",
"\n",
"1. Fackler and Miranda exercise 5.1: Demand for a commodity is given by $q(p)=2p^{-0.5}$. The price of the good falls from 4 to 1. Compute the change in consumer surplus **analytically** using calculus and **numerically** using three different methods: a Gauss-Legendre rule, Monte-Carlo, and Quasi Monte Carlo.\n",
" 1. Start out by plotting the demand function. In that plot, label the axis properly ($p$ on the $x$ axis), and add 2 horizontal lines for the equilibrium quantities at both prices $p=1,p=4$. This should help you visualize the consumer surplus, and it should guide you in the analytical solution. The simplest way to solve this first part would be to edit this `IJulia` notebook, just adding a code cell below (for the plot), and then one for the analytic solution. You can get and see how to use `IJulia` [by clicking on this link](https://github.com/JuliaLang/IJulia.jl).\n",
"\n",
" To implement the integration rules, write 3 functions, one for each sub question, and detailed below. Let each function take as argument the number of integration points. Use $n=10,15,20$ points. Each of those functions should produce the result (of course), as well as a plot where we can see integration nodes vs function value. We want to get an idea where each method places the points, and how this might influence the different results. Ideally, the result would tell us how far the corresponding method is away from your analytic solution. *Ideally*, your plot would show us all the results at once, so we can easily compare across methods. Keyword *subplots*.\n",
" 1. Gauss-legendre rule. Note that you have to change the function domain to $[-1,1]$ first. This is achieved with the following transformation\n",
"$$ \\int_a^b f(x)\\,dx = \\frac{b-a}{2} \\int_{-1}^1 f\\left(\\frac{b-a}{2}x + \\frac{a+b}{2}\\right)dx $$\n",
" 1. monte-carlo, again taking $n$ as an argument.\n",
" 1. pseudo monte-carlo. Use a Sobol Sequence.\n",
"2. Fackler and Miranda exercise 5.5: A government stabilizes the supply of a commodity at $S=2$, but allows the price to be determined on the market. Domestic and export demand for the commodity are given by $D = \\tilde{\\theta_1} p^{-1}, X = \\tilde{\\theta_2} p^{-0.5}$, and where $\\log \\tilde{\\theta_1}$ and $\\log \\tilde{\\theta_1}$ are both normally distributed with means 0, variances 0.02 and 0.01 respectively, and covariance 0.01.\n",
" 1. compute the expected price $E[p]$ and expected variance $V[p]$ using a kronecker product rule with gauss-hermite grids (10) in each dimension of the shock.\n",
" 1. perform the same computation with a monte carlo integration scheme.\n",
" 1. Bonus question: same thing with quasi-monte carlo."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## (A) Plot"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 600 400\">\n",
"<defs>\n",
" <clipPath id=\"clip9200\">\n",
" <rect x=\"0\" y=\"0\" width=\"600\" height=\"400\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polygon clip-path=\"url(#clip9200)\" points=\"\n",
"0,400 600,400 600,0 0,0 \n",
" \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip9201\">\n",
" <rect x=\"120\" y=\"0\" width=\"421\" height=\"400\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polygon clip-path=\"url(#clip9200)\" points=\"\n",
"32.4745,375.869 580.315,375.869 580.315,11.811 32.4745,11.811 \n",
" \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip9202\">\n",
" <rect x=\"32\" y=\"11\" width=\"549\" height=\"365\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 123.781,375.869 123.781,11.811 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 245.524,375.869 245.524,11.811 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 367.266,375.869 367.266,11.811 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 489.008,375.869 489.008,11.811 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 32.4745,317.221 580.315,317.221 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 32.4745,233.704 580.315,233.704 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 32.4745,150.187 580.315,150.187 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n",
" 32.4745,66.6695 580.315,66.6695 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,375.869 580.315,375.869 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,375.869 32.4745,11.811 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 123.781,375.869 123.781,370.408 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 245.524,375.869 245.524,370.408 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 367.266,375.869 367.266,370.408 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 489.008,375.869 489.008,370.408 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,317.221 40.6921,317.221 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,233.704 40.6921,233.704 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,150.187 40.6921,150.187 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,66.6695 40.6921,66.6695 \n",
" \"/>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 123.781, 389.669)\" x=\"123.781\" y=\"389.669\">2</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 245.524, 389.669)\" x=\"245.524\" y=\"389.669\">4</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 367.266, 389.669)\" x=\"367.266\" y=\"389.669\">6</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 489.008, 389.669)\" x=\"489.008\" y=\"389.669\">8</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 321.721)\" x=\"26.4745\" y=\"321.721\">1.0</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 238.204)\" x=\"26.4745\" y=\"238.204\">1.5</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 154.687)\" x=\"26.4745\" y=\"154.687\">2.0</text>\n",
"</g>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 71.1695)\" x=\"26.4745\" y=\"71.1695\">2.5</text>\n",
"</g>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#009af9; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 32.4745,11.811 93.3457,211.489 154.217,272.971 215.088,305.688 275.959,326.773 336.83,341.808 397.701,353.222 458.573,362.27 519.444,369.67 580.315,375.869 \n",
" \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#008000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 245.524,375.869 245.524,11.811 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9202)\" style=\"stroke:#ffa500; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 62.9101,375.869 62.9101,11.811 \n",
" \"/>\n",
"<polygon clip-path=\"url(#clip9200)\" points=\"\n",
"475.216,93.171 562.315,93.171 562.315,32.691 475.216,32.691 \n",
" \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 475.216,93.171 562.315,93.171 562.315,32.691 475.216,32.691 475.216,93.171 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#009af9; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 481.216,47.811 517.216,47.811 \n",
" \"/>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:start;\" transform=\"rotate(0, 523.216, 52.311)\" x=\"523.216\" y=\"52.311\">y1</text>\n",
"</g>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#008000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 481.216,62.931 517.216,62.931 \n",
" \"/>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:start;\" transform=\"rotate(0, 523.216, 67.431)\" x=\"523.216\" y=\"67.431\">p = 4</text>\n",
"</g>\n",
"<polyline clip-path=\"url(#clip9200)\" style=\"stroke:#ffa500; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n",
" 481.216,78.051 517.216,78.051 \n",
" \"/>\n",
"<g clip-path=\"url(#clip9200)\">\n",
"<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:start;\" transform=\"rotate(0, 523.216, 82.551)\" x=\"523.216\" y=\"82.551\">p = 1</text>\n",
"</g>\n",
"</svg>\n"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Plots\n",
"gr()\n",
"x=0.5:10\n",
"plot(x, 2 ./ sqrt.(x))\n",
"vline!([4], color=:green,lw=1,label=\"p = 4\")\n",
"vline!([1], color=:orange,lw=1,label=\"p = 1\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## (A) Analytical solution\n",
"\n",
"$$q(p) = 2p^{0.5}$$\n",
"\n",
"The change in consumer surplus is the difference between the consumer surplus after and before the change in prices. Let denote $\\Delta$ that number. \n",
"\n",
"$\\begin{array}{lcl}\n",
"\\Delta & = & \\displaystyle\\int_1^\\infty q(p)dp - \\displaystyle\\int_4^\\infty q(p)dp \\\\\n",
"\\Delta & = & \\displaystyle\\int_1^4 q(p)dp \\\\\n",
"\\Delta & = & \\left[4\\sqrt{p}\\right]_1^4 \\\\\n",
"\\Delta & = & 4 \\\\\n",
"\\end{array}$\n",
"\n",
"The change in consumer surplus is $4$. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.6.2",
"language": "julia",
"name": "julia-0.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
language: julia
os:
- linux
julia:
- 0.6
notifications:
email: false
testscript:
julia -e 'Pkg.build("HWintegration")'
if [ -f test/runtests.jl ]; then
julia --check-bounds=yes -e 'Pkg.test("HWintegration", coverage=true)'
fi
#script: # the default script is equivalent to the following
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'Pkg.clone(pwd()); Pkg.build("Example"); Pkg.test("Example"; coverage=true)';
Loading