-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHWintegration_LB_DB.jl
More file actions
146 lines (129 loc) · 3.74 KB
/
Copy pathHWintegration_LB_DB.jl
File metadata and controls
146 lines (129 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module HWintegration
const A_SOL = 4
println("Analytically, the change of CR equals 4")
# imports
using FastGaussQuadrature
using Roots
using Sobol
using Plots
using Distributions
using NullableArrays
using DataFrames
using DataStructures # OrderedDict
# set random seed
srand(12345)
# demand function
function f(x)
2*x^(-.5)
end
# makes a plot for question 1
function plot_q1()
global dem = plot(f, 0.1, 10, title="Demand", xaxis=("p"), yaxis=("quantitiy"), labels = "f(x)")
hline!([2, 1], label = "")
end
function question_1a(np)
for n in (10,15,20)
np = gausslegendre(n)
glx = 3/2*np[1] + 3/2
gly = [f(3/2*x + 5/2) for x in np[1]]
integ = 3/2*sum([f(3/2*x + 5/2) for x in np[1]] .* np[2])
global gl = scatter(glx, gly, labels = "GL", title = "$n nodes")
error = A_SOL-integ
println("GL-M: integral = $integ and distance to optimal sol. = $error (with $n nodes)")
end
end
function question_1b(n)
for n in (10,15,20)
mcx = rand(Uniform(1,4), n)
mcy = f.(mcx)
integ = 3/n * sum([f(x) for x in mcx])
global mc = scatter(mcx, mcy, labels = "MC", title = "$n nodes")
error = A_SOL-integ
println("MC: integral = $integ and distance to optimal sol. = $error (with $n nodes)")
end
end
function question_1c(n)
for n in (10,15,20)
s = SobolSeq(1)
qmcx = 3*[ hcat([next(s) for i = 1:n])[x][1] for x = 1:n]+1
qmcy = f.(qmcx)
integ = 3/n * sum([f(x) for x in qmcx])
global qmc = scatter(qmcx, qmcy, labels = "Quasi MC", title = "$n nodes")
println("Quasi MC: integral = $integ and distance to optimal sol. = $error (with $n nodes)")
end
end
# question 2
# theta1*p^(-1) + theta2*p^(-0.5) = 2
function question_2a()
function p(t = [0,0])
function eq(x)
exp(t[1])* float(x)^(-1) + exp(t[2])*float(x)^(-0.5) - 2
end
fzero(eq, [0,1000])
end
immutable MvNormal{Cov<:AbstractPDMat,Mean<:Vector} <: AbstractMvNormal
μ::Mean
Σ::Cov
end
μ = [0,0]
Σ = [0.02 0.01 ; 0.01 0.01]
rand(MvNormal(μ, Σ), n)
nodes = Any[]
push!(nodes,repeat(rules["hermite"][1],inner=[1],outer=[10])) # dim1
push!(nodes,repeat(rules["hermite"][1],inner=[2],outer=[5])) # dim2
weights = kron(rules["hermite"][2],kron(rules["hermite"][2],rules["hermite"][2]))
df = hcat(DataFrame(weights=weights),DataFrame(nodes,[:dim1,:dim2]))
end
function question_2b(n)
for n in (10,15,20)
μ = [0,0]
Σ = [0.02 0.01 ; 0.01 0.01]
draws = rand(MvNormal(μ, Σ), n)
draws = [[draws[1,i], draws[2,i]] for i in 1:n]
function p(t = [0,0])
function eq(x)
exp(t[1])* float(x)^(-1) + exp(t[2])*float(x)^(-0.5) - 2
end
fzero(eq, [0,1000])
end
expec= 1/n * sum(p.(draws))
var = 1/n * sum((p.(draws) - expec).^2)
println("By Monte Carlo, the expectation is $expec and the variance is $var")
end
end
function question_2bonus()
end
# function to run all questions
function runall()
info("Running all of HWintegration")
info("question 1:")
plot_q1()
for n in (10,15,20)
info("============================")
info("now showing results for n=$n")
info("question 1b:")
question_1a(n)
info("question 1c:")
question_1b(n)
info("question 1d:")
question_1c(n)
println("")
allplots = plot(dem, gl, mc, qmc, layout = (2,2))
display(allplots)
savefig("n=$n.png")
end
info("question 2a:")
q2 = question_2a(n)
println(q2)
info("question 2b:")
for n in (10,15,20)
info("Now showing results for n = $n")
question_2b(n)
println(q2b)
end
info("bonus question: Quasi monte carlo:")
q2bo = question_2bonus(n)
println(q2bo)
end
info("end of HWintegration")
end