Need Help with Monte Carlo Algorithms in C++?

Click For Summary

Discussion Overview

The discussion revolves around implementing Monte Carlo algorithms, specifically the hit-and-miss method and the veto algorithm, in C++. Participants express challenges with coding syntax and seek assistance with specific problems related to generating random numbers according to a given distribution.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks help with coding Monte Carlo methods in C++ after a long absence from programming.
  • Another participant suggests creating separate posts for each specific problem and using a homework template if applicable.
  • A participant introduces the function (sin x)^2/x^2 and discusses generating random numbers using the Monte Carlo hit-and-miss method.
  • There is a question regarding the interpretation of the function and its implications for the Monte Carlo method, particularly concerning the unbounded range of x.
  • One participant proposes using L'Hôpital's rule to address concerns about the function's behavior at x=0.
  • Another participant suggests using a test function g(y) to improve the efficiency of the algorithm and discusses the potential for high rejection rates when x is large.
  • Concerns are raised about maintaining the probability density when using the test function g(x) and suggestions for a stratified rejection method are made.
  • A participant provides a link to additional resources that may be relevant to the discussion.

Areas of Agreement / Disagreement

Participants express varying interpretations of the function and its implications for the Monte Carlo method. There is no consensus on the best approach to address the challenges presented by the unbounded range of x and the efficiency of the algorithm.

Contextual Notes

Participants highlight limitations related to the unbounded x range and the potential for high rejection rates in the Monte Carlo method. The discussion includes unresolved mathematical steps and differing opinions on the use of test functions.

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
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 3 ·
Replies
3
Views
9K
Replies
6
Views
3K