montecarlo

Calculate overlap area of two functions

徘徊边缘 提交于 2019-11-27 16:50:14
问题 I need to calculate the area where two functions overlap. I use normal distributions in this particular simplified example, but I need a more general procedure that adapts to other functions too. See image below to get an idea of what I mean, where the red area is what I'm after: This is the MWE I have so far: import matplotlib.pyplot as plt import numpy as np from scipy import stats # Generate random data uniformly distributed. a = np.random.normal(1., 0.1, 1000) b = np.random.normal(1., 0.1

Fixing set.seed for an entire session

对着背影说爱祢 提交于 2019-11-27 15:39:15
问题 I am using R to construct an agent based model with a monte carlo process. This means I got many functions that use a random engine of some kind. In order to get reproducible results, I must fix the seed. But, as far as I understand, I must set the seed before every random draw or sample. This is a real pain in the neck. Is there a way to fix the seed? set.seed(123) print(sample(1:10,3)) # [1] 3 8 4 print(sample(1:10,3)) # [1] 9 10 1 set.seed(123) print(sample(1:10,3)) # [1] 3 8 4 回答1: There

Speed up sampling of kernel estimate

懵懂的女人 提交于 2019-11-27 06:49:37
问题 Here's a MWE of a much larger code I'm using. Basically, it performs a Monte Carlo integration over a KDE (kernel density estimate) for all values located below a certain threshold (the integration method was suggested over at this question BTW: Integrate 2D kernel density estimate). import numpy as np from scipy import stats import time # Generate some random two-dimensional data: def measure(n): "Measurement model, return two coupled measurements." m1 = np.random.normal(size=n) m2 = np

Choosing random numbers efficiently

邮差的信 提交于 2019-11-27 04:51:50
I have a method, which uses random samples to approximate a calculation. This method is called millions of times, so its very important that the process of choosing the random numbers is efficient. I'm not sure how fast javas Random().nextInt really are, but my program does not seem to benefit as much as I would like it too. When choosing the random numbers, I do the following (in semi pseudo-code): // Repeat this 300000 times Set set = new Set(); while(set.length != 5) set.add(randomNumber(MIN,MAX)); Now, this obviously has a bad worst-case running time, as the random-function in theory can

Uniform random (Monte-Carlo) distribution on unit sphere

送分小仙女□ 提交于 2019-11-26 17:13:06
问题 I need a clarification with algorithm generating random values for my pet ray-tracer. I emit rays from one point. And I have the problem with distribution of these rays: I need the distribution to be uniform, but it isn't... The problem I face now is that the distribution being uniform initially is not uniform after my distortions of the space of results. So for example, I generate r and t angles if the polar coordinate system. The distribution is not uniform and it cannot be uniform: space

Choosing random numbers efficiently

和自甴很熟 提交于 2019-11-26 11:24:09
问题 I have a method, which uses random samples to approximate a calculation. This method is called millions of times, so its very important that the process of choosing the random numbers is efficient. I\'m not sure how fast javas Random().nextInt really are, but my program does not seem to benefit as much as I would like it too. When choosing the random numbers, I do the following (in semi pseudo-code): // Repeat this 300000 times Set set = new Set(); while(set.length != 5) set.add(randomNumber