# MC Optimization

## Recursive Integration

![](/files/-LKMyCROjraPKjPRe3v0)

## Monte Carlo Maximization

![](/files/-LKMyCRQqj6MlP1xK4DQ)

## EM Algorithm

![](/files/-LKMyCRSf_foBqfTfZB_)

## 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**.

![](/files/-LTBZr-rpGxxlhogJdxm)

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$$.

![](/files/-LTBZr-vlr3_-gG7AdQy)

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

![](/files/-LTBZr034uqVfcRznWN6)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mc.hohoweiya.xyz/mcoptim.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
