import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.io import output_notebook
b = 0.8 # 相関項
x = 10.0
y = 3.0
times = 200
output_notebook()
p = figure()
for i in range(1, times):
x_old = x
x = np.random.normal(b*y, 1.0)
p.line([x_old, x], [y, y], color="blue",line_width=2)
y_old = y
y = np.random.normal(b*x, 1.0)
p.line([x, x], [y_old, y], color="blue", line_width=2)
show(p)
# ボックスミューラー法による実装.
b = 0.8 # 相関項
x = 10.0
y = 3.0
times = 150
output_notebook()
p = figure()
for i in range(1, times):
u1 = np.random.rand()
u2 = np.random.rand()
x_old = x
x = np.sqrt(-2*np.log(u1))*np.cos(2*np.pi*u2) + b*y
p.line([x_old, x], [y, y], color="blue",line_width=2)
y_old = y
y = np.sqrt(-2*np.log(u1))*np.sin(2*np.pi*u2) + b*x
p.line([x, x], [y_old, y], color="blue", line_width=2)
show(p)
p = figure()
p.line([5, 4],[4,4])
show(p)
p = figure()
imax=400
for i in range(1, imax):
u1 = np.random.rand()
u2 = np.random.rand()
p.scatter(u1, u2)
show(p)
np.random.rand()
0.6564043688589801
y=2
y_old =1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - b*y)**2 / (2 * sigma**2))
y_old
array([ 0.00914707, 0.01001891, 0.01096165, 0.01197978, 0.01307794, 0.01426092, 0.01553363, 0.01690113, 0.0183686 , 0.01994131, 0.02162464, 0.02342403, 0.02534497, 0.02739298, 0.02957361, 0.03189237, 0.03435474, 0.03696614, 0.03973186, 0.04265708, 0.04574681, 0.04900586, 0.05243879, 0.05604988, 0.05984312, 0.06382211, 0.06799009, 0.07234982, 0.07690362, 0.08165327, 0.08659998])
p = figure()
mu, sigma = 0, 0.1
s = np.random.normal(mu, sigma, 100)
p.scatter(s, s)
show(p)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# ボックスミューラー法による実装.
b = 0.8 # 相関項
x = 10.0
y = 3.0
times = 200
im = []
ims = []
fig = plt.figure()
for i in range(1, times):
u1 = np.random.rand()
u2 = np.random.rand()
x_old = x
x = np.sqrt(-2*np.log(u1))*np.cos(2*np.pi*u2) + b*y
im = im + plt.plot([x_old, x], [y, y], color="blue")
y_old = y
y = np.sqrt(-2*np.log(u1))*np.sin(2*np.pi*u2) + b*x
im = im + plt.plot([x, x], [y_old, y], color="blue")
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=400)
ani.save("output.gif", writer="imagemagick")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro', animated=True)
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init)
plt.show()