xxxxxxxxxx
using Plots
xxxxxxxxxx
using LaTeXStrings
xxxxxxxxxx
using QuadGK
f (generic function with 1 method)
xxxxxxxxxx
f(x) = x.*exp.(x)
1.0
0.0
xxxxxxxxxx
integral,err = quadgk(x->f(x),0,1,rtol=1e-8)
xxxxxxxxxx
using Distributions, Random
0x00003039
-870096391
1072918504
-1812426662
1073255081
-733866021
1073404543
807620846
1073368448
1919433844
1072852359
-1314842017
1073052936
-914011144
1072968597
1597808632
1073364414
-945588292
1073128917
1585669299
1073044155
-362113007
1073100625
-166402106
1073460158
-1907020342
721295190
-750225566
-1300227565
382
0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
0x00000000000000000000000000000000
1002
0
xxxxxxxxxx
Random.seed!(12345)
Distributions.Uniform{Float64}(a=0.0, b=1.0)
xxxxxxxxxx
udist = Uniform(0,1)
1.1060069030617743
xxxxxxxxxx
begin
N1 = 100
x1 = rand(udist,N1)
Ihat1 = sum(f(x1))/N1
end
1.0142308549286678
xxxxxxxxxx
begin
N2 = 5000
x2 = rand(udist,N2)
Ihat2 = sum(f(x2))/N2
end
Approximating the expected value of the Beta distribution
where
Step 1: we identify
Step 2: the function
Step 3 we can use Julia to easily draw
Step 4 we approximate the expectation with the expression
xxxxxxxxxx
md"""
Approximating the expected value of the Beta distribution
$\mathbb{E}[x] = \int_{p(x)}p(x)xdx$ \
where $x \sim p(x) = Beta(\alpha_1, \alpha_2)$
**Step 1**: we identify $h(x) = x$ \
**Step 2**: the function $g(x)$ is simply the probability density function $p(x)$ due the expression for $p(x)$ above:
$p(x)^* = \frac{p(x)}{\int p(x)dx} = p(x)$
**Step 3** we can use Julia to easily draw $N$ independent sample $p(x)$ using the function $Beta$. And finally \
**Step 4** we approximate the expectation with the expression
$\mathbb{E}_{Beta(\alpha_1,\alpha_2)} \approx \frac{1}{N}\sum_i x_i$
"""
0.124967
0.118652
0.180817
0.0861668
0.29038
0.187994
0.0612246
0.265767
0.244011
0.0224634
0.165698
0.104004
0.259315
0.0518921
0.0215428
0.0259476
0.0996249
0.481008
0.213145
0.224821
0.185653
0.117721
0.0683293
0.290925
0.215163
0.270876
0.0719986
0.233029
0.152301
0.0936658
xxxxxxxxxx
begin
alpha1 = 2
alpha2 = 10
N = 100000
betaDist = Beta(alpha1,alpha2)
x_b = rand(betaDist,N)
end
0.1667417098821608
xxxxxxxxxx
# Montecarlo expectation
expectMC = mean(x_b)
0.16666666666666666
xxxxxxxxxx
# Analytic Expression for Beta Mean
expectAnalytic = alpha1 / (alpha1 + alpha2)
Monte Carlo Approximation for Optimization
Monte Carlo Approximation can also be used to solve optimization problems of the form:
if g(x) fulfills the same criteria described above (namely that it is a scaled version of a probability distribution), then (as above) we can define the probability function
This allows us to instead solve problem
if we can sample form
xxxxxxxxxx
md"""
#### Monte Carlo Approximation for Optimization
Monte Carlo Approximation can also be used to solve optimization problems of the form:
$\hat{x} = \underset{x\in(a,b)}{\operatorname{argmax}} \;g(x)$
if g(x) fulfills the same criteria described above (namely that it is a scaled version of a probability distribution), then (as above) we can define the probability function
$p(x) = \frac{g(x)}{C}$
This allows us to instead solve problem
$\hat{x} = \underset{x}{\operatorname{argmax}}\;Cp(x)$
if we can sample form $p(x)$, the solution $\hat{x}$ is easily found by drawing samples from $p(x)$ and determining the location of the samples that has the highest density (Note that the solution is not dependent of the value of $C$). The following example demonstrates Monte Carlo optimization:
"""
Example: Monte Carlo Optimization of
Say we would like to find the value of
We could solve for
where
xxxxxxxxxx
md"""
#### Example: Monte Carlo Optimization of $g(x) = e^{-\frac{(x-4)^2}{2}}$
Say we would like to find the value of $x_{opt}$ which optimizes the function $g(x) = e^{-\frac{(x-4)^2}{2}}$. In other words we want to solve the problem
$x_{opt} = \underset{x}{\operatorname{argmax}}\; e^{-\frac{(x-4)^2}{2}}$
We could solve for $x_{opt}$ using standard calculus methods, but a clever trick is to use Monte Carlo approximation to solve the problem. First, notice that $g(x)$ is a scaled version of a Normal distribution with mean equal to 4 and unit variance:
$g(x) = C \times \frac{1}{\sqrt{2\pi}}e^{-\frac{(x-4)^2}{2}}$
$= C \times \mathcal{N}(4,1)$
where $C=\sqrt{2\pi}$. This means we can solve for $x_{opt}$ by drawing samples from the normal distribution and determining where those samples have the highest density. The following chunck of Julia code solves the optimization problem in this way
"""
g₃ (generic function with 1 method)
xxxxxxxxxx
# Monte Carlo Optimization of exp(x-4)^2
begin
N₃ = 100000
x₃ = 0:0.1:6
C₃ = sqrt(2*pi)
g₃(x) = exp.(-0.5(x.-4)^2 )
end
xxxxxxxxxx
using StatsBase
xxxxxxxxxx
begin
y₃ = pdf.(Normal(4,1),x₃)
# Calculate Monte Carlo Approximation
x_normal = rand(Normal(4,1),N₃)
bins = range(min(x_normal...),stop=max(x_normal...),length=100)
h = fit(Histogram,x_normal,bins)
xHat = bins[argmax(h.weights)]
bar(bins[1:99],h.weights ./ max(h.weights...),fillcolor=:yellow,fillalpha=0.2,linewidth=0.1,label="Normalized Histogram")
plot!(x₃,g₃.(x₃)/C₃,linecolor=:red,linewidth=3,label=L"g_3(x)")
plot!(x₃,g₃.(x₃),linecolor=:blue,linewidth=2, label=L"p(x)=\frac{g_3(x)}{C}")
plot!([4,4],[0,1],linecolor=:black,linewidth=2,label=L"x_{opt}")
plot!([xHat,xHat],[0,1],linecolor=:lightgreen,linewidth=2,label=L"\hat{x}")
end
xxxxxxxxxx
begin
hh = fit(Histogram,x_normal,bins);
bar(hh)
end