Skip to content

Fix order heads and tails in the Beta distribution #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Fix order heads and tails in the Beta distribution #91

wants to merge 1 commit into from

Conversation

rikhuijzer
Copy link
Contributor

The order was reversed as could be seen by computing the posterior distribution for one heads or one tails.
For example, if you calculated the posterior for a sample of size one and containing one heads, the posterior plot would show that the parameter is most likely to be 0 whereas the plot shows that an higher value means a higher probability for heads.

Note that I haven't run the notebook again to fix the GIF. This is because it would take me about 30 minutes to setup Jupyter again. This is a minor fix, which I guess will be end up into the tutorials at some point.

The order was reversed as could be seen by computing the posterior distribution for one heads or one tails.
For example, if you calculated the posterior for a sample of size one and containing one heads, the posterior plot would show that the parameter is most likely to be `0` whereas the plot shows that an higher value means a higher probability for heads.
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@devmotion
Copy link
Member

Isn't the (main) problem here that heads is compute as sum(data[1:i-1]), i.e., it will always be 0 for a sample of size 1?

@rikhuijzer
Copy link
Contributor Author

It seems that I made a mistake when copying the code. When using

data = [0, 1, 0, 1]
prior_belief = Beta(1, 1);
	
function plot_dist(i, N) 

    # Count the number of heads and tails.
    heads = sum(data[1:i-1])
    tails = N - heads
    
    # Update our prior belief in closed form (this is possible because we use a conjugate prior).
    updated_belief = Beta(prior_belief.α + heads, prior_belief.β + tails)

    # Plotting
    plot(updated_belief, 
        size = (500, 250), 
        title = "... after $N observations",
        xlabel = "probability of heads", 
        ylabel = "", 
        legend = nothing,
        xlim = (0,1),
        fill=0, α=0.3, w=3)
    vline!([p_true])
	end
end

plot(plot_dist(1, 0), plot_dist(2, 1), plot_dist(3, 2), plot_dist(4, 3))

I get

image

as expected. Apologies for the inconvenience.

@rikhuijzer rikhuijzer closed this Oct 26, 2020
@rikhuijzer rikhuijzer deleted the patch-1 branch October 26, 2020 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants