Packages

Probabilistic functional programming in Gleam

Current section

Files

Jump to
probly src probly.erl
Raw

src/probly.erl

-module(probly).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([probability_of_event/2, combine_dist/2, normalize/1, combine_dist_normalized/2]).
-file("/home/race/repos/probly/src/probly.gleam", 31).
-spec sum(list(float())) -> float().
sum(Xs) ->
gleam@list:fold(Xs, +0.0, fun(X, Acc) -> X + Acc end).
-file("/home/race/repos/probly/src/probly.gleam", 23).
-spec probability_of_event(fun((FDC) -> boolean()), list({FDC, float()})) -> float().
probability_of_event(Event, Dist) ->
_pipe = Dist,
_pipe@1 = gleam@list:filter(
_pipe,
fun(E) -> Event(erlang:element(1, E)) end
),
_pipe@2 = gleam@list:map(_pipe@1, fun(E@1) -> erlang:element(2, E@1) end),
sum(_pipe@2).
-file("/home/race/repos/probly/src/probly.gleam", 36).
-spec combine_dist(list({FDG, float()}), list({FDI, float()})) -> list({{FDG,
FDI},
float()}).
combine_dist(Dist1, Dist2) ->
_pipe = Dist1,
gleam@list:flat_map(
_pipe,
fun(E1) ->
Val1 = gleam@pair:first(E1),
Prob1 = gleam@pair:second(E1),
_pipe@1 = Dist2,
gleam@list:map(
_pipe@1,
fun(E2) ->
Val2 = gleam@pair:first(E2),
Prob2 = gleam@pair:second(E2),
{{Val1, Val2}, Prob1 * Prob2}
end
)
end
).
-file("/home/race/repos/probly/src/probly.gleam", 61).
-spec insert_or_update({FDO, float()}, list({FDO, float()})) -> list({FDO,
float()}).
insert_or_update(Event, Acc) ->
Val = gleam@pair:first(Event),
Prob = gleam@pair:second(Event),
case Acc of
[] ->
[{Val, Prob}];
[{Existing_val, Existing_prob} | Tail] ->
case Existing_val =:= Val of
true ->
[{Existing_val, Existing_prob + Prob} | Tail];
false ->
[{Existing_val, Existing_prob} |
insert_or_update(Event, Tail)]
end
end.
-file("/home/race/repos/probly/src/probly.gleam", 54).
-spec normalize(list({FDL, float()})) -> list({FDL, float()}).
normalize(Dist) ->
gleam@list:fold(
Dist,
[],
fun(Acc, Event) -> insert_or_update(Event, Acc) end
).
-file("/home/race/repos/probly/src/probly.gleam", 79).
-spec combine_dist_normalized(list({FDR, float()}), list({FDT, float()})) -> list({{FDR,
FDT},
float()}).
combine_dist_normalized(Dist1, Dist2) ->
_pipe = combine_dist(Dist1, Dist2),
normalize(_pipe).