using PyPlot
using Statistics
using LinearAlgebra
Let's consider a setting in which we are using 0-1 loss for our empirical risk, and imagine that our error rate is $p = 0.3$ over the whole dataset of $n = 1000000$ examples. Without loss of generality, suppose that the first $30\%$ of the examples are errors and the remainder are not. We can construct the losses of these examples as follows.
n = 1000000;
p = 0.3;
L = vcat(ones(Int64(n * p)), zeros(n - Int64(n * p)));
Next, let's sample some variables $Z_k$ which are samples from the empirical risk.
Kmax = 100000;
Z = [rand(L) for k = 1:Kmax];
Next, we compute the partial averages $$ S_K = \frac{1}{K} \sum_{k=1}^K Z_k. $$
S = cumsum(Z) ./ (1:Kmax);
Now we can plot this average and see how it changes as we increase $K$.
Kplot = 100;
plot(collect(1:Kplot), S[1:Kplot]; label="average");
plot(collect(1:Kplot), p * ones(Kplot), "k:"; label="true empirical risk");
legend();
show();
# what's the error at the end?
println("true empirical risk: $p");
println("approx empirical risk: $(S[Kplot])");
println("error : $(abs(S[Kplot]-p))");
Kplot = 1000;
plot(collect(1:Kplot), S[1:Kplot]; label="average");
plot(collect(1:Kplot), p * ones(Kplot), "k:"; label="true empirical risk");
legend();
show();
# what's the error at the end?
println("true empirical risk: $p");
println("approx empirical risk: $(S[Kplot])");
println("error : $(abs(S[Kplot]-p))");
Kplot = Kmax;
plot(collect(1:Kplot), S[1:Kplot]; label="average");
plot(collect(1:Kplot), p * ones(Kplot), "k:"; label="true empirical risk");
legend();
show();
# what's the error at the end?
println("true empirical risk: $p");
println("approx empirical risk: $(S[Kplot])");
println("error : $(abs(S[Kplot]-p))");
Let's choose some $x$ and $y$ completely at random, and evaluate the hypothesis $$h_w(x) = \operatorname{sign}(x^T w).$$
n = 1000000;
d = 256;
Xs = [randn(d) for i=1:n];
w = randn(d);
Ys = [sign(rand([-1.0,0.9,1.0,1.1])*sign(dot(Xs[i],w))) for i=1:n];
# error should be about 25%
function total_error(Xs::Array{Array{Float64,1},1}, Ys::Array{Float64,1}, w::Array{Float64,1})
n = length(Ys);
return mean(sign(dot(Xs[i],w)) != Ys[i] for i = 1:n);
end
# warm up the compiler
total_error(Xs, Ys, w);
total_error(Xs, Ys, w);
@time total_error(Xs, Ys, w)
function estimate_error(Xs::Array{Array{Float64,1},1}, Ys::Array{Float64,1}, w::Array{Float64,1}, K::Int64)
n = length(Ys);
return mean(sign(dot(Xs[i],w)) != Ys[i] for i = rand(1:n, K));
end
# warm up the compiler
estimate_error(Xs, Ys, w, 1000);
estimate_error(Xs, Ys, w, 1000);
@time estimate_error(Xs, Ys, w, 1000)
@time estimate_error(Xs, Ys, w, 10000)
@time estimate_error(Xs, Ys, w, 100000)
@time estimate_error(Xs, Ys, w, 1000000)
50*log(200)