times = 10 state = 0 ran_num = 0.0 for i = 1:times ran_num = rand() if ran_num >= 0.0 && ran_num <= 0.5 state += 1 elseif ran_num > 0.5 && ran_num<= 1.0 state -= 1 end print("\$state ,") end state # ２項分布の計算 ((factorial(10)/(factorial(10-6)))/factorial(6)) * (1/2)^(6) * (1/2)^4 using PyPlot using Plots function binomial_dist(n) state = 0 global X = [] for i = 0:n ran_num = rand() if ran_num >= 0.0 && ran_num <= 0.5 state += 1 elseif ran_num > 0.5 && ran_num<= 1.0 state -= 1 end #print("\$state ,") # 組み合わせの計算をしているところ var = (1 * i)+(-1 * (n - i)) #print("var: \$var , \n") # mpmathとかのインポートエラーになるのでダメ # p = ((factorial(big(n)))/(factorial(big(n-abs(var))))/factorial(big(abs(var)))) * (1/2)^(abs(var)) * (1/2)^(n-abs(var)) p = binomial(n,abs(var)) * (1/2)^(abs(var)) * (1/2)^(n-abs(var)) #p = ((factorial(n)/(factorial(n-abs(var))))/factorial(abs(var))) * (1/2)^(abs(var)) * (1/2)^(n-abs(var)) #print("p: \$p \n") push!(X,p) end end binomial_dist(15) histogram(X) PyPlot.plot(X) @show ims = [] @time for i = 1:50 binomial_dist(i) push!(ims,X) end @show ims = Any[] @time for i = 1:50 binomial_dist(i) push!(ims,X) end function binomial_repitition() @show ims = Any[] @time for i = 1:50 binomial_dist(i) push!(ims,X) end end # functionで囲ったほうが早くなる． @time binomial_repitition using PyPlot using PyCall @pyimport matplotlib.animation as anim ########## display file function displayfile(mimetype, filename) open(filename) do f base64text = base64encode(f) display("text/html", """""") end end ########## initialize the frame #xs = [ims[i][1] for j in 1:2, i in 1:length(ims)] #ys = [ims[i][2] for j in 1:2, i in 1:length(ims)] #xmin, xmax = minimum(xs)-0.5, maximum(xs)+0.5 #ymin, ymax = minimum(ys)-0.5, maximum(ys)+0.5 function initframe() xlim(xmin, xmax) ylim(ymin, ymax) grid(ls=":") end ########## update the frame function updateframe(t) # clf() #←全部消して書き直す場合にはコメントを外す #---------- フレームのアップデート開始 if t ≥ 1 X = ims[t] PyPlot.plot(X, lw=1.0) end title("t = \$t") #---------- フレームのアップデート終了 # plot() #←念のためのおまじない. []と書いてもよい. いらない場合も多い. end ########## filename filename = "randomwalk.gif" ######### Construct Figure and Plot Data fig = figure(figsize=(6.4, 4.8)) # Create the animation object by anim.FuncAnimaton frames = collect(0:length(ims)) #@time myanim = anim.FuncAnimation(fig, updateframe, init_func=initframe, frames=frames, interval=100) @time myanim = anim.FuncAnimation(fig, updateframe, frames=frames, interval=500) # Convert it to an animated GIF file by Imagemagick. @time myanim[:save](filename, writer="imagemagick") sleep(0.1) # Display the movie. displayfile("image/gif", filename) # Don't display the axes of figure(). clf() function binomial_dist_plot(n) state = 0 p = Float64 global X = Any[] for i = 1:n ran_num = rand() if ran_num >= 0.0 && ran_num <= 0.5 state += 1 elseif ran_num > 0.5 && ran_num<= 1.0 state -= 1 end # 組み合わせの計算をしているところ var = (1 * i)+(-1 * (n - i)) p = binomial(n,abs(var)) * (1/2)^(abs(var)) * (1/2)^(n-abs(var)) push!(X,p) end PyPlot.plot(X) end binomial_dist_plot(10) using PyPlot function random_walk() value = 0 value_array = Int64[] for i = 1:10000000 ran_num = rand() if ran_num >= 0.0 && ran_num < 0.5 value += 1 else value -= 1 end push!(value_array, value) end PyPlot.plot(value_array) end random_walk() random_walk() random_walk() using Plots using GR # 座標の初期値 x = 0.0 y = 0.0 xlist = Float64[] ylist = Float64[] @time function brown_plot(x,y) for i = 1:10000 # 正規分布に従う乱数の生成（rand（）は一様分布，randn()は正規乱数） x 　+= randn() y += randn() push!(xlist, x) push!(ylist, y) end end brown_plot(x, y) gr() Plots.plot(xlist, ylist,title="Brownian motion",lw=0.4) using Plots using GR # 座標の初期値 x = 0.0 y = 0.0 z = 0.0 xlist = Float64[] ylist = Float64[] zlist = Float64[] @time function brown_plot(x,y,z) for i = 1:10000 # 正規分布に従う乱数の生成（rand（）は一様分布，randn()は正規乱数） x += randn() y += randn() z += randn() push!(xlist, x) push!(ylist, y) push!(zlist, z) end end brown_plot(x, y, z) gr() # lw は線の太さ Plots.plot(xlist, ylist,zlist, title="Brownian motion",lw=0.4) using Plots gr() w = cumsum(randn(10000,3)) Plots.plot(w[:,1],w[:,2],w[:,3], title="Brownian motion",lw=0.4) using Plots plotly() N = 10000 rw(N) = cumsum(randn(N)) plot(rw(N), rw(N), rw(N), size=(500, 500))