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”. 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.

Exercise regularly.

Limit caffeine and alcohol consumption.

Don’t eat late at night.

Don’t nap.

Wind down an hour before bed.

Invest in your bed. Keep your bedroom dark and cool.

Unintuitive New Invariants

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

Never get in bed and try to sleep because “it’s bedtime”. Only get in bed when you are dying to go to bed. If you’re in bed for more than 20 minutes and haven’t fallen asleep yet, get out and do something else. Get out of bed right away when you wake up in the morning. Don’t linger and don’t try to sleep in. 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.”

Start out by setting a wake up time that leaves you plenty of time in the morning. For me, this was 6 a.m. 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. 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. When you wake up, jot down the following: When you went to bed When you fell asleep How long you spent awake in the middle of the night When you woke up When you got out of bed 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 : efficiency = average( time asleep time in bed ) = 1 7 ∑ i = 1 7 d i − b i − c i e i − a i \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} efficiency=average(time in bedtime asleep​)=71​∑i=17​ei​−ai​di​−bi​−ci​​ If efficiency < . 8 \text{efficiency} < .8 efficiency < . 8 , push your target sleep time back even later by 20 minutes. If . 9 ≤ efficiency < . 95 .9 \leq \text{efficiency} < .95 . 9 ≤ efficiency < . 9 5 , push your target sleep time up earlier by 20 minutes. Otherwise, stay the course. 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 rhythm. 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.

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: