Need Help with Monte Carlo Algorithms in C++?

Click For Summary
SUMMARY

The discussion focuses on implementing Monte Carlo algorithms in C++, specifically the hit-and-miss method and the veto algorithm. Participants emphasize the importance of addressing specific programming challenges, particularly with the function (sin x)²/x², and the complexities arising from its unbounded range. They recommend using rejection sampling techniques and suggest employing a test function g(y) to improve algorithm efficiency. The conversation highlights the necessity of clear problem definitions and structured approaches to coding in C++.

PREREQUISITES
  • Understanding of Monte Carlo methods, specifically hit-and-miss and rejection sampling techniques.
  • Proficiency in C++ programming, including syntax and function implementation.
  • Familiarity with mathematical concepts such as L'Hôpital's rule and probability density functions.
  • Knowledge of algorithm efficiency and optimization strategies in computational methods.
NEXT STEPS
  • Research Monte Carlo rejection sampling techniques for generating random variables from probability densities.
  • Explore the implementation of test functions in Monte Carlo algorithms to enhance efficiency.
  • Study the application of L'Hôpital's rule in resolving indeterminate forms in mathematical functions.
  • Investigate stratified sampling methods to reduce rejection rates in Monte Carlo simulations.
USEFUL FOR

Students, researchers, and developers working on numerical simulations, particularly those interested in Monte Carlo methods and C++ programming.

Chromatic_Universe
Gold Member
Messages
12
Reaction score
0
I would like to discuss code for hit and miss monte carlo methods, and also monte carlo with veto algorithm in C++. Since I am coding in C++ after a long time, I am messed up with syntax too. I have a specific set of problems to work with. If interested we can start working on it here.
 
Technology news on Phys.org
Could you code Matlab, besides C++?
 
Nguyen Son said:
Could you code Matlab, besides C++?
Used it once before, but lost touch.
 
Hello @Chromatic_Universe
:welcome:
Chromatic_Universe said:
I have a specific set of problems to work with.
There are numerous members here (including myself) with experience in C++ and monte carlo methods. If you have specific problems that you are stuck on, please create a post for each specific set of issues. Also if these are homework problems, please remember to use the homework template in your post.
 
NFuller said:
Hello @Chromatic_Universe
:welcome:

There are numerous members here (including myself) with experience in C++ and monte carlo methods. If you have specific problems that you are stuck on, please create a post for each specific set of issues. Also if these are homework problems, please remember to use the homework template in your post.
Will do so! Thanks !
 
We have the function sin^2/x^2, given x>=0. Now we have to generate random numbers according to the given distribution using Monte Carlo hit-and-miss method. I want to do it in C++.
@NFuller you can see the problem now.
 
Chromatic_Universe said:
We have the function sin^2/x^2, given x>=0.
Is this correct? What are you taking the sin of? sin2(?)/x2
Now we have to generate random numbers according to the given distribution using Monte Carlo hit-and-miss method. I want to do it in C++.
Do you mean the Monte Carlo rejection sampling method for generating a random variable from a probability density? This example presents a problem because of the unbounded x range. That is more than a programming problem. Do you have a specific way that you want to address that problem? (You may want to consider that there will be a lot of samples rejected when x is large.)
 
FactChecker said:
Is this correct? What are you taking the sin of? sin2(?)/x2
Do you mean the Monte Carlo rejection sampling method for generating a random variable from a probability density? This example presents a problem because of the unbounded x range. That is more than a programming problem. Do you have a specific way that you want to address that problem? (You may want to consider that there will be a lot of samples rejected when x is large.)
@FactChecker It would be (sin x)2/x2. And it does not blow up at x=0 because we apply L'Hopital's rule(https://en.wikipedia.org/wiki/L'Hôpital's_rule), hence it's finite(You can also check the intensity distribution for a single slit diffraction experiment - http://hyperphysics.phy-astr.gsu.edu/hbase/phyopt/sinint.html).
A way to do it would be to take a test function g(y) which is always greater than f(x)=(sin x)2/x2(where x is a random number) for the given range and generate random "y' " and calculate values of g(y') and check with f(x). If g(y) <= f(x), we accept, otherwise we reject it. Then we repeat the procedure again.
We consider g(y) to increase efficiency of the algorithm. Otherwise, as you mentioned -
You may want to consider that there will be a lot of samples rejected when x is large.
 
Chromatic_Universe said:
@FactChecker It would be (sin x)^2/x^2. And it does not blow up at x=0 because we apply L'Hopital's rule(https://en.wikipedia.org/wiki/L'Hôpital's_rule), hence it's finite(You can also check the intensity distribution for a single slit diffraction experiment - http://hyperphysics.phy-astr.gsu.edu/hbase/phyopt/sinint.html).
Yes. I forgot for a moment that the sin was squared. I corrected it a few minutes later.
A way to do it would be to take a test function g(y) which is always greater than f(x)=(sin x)2/x2(where x is a random number) for the given range and generate random "y' " and calculate values of g(y') and check with f(x). If g(y) <= f(x), we accept, otherwise we reject it. Then we repeat the procedure again.
We consider g(y) to increase efficiency of the algorithm. Otherwise, as you mentioned -
Ok. You may want to consider the problems presented by the unbounded x range and the high number of rejections for large values of x.
 
  • #10
I don't see at the moment how you can use g(x) without changing the probability density.

You can try a simple rejection method using reasonably high limit on x and see if there is a problem with too many rejections.
If the rejections are too high, I suggest a "stratified" rejection method. For instance, figure out x0, where P( x>x0) = 1/100. Then use the rejection method for 0<x<x0 and randomly (with probability 1/100) use the rejection method on 100*f(x) where x > x0.
You can extend this to more strata if necessary.
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
86
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 3 ·
Replies
3
Views
9K
Replies
6
Views
3K