Created
November 6, 2016 06:30
-
-
Save TheSeamau5/ac641dc17479c4aeb9b361af7ab6ad0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Html exposing (Html, div, text, button) | |
import Html.App exposing (beginnerProgram) | |
import Html.Events exposing (onClick) | |
import Random exposing (Seed, initialSeed, step, float, bool) | |
-- Start with initial probability | |
-- For every miss : | |
-- p = p * 0.9 | |
-- For every hit : | |
-- p = p + (1 - p) * 0.1 | |
-- p = p * 0.8 + 0.2 | |
-- State | |
type alias State = | |
{ probability : Float | |
, seed : Seed | |
} | |
-- Initialize with base probability | |
initState : Float -> State | |
initState p = | |
{ probability = clamp 0 1 p | |
, seed = initialSeed 42 | |
} | |
-- Get next result | |
next : State -> (State, Bool) | |
next { probability, seed } = | |
let | |
genProb = float 0 1 | |
(p, seed') = step genProb seed | |
hit = p < probability | |
prob = | |
if hit | |
then | |
probability + (1 - probability) * 0.1 | |
else | |
0.9 * probability | |
nextState = | |
{ probability = prob | |
, seed = seed' | |
} | |
in | |
(nextState, hit) | |
type alias Model = | |
{ state : State | |
, hits : Int | |
, misses : Int | |
} | |
init : Float -> Model | |
init p = | |
{ state = initState p | |
, hits = 0 | |
, misses = 0 | |
} | |
update : Model -> Model | |
update { state, hits, misses } = | |
let | |
(nextState, hit) = next state | |
hits' = | |
if hit | |
then | |
hits + 1 | |
else | |
hits | |
misses' = | |
if hit | |
then | |
misses | |
else | |
misses + 1 | |
in | |
{ state = nextState | |
, hits = hits' | |
, misses = misses' | |
} | |
view : Model -> Html () | |
view { state, hits, misses } = | |
div | |
[] | |
[ div [] | |
[ text ("Probability: " ++ toString state.probability) ] | |
, div [] | |
[ text ("Hits: " ++ toString hits)] | |
, div [] | |
[ text ("Misses: " ++ toString misses)] | |
, button | |
[ onClick () ] | |
[ text "Next" ] | |
] | |
main = | |
beginnerProgram | |
{ model = init 0.19 | |
, view = view | |
, update = always update | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment