h-Klok / StatsWithJuliaBook

https://statisticswithjulia.org/
MIT License
1.08k stars 279 forks source link

Error in BirthdayProblem #44

Closed JTHooker closed 3 years ago

JTHooker commented 3 years ago

Hi Team - I am really loving the book - thanks so much for writing it.

I am receiving a consistent error in the Chapter 2 Birthday problem scatter plot:

MethodError: no method matching Val{:scatter}(::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char) apply_recipe(plotattributes::Dict{Symbol, Any}, #unused#::Type{Val{:scatter}}, plt::Plots.Plot{Plots.PyPlotBackend}) at recipes.jl:50 _process_plotrecipe(plt::Any, kw::Any, kw_list::Any, still_to_process::Any) at plot_recipe.jl:32 _process_plotrecipes!(plt::Any, kw_list::Any) at plot_recipe.jl:18 recipe_pipeline!(plt::Any, plotattributes::Any, args::Any) at RecipesPipeline.jl:81 _plot!(plt::Plots.Plot, plotattributes::Any, args::Any) at plot.jl:172 plot!(::Plots.Plot, ::Any, ::Vararg{Any, N} where N; kw::Any) at plot.jl:162 (::RecipesBase.var"#plot!##kw")(::NamedTuple{(:c, :ms, :msw, :shape, :label, :xlims, :ylims, :xlabel, :ylabel, :legend, :seriestype), Tuple{Symbol, Int64, Int64, Symbol, String, Tuple{Int64, Int64}, Tuple{Int64, Int64}, String, String, Symbol, Symbol}}, ::typeof(plot!), ::Plots.Plot{Plots.PyPlotBackend}, ::UnitRange{Int64}, ::Vararg{Any, N} where N) at plot.jl:159 plot!(::Any, ::Vararg{Any, N} where N; kw::Any) at plot.jl:153 (::RecipesBase.var"#plot!##kw")(::NamedTuple{(:c, :ms, :msw, :shape, :label, :xlims, :ylims, :xlabel, :ylabel, :legend, :seriestype), Tuple{Symbol, Int64, Int64, Symbol, String, Tuple{Int64, Int64}, Tuple{Int64, Int64}, String, String, Symbol, Symbol}}, ::typeof(plot!), ::UnitRange{Int64}, ::Vector{Float64}) at plot.jl:147 scatter!(::Any, ::Vararg{Any, N} where N; kw::Any) at RecipesBase.jl:405 (::Plots.var"#scatter!##kw")(::Any, ::typeof(scatter!), ::Any, ::Vararg{Any, N} where N) at RecipesBase.jl:405 top-level scope at Week 1.jl:271 eval at boot.jl:360 [inlined]

I have tried the ocde in Juno, Jupyter and Pluto - same error.

I am unsure whether there have been updates to packages or dependencies, etc. that are causing this.

The code I have implemented is below.

import Pkg; Pkg.add("Combinatorics")

import Pkg; Pkg.add("PyPlot")

using StatsBase, Combinatorics, Plots ; pyplot()

matchExists1(n) = 1 - prod([k/365 for k in 365:-1:365-n+1]) matchExists2(n) = 1- factorial(365,365-big(n))/365^big(n)

function bdEvent(n) birthdays = rand(1:365,n) dayCounts = counts(birthdays, 1:365) return maximum(dayCounts) > 1 end

probEst(n) = sum([bdEvent(n) for _ in 1:N])/N

xGrid = 1:50 analyticSolution1 = [matchExists1(n) for n in xGrid] analyticSolution2 = [matchExists2(n) for n in xGrid] println("Maximum error: $(maximum(abs.(analyticSolution1 - analyticSolution2)))")

N = 10^3 mcEstimates = [probEst(n) for n in xGrid]

plot(xGrid, analyticSolution1, c=:blue, label="Analytic solution") scatter!(xGrid, mcEstimates, c=:red, ms=6, msw=0, shape=:xcross, label="MC estimate", xlims=(0,50), ylims=(0, 1), xlabel="Number of people in room", ylabel="Probability of birthday match", legend=:topleft)

JTHooker commented 3 years ago

It appears to be a package issue with recent updates, so I think this can be 'resolved' from your perspective.

yoninazarathy commented 2 years ago

Thank you very much. Enjoy.