Error message here!

Hide Error message here!

Error message here!

Hide Error message here!

Error message here!

Close

# 【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-2 蒙特卡罗(二) 重要性采样

preface

Chapter 2：One Dimensional MC Integration

```void MC_integration(double(*f)(double x), double low, double high)
{
size_t all = 1000000;
double sum{ 0 };
for (int i = 0; i < all; ++i)
{
double x = 2 * rand01();
sum += x*x;
}
stds cout << "I = " << 2 * sum / all << stds endl;
}
int main()
{
MC_integration([](double x) {return x*x; }, 0, 2);
}```

r位于（x0,x1）的概率为C*area(p(r),x0,x1),其中C为常量,为了简便，C一般取1

area(p(r),0,2) = 1（概率密度函数的总面积就是总概率）

1 = ∫0->2 C'r dr = C'r2/2|0->2 = 2C' =>C' = 1/2  公式2-1

P(x) = 0, x < 0

P(x) = x2/4, 0 < x < 2

P(x) = 1, x > 2

P(1.0) = 1/4

e = P-1(rand01())

e = sqrt(4*rand01())

```constexpr double pdf(const double x) {return 0.5 * x; }
void pdf1()
{
size_t all{ 100000 };
double sum{ 0. };
for (int i = 0; i < all; ++i)
{
double x = sqrt(4 * rand01());
sum += x*x / pdf(x);
}
stds cout << "I = " << sum / all << stds endl;
}```

1 = ∫0->2 C dr = Cr|0->2 = 2C =>C = 1/2

```constexpr double pdf(const double x) {return 0.5; }
void pdf1()
{
size_t all{ 100000 };
double sum{ 0. };
for (int i = 0; i < all; ++i)
{
double x = 2 * rand01();
sum += x*x / pdf(x);
}
stds cout << "I = " << sum / all << stds endl;
}```

p(x) = (3/8)x2

P-1(x) = pow(8x,1/3)

```constexpr double pdf(const double x) { return 3 * x * x / 8; }
void pdf1()
{
size_t all{ 1 };
double sum{ 0. };
for (int i = 0; i < all; ++i)
{
double x = pow(8 * rand01(), 1. / 3);
sum += x*x / pdf(x);
}
stds cout << "I = " << sum / all << stds endl;
}```

1. 你有一个被积函数f(x)的积分域【a,b】

2. 在域【a,b】中选择一个非零的pdf函数p()

3. 对所有的积分采样Ii = f(r)/p(r)取平均，pdf函数p()，随机数r

https://www.cnblogs.com/lv-anchoret/p/10332262.html