目次

モンテカルロ法と乱数

モンテカルロ法

例)多重積分

$$ \begin{align} \int_0^1\int_0^1\cdots\int_0^1f(x_1,x_2,\cdots,x_L)dx_1dx_2\cdots dx_L\\ \sim\frac{1}{N^L}\sum_{n_1=0}^{N-1}\sum_{n_2=0}^{N-1}\cdots\sum_{n_L=0}^{N-1}f(\textstyle\frac{n_1}{N},\frac{n_2}{N},\cdots,\frac{n_L}{N}) \end{align} $$ * $N$分割して$L$重積分を行うためには$N^L$回の和の計算が必要

数値積分

$$ \int_a^bf(x)dx\simeq\sum_{k=0}^{N-1}w_kf(x_k) $$

#@title Left Riemann sum
import matplotlib.pyplot as plt
import numpy as np
 
def f(x):
  return 1 - 3*x**2/5
 
xlist = np.arange(-0.2, 1.2, 0.01)
ylist = [f(x) for x in xlist]
xsample = [0.0, 0.5]
ysample = [f(x) for x in xsample]
xbar = [0.25, 0.75]
# plot
fig, ax = plt.subplots()
 
 
ax.plot(xlist, ylist, color="black")
ax.bar(xbar, ysample, width=0.5, edgecolor="white", alpha=.5, linewidth=0.7)
ax.scatter(xsample, ysample, s = 80, c = "red")
 
ax.set(xlim=(-0.1, 1.1), ylim=(0, 1.1))
 
plt.show()