Jun / Sep 25 2019

Chapter08 Expectation VS Sample

using ReinforcementLearning
using StatsBase, Plots
function run_once(b)
    rms = Float64[]
    distribution = randn(b)
    expectation = mean(distribution)
    sample_avg = SampleAvg()
    
    for i in 1:2*b
        avg = sample_avg(distribution[rand(1:b)])
        push!(rms, abs(avg - expectation))
    end
    rms
end

n_runs = 1000
p = plot(legend=:topright)

for b in [2, 10, 100, 1000]
    rms = mean(run_once(b) for _ in 1:n_runs)
    xs = (1:2*b) ./ b
    plot!(p, xs, rms, label="b=$b")
end

p