Last active
April 11, 2023 12:59
-
-
Save HarryMcCarney/29e514f433a96e113ddb47dfcab89428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://allendowney.github.io/ThinkBayes2/chap03.html#the-dice-problem | |
#r "nuget: FSharp.Stats, 0.4.12-preview.1" | |
open FSharp.Stats | |
open FSharp.Stats.Distributions | |
let normalise (dist: Map<'a, float>) = | |
let totalProbability = dist |> Map.toSeq |> Seq.sumBy snd | |
dist |> Map.map (fun k v -> v / totalProbability) | |
let updatePosteriorDist (likelihoods: Map<'a, float>) (priorDist: Map<'a, float>) = | |
priorDist | |
|> Map.map (fun k v -> | |
match (likelihoods.TryFind k) with | |
| Some l -> v * l | |
| None -> v) | |
|> normalise | |
let priorDiceDist = EmpiricalDistribution.createNominal () ([ 6; 8; 12 ]) | |
let updateDice data priorDist = | |
let likelihood = | |
priorDist | |
|> Map.toSeq | |
|> Seq.map (fun (k, v) -> k, (if data > k then 0. else 1. / (float k))) | |
|> Map.ofSeq | |
updatePosteriorDist likelihood priorDist | |
updateDice 7 priorDiceDist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment