# MC Optimization

## Recursive Integration

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKMxpHIjZNJid1WlFRi%2F-LKMyCROjraPKjPRe3v0%2Frecur-int.png?generation=1534779252121026\&alt=media)

## Monte Carlo Maximization

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKMxpHIjZNJid1WlFRi%2F-LKMyCRQqj6MlP1xK4DQ%2Fmc-max.png?generation=1534779252071926\&alt=media)

## EM Algorithm

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LKMxpHIjZNJid1WlFRi%2F-LKMyCRSf_foBqfTfZB_%2Fem.png?generation=1534779252087811\&alt=media)

## Simulated Annealing

Fundamental idea: A change of scale, called **temperature**, allows for faster moves on the surface of the function of $$h$$ to maximize, whose negative is called **energy**.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LTBZqJ3XPtb9iB23Pwv%2F-LTBZr-rpGxxlhogJdxm%2Fsimulated-annealing.png?generation=1544251733598420\&alt=media)

It is important to note that if the larger $$T$$ is, accepting one decreasing is more likely.

Example: To maximize $$h(x)=\[\cos(50x)+\sin(20x)]^2$$.

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LTBZqJ3XPtb9iB23Pwv%2F-LTBZr-vlr3_-gG7AdQy%2Fex-simulated-annealing.png?generation=1544251733689519\&alt=media)

We can use the following Julia code to solve this problem:

```julia
r = 0.5
function T(t)
    return 1/log(t)
end
# target function
function h(x)
    return (cos(50x) + sin(20x))^2
end

N = 2500
x = ones(N)
y = ones(N)
for t = 1:(N-1)
    # step 1
    at = max(x[t]-r, 0)
    bt = min(x[t]+r, 1)
    u = rand() * (bt - at) + at 
    # step 2
    rho = min(exp( (h(u) - h(x[t])) / T(t) ), 1)
    if rand() < rho
        x[t+1] = u
        y[t+1] = h(u)
    else
        x[t+1] = x[t]
        y[t+1] = y[t]
    end
end
```

The trajectory of 2500 pairs $$(x^{(t)}, y^{(t)})$$ is

![](https://666993855-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJfsESZOIJn_3uGIecs%2F-LTBZqJ3XPtb9iB23Pwv%2F-LTBZr034uqVfcRznWN6%2Fex-sim-ann.png?generation=1544251733340210\&alt=media)
