{-# LANGUAGE DerivingStrategies #-}

-- | The 34 weights of FSRS-7, and typed views onto the blocks they form.
--
-- FSRS-7 groups its weights like this:
--
-- +-----------+------------------------------------------------------------+
-- | @w0..w3@  | initial stability, indexed by rating                       |
-- +-----------+------------------------------------------------------------+
-- | @w4..w6@  | difficulty                                                 |
-- +-----------+------------------------------------------------------------+
-- | @w7..w14@ | stability update, slow trace                               |
-- +-----------+------------------------------------------------------------+
-- | @w15..w22@| stability update, fast trace                               |
-- +-----------+------------------------------------------------------------+
-- | @w23..w30@| the two-component forgetting curve                         |
-- +-----------+------------------------------------------------------------+
-- | @w31..w33@| how difficulty and stability modulate that curve           |
-- +-----------+------------------------------------------------------------+
--
-- The two stability blocks have identical shapes, which is why
-- 'slowTraceWeights' and 'fastTraceWeights' both produce a t'StabilityWeights'.
module FSRS.Parameters
  ( -- * Parameters
    Parameters
  , parameterCount
  , defaultParameters
  , mkParameters
  , clampParameters
  , parametersToList
  , parameterAt

    -- * Validation
  , ParameterError (..)
  , parameterBounds
  , orderingConstraints
  , validateParameters

    -- * Weight blocks
  , initialStabilityWeight
  , initialDifficultyBase
  , initialDifficultyRate
  , difficultyDelta
  , StabilityWeights (..)
  , slowTraceWeights
  , fastTraceWeights
  , CurveWeights (..)
  , curveWeights
  ) where

import Data.Array.Unboxed (UArray, bounds, elems, listArray, (!))

import FSRS.Types (Rating, ratingToInt)

-- | An FSRS-7 parameter vector: exactly 'parameterCount' weights.
--
-- Build one with 'mkParameters' or 'clampParameters'; the constructor is
-- hidden so that the length invariant cannot be broken.
newtype Parameters = Parameters (UArray Int Double)
  deriving stock (Parameters -> Parameters -> Bool
(Parameters -> Parameters -> Bool)
-> (Parameters -> Parameters -> Bool) -> Eq Parameters
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Parameters -> Parameters -> Bool
== :: Parameters -> Parameters -> Bool
$c/= :: Parameters -> Parameters -> Bool
/= :: Parameters -> Parameters -> Bool
Eq, Eq Parameters
Eq Parameters =>
(Parameters -> Parameters -> Ordering)
-> (Parameters -> Parameters -> Bool)
-> (Parameters -> Parameters -> Bool)
-> (Parameters -> Parameters -> Bool)
-> (Parameters -> Parameters -> Bool)
-> (Parameters -> Parameters -> Parameters)
-> (Parameters -> Parameters -> Parameters)
-> Ord Parameters
Parameters -> Parameters -> Bool
Parameters -> Parameters -> Ordering
Parameters -> Parameters -> Parameters
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Parameters -> Parameters -> Ordering
compare :: Parameters -> Parameters -> Ordering
$c< :: Parameters -> Parameters -> Bool
< :: Parameters -> Parameters -> Bool
$c<= :: Parameters -> Parameters -> Bool
<= :: Parameters -> Parameters -> Bool
$c> :: Parameters -> Parameters -> Bool
> :: Parameters -> Parameters -> Bool
$c>= :: Parameters -> Parameters -> Bool
>= :: Parameters -> Parameters -> Bool
$cmax :: Parameters -> Parameters -> Parameters
max :: Parameters -> Parameters -> Parameters
$cmin :: Parameters -> Parameters -> Parameters
min :: Parameters -> Parameters -> Parameters
Ord)

instance Show Parameters where
  showsPrec :: Int -> Parameters -> ShowS
showsPrec Int
d Parameters
p =
    Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
      [Char] -> ShowS
showString [Char]
"mkParameters " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Double] -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Parameters -> [Double]
parametersToList Parameters
p)

-- | @34@. FSRS-6 had 21 weights. FSRS-7 keeps two stability blocks of eight
-- weights each — one per memory trace — and spends eleven on a forgetting
-- curve that mixes the two traces and is modulated by difficulty.
parameterCount :: Int
parameterCount :: Int
parameterCount = Int
34

-- | The 0-based weight at the given index. Indices outside
-- @[0, 'parameterCount')@ are a programmer error and raise an exception.
parameterAt :: Parameters -> Int -> Double
parameterAt :: Parameters -> Int -> Double
parameterAt (Parameters UArray Int Double
a) Int
i
  | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
lo Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
hi = UArray Int Double
a UArray Int Double -> Int -> Double
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
i
  | Bool
otherwise =
      [Char] -> Double
forall a. HasCallStack => [Char] -> a
error ([Char] -> Double) -> [Char] -> Double
forall a b. (a -> b) -> a -> b
$
        [Char]
"FSRS.Parameters.parameterAt: index " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
" out of range " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> (Int, Int) -> [Char]
forall a. Show a => a -> [Char]
show (Int
lo, Int
hi)
  where
    (Int
lo, Int
hi) = UArray Int Double -> (Int, Int)
forall i. Ix i => UArray i Double -> (i, i)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds UArray Int Double
a

-- | The weights as a plain list, in index order.
parametersToList :: Parameters -> [Double]
parametersToList :: Parameters -> [Double]
parametersToList (Parameters UArray Int Double
a) = UArray Int Double -> [Double]
forall (a :: * -> * -> *) e i. (IArray a e, Ix i) => a i e -> [e]
elems UArray Int Double
a

-- | The default FSRS-7 parameters, obtained by the upstream authors through
-- multi-user optimisation.
--
-- These are the @DEFAULT_PARAMETERS@ of the finished FSRS-7 reference
-- implementation, <https://github.com/Expertium/fsrs-rs-speed-autoresearch>.
defaultParameters :: Parameters
defaultParameters :: Parameters
defaultParameters = UArray Int Double -> Parameters
Parameters ((Int, Int) -> [Double] -> UArray Int Double
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
0, Int
parameterCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Double]
ws)
  where
    ws :: [Double]
ws =
      [ -- Initial stability, indexed by rating - 1
        Double
0.1104
      , Double
2.2395
      , Double
3.9221
      , Double
11.7841
      , -- Difficulty
        Double
6.1686
      , Double
0.6457
      , Double
3.6807
      , -- Stability, slow trace
        Double
1.9795
      , Double
0.0
      , Double
1.3826
      , Double
0.7024
      , Double
0.5999
      , Double
0.8146
      , Double
0.6398
      , Double
1.0
      , -- Stability, fast trace
        Double
1.3207
      , Double
0.6707
      , Double
3.8668
      , Double
0.4416
      , Double
0.0934
      , Double
1.8631
      , Double
0.6162
      , Double
1.0869
      , -- Forgetting curve
        Double
0.1567
      , Double
0.0801
      , Double
0.2421
      , Double
0.9464
      , Double
0.1433
      , Double
0.7145
      , Double
0.0
      , Double
0.5667
      , -- Modulation of the curve by difficulty and stability
        Double
0.3734
      , Double
0.5333
      , Double
0.3048
      ]

-- | Why a list of weights is not a valid parameter vector.
data ParameterError
  = -- | Got this many weights instead of 'parameterCount'.
    WrongParameterCount !Int
  | -- | @index@, @value@, @lower bound@, @upper bound@.
    ParameterOutOfBounds !Int !Double !Double !Double
  | -- | @w[i] <= w[j]@ is required but was violated: @i@, @j@, @w[i]@, @w[j]@.
    ParameterOutOfOrder !Int !Int !Double !Double
  | -- | @index@, the offending @NaN@ or infinity.
    ParameterNotFinite !Int !Double
  deriving stock (ParameterError -> ParameterError -> Bool
(ParameterError -> ParameterError -> Bool)
-> (ParameterError -> ParameterError -> Bool) -> Eq ParameterError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ParameterError -> ParameterError -> Bool
== :: ParameterError -> ParameterError -> Bool
$c/= :: ParameterError -> ParameterError -> Bool
/= :: ParameterError -> ParameterError -> Bool
Eq, Int -> ParameterError -> ShowS
[ParameterError] -> ShowS
ParameterError -> [Char]
(Int -> ParameterError -> ShowS)
-> (ParameterError -> [Char])
-> ([ParameterError] -> ShowS)
-> Show ParameterError
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ParameterError -> ShowS
showsPrec :: Int -> ParameterError -> ShowS
$cshow :: ParameterError -> [Char]
show :: ParameterError -> [Char]
$cshowList :: [ParameterError] -> ShowS
showList :: [ParameterError] -> ShowS
Show)

-- | The inclusive @(lower, upper)@ bound of every weight, in index order.
--
-- Taken from the parameter clipper the upstream optimiser applies after each
-- gradient step, so any parameter set produced by a real optimiser satisfies
-- them.
parameterBounds :: [(Double, Double)]
parameterBounds :: [(Double, Double)]
parameterBounds =
  [ -- Initial stability
    (Double
1.0e-4, Double
50.0)
  , (Double
1.0e-4, Double
100.0)
  , (Double
1.0e-4, Double
100.0)
  , (Double
1.0e-4, Double
100.0)
  , -- Difficulty
    (Double
1.0, Double
10.0)
  , (Double
0.001, Double
4.0)
  , (Double
0.1, Double
4.0)
  , -- Stability, slow trace
    (Double
0.0, Double
4.0)
  , (Double
0.0, Double
1.2)
  , (Double
0.3, Double
3.0)
  , (Double
0.01, Double
1.5)
  , (Double
0.1, Double
1.0)
  , (Double
0.0, Double
3.5)
  , (Double
0.0, Double
1.0)
  , (Double
1.0, Double
7.0)
  , -- Stability, fast trace
    (Double
0.0, Double
4.0)
  , (Double
0.0, Double
2.0)
  , (Double
0.5, Double
6.0)
  , (Double
0.001, Double
1.5)
  , (Double
0.001, Double
1.0)
  , (Double
0.0, Double
5.0)
  , (Double
0.0, Double
1.0)
  , (Double
1.0, Double
7.0)
  , -- Forgetting curve
    (Double
0.01, Double
0.25)
  , (Double
0.01, Double
0.95)
  , (Double
0.2, Double
0.85)
  , (Double
0.5, Double
0.99)
  , (Double
0.01, Double
1.0)
  , (Double
0.1, Double
1.0)
  , (Double
0.0, Double
0.9)
  , (Double
0.1, Double
1.1)
  , -- Modulation of the curve
    (Double
0.0, Double
1.0)
  , (Double
0.0, Double
0.6)
  , (Double
0.0, Double
0.6)
  ]

-- | Pairs @(i, j)@ for which @w[i] <= w[j]@ must hold: the initial stabilities
-- rise with the rating, and the slow component of the forgetting curve starts
-- above the fast one.
orderingConstraints :: [(Int, Int)]
orderingConstraints :: [(Int, Int)]
orderingConstraints = [(Int
0, Int
1), (Int
1, Int
2), (Int
2, Int
3), (Int
25, Int
26)]

-- | Every problem with a candidate weight list, or @[]@ if there is none.
validateParameters :: [Double] -> [ParameterError]
validateParameters :: [Double] -> [ParameterError]
validateParameters [Double]
ws
  | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
parameterCount = [Int -> ParameterError
WrongParameterCount Int
n]
  | Bool
otherwise = [ParameterError]
boundErrors [ParameterError] -> [ParameterError] -> [ParameterError]
forall a. Semigroup a => a -> a -> a
<> [ParameterError]
orderErrors
  where
    n :: Int
n = [Double] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Double]
ws
    boundErrors :: [ParameterError]
boundErrors = [[ParameterError]] -> [ParameterError]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ((Int -> Double -> (Double, Double) -> [ParameterError])
-> [Int] -> [Double] -> [(Double, Double)] -> [[ParameterError]]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 Int -> Double -> (Double, Double) -> [ParameterError]
check [Int
0 ..] [Double]
ws [(Double, Double)]
parameterBounds)
    check :: Int -> Double -> (Double, Double) -> [ParameterError]
check Int
i Double
w (Double
lo, Double
hi)
      | Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
w Bool -> Bool -> Bool
|| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
w = [Int -> Double -> ParameterError
ParameterNotFinite Int
i Double
w]
      | Double
w Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
lo Bool -> Bool -> Bool
|| Double
w Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
hi = [Int -> Double -> Double -> Double -> ParameterError
ParameterOutOfBounds Int
i Double
w Double
lo Double
hi]
      | Bool
otherwise = []
    -- Only meaningful once every weight is known to be finite; otherwise a
    -- NaN would be reported twice, once as itself and once as out of order.
    orderErrors :: [ParameterError]
orderErrors
      | [ParameterError] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ParameterError]
boundErrors = ((Int, Int) -> [ParameterError])
-> [(Int, Int)] -> [ParameterError]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int, Int) -> [ParameterError]
checkOrder [(Int, Int)]
orderingConstraints
      | Bool
otherwise = []
    checkOrder :: (Int, Int) -> [ParameterError]
checkOrder (Int
i, Int
j)
      | Double
wi Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
wj = []
      | Bool
otherwise = [Int -> Int -> Double -> Double -> ParameterError
ParameterOutOfOrder Int
i Int
j Double
wi Double
wj]
      where
        wi :: Double
wi = [Double]
ws [Double] -> Int -> Double
forall a. HasCallStack => [a] -> Int -> a
!! Int
i
        wj :: Double
wj = [Double]
ws [Double] -> Int -> Double
forall a. HasCallStack => [a] -> Int -> a
!! Int
j

-- | Build a parameter vector, rejecting anything out of bounds.
mkParameters :: [Double] -> Either [ParameterError] Parameters
mkParameters :: [Double] -> Either [ParameterError] Parameters
mkParameters [Double]
ws = case [Double] -> [ParameterError]
validateParameters [Double]
ws of
  [] -> Parameters -> Either [ParameterError] Parameters
forall a b. b -> Either a b
Right (UArray Int Double -> Parameters
Parameters ((Int, Int) -> [Double] -> UArray Int Double
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
0, Int
parameterCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Double]
ws))
  [ParameterError]
errs -> [ParameterError] -> Either [ParameterError] Parameters
forall a b. a -> Either a b
Left [ParameterError]
errs

-- | Build a parameter vector by pulling every weight into its valid range,
-- exactly as the upstream optimiser's clipper does: every weight is clamped to
-- its own bounds first, and only then are the ordering constraints restored by
-- raising the /larger/ index. Only a wrong number of weights can still fail.
clampParameters :: [Double] -> Either [ParameterError] Parameters
clampParameters :: [Double] -> Either [ParameterError] Parameters
clampParameters [Double]
ws
  | [Double] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Double]
ws Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
parameterCount = [ParameterError] -> Either [ParameterError] Parameters
forall a b. a -> Either a b
Left [Int -> ParameterError
WrongParameterCount ([Double] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Double]
ws)]
  | Bool
otherwise = Parameters -> Either [ParameterError] Parameters
forall a b. b -> Either a b
Right (UArray Int Double -> Parameters
Parameters ((Int, Int) -> [Double] -> UArray Int Double
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
0, Int
parameterCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Double]
ordered))
  where
    boxed :: [Double]
boxed = (Double -> (Double, Double) -> Double)
-> [Double] -> [(Double, Double)] -> [Double]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Double -> (Double, Double) -> Double
forall {a}. RealFloat a => a -> (a, a) -> a
clampInto [Double]
ws [(Double, Double)]
parameterBounds
    clampInto :: a -> (a, a) -> a
clampInto a
x (a
lo, a
hi)
      | a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
x = a
lo
      | Bool
otherwise = a -> a -> a
forall a. Ord a => a -> a -> a
min a
hi (a -> a -> a
forall a. Ord a => a -> a -> a
max a
lo a
x)
    -- The constraints are listed in increasing order and chain through the
    -- initial stabilities, so a single left-to-right pass settles them.
    ordered :: [Double]
ordered = ([Double] -> (Int, Int) -> [Double])
-> [Double] -> [(Int, Int)] -> [Double]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl [Double] -> (Int, Int) -> [Double]
forall {a}. Ord a => [a] -> (Int, Int) -> [a]
raise [Double]
boxed [(Int, Int)]
orderingConstraints
    raise :: [a] -> (Int, Int) -> [a]
raise [a]
acc (Int
i, Int
j) = Int -> a -> [a] -> [a]
forall {a}. Int -> a -> [a] -> [a]
setAt Int
j (a -> a -> a
forall a. Ord a => a -> a -> a
max ([a]
acc [a] -> Int -> a
forall a. HasCallStack => [a] -> Int -> a
!! Int
j) ([a]
acc [a] -> Int -> a
forall a. HasCallStack => [a] -> Int -> a
!! Int
i)) [a]
acc
    setAt :: Int -> a -> [a] -> [a]
setAt Int
i a
x [a]
xs = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
i [a]
xs [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a
x] [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs

-- | @w[rating - 1]@: the stability a card is born with after its first review.
initialStabilityWeight :: Parameters -> Rating -> Double
initialStabilityWeight :: Parameters -> Rating -> Double
initialStabilityWeight Parameters
p Rating
r = Parameters -> Int -> Double
parameterAt Parameters
p (Rating -> Int
ratingToInt Rating
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

-- | @w4@.
initialDifficultyBase :: Parameters -> Double
initialDifficultyBase :: Parameters -> Double
initialDifficultyBase Parameters
p = Parameters -> Int -> Double
parameterAt Parameters
p Int
4

-- | @w5@.
initialDifficultyRate :: Parameters -> Double
initialDifficultyRate :: Parameters -> Double
initialDifficultyRate Parameters
p = Parameters -> Int -> Double
parameterAt Parameters
p Int
5

-- | @w6@.
difficultyDelta :: Parameters -> Double
difficultyDelta :: Parameters -> Double
difficultyDelta Parameters
p = Parameters -> Int -> Double
parameterAt Parameters
p Int
6

-- | One of the two eight-weight blocks that drive the stability update, one
-- per memory trace.
data StabilityWeights = StabilityWeights
  { StabilityWeights -> Double
swIncreaseBase :: !Double
  -- ^ @w7@ \/ @w15@, used as @exp (base - 1.5)@.
  , StabilityWeights -> Double
swIncreaseStabilityExponent :: !Double
  -- ^ @w8@ \/ @w16@.
  , StabilityWeights -> Double
swIncreaseRetrievabilityFactor :: !Double
  -- ^ @w9@ \/ @w17@.
  , StabilityWeights -> Double
swFailureFactor :: !Double
  -- ^ @w10@ \/ @w18@.
  , StabilityWeights -> Double
swFailureStabilityExponent :: !Double
  -- ^ @w11@ \/ @w19@.
  , StabilityWeights -> Double
swFailureRetrievabilityFactor :: !Double
  -- ^ @w12@ \/ @w20@.
  , StabilityWeights -> Double
swHardPenalty :: !Double
  -- ^ @w13@ \/ @w21@.
  , StabilityWeights -> Double
swEasyBonus :: !Double
  -- ^ @w14@ \/ @w22@.
  }
  deriving stock (StabilityWeights -> StabilityWeights -> Bool
(StabilityWeights -> StabilityWeights -> Bool)
-> (StabilityWeights -> StabilityWeights -> Bool)
-> Eq StabilityWeights
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StabilityWeights -> StabilityWeights -> Bool
== :: StabilityWeights -> StabilityWeights -> Bool
$c/= :: StabilityWeights -> StabilityWeights -> Bool
/= :: StabilityWeights -> StabilityWeights -> Bool
Eq, Int -> StabilityWeights -> ShowS
[StabilityWeights] -> ShowS
StabilityWeights -> [Char]
(Int -> StabilityWeights -> ShowS)
-> (StabilityWeights -> [Char])
-> ([StabilityWeights] -> ShowS)
-> Show StabilityWeights
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StabilityWeights -> ShowS
showsPrec :: Int -> StabilityWeights -> ShowS
$cshow :: StabilityWeights -> [Char]
show :: StabilityWeights -> [Char]
$cshowList :: [StabilityWeights] -> ShowS
showList :: [StabilityWeights] -> ShowS
Show)

stabilityWeightsAt :: Int -> Parameters -> StabilityWeights
stabilityWeightsAt :: Int -> Parameters -> StabilityWeights
stabilityWeightsAt Int
base Parameters
p =
  StabilityWeights
    { swIncreaseBase :: Double
swIncreaseBase = Int -> Double
at Int
0
    , swIncreaseStabilityExponent :: Double
swIncreaseStabilityExponent = Int -> Double
at Int
1
    , swIncreaseRetrievabilityFactor :: Double
swIncreaseRetrievabilityFactor = Int -> Double
at Int
2
    , swFailureFactor :: Double
swFailureFactor = Int -> Double
at Int
3
    , swFailureStabilityExponent :: Double
swFailureStabilityExponent = Int -> Double
at Int
4
    , swFailureRetrievabilityFactor :: Double
swFailureRetrievabilityFactor = Int -> Double
at Int
5
    , swHardPenalty :: Double
swHardPenalty = Int -> Double
at Int
6
    , swEasyBonus :: Double
swEasyBonus = Int -> Double
at Int
7
    }
  where
    at :: Int -> Double
at Int
k = Parameters -> Int -> Double
parameterAt Parameters
p (Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k)

-- | @w7..w14@: the block that updates the slow trace.
slowTraceWeights :: Parameters -> StabilityWeights
slowTraceWeights :: Parameters -> StabilityWeights
slowTraceWeights = Int -> Parameters -> StabilityWeights
stabilityWeightsAt Int
7

-- | @w15..w22@: the block that updates the fast trace.
fastTraceWeights :: Parameters -> StabilityWeights
fastTraceWeights :: Parameters -> StabilityWeights
fastTraceWeights = Int -> Parameters -> StabilityWeights
stabilityWeightsAt Int
15

-- | @w23..w33@: the mixture of two power laws that makes up the FSRS-7
-- forgetting curve, and the three weights that modulate it.
--
-- The last three are stored the way the reference stores them — shifted so
-- that their range starts at zero — and the formulas in "FSRS.Algorithm"
-- offset them back. The field documentation gives the offset each one carries.
data CurveWeights = CurveWeights
  { CurveWeights -> Double
cwDecayBase1 :: !Double
  -- ^ @w23@: sets the fast component's decay, which is also scaled by the
  -- fast stability, so unlike every earlier version of FSRS the decay is not
  -- a constant.
  , CurveWeights -> Double
cwDecay2 :: !Double
  -- ^ @w24@: the slow component's decay magnitude.
  , CurveWeights -> Double
cwBase1 :: !Double
  -- ^ @w25@: the fast component's recall probability at @t == s_fast@.
  , CurveWeights -> Double
cwBase2 :: !Double
  -- ^ @w26@: likewise for the slow component at @t == s@.
  , CurveWeights -> Double
cwWeight1 :: !Double
  -- ^ @w27@: how much the fast component counts in the mixture.
  , CurveWeights -> Double
cwWeight2 :: !Double
  -- ^ @w28@: likewise for the slow component.
  , CurveWeights -> Double
cwStabilityPower1 :: !Double
  -- ^ @w29@; applied as @s_fast ** negate cwStabilityPower1@.
  , CurveWeights -> Double
cwStabilityPower2 :: !Double
  -- ^ @w30@; applied as @s ** cwStabilityPower2@.
  , CurveWeights -> Double
cwDifficultyWeight :: !Double
  -- ^ @w31@; the mixture weight of the slow component is scaled by
  -- @exp ((d - 5) * (cwDifficultyWeight - 0.5))@.
  , CurveWeights -> Double
cwDifficultyDecay :: !Double
  -- ^ @w32@; the slow component sees time scaled by
  -- @exp ((d - 5) * (cwDifficultyDecay - 0.3))@, so a hard card experiences
  -- time faster while decaying with the same slope.
  , CurveWeights -> Double
cwStabilityDecay1 :: !Double
  -- ^ @w33@; the fast component's decay is scaled by
  -- @s_fast ** (cwStabilityDecay1 - 0.3)@.
  }
  deriving stock (CurveWeights -> CurveWeights -> Bool
(CurveWeights -> CurveWeights -> Bool)
-> (CurveWeights -> CurveWeights -> Bool) -> Eq CurveWeights
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CurveWeights -> CurveWeights -> Bool
== :: CurveWeights -> CurveWeights -> Bool
$c/= :: CurveWeights -> CurveWeights -> Bool
/= :: CurveWeights -> CurveWeights -> Bool
Eq, Int -> CurveWeights -> ShowS
[CurveWeights] -> ShowS
CurveWeights -> [Char]
(Int -> CurveWeights -> ShowS)
-> (CurveWeights -> [Char])
-> ([CurveWeights] -> ShowS)
-> Show CurveWeights
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CurveWeights -> ShowS
showsPrec :: Int -> CurveWeights -> ShowS
$cshow :: CurveWeights -> [Char]
show :: CurveWeights -> [Char]
$cshowList :: [CurveWeights] -> ShowS
showList :: [CurveWeights] -> ShowS
Show)

-- | Read the forgetting-curve block out of a parameter vector.
curveWeights :: Parameters -> CurveWeights
curveWeights :: Parameters -> CurveWeights
curveWeights Parameters
p =
  CurveWeights
    { cwDecayBase1 :: Double
cwDecayBase1 = Parameters -> Int -> Double
parameterAt Parameters
p Int
23
    , cwDecay2 :: Double
cwDecay2 = Parameters -> Int -> Double
parameterAt Parameters
p Int
24
    , cwBase1 :: Double
cwBase1 = Parameters -> Int -> Double
parameterAt Parameters
p Int
25
    , cwBase2 :: Double
cwBase2 = Parameters -> Int -> Double
parameterAt Parameters
p Int
26
    , cwWeight1 :: Double
cwWeight1 = Parameters -> Int -> Double
parameterAt Parameters
p Int
27
    , cwWeight2 :: Double
cwWeight2 = Parameters -> Int -> Double
parameterAt Parameters
p Int
28
    , cwStabilityPower1 :: Double
cwStabilityPower1 = Parameters -> Int -> Double
parameterAt Parameters
p Int
29
    , cwStabilityPower2 :: Double
cwStabilityPower2 = Parameters -> Int -> Double
parameterAt Parameters
p Int
30
    , cwDifficultyWeight :: Double
cwDifficultyWeight = Parameters -> Int -> Double
parameterAt Parameters
p Int
31
    , cwDifficultyDecay :: Double
cwDifficultyDecay = Parameters -> Int -> Double
parameterAt Parameters
p Int
32
    , cwStabilityDecay1 :: Double
cwStabilityDecay1 = Parameters -> Int -> Double
parameterAt Parameters
p Int
33
    }