From e73dbaf370182dd73dc08b488487775804cb185f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Fri, 11 Feb 2022 00:14:38 +0100 Subject: [PATCH] update chapters 4 and 5 --- appB.jl | 12 +-- ch045.jl => ch04.jl | 177 ++++---------------------------------------- ch05.jl | 161 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+), 169 deletions(-) rename ch045.jl => ch04.jl (58%) create mode 100644 ch05.jl diff --git a/appB.jl b/appB.jl index bebab7c..137c16a 100644 --- a/appB.jl +++ b/appB.jl @@ -16,9 +16,7 @@ y = collect(x); @btime sort($y); @edit sort(x) -# CODES BELOW REQUIRE RE-NUMBERING - -# Code for exercise 3.1 +# Code for exercise 4.1 using Statistics using BenchmarkTools @@ -36,7 +34,7 @@ aq = [10.0 8.04 10.0 9.14 10.0 7.46 8.0 6.58 @benchmark [cor($aq[:, i], $aq[:, i+1]) for i in 1:2:7] @benchmark [cor(view($aq, :, i), view($aq, :, i+1)) for i in 1:2:7] -# Code for exercise 3.2 +# Code for exercise 4.2 function dice_distribution(dice1, dice2) distribution = Dict{Int, Int}() @@ -73,17 +71,19 @@ end test_dice() -# Code for exercise 3.3 +# Code for exercise 4.3 plot(scatter(data.set1.x, data.set1.y; legend=false), scatter(data.set2.x, data.set2.y; legend=false), scatter(data.set3.x, data.set3.y; legend=false), scatter(data.set4.x, data.set4.y; legend=false)) -# Code for exercise 3.4 +# Code for exercise 5.1 parse.(Int, ["1", "2", "3"]) +# CODES BELOW REQUIRE RE-NUMBERING + # Code for exercise 4.1 years_table = freqtable(years) diff --git a/ch045.jl b/ch04.jl similarity index 58% rename from ch045.jl rename to ch04.jl index d28386b..866700c 100644 --- a/ch045.jl +++ b/ch04.jl @@ -1,8 +1,8 @@ # Bogumił Kamiński, 2021 -# Codes for chapter 3 +# Codes for chapter 4 -# Code for listing 3.1 +# Code for listing 4.1 aq = [10.0 8.04 10.0 9.14 10.0 7.46 8.0 6.58 8.0 6.95 8.0 8.14 8.0 6.77 8.0 5.76 @@ -32,13 +32,13 @@ v[1] = 10 v t[1] = 10 -# Code for figure 3.2 +# Code for figure 4.2 using BenchmarkTools @benchmark (1, 2, 3) @benchmark [1, 2, 3] -# Code for section 3.1.2 +# Code for section 4.1.2 using Statistics mean(aq; dims=1) @@ -54,7 +54,7 @@ end [mean(col) for col in eachcol(aq)] [std(col) for col in eachcol(aq)] -# Code for section 3.1.3 +# Code for section 4.1.3 [mean(aq[:, j]) for j in axes(aq, 2)] [std(aq[:, j]) for j in axes(aq, 2)] @@ -65,7 +65,7 @@ axes(aq, 2) [mean(view(aq, :, j)) for j in axes(aq, 2)] [std(@view aq[:, j]) for j in axes(aq, 2)] -# Code for section 3.1.4 +# Code for section 4.1.4 using BenchmarkTools x = ones(10^7, 10) @@ -73,12 +73,12 @@ x = ones(10^7, 10) @benchmark [mean($x[:, j]) for j in axes($x, 2)] @benchmark mean($x, dims=1) -# Code for section 3.1.5 +# Code for section 4.1.5 [cor(aq[:, i], aq[:, i+1]) for i in 1:2:7] collect(1:2:7) -# Code for section 3.1.6 +# Code for section 4.1.6 y = aq[:, 2] X = [ones(11) aq[:, 1]] @@ -99,7 +99,7 @@ end ?² -# Code for section 3.1.7 +# Code for section 4.1.7 using Plots scatter(aq[:, 1], aq[:, 2]; legend=false) @@ -112,7 +112,7 @@ plot(scatter(aq[:, 1], aq[:, 2]; legend=false), plot([scatter(aq[:, i], aq[:, i+1]; legend=false) for i in 1:2:7]...) -# Code for section 3.2 +# Code for section 4.2 two_standard = Dict{Int, Int}() for i in [1, 2, 3, 4, 5, 6] @@ -156,7 +156,7 @@ for d1 in all_dice, d2 in all_dice end end -# Code for section 3.3 +# Code for section 4.3 aq = [10.0 8.04 10.0 9.14 10.0 7.46 8.0 6.58 8.0 6.95 8.0 8.14 8.0 6.77 8.0 5.76 @@ -175,14 +175,14 @@ dataset1 = (x=aq[:, 1], y=aq[:, 2]) dataset1[1] dataset1.x -# Code for listing 3.2 +# Code for listing 4.2 data = (set1=(x=aq[:, 1], y=aq[:, 2]), set2=(x=aq[:, 3], y=aq[:, 4]), set3=(x=aq[:, 5], y=aq[:, 6]), set4=(x=aq[:, 7], y=aq[:, 8])) -# Code for section 3.3.2 +# Code for section 4.3.2 using Statistics map(s -> mean(s.x), data) @@ -194,7 +194,7 @@ model = lm(@formula(y ~ x), data.set1) r2(model) -# Code for section 3.3.3 +# Code for section 4.3.3 model.mm @@ -208,152 +208,3 @@ empty_field!(nt, i) = empty!(nt[i]) nt = (dict = Dict("a" => 1, "b" => 2), int=10) empty_field!(nt, 1) nt - -# Code for section 3.4.1 - -x = [1 2 3] -y = [1, 2, 3] -x * y - -a = [1, 2, 3] -b = [4, 5, 6] -a * b - -a .* b - -map(*, a, b) -[a[i] * b[i] for i in eachindex(a, b)] - -eachindex(a, b) - -eachindex([1, 2, 3], [4, 5]) - -map(*, [1, 2, 3], [4, 5]) - -[1, 2, 3] .* [4, 5] - -# Code for section 3.4.2 - -[1, 2, 3] .* [4] - -[1, 2, 3] .^ 2 - -[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .* [1 2 3 4 5 6 7 8 9 10] - -["x", "y", "z"] .=> [sum minimum maximum] - -abs.([1, -2, 3, -4]) - -abs([1, 2, 3]) - -string(1, 2, 3) - -string.("x", 1:10) - -f(i::Int) = string("got integer ", i) -f(s::String) = string("got string ", s) -f.([1, "1"]) - -# Code for section 3.4.3 - -in(1, [1, 2, 3]) -in(4, [1, 2, 3]) - -in([1, 3, 5, 7, 9], [1, 2, 3, 4]) - -in.([1, 3, 5, 7, 9], [1, 2, 3, 4]) - -in.([1, 3, 5, 7, 9], Ref([1, 2, 3, 4])) - -# Code for section 3.4.4 - -aq = [10.0 8.04 10.0 9.14 10.0 7.46 8.0 6.58 - 8.0 6.95 8.0 8.14 8.0 6.77 8.0 5.76 - 13.0 7.58 13.0 8.74 13.0 12.74 8.0 7.71 - 9.0 8.81 9.0 8.77 9.0 7.11 8.0 8.84 - 11.0 8.33 11.0 9.26 11.0 7.81 8.0 8.47 - 14.0 9.96 14.0 8.1 14.0 8.84 8.0 7.04 - 6.0 7.24 6.0 6.13 6.0 6.08 8.0 5.25 - 4.0 4.26 4.0 3.1 4.0 5.39 19.0 12.50 - 12.0 10.84 12.0 9.13 12.0 8.15 8.0 5.56 - 7.0 4.82 7.0 7.26 7.0 6.42 8.0 7.91 - 5.0 5.68 5.0 4.74 5.0 5.73 8.0 6.89] -using Statistics - -mean.(eachcol(aq)) - -mean(eachcol(aq)) - -function R²(x, y) - X = [ones(11) x] - model = X \ y - prediction = X * model - error = y - prediction - SS_res = sum(v -> v ^ 2, error) - mean_y = mean(y) - SS_tot = sum(v -> (v - mean_y) ^ 2, y) - return 1 - SS_res / SS_tot -end - -function R²(x, y) - X = [ones(11) x] - model = X \ y - prediction = X * model - SS_res = sum((y .- prediction) .^ 2) - SS_tot = sum((y .- mean(y)) .^ 2) - return 1 - SS_res / SS_tot -end - -# Code for section 3.5 - -[] -Dict() - -Float64[1, 2, 3] - -Dict{UInt8, Float64}(0 => 0, 1 => 1) - -UInt32(200) - -Real[1, 1.0, 0x3] - -v1 = Any[1, 2, 3] -eltype(v1) -v2 = Float64[1, 2, 3] -eltype(v2) -v3 = [1, 2, 3] -eltype(v2) -d1 = Dict() -eltype(d1) -d2 = Dict(1 => 2, 3 => 4) -eltype(d2) - -p = 1 => 2 -typeof(p) - -# Code for section 3.5.1 - -[1, 2, 3] isa AbstractVector{Int} -[1, 2, 3] isa AbstractVector{Real} - -AbstractVector{<:Real} - -# Code for section 3.5.2 - -using Statistics -function ourcov(x::AbstractVector{<:Real}, - y::AbstractVector{<:Real}) - len = length(x) - @assert len == length(y) > 0 - return sum((x .- mean(x)) .* (y .- mean(y))) / (len - 1) -end - -ourcov(1:4, [1.0, 3.0, 2.0, 4.0]) -cov(1:4, [1.0, 3.0, 2.0, 4.0]) - -ourcov(1:4, Any[1.0, 3.0, 2.0, 4.0]) - -x = Any[1, 2, 3] -identity.(x) -y = Any[1, 2.0] -identity.(y) diff --git a/ch05.jl b/ch05.jl new file mode 100644 index 0000000..7bf3c92 --- /dev/null +++ b/ch05.jl @@ -0,0 +1,161 @@ +# Bogumił Kamiński, 2021 + +# Codes for chapter 5 + +# Code for section 5.1.1 + +x = [1 2 3] +y = [1, 2, 3] +x * y + +a = [1, 2, 3] +b = [4, 5, 6] +a * b + +a .* b + +map(*, a, b) +[a[i] * b[i] for i in eachindex(a, b)] + +eachindex(a, b) + +eachindex([1, 2, 3], [4, 5]) + +map(*, [1, 2, 3], [4, 5]) + +[1, 2, 3] .* [4, 5] + +# Code for section 5.1.2 + +[1, 2, 3] .^ [2] + +[1, 2, 3] .^ 2 + +[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .* [1 2 3 4 5 6 7 8 9 10] + +["x", "y", "z"] .=> [sum minimum maximum] + +abs.([1, -2, 3, -4]) + +abs([1, 2, 3]) + +string(1, 2, 3) + +string.("x", 1:10) + +f(i::Int) = string("got integer ", i) +f(s::String) = string("got string ", s) +f.([1, "1"]) + +# Code for section 5.1.3 + +in(1, [1, 2, 3]) +in(4, [1, 2, 3]) + +1 in [1, 2, 3] +4 in [1, 2, 3] + +in([1, 3, 5, 7, 9], [1, 2, 3, 4]) + +in([1, 3, 5, 7, 9], [1, 2, 3, 4, ([1, 3, 5, 7, 9]]) + +in.([1, 3, 5, 7, 9], [1, 2, 3, 4]) + +in.([1, 3, 5, 7, 9], Ref([1, 2, 3, 4])) + +isodd.([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .+ [1 2 3 4 5 6 7 8 9 10]) + +Matrix{Any}(isodd.([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .* [1 2 3 4 5 6 7 8 9 10])) + +# Code for section 5.1.4 + +aq = [10.0 8.04 10.0 9.14 10.0 7.46 8.0 6.58 + 8.0 6.95 8.0 8.14 8.0 6.77 8.0 5.76 + 13.0 7.58 13.0 8.74 13.0 12.74 8.0 7.71 + 9.0 8.81 9.0 8.77 9.0 7.11 8.0 8.84 + 11.0 8.33 11.0 9.26 11.0 7.81 8.0 8.47 + 14.0 9.96 14.0 8.1 14.0 8.84 8.0 7.04 + 6.0 7.24 6.0 6.13 6.0 6.08 8.0 5.25 + 4.0 4.26 4.0 3.1 4.0 5.39 19.0 12.50 + 12.0 10.84 12.0 9.13 12.0 8.15 8.0 5.56 + 7.0 4.82 7.0 7.26 7.0 6.42 8.0 7.91 + 5.0 5.68 5.0 4.74 5.0 5.73 8.0 6.89] +using Statistics + +mean.(eachcol(aq)) + +mean(eachcol(aq)) + +function R²(x, y) + X = [ones(11) x] + model = X \ y + prediction = X * model + error = y - prediction + SS_res = sum(v -> v ^ 2, error) + mean_y = mean(y) + SS_tot = sum(v -> (v - mean_y) ^ 2, y) + return 1 - SS_res / SS_tot +end + +function R²(x, y) + X = [ones(11) x] + model = X \ y + prediction = X * model + SS_res = sum((y .- prediction) .^ 2) + SS_tot = sum((y .- mean(y)) .^ 2) + return 1 - SS_res / SS_tot +end + +# Code for section 5.2 + +[] +Dict() + +Float64[1, 2, 3] + +Dict{UInt8, Float64}(0 => 0, 1 => 1) + +UInt32(200) + +Real[1, 1.0, 0x3] + +v1 = Any[1, 2, 3] +eltype(v1) +v2 = Float64[1, 2, 3] +eltype(v2) +v3 = [1, 2, 3] +eltype(v3) +d1 = Dict() +eltype(d1) +d2 = Dict(1 => 2, 3 => 4) +eltype(d2) + +p = 1 => 2 +typeof(p) + +# Code for section 5.2.1 + +[1, 2, 3] isa AbstractVector{Int} +[1, 2, 3] isa AbstractVector{Real} + +AbstractVector{<:Real} == AbstractVector{T} where T<:Real + +# Code for section 5.2.2 + +using Statistics +function ourcov(x::AbstractVector{<:Real}, + y::AbstractVector{<:Real}) + len = length(x) + @assert len == length(y) > 0 + return sum((x .- mean(x)) .* (y .- mean(y))) / (len - 1) +end + +ourcov(1:4, [1.0, 3.0, 2.0, 4.0]) +cov(1:4, [1.0, 3.0, 2.0, 4.0]) + +ourcov(1:4, Any[1.0, 3.0, 2.0, 4.0]) + +x = Any[1, 2, 3] +identity.(x) +y = Any[1, 2.0] +identity.(y)