How I failed to hack my sleep

Sleep is essential for a human. We spend around one-third of our life sleeping. Why not try to know a bit more about it?

People often write about their success, and it’s really cool. This article would be different, though.

Preface

Five weeks ago I started the fantastic Machine Learning course by Andrew Ng on Coursera. Soon I’ve learned a linear regression, and since then it was tempting to apply it somewhere outside of the course. Another important thing for this story is that around a year ago I’ve bought a SmartWatch with sleep tracking functionality.

The Idea

Two previous facts have melted together into the idea: why not to create a formula for my sleep? I wanted to know what is the perfect time to go to bed and how much ideally, should I sleep? Also, if I fell asleep later - how much more I need to rest to catch up? Is this dependency linear, quadratic, cubic or something else? I was eager to get answers to all these questions!

To measure how good my sleep was I chose relation of deep sleep to total time in bed. If I would find a function, which by In bed at time and Duration time can predict this relation - I would only need to find the maximums.

x1 = In bed at
x2 = Duration of sleep
y = deep sleep / total sleep
f (x1, x2) = y

Why only two parameters? Because the chart would be 3D and I could observe these maximums visually without trying to find them programmatically. Latter could be hard because the function can have multiple extremums.

Math

One of the exercises during the course was similar - you have two variables, value and you need to find a function that predicts the value for arbitrary data. I wanted to reuse the code, so I’ve arranged my sleep data to exact the same format from this exercise. Also, I did some filtering, and after all of this, I had around 300+ data records in the following form:

In bed at (minutes after 22:00) Duration (minutes) deep sleep / total sleep
60 480 0.35
120 420 0.2

I pointed the code to my sleep data file and pressed “Enter”. I was waiting for some “nice” function. Well, guess what, I did not get one. I plotted data points together with the prediction function chart, and they were like a swarm, not a surface of some shape (colorful bubbles on the picture below).

Alt Text Duration in minutes. In bed at in minutes after 22:00

I think in these circumstances, no proper linear prediction function can be found, but I still have tried different powers of polynomial and played with some coefficients. One of these attempts you can see on the image above (dark coloured surface). But all of these did not solve “the sleep formula” for me.

Caveats of my experiment

Deep sleep to total sleep relation, probably, is not the right way to measure the quality of sleep.

Data from my SmartWatch could be really noisy.

Conclusion

I have not found the correlation I was looking. But, what if, there should not be one? Because of other factors, like sport, stress, alcohol and so on influence sleep as well and I did not encounter them in my function. Probably, an absence of the correlation was an even better result, that I hoped. Now I can say, that one cannot determine the relation of deep sleep to total sleep by only these two factors.

Sleep is complicated. I’ve tried to “hack” it and failed, but I’m happy that I’ve done this and I would do it again in the future with some more skills and knowledge applied.

Sleep well. Eat fruits and vegetables!

October 17, 2018