SM-2 Algorithm Pseudo-code

TL;DR: How exactly is recall period calculated in Anki (in a few lines of pseudo-code or equations)?

I would like to understand the logic which calculates the card review period.

Currently, I only have a high level conceptual understanding of how it works i.e. the more difficult the question is rated (by user response at each card recall and difficulty level assigned by the card creator (Easiness-Factor), the shorter the period until next recall.

The Anki Manual says Anki uses SuperMemo’s SM-2 algorithm but the link referencing that just takes me back to the Anki Manual home page (Anki Manual).

The nearest answer I could find was on SuperMemo:

1. Split the knowledge into smallest possible items.
2. With all items associate an E-Factor equal to 2.5.
3. Repeat items using the following intervals:

**I(1):=1**

**I(2):=6**

**for n>2: I(n):=I(n-1)*EF**

where:
  * I(n) - inter-repetition interval after the n-th repetition (in days),
  * EF - E-Factor of a given item

If interval is a fraction, round it up to the nearest integer.

4. After each repetition assess the quality of repetition response in 0-5 grade scale:

5 - perfect response

4 - correct response after a hesitation

3 - correct response recalled with serious difficulty

2 - incorrect response; where the correct one seemed easy to recall

1 - incorrect response; the correct one remembered

0 - complete blackout.

5. After each repetition, before computing the new interval, modify the E-Factor of the recently repeated item according to the formula:

EF':=EF+(0.1-(5-q)*(0.08+(5-q)*0.02))

where:
  * EF' - new value of the E-Factor,
  * EF - old value of the E-Factor,
  * q - quality of the response in the 0-5 grade scale.

If EF is less than 1.3 then let EF be 1.3.

6. If the quality of the response was lower than 3 then start repetitions for the item from the beginning without changing the E-Factor (i.e. use intervals I(1), I(2) etc. as if the item was memorized anew).
7. After each repetition session of a given day repeat again all items that scored below four in the quality assessment. Continue the repetitions until all of these items score at least four.

So I tested this for the case of a single new card in a new deck in Anki v2.1.30 (without changing any of the default settings). Answered it correctly (selected “Good <10 mins”). It immediately asked me the same question again. I gave the same response again (“Good <10 mins”). Then it ended the session ("… finished this deck for now"). I checked the card details to find the the next recall was due tomorrow.

I don’t know what the units of the equation provided by SuperMemo for the first and second recall times (I(1):=1 … ) but I cannot see how I would get from those equations to the results I see in Anki. Please could someone help me understand what I am missing? Thank you.

1 Like

Basically: new interval = last interval x ease factor. There are a bunch of exceptions to this about which you can read in the manual, most notably learning cards as in your example.
It’s not formulated as pseudo code, though, because it’s focused on the practical side and you can always read the source code directly.

3 Likes

https://faqs.ankiweb.net/what-spaced-repetition-algorithm.html

3 Likes

image

AFAIK, algorithm of learning new cards is very different from reviewing cards(that you copy from SuperMemo).

You clicked Good twice, the second time is not (“Good <10 mins”), but (“Good 1d”), after you click it, the card “graduated”, next call will be Graduating interval, i.e. 1 day.

If you click Easy, it will be 4 days (that is Easy interval).

And “Start Ease” for graduated card is always 250%, no matter how much times Hard, Good you clicked.

1 Like

Hi, as suggested, I’m trying to read the source code directly but I’m having some problems so I will submit another post for this but in the meanwhile, please could you copy the relevant functions over here? Thank you.

What exactly is the criteria Anki uses differentiate between ‘learning’ cards and ‘reviewing’ cards? Does it go from ‘learning’ to ‘reviewing’ when you respond with at least ‘good’ a certain number of consecutive times?

What exactly is the algorithm it uses for ‘learning’ cards? For my example it seems it ‘graduated’ from ‘learning’ to ‘reviewing’ after two consecutive ‘good responses’ and the space between them was ‘<10 mins’.
In your screenshot, I can see the ‘steps (in minutes): 2, 12’ which I assume means if you answer it correct first time, it waits at least 2 mins, then if you answer it correct again, it waits 12 mins, then if you answer at least ‘good’ again it graduates to ‘reviewing’ (next recall after one day)?
How do I see those options you showed in your screenshot? The nearest I could find (in Anki 2.1.30) was Tools>Preferences>Scheduling

But I cannot see those options there.

2min is Again interval , 12 is Good, and in scheduler V2, there is a Hard=(Again+Good)/2=7

About option:

1 Like

Ok so now I understand how Anki calculates the recall period between sessions (in the order of days). Now, I would like to understand how exactly Anki decides which card to present next within a session. How does it choose between presenting a new card and cards which all satisfy the criteria of “<1min” and “<10 mins” since their last recall? Which has priority and when? What’s the logic behind it?