Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small error in "One Dimensional MC Integration" #438

Closed
domitry opened this issue Apr 3, 2020 · 1 comment
Closed

Small error in "One Dimensional MC Integration" #438

domitry opened this issue Apr 3, 2020 · 1 comment

Comments

@domitry
Copy link

domitry commented Apr 3, 2020

$$ I = \int_{0}^{2} x^2 dx $$

While the interval of integration is [0, 2], corresponding sample code is as below:

int N = 1000000;
double sum;
for (int i = 0; i < N; i++) {
auto x = random_double(0,2);
sum += x*x;
}
std::cout << std::fixed << std::setprecision(12);
std::cout << "I = " << sum/N << '\n';

Should sum/N be replaced to 2*sum/N?

@petershirley
Copy link
Collaborator

Let's try f(x) = 1 as the integrand. That gives "I = 2" as the analytic answer. Then the sum += 1 should be what we have. So sum = N. Then we divide by N and get I=1. OK yes something is wrong here. We need to divide by p(x) which is 1/2, so multiply by 2. I agree with the fix.

hollasch added a commit that referenced this issue Apr 8, 2020
Also:
- fixes uninitialized sum
- removes unreferenced variables

Resolves #438
@hollasch hollasch modified the milestones: v3.1.0, v3.0.2 Apr 8, 2020
@hollasch hollasch assigned hollasch and unassigned petershirley and trevordblack Apr 8, 2020
hollasch added a commit that referenced this issue Apr 8, 2020
Also:
- fixes uninitialized sum
- removes unreferenced variables

Resolves #438
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants