Ilya Sukhar

An Algorithmic Solution to Insomnia

I’ve struggled with insomnia for all of my adult life. It began in college and has waxed and waned in severity ever since, correlating with stress levels but not entirely.

My form of insomnia starts with an active mind some evening. Maybe it’s active because I’m thinking through a challenge at work. Or I’m replaying a lively dinner conversation. Or I’m on a trip and excited. So, not necessarily stressful thoughts, just engaging ones. I go to bed but my mind doesn’t quiet down. 30 minutes later, I realize I’m not asleep and I start to think, “I really need to get to sleep. I have a lot going on tomorrow.” That only exacerbates the problem. My thoughts turn entirely meta and self defeating, “What’s wrong with me? Ugh, tomorrow will be miserable. Ok, let’s try for real now.” Repeat ad nauseam, laying in bed for hours.

Then, as you might imagine, the next day is indeed terrible and the next evening my mind is primed with negative thoughts about my sleep habits. The cycle continues. At its worst, I would go entirely without sleep for a half a dozen nights out of a month. In the steady state, it’d be one or two nights a month. Needless to say, both the peaks and troughs were quite disruptive to my quality of life and work output.

Over the years I had come to just accept it and cope. I figured I’d been dealt a poor hand in the relevant genetic lottery and this was the cost of doing business. Sometimes I’d use Ambien or other aids to get through it but I was always wary of the side effects and addiction potential.

In early 2016, I got fed up. I was blissfully unemployed and thoroughly de-stressed from having traveled extensively after quitting my job. Yet, I was still having issues sleeping — still going almost entirely sleepless for a couple nights a month. This seemed ridiculous, so I resolved to fix it.

I tried to read up on the latest research about insomnia but I found the volume of academic literature overwhelming for a layman. And it was mostly pharmacologically-oriented which I was intent on avoiding. Outside of journals, the online information on sleep is largely terrible — a rehash of common wisdom or an attempt to sell you something.

So, I sought out a Stanford researcher with a private sleep-therapy practice. After disqualifying me of more serious mental health issues, she ran me through a variant of a program known as CBT-i or “Cognitive Behavioral Therapy for Insomnia”1. It turned out to be extremely effective and I now have a playbook for good sleep even as my stress levels have rebounded.

I’m thrilled to have made this breakthrough. But it was time consuming and expensive to get there. And it struck me how it all boiled down to a fairly simple algorithm albeit one that requires a lot of willpower and would be hard to derive on your own.

I wish someone had told me to try this many years earlier. So I write this in hopes that it’ll help someone out there.

Common Wisdom Invariants

If you have trouble sleeping, I’m sure you’ve heard these all before. In my experience they are necessary but not sufficient.

Unintuitive New Invariants

The gist here is: “don’t force it and don’t hang out in bed.”

  1. Never get in bed and try to sleep because “it’s bedtime”. Only get in bed when you are dying to go to bed.
  2. If you’re in bed for more than 20 minutes and haven’t fallen asleep yet, get out and do something else. 3
  3. Get out of bed right away when you wake up in the morning. Don’t linger and don’t try to sleep in.
  4. Don’t change your day based on your quality of sleep. No matter how bad it was, don’t deviate from your normal routine — don’t cancel meetings, don’t skip a workout, don’t try to sleep earlier the following evening.

The Algorithm

The gist here is: “sleep a lot less before you can sleep more.”

  1. Start out by setting a wake up time that leaves you plenty of time in the morning. For me, this was 6 a.m.
  2. Pick a target sleep time that leaves you with about two hours of sleep less than you think you need. For me, this was midnight.
  3. Treat the target sleep time as a goalpost to get past. That is, don’t sleep, nap, or get in bed before it — no matter what. And make sure to strictly abide by all the invariants above.
  4. When you wake up, jot down the following:
    1. When you went to bed
    2. When you fell asleep
    3. How long you spent awake in the middle of the night
    4. When you woke up
    5. When you got out of bed
  5. At the end of every week, calculate the average of your time spent asleep divided by your time spent in bed each night. Like so 4: efficiency=average(time asleeptime in bed)=17i=17dibicieiai\text{efficiency} = \text{average(}\frac{\text{time asleep}}{\text{time in bed}}\text{)} = \frac{1}{7}\sum_{i=1}^{7}\frac{d_i-b_i-c_i}{e_i-a_i}

    1. If efficiency<.8\text{efficiency} < .8, push your target sleep time back even later by 20 minutes.
    2. If .9efficiency<.95.9 \leq \text{efficiency} < .95, push your target sleep time up earlier by 20 minutes.
    3. Otherwise, stay the course.
  6. Go back to step #3. Repeat forever.

What’s the point?

Well, it turns out that insomnia is largely a form of performance anxiety that accrues over years of episodic poor sleep due to stress, travel, environmental changes, etc. Your mind gradually loses confidence in the act of going to sleep and jumps quickly to counter-productive thoughts while you lay in bed. If you expect to sleep poorly, you will.

Drastically restricting your sleep with this regimen wrestles control away from your mind and puts it back into the rightful hands of your body and its circadian rhythm5. If you stick to the rules, you’re effectively running a search algorithm to find your body’s optimal schedule.

The algorithm starts by finding an aggregate level of sleep low enough that your body’s need for it overpowers your mind’s obstructionism. Finding this lower bound is painful but it definitely exists. Once you’ve found it, you relax the constraint gradually as your mind builds confidence that it can indeed sleep well and that the bed is a relaxing oasis. 6

Eventually the algorithm converges and you have a schedule that works without any of the bookkeeping. You still need to stick to the invariants so as not to trip up the rhythm you’ve locked in. If you do get knocked off track by a spike in stress or long term travel, just restart the algorithm.

Please let me know if this helps you!

Written December 2017

Footnotes:

  1. There’s a lot of literature about it. The algorithm described later is a subset of CBT-i called “sleep restriction therapy.” 

  2. I expected the therapist to berate me for device usage in the evening but she considered the issue entirely overblown relative to the research. 

  3. It’s helpful to have a default activity you can jump to quickly and not leave any room for engaging with daytime thoughts. I either read or watch videos about chess. 

  4. There are some apps that will help you keep track of everything. I used one made by the VA

  5. The modern model of sleep describes two related processes of which circadian rhythm is one. This regimen primarily acts by increasing “sleep pressure” in sleep-wake homeostasis, the other one. This is a good resource from Michigan. 

  6. I imagine that getting very good at evening-time meditation would also work but this seems easier and more direct.