Current section
Files
Jump to
Current section
Files
src/aarondb@cognitive@scoring.erl
-module(aarondb@cognitive@scoring).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/aarondb/cognitive/scoring.gleam").
-export([exp/1, log/1, log10/1, log2/1, tanh/1, ebbinghaus_with_floor/3, compute_stability/2, hebbian_update/2, softmax/1, bayesian_update/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/aarondb/cognitive/scoring.gleam", 5).
-spec exp(float()) -> float().
exp(X) ->
math:exp(X).
-file("src/aarondb/cognitive/scoring.gleam", 8).
-spec log(float()) -> float().
log(X) ->
math:log(X).
-file("src/aarondb/cognitive/scoring.gleam", 11).
-spec log10(float()) -> float().
log10(X) ->
math:log10(X).
-file("src/aarondb/cognitive/scoring.gleam", 14).
-spec log2(float()) -> float().
log2(X) ->
math:log2(X).
-file("src/aarondb/cognitive/scoring.gleam", 17).
-spec tanh(float()) -> float().
tanh(X) ->
math:tanh(X).
-file("src/aarondb/cognitive/scoring.gleam", 34).
?DOC(" EbbinghausWithFloor computes the Ebbinghaus retention with a floor value.\n").
-spec ebbinghaus_with_floor(float(), float(), float()) -> float().
ebbinghaus_with_floor(Days_since_access, Stability, Floor) ->
Stab = case Stability =< +0.0 of
true ->
14.0;
false ->
Stability
end,
R = math:exp(+0.0 - (case Stab of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> Days_since_access / Gleam@denominator
end)),
case R < Floor of
true ->
Floor;
false ->
R
end.
-file("src/aarondb/cognitive/scoring.gleam", 51).
?DOC(" ComputeStability computes new stability from access count and spacing.\n").
-spec compute_stability(integer(), float()) -> float().
compute_stability(Access_count, Avg_days_between_accesses) ->
Count_float = erlang:float(Access_count),
Base = math:log(1.0 + Count_float) * 20.0,
Spacing = math:tanh(case 7.0 of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> Avg_days_between_accesses / Gleam@denominator
end),
Stability = Base * (1.0 + (0.5 * Spacing)),
Stability1 = case Stability > 365.0 of
true ->
365.0;
false ->
Stability
end,
case Stability1 < 14.0 of
true ->
14.0;
false ->
Stability1
end.
-file("src/aarondb/cognitive/scoring.gleam", 72).
?DOC(" Hebbian update for association weight\n").
-spec hebbian_update(float(), float()) -> float().
hebbian_update(Current_weight, Effective_signal) ->
Clamped_current = case Current_weight =< +0.0 of
true ->
0.01;
false ->
Current_weight
end,
Log_new = math:log(Clamped_current) + (Effective_signal * math:log(
1.0 + 0.01
)),
New_weight = math:exp(Log_new),
case New_weight > 1.0 of
true ->
1.0;
false ->
New_weight
end.
-file("src/aarondb/cognitive/scoring.gleam", 90).
?DOC(" Softmax normalizes the weight vector so values sum to 1.\n").
-spec softmax(list(float())) -> list(float()).
softmax(Weights) ->
Max_w = case gleam@list:reduce(
Weights,
fun(Acc, W) -> gleam@float:max(Acc, W) end
) of
{ok, M} ->
M;
{error, _} ->
+0.0
end,
Shifted_exps = gleam@list:map(
Weights,
fun(W@1) -> math:exp(W@1 - Max_w) end
),
Sum = case gleam@list:reduce(Shifted_exps, fun(Acc@1, E) -> Acc@1 + E end) of
{ok, S} ->
S;
{error, _} ->
1.0
end,
gleam@list:map(Shifted_exps, fun(E@1) -> case Sum of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> E@1 / Gleam@denominator
end end).
-file("src/aarondb/cognitive/scoring.gleam", 122).
?DOC(" Bayesian update applies a Bayesian update to the prior confidence.\n").
-spec bayesian_update(float(), float()) -> float().
bayesian_update(Prior, Evidence) ->
P = case Prior < +0.0 of
true ->
+0.0;
false ->
case Prior > 1.0 of
true ->
1.0;
false ->
Prior
end
end,
E = case Evidence < +0.0 of
true ->
+0.0;
false ->
case Evidence > 1.0 of
true ->
1.0;
false ->
Evidence
end
end,
Numerator = P * E,
Denominator = Numerator + ((1.0 - P) * (1.0 - E)),
D = case Denominator < 0.000000001 of
true ->
0.000000001;
false ->
Denominator
end,
Posterior = case D of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> Numerator / Gleam@denominator
end,
(0.95 * Posterior) + 0.025.