xxxxxxxxxx1
1
using StatisticsSampleAvgxxxxxxxxxx4
1
Base. mutable struct SampleAvg2
t::Int = 03
avg::Float64 = 0.04
endxxxxxxxxxx5
1
function (s::SampleAvg)(x)2
s.t += 13
s.avg += (x - s.avg) / s.t4
s.avg5
endrun_once (generic function with 1 method)xxxxxxxxxx12
1
function run_once(b)2
rms = Float64[]3
distribution = randn(b)4
expectation = mean(distribution)5
sample_avg = SampleAvg()6
7
for i in 1:2*b8
avg = sample_avg(distribution[rand(1:b)])9
push!(rms, abs(avg - expectation))10
end11
rms12
endxxxxxxxxxx1
1
using Plotsxxxxxxxxxx12
1
begin2
n_runs = 10003
p = plot(legend=:topright)4
5
for b in [2, 10, 100, 1000]6
rms = mean(run_once(b) for _ in 1:n_runs)7
xs = (1:2*b) ./ b8
plot!(p, xs, rms, label="b=$b")9
end10
11
p12
end