Need Help with Monte Carlo Algorithms in C++?

In summary: 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.
  • #1
Chromatic_Universe
Gold Member
13
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
  • #2
Could you code Matlab, besides C++?
 
  • #3
Nguyen Son said:
Could you code Matlab, besides C++?
Used it once before, but lost touch.
 
  • #4
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.
 
  • #5
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 !
 
  • #6
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.
 
  • #7
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.)
 
  • #8
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.
 
  • #9
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:

1. What is a Monte Carlo algorithm?

A Monte Carlo algorithm is a computational method that uses random sampling to solve problems. It is named after the famous casino city in Monaco, as the algorithm relies on the principle of chance and randomness.

2. How does a Monte Carlo algorithm work?

A Monte Carlo algorithm works by using random numbers to simulate a large number of possible outcomes. These outcomes are then averaged to approximate the solution to a problem. The more simulations that are run, the more accurate the approximation becomes.

3. What are the advantages of using a Monte Carlo algorithm?

Monte Carlo algorithms are highly versatile and can be applied to a wide range of problems in various fields, including physics, economics, and computer science. They also do not require a deep understanding of the problem and can handle complex systems with many variables.

4. Are there any limitations to using Monte Carlo algorithms?

One limitation of Monte Carlo algorithms is that they can be computationally intensive and may require a large amount of time and resources to run. Additionally, they are only approximate solutions and may not always provide the most accurate results.

5. How can Monte Carlo algorithms be implemented in C++?

Monte Carlo algorithms can be implemented in C++ by using random number generators, loops, and conditional statements. Libraries such as random and ctime can also be utilized to generate random numbers. Additionally, various techniques such as importance sampling and Markov chain Monte Carlo can be incorporated into the algorithm to improve its efficiency and accuracy.

Similar threads

  • Programming and Computer Science
Replies
1
Views
977
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
646
Replies
67
Views
5K
  • Programming and Computer Science
Replies
1
Views
771
Back
Top