Instantly share code, notes, and snippets.

# riceissa/anki_algorithm.py

Last active December 15, 2023 09:36
Show Gist options
• Save riceissa/1ead1b9881ffbb48793565ce69d7dbdd to your computer and use it in GitHub Desktop.
my current understanding of Anki's spacing algorithm
This file contains 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

### ilbonte commented Apr 17, 2020

On line 74 I think you mean `status`, not `state`

### riceissa commented Apr 17, 2020

@ilbonte Thanks, fixed.

### ilbonte commented Apr 20, 2020

Also there is something odd: the `INTERVAL_MODIFIER` is constant to 100 and it's always divided for 100

### riceissa commented Apr 21, 2020

Also there is something odd: the `INTERVAL_MODIFIER` is constant to 100 and it's always divided for 100

I think that's the intended behavior (i.e. do absolutely nothing by default). From the Anki docs:

Interval modifier allows you to apply a multiplication factor to the intervals Anki generates. At its default of 100% it does nothing; if you set it to 80% for example, intervals will be generated at 80% of their normal size (so a 10 day interval would become 8 days). You can thus use the multiplier to make Anki present cards more or less frequently than it would otherwise, trading study time for retention or vice versa.

### ilbonte commented Apr 22, 2020

Thanks for the explanation, I've missed that :)

### ctrngk commented Sep 22, 2020

Thank you for your code. It's awesome.

How to calculate retention rate by the way?
It is somehow related to mature cards (>21 days ) and other stuffs.
Suppose we have 100 matured cards, we review all on the next day(22th). 50% wrong, 50% good. What is retention rate and true retention rate?
some related sources I am still confused

### riceissa commented Sep 22, 2020

@ctrngk If you have 100 cards and tend to get 90% of them correct if you review them when each card is due (rather than all on the next day), then the (ordinary) retention rate is 0.9 (if you go to Anki stats, this is the number you find in the "Answer Buttons" section where it says "Correct: X%"), and the forgetting index (FI) is 0.1. The true retention is -FI/log(1-FI) = 0.95. As Matt explains in the video you link to, the true retention is accounting for the fact that reviewing when a card is due is "unfair" (because even if you fail you would have remembered it for possibly most of the time period between the review session where you got it right and the review session where you got it wrong).

If you review all of your cards on a single day, then that is the true retention. (The true retention is just measuring "if you randomly stopped me on the street and asked me to review a random Anki card in my collection, what is the probability I get it right?" So by reviewing all cards at once we simulate this experiment.)

If for some magical reason all of your cards just happened to be due on the same day, then on the day the cards are due your true retention would equal your ordinary retention. But in real life your cards are never all due on the same day, so your true retention is higher.

### nejedlypetr commented Aug 11, 2021

Lines 77-79:
`the Anki manual says "the current interval is multiplied by the value of new interval", but I have no idea what the "new interval" is`
At first, I was confused by this as well but I think that the new interval is already defined on line 31:
`NEW_INTERVAL = 70 # in percent`

Other than that great job @riceissa, thanks a lot.

### riceissa commented Aug 11, 2021

@nejedlypetr Ah ok great! It looks like I was applying the `NEW_INTERVAL` when going from 'relearning' back to 'learned', whereas the Anki manual says to apply it already when going from 'learned' to 'relearning'. I've fixed this now so it works more like what the Anki manual says.

### grandinquisitor commented Jan 12, 2023

there's a spelling inconsistency: `LAPSE_STEPS` vs `LAPSES_STEPS`

### riceissa commented Jan 12, 2023

@grandinquisitor Thanks, fixed!

### L-M-Sherlock commented Jan 13, 2023

I developed a new spacing algorithm for Anki. Maybe you will be interested in it: https://github.com/open-spaced-repetition/fsrs4anki

### riceissa commented Dec 15, 2023

@L-M-Sherlock I saw that Anki now supports FSRS by default. I've sadly not had any time to look into FSRS or to use it. I've added a note at the top of the script mentioning this.