## Solving equations – finding roots on Monte Carlo integrals

I have an example function `f` This is expressed as a Monte Carlo integral because other methods appear to behave even less consistently. The problem with this function is that when I try to find a root of it, the function, which itself is not strictly increasing, selects sample noise from the Monte Carlo method and does so `FindRoot` Results unreliable. Below you will find a visualization of the evaluated values ​​and solutions in five separate runs:

``````Module({eqn, max, f, sols},
eqn = 8.746810530103856`*^-135 E^(-0.026341968490772614` x^2 +
x (1.8820106271944423` +
0.053267078807894754` y) + (4.776509956008959` -
0.06826554264256757` y) y) +
1.4274842937342346`*^-54 E^(-0.02045065473367165` x^2 +
x (0.8572328865549865` +
0.04911905479743718` y) + (1.8187631911441784` -
0.04806211211554676` y) y);
max = NMaxValue(eqn, {x, y});
f(p_?NumericQ) :=
Piecewise({{1, p <= 0}, {0, p >= max}},
With({v =
NIntegrate(eqn Boole(eqn >= p), {x, 0, 200}, {y, 0, 200},
Method -> "MonteCarlo", MaxPoints -> 1000000)},
Sow({p, v}); v));
sols = Table(
Reap(Quiet@
FindRoot(f(p) == 1/2, {p, 0, max}, Method -> "Brent",
PrecisionGoal -> 3, AccuracyGoal -> 3))((2, 1)), 5);
ListLinePlot(Sort /@ sols,
Epilog -> {PointSize@Large, Point@sols((All, -1))}))
``````

Okay `FindRoot` In order to achieve reasonable results, the evaluation values ​​should clearly not increase, and peaks in the diagrams clearly indicate that the search for a solution is no longer possible and that an incorrect solution is ultimately accepted due to sample noise.

This is tangentially similar to my previous question, but in my opinion sufficiently dissimilar to justify another one. Are there ways to gracefully solve this type of root finding problem with stochastic integration, for example by adjusting methods or even using another root finding function?

## Python – Monte Carlo method for checking the triangle inequality

This is a code to find inequality $$abc> 2 ( text {area of ​​the triangle})$$
Where $$a$$, $$b$$ and $$c$$ are the sides of a triangle.

The following and the corresponding code work as intended. However, they take a long time. Performance can be improved without reducing `max_tries`?

``````import random
import math
hn=100000000
num=0
max_tries=10000000
tries = 0
while tries != max_tries:
a= random.randint(1,hn)
b= random.randint(1,hn)
c= random.randint(1,hn)
if a+b>c and c+b>a and a+c>b :
s=(a+b+c)/2
tries= tries +1
k =math.sqrt(s*(s-a)*(s-b)*(s-c))
if a*b*c> 2*k:
num= num+1

print(num/max_tries)
``````

## Linear Algebra – Hit-n-Run Monte Carlo Scanning on a Convex Polytope

So I'm currently trying to implement an MCMC to evenly sample hyperpoints from the polytope that is defined as $$mathbb {K} = {x in mathbb {R} ^ {n} ; ; text {s.t.} ; ; A , x = b }$$ in the special case where there is a generic linear transformation $$A in mathbb {R} ^ {m times n}$$. $$b equiv0 in mathbb {R} ^ m$$ and the boundary conditions $$0 leq x leq1$$ stop.

Although I was able to successfully run the simulation (I'm using Julia), there are a few things I'm not sure about:

• Given that polytopes of this type tend to have star-shaped shapes in larger dimensions, two preprocessing steps are required before starting the simulation:

1. The first concerns the so-called Blocked river setting That is to find quotation from the text of the exercise, Flux $$i$$ so that $$max_ {x in K} x_i = min_ {x in K} x_i = z_i$$ and remove such variables from the system by adjusting the vector $$b$$, Can someone please explain what the hell that means?

2. The second is to find an optimal inner point $$mathbb {K}$$ as the starting point of the chain, which must be intuitively far from the vertices of the polytope. The text tells me the following: e.g. by calculating $$frac {1} {2n} sum_ {i = 1} ^ n (x ^ { min, i} + x ^ { max, i})$$ Where $$x ^ { min, i} in arg min_ {x in K} x_i$$ and $$x ^ { max, i} in arg max_ {x in K} x_i$$, Here I simply don't understand the notation: I suppose I should calculate a weighted average of the centers of each edge of the polytope, but I cannot see how this is related to the above wording.

• as with any coherent mcmc, the walk in the state space must be at t. it satisfies the detailed balance sheetIn this case, the text says that the goal should be distributed $$p (x) propto delta ^ m (Sx-b) prod_i theta (u_i-x_i) theta (x_i-l_i)$$
Again, I have no idea how to get this and not how to calculate me.

## mp.mathematical physics – Optimization of stratified sampling to generate random numbers from analytical distribution laws (e.g. for Monte Carlo applications)

Suppose I use a Monte Carlo method to calculate a value of interest $$y$$ of input parameters $$x_ {i}$$that I use to draw statistical sets from simple distribution laws.
In my case, for a single Monte Carlo iteration $$j$$I can get one $$y_j$$ Value by sampling $$N$$ Values ​​for each of my $$M$$ Input parameters that all follow a certain distribution (in my case, $$x_ {i} sim chi ^ 2_i$$), so that:

$$y_j = f (x_ {1} ^ 1, points, x_ {1} ^ N, x_ {2} ^ 1, points, x_ {2} ^ N, points, x_ {M} ^ 1, points , x_ {M} ^ N)$$

Based on some literature and to improve the convergence rate for $$y$$I tried to use a stratified sample to get a better examination of the distributions, followed by my input parameters. Basically for everyone $$x_i$$, instead of random samples $$N$$ Values ​​of a $$chi_i ^ 2$$ I split right $$N$$ Layers of $$chi_i ^ 2$$ cumulative distribution. Then I use the inverse cumulative method to get $$(x_ {i} ^ 1, dots, x_ {i} ^ N)$$ Values ​​of $$(u ^ 1, dots, u ^ N)$$ Values ​​sampled from an even distribution. To the $$N = 5$$:

Example of a stratified sample

As soon as this is achieved, I mix the set of $$(x_ {i} ^ 1, dots, x_ {i} ^ N)$$ Values. All the $$M$$ The sets obtained are mixed independently of one another.

It appears that such stratification leads to systemic distortion. I looked for a reason for this because I expected the results to be at least asymptotically the same as in a non-stratified case.
I examined a hypothesis that led me to two questions:

1. What is the better number of layers to consider? Maybe take $$N$$ Layers are a kind of overlay. In the context of the stratified sample for survey applications, some interesting work has been done to optimize the stratification of the cumulative curve to generally get the best estimate of a population characteristic (i.e. with less cost, which may mean only a few strata), few samples ). In particular, the next work by M. Khan and al. ("Determining the optimal layer limits using dynamic programming", https://www150.statcan.gc.ca/n1/en/pub/12-001-x/2008002/article/10761-eng.pdf?st=FdsrqVKZ) a method implemented in R to determine the best strata and samples for a population that follows a known distribution (e.g. normal, chi-square …). A particular problem with these methods is the fact that they take into account the actual finite size of the population from which a partial sample is taken. The situation is very different here because I want to generate random numbers for Monte Carlo applications, i.e. H. My population is theoretically "infinite".

2. Is this layering method only valid for one form of layering? $$f$$ Function (e.g. linear, like an average)? I instinctively assumed that stratification would lead to "better" random numbers and that the form of $$f$$ would not affect this hypothesis, but my biased results now make me doubtful.

If anyone could have a hint, I would be grateful.

## Python – Appreciate the area of ​​the circumcised circle with Monte Carlo

When I looked around and found nothing, I developed a simple function for estimating the area of ​​a possibly truncated circle within a frame. It uses a very simple MC implementation.

It's pretty fast, but I think it could be easier. Any thoughts are appreciated.

``````import numpy as np
from scipy.spatial import distance

def circFrac(cx, cy, rad, x0, x1, y0, y1, N_tot=100000):
"""
Use Monte Carlo to estimate the fraction of the area of a circle centered
in (cx, cy) with a radius of 'rad', that is located within the frame given
by the limits 'x0, x1, y0, y1'.
"""
# Generate a square containing the circle.

# Generate 'N_tot' uniform random points inside that square.
xr = np.random.uniform(xmin, xmax, N_tot)
yr = np.random.uniform(ymin, ymax, N_tot)

# Obtain the distances of those points to the center of the circle.
dist = distance.cdist(((cx, cy)), np.array((xr, yr)).T)(0)

# Find the points within the circle.

# Find the points that are within the frame, from the points that are
# within the circle.
msk_xy = (xr(msk_in_circ) > x0) & (xr(msk_in_circ) < x1) &
(yr(msk_in_circ) > y0) & (yr(msk_in_circ) < y1)

# The area is the points within circle and frame over the points within
# circle.
return msk_xy.sum() / msk_in_circ.sum()

# Define the (x, y) limits of the frame
x0, x1 = 0., 1.
y0, y1 = 0., 1.

for _ in range(10):
# Random center coordinates within the frame
cx = np.random.uniform(x0, x1)
cy = np.random.uniform(y0, y1)

frac = circFrac(cx, cy, rad, x0, x1, y0, y1)

print("Fraction of circle inside frame: {:.2f}".format(frac))
``````

## graphs – Monte Carlo improve

Can I improve a Monte Carlo search for the described problem?

So I have a diagram / network that consists of segments a1. a2, …, b1. b2, …, and c1. c2, …

For all underlying segments, there is some weighting, e.g. a1 = 2. b3 = 0. c3 = 4

Additionally, I have a matrix of the distances from each segment to all other segments in the network, e.g.

``````|from/to | a1  | a2  | a3  | b1  | b2  | b3  | c1  | c2  | c3  |
|--------|-----|-----|-----|-----|-----|-----|-----|-----|-----|
|   a1   |  0  | 4   | 6   | 84  | 82  | 80  | 150 | 148 | 146 |
|   a2   | ... | 0   | ... | ... | ... | ... | ... | ... | ... |
|   a3   | ... | ... | 0   | ... | ... | ... | ... | ... | ... |
|   b1   | ... | ... | ... | 0   | ... | ... | ... | ... | ... |
|   b2   | ... | ... | ... | ... | 0   | ... | ... | ... | ... |
|   b3   | ... | ... | ... | ... | ... | 0   | ... | ... | ... |
|   c1   | ... | ... | ... | ... | ... | ... | 0   | ... | ... |
|   c2   | ... | ... | ... | ... | ... | ... | ... | 0   | ... |
|   c3   | ... | ... | ... | ... | ... | ... | ... | ... | 0   |
| ...    | ... | ... | ... | ... | ... | ... | ... | ... | ... |
``````

I want to place n Agents (pictured n = 3) to cover as much of the segments as possible by weighting within a distance of 50. And to be able to, for each parameter combination, e.g. any n and every distance.

So far I have tried:

• a greedy approach: Place an agent with most segments covered (local optimum), and then place the next agent covering most segments, and so on n,
• A Monte Carlo Approach: Selection n random segments and rate, repeat many times and pick the best solution.

In reality this network and the number of agents n can be much bigger and more complex.

I wonder what other approaches might work better than a Monte Carlo?

## Distributions – Acceleration of Probability with Monte Carlo

I have a recipe for generating custom distributions for which I want to use probability (…), but find that it quickly becomes unsolvable with more than a few variables (it runs for hours). I know you may be able to approximate these probabilities with a Monte Carlo simulation, but for some reason, this does not work in my case – even for very small examples.

For example with:

``````dist =
ProbabilityDistribution(
Piecewise({{3/4, x1 == 1 && x2 == 0}, {1/4, x1 == 2 && x2 == 1}}),
{x1, 1, 2, 1}, {x2, 0, 1, 1})
``````

then

``````Probability(x1 == 1, {x1, x2} (Distributed) dist)
``````

gives the answer of ¾ well, but

``````NProbability(x1 == 1, {x1, x2} (Distributed) dist,
Method -> "MonteCarlo")
``````

gives the error "NProbability: The required samples can not be generated (dist)."

Strangely, an example with only one variable seems to work. For example with

``````dist2 =
ProbabilityDistribution(
Piecewise({{3/4, x1 == 1}, {1/4, x1 == 2}}),
{x1, 1, 2, 1})
``````

then

``````NProbability(x1 == 1, x1 (Distributed) dist2, Method -> "MonteCarlo")
``````

gives an answer that is (approximately) ¾, without error.

I know that the "MonteCarlo" method has to have some basic stuff that I do not understand, but I had a hard time finding a lot of documentation about it. Any help would be appreciated!

## Monte Carlo method

How to use a Monte Carlo method to approximate the area enclosed by the circle with radius 1 centered at the origin and the circle with radius 2 centered at (1,1)?

## Probability theory – Monte Carlo method for integrating functions

I want to approximate the integral

$$int_0 ^ infty , text {d} x exp (-x ^ 2) h (x)$$

with a limited function $$h: mathbb {R} rightarrow mathbb {R}$$ with a Monte Carlo method.

I tried the following: Let $$X_k, k in mathbb {N}$$ be exponentially distributed random variables with $$lambda = 1$$, Then we get for the random variables $$Y_k: = h (X_k)$$ the expectation:

$$int_0 ^ infty , text {d} x exp (-x) h (x)$$

I seem to be almost there, but can not get the square in the exponent.

## Can the problem of the shortest path be solved with Monte Carlo Tree Search?

I think the Monte Carlo tree search could be used to find the shortest path, but it seems that this method is only used considering the win / loss results in the simulation step.

If we consider the path length as the result of the simulation step, how would the retransmission work? It would seem that one of the nodes along the optimal path could be penalized if a child ends up with a long path in the simulation step.