Wednesday, March 23, 2016

Fringe in Practice: How Does Sleep Affect Performance?

Does Sleep Affect Performance?

This is a great question! Conventional wisdom in the running world has taught me that it is not the night before which affects performance - the sleep two nights prior is key.

I decided to test this.

As of today, I have been recording a number of data points daily for 1 year exactly. It is apropos to investigate these numbers, then. The recoreded data points contain features such as:
  • Hours of sleep
  • Training volume (hours)
  • Calories in
  • Calories out
  • Fatigue (1-10, 1 being the least fatigued)
  • Freshness (1-10, 1 being the least fresh)
  • RPE (rating of perceived effort, 1-10, 1 being the least taxing level)
  • RPP (rating of perceived performance, 1-10, 1 being the most taxing level)
I also keep track of a number of other features (such as ATL, CTL, TSB, etc.), but these are not relevant for this discussion, as you will see briefly.

It is worth noting that I am not a fanatic about recording data - there are plenty of holes in the data which reflect times when it was not feasible to record data. For instance, ATL and CTL rely on heart rate or power data to be accurate - but I do not always use a heart rate monitor or power meter for my workouts. If I am travelling, I may not have access to the measurement tools (such as a scale).

It is also worth noting that I use specialized software to track workouts and caloric intake (in the form of macronutrients). While not a perfect representation of reality, the tool does model things reasonably well. I recently compared my actual weight differential over the past year with the estimated weight differential from the software and the difference between the two values was somewhere about 1%. I probably got lucky because hydration levels could easily account for a few pounds differential - but the point is that tracking does provide some degree of accuracy.

With that out of the way, let's get to the fringe. Using R, I quickly created a correlation plot of some of the features I suspected might affect RPP (my perception of performance):

Correlation Plot of Features Thought to Affect Performance
This plot does not really tell a whole lot. The red dots indicate that TSB is strongly and inversely related to ATL, which is basically the definition of TSB. Consequently, we should remove ATL and TSB for our next attempt. Same for ATL and RPE, also CTL and RPE. These are all expected, this correlation plot just validates that thought.

Sleep seems to be somewhat related to TSS and TSS is strongly related to RPE, calories consumed, IF and ATL. Again, these are all expected - the longer you work out per day, the harder the effort and the more food you need.

Let's try again, this time removing the strong (but obvious) correlations in order to show any correlations which may have been shaddowed by strong correlations.

Take 2: Remove obvious correlations
Now we are getting somewhere! Despite the direct correlation between calories in (KCal.in) and RPE, we see that sleep is correlated to RPE.

Time to investigate this further. I am performing these next calculations in Google Sheets (a very capable online spreadsheet). Armed with what I just learned from the correlation plots, I will look at the the covariance and correlation between some of these interesting features.

The covariance value tells us how much two values are related to each other, but that number is not scaled for comparison. The correlation is sort of like the normalized covariance and is represented in a number between -1 and 1. For both covariance and correlation, a negative value indicates an inverse relationship and a positive value indicates a direct relationship.

For example, consider that a and b have a covariance of 0.29, while c and d have a covariance of 0.98. We cannot make any claims that a and b are less related than c and d because the relationships between the two pairs may be vastly different. However, after we calculate the correlation of the two covariances, we can compare the correlation values. Continuing the example, let's say that a and b have a correlation of 0.62 and c and d have a correlation value of 0.22. We can now have more confidence when we assert that the correlation between and b is stronger than that of and d.

My end-game is to figure out what contributes to optimal performance, so I need to start defining what "optimal performance" is. For now, let's start with "low RPE and high RPP". The actual optimization will be a topic for another day, but for now, I am using a combing function which examines both RPE and RPP in order to derive a value, QWS (quality workout score). The higher QWS is, the better.


Considering the amount of sleep the night before, these are my findings:



Sleep The Night Prior
RPERPPQWS
Covariance0.290.240.02
Correlation0.130.110.12

The most important number is the bottom-right number: the "correlation" of sleep vs. QWS. The correlation is somewhat low, but perhaps still significant. 

For the next two charts, keep in mind that the slope indicates the correlation - the steeper the slope, the stronger the correlation. Plotting the values of sleep the night prior vs. QWS , we get this chart:


Sleep Two Nights Prior

RPERPPQWS
Covariance0.200.180.00
Correlation0.100.080.02

In this case, the sleep vs. QWS correlation is extremely low and probably not significant at all. Plotting this data we end up with a chart that looks like this:


My data and analysis shows that the sleep 2 nights prior is far less important than the sleep the night prior. However, the sleep the night prior is not a terribly good indicator of QWS. Perhaps we would have to look at multiple days of sleep leading up to a workout in order to get a better idea of how sleep plays into performance. Or consider more than one feature at a time (i.e. CTL, ATL and sleep). Also, using data for higher volume only days might yield better results. This is because most shorter workouts tend to be easy, steady state efforts and I suspect that I am not as likely to consider the workouts as tough.

This is certainly a fascinating topic and I would love to explore this further.

Takeaway

The sleep the night before does not appear to have a big affect on the QWS of a workout, and the sleep two days prior is essentially a non-player. Your mileage may vary.

One Last Point

NOTE: I looked at the covariance and correlation between weight and QWS as well as carry over balance (basically eating a lot of "extra" food the day before a long workout) and QWS, and these are my findings:
  • The heavier I am (within my normal range), the fresher I feel; QWS is slightly negatively correlated. I think the physiological advantage gained by being a few pounds heavier outweighs the negative affect on performance. It might be useful to figure out the optimal weight, as well.
  • Carrying extra calories into a workout has an extremely small affect QWS. Consequently, there is little need to consume extra calories the day before a big workout or event.

I also looked at how training volume correlates to QWS and found a significant inverse correlation. Simply put, the more time I work out in any one day, the less fatigued I feel. As well, more sleep leads to less fatigue.


Tuesday, March 22, 2016

Contextual Hints And Using Them For Personal Gain

Contextual Hints

These are "things that tell us more information about something". In technical speak, we call this "adding context". But you probably know this as "Facebook Tags". As in:

This image comes from a TechCrunch article

Here, the image has a "contextual hint", or additional information about the contents of the image. In these cases, we are told who is in the image. I am sure that friends of Josh and Zack know who Josh and Zack are - but I did not know. That is, until I saw the contextual hint.

Contextual hints can sometimes provide information not captured within the original item - an example may be "recommended movies" from Netflix. The recommended items for a particular movie might give us information about the current movie we are viewing - perhaps the genre of the movie may become apparent. Here is an example:

Netflix Recommended Movies Example

You stumble upon a movie called "The Bad Day" (a movie that I made up for this example). The recommended movies immediately visible are:

  • Failure to Launch
  • How to Lose a Guy in 10 Days
  • Fool's Gold


Based on those recommendations, you may conclude that "The Bad Day" is a great rom-com, likely starring Matthew McConaughey.


But what if  the recommended movies for "The Bad Day" were:
  • Saw
  • Hostel
  • The Bad Day 7 (in 3D)
Well, now you may consider that "The Bad Day" is a horror movie.


In both these examples, we can make inferences about the original item based on the information we have about related items.

Other Familiar Examples


Some other forms of contextual hints:
  • Location data encoded in photos
  • "Similar Items" recommendations from Amazon
  • Accessibility hints (notice the pop-up hint that helps explain what the image is)
  • Info Cards on movies
This image is from the Official Android Blog
  • The Chrome Omnibar (bringing awareness to justice beavers across the world)

A good contextual hints is not necessary, but should be helpful. In other words, we should be able to figure out what something is in the absence of contextual hints, but having the extra information can aid in recognition and understanding.

How This Relates to Me

Imagine that I have a lot of digital images of my friends, and I want to sort them based on who is in the image. With a small amount of code and a good image processing library, I could build an individualized facial recognition model for each person I expect to find in my collection of photos. But the problem is that I would first need to sort the photos in order to train the models - so the solution to my problem is the very problem I am trying to solve. In computer science, we call this recursion. In the normal world, we call this the chicken and egg problem.

But, if we are clever, we may be able to use existing contextual information to do a lot of the work for us. Within Facebook, people may be tagged in images; generally, the face of the person is outlined in a overlay box and the Facebook profile for the user is linked in a popup hint. This is a great form of contextual information - it is also a great way for me to extract the face of the user so that I can use it as training data. In fact, Facebook provides all the information we need - the face and a link to associated user's Facebook profile.

Warning, this next part is a little techy...

Let's figure out a general algorithm. Assume that we can use the Facebook Graph API to query for users and images (for the time being, ignore any oauth scope or permission problems).

For each image for a specific user
  • Step 1: Save the image and crop down to the overly box, resulting in a small image of the user's face
  • Step 2: Get the Facebook user id (or the username) from the URL provided in the contextual hint
  • Step 3: Optionally query the graph API for other images which contain tags of the user
  • Step 4: Repeat with other images until enough training data is present
During these steps, we might also save the full image and other contextual information, such as:
  • Any location data in the image (GPS or place information)
  • Who else is tagged in the image
  • The time and date of the photo (cross referenced with weather data and GPS data, we may be able to use this additional information to make inferences about the image - I am getting ahead of myself, but hopefully you get the point)

// Get images and metadata from Facebook using the Graph API
//
// Get the Facebook User ID
var fbProfile = fb.queryGraphForProfile(fbUsername);
// Get the profile object for the Facebook User (given the ID)
var fbID = fbProfile.fbUserId;
// Get all images in which the user is tagged
//  NOTE: The graph call will actually return a series of pages with results, but
//        for this example, we have reduced the stream to an array
var imageIDs[] = fb.queryGraphForTaggedImages(fbID);

// Go through all the images in the array
forEach(imageId in imageIDs) {
    // Get the image from the Image ID
    var image = fb.queryGraphForImage(imageId);

    // Crop the image based on tag information
    //  NOTE: This is a bit more involved because we need to query to find the crop coordinates
    //
    // It might be worthwhile to also save the original image and any metadata about the image
    // for future reference.
    //
    // Use fbProfile to associate the image with the cropped image and the Facebook profile
    saveImageToStorage(getAndCropByMetadata(image), fbProfile, image);
}

I have just written pseudo-code to automatically crowd-source Facebook image tags in order to build a database of tagged facial images custom tailored to specific people. We can now use an image processing library to build a facial recognition model for every person in my digital images. Hopefully we have enough data so we can build a decent model and validate that model with a good degree of accuracy and precision.

// Load the images from  storage (each image is associated with a Facebook profile
var allImages = getImagesFromStorage();

// Get 90% of the images
var trainingImages = sample(allImages, .90);

// The other 10% is used for validation
var validationImages = Sets.difference(allImages, trainingImages);

// Build a facial recognition model from the training data (remember, each image
// is associated with a Facebook profile; let's assume that the model associates the
// image to the Facebook UserID
var model = FancyImageLib.buildFacialRecognitionModel(trainingImages);

// Validate the model
//  NOTE: The validation is a bit more complex and we should use images from other people
//        in the validation routine in order to measure false positives
var validationResults = Validator.validate(model, validationImages);

// Hope that our model is good!
output(validationResults);

Since this is pseudo-code, we can assume that the pseudo-results are quick and accurate. This leaves us with a facial recognition model that I can use to automatically sort my digital images based on faces.

At this point, we can (hopefully) reliably sort my entire digital archive based on people in the photos. But the cool part is that the facial recognition model was built entirely from contextual hints provided by Facebook.

Takeaway

The important point to takeaway from this discussion is that contextual information can be very valuable. What makes my particular application exciting is that we can leverage otherwise boring contextual information (photo tags) in order to automatically perform some higher-order functionality (build and use facial recognition).

One Last Point

This is a blog about theory. While the ability to build individualized facial recognition models based on Facebook image tags is possible, I am unaware of any actual implementation in the wild. This does not mean such an implementation does not exist, rather that if it does exist, I have not seen it. 

Friday, March 18, 2016

AI is Just A, Corn Mazes and Training Your Model

AI

Artificial Intelligence seems to be pretty popular these days. We see it in movies, we hear about it in popular culture (Watson, the computer that kicked butt in Jeopardy) and we read about it in books.

For decades, people have been prophesying that artificial intelligence will rise up. Machines will become self aware (2001: A Space Odyssey, War Games, Terminator, etc.). This makes for a great story and movies with a lot of explosions and Schwarzenegger's, but the concept is not really grounded in fact.

The truth is, artificial intelligence really has little intelligence - insofar as actually being able to reason. Computer Scientists say that computers "learn" (a topic I hope to cover in depth later), and this is true to an extent - I suppose it depends on your definition of "learn". But Computer Scientists pretty much agree that "reasoning" is substantially harder for a computer.

Learning

When a computer "learns", the computer is looking for patterns and correlations within a set of data. If your memory was super fast and your recall was immediate, you could easily processes thousands of images and draw some conclusions about the images. Consider a case where all the images have some sort of plant in them. If you come across an image of a plant which you have never seen, you likely will be able to recognize the image as an image of a plant. How your brain stores, processes and predicts what is likely to be a plant is extraordinarily complicated.

Yet, we can "train" a computer to recognize a plant, too. It is important to understand that the computer "learns" what a plant is in a very different way. One approach might be to tell the computer that it is about to look at images of plants. For each image, the computer might evaluate the colors in the image, the shapes within the image, identify contours to better understand the relationships of the shapes and so on. After looking at thousands of images, the computer can then generate a "model" which roughly estimates what features are common across most of the images. For example, most of the images are likely to be rich in green and have somewhat rounded shapes (leaves) with some straight lines connecting the round shapes (branches).

But wait! What if we trick the computer and show it a black and white image? Based on the model that the computer has previously generated, it is very unlikely that the computer will be able to recognize the image as a plant. The failure in this case represents a fault in the model: perhaps we depended too much on color images. An improvement may be to train the computer with both color and black and white images in order to build some variation into the model.

The "how" portion of finding correlations in this example is somewhat complex, but you can conceptually think of the computer looking for "things that are the same" and assigning percentage values to these features - when presented with a new image, the features are identified and the percentages are processed. Most plants have roundish leaves, so "round things" should have a very high percentage associated with it. On the other hand, "spots" probably ranks low because a good number of plants are not spotted. And so it goes with other interesting features that the computer is programmed to look for. The key is programmed. Without instructions of what to look for, the computer simply will not be able to recognize a plant.

Fortunately, we do not have to be terribly specific when we tell the computer what to look for. By way of example, we can simply tell the computer what "shapes" are and how to identify them. Then we can instruct the computer to identify all  the shapes in the images and figure out what the most common shape is. So as long as the computer knows how to identify a rectangle or circle, the computer should be able to figure out how common these shapes are in the image.

The Vroomin Koomen And Corn Mazes (Spoiler Alert)

When I was doing my undergraduate work, I was involved with a team project that involved building and programming a robot to navigate a maze. At first, this seemed like a pretty hard task - but once we researched maze theory and built a proof-of-concept robot, we found that it is not hard at all - there is little "intelligence" when navigating a maze. At least, some mazes. You see, if the maze has no moving parts (unlike like Maze Runner) and no cycles (endless loops), then one simply applies the "right hand rule" (or "left hand rule" if one is a southpaw). The "rule" is just a simple algorithm:
  1. If you can go right, go right
  2. If you cannot go right, go straight
  3. If you cannot go straight, go left
That is it - I have given you the solution to every corn maze in the nation! While this approach is guaranteed to generate a correct result (caveat - the maze entrance and exit must be on an edge of the maze), the result may not be optimal. Moreover, corn mazes tend to have "checkpoints" within the maze. These checkpoints add some level of adventure to the maze, but also serve to draw players in from the edges - rendering the "rule" useless. A number of corn mazes also have high bridges that allow players to climb up and see the maze from a higher perspective. This may help players see parts of the maze from an advantageous position, but often times the bridges introduce "cycles" in the maze; essentially allowing for infinite loops. Think of it as trying to find your way out of a maze when you are next to a tree - if you apply the right hand rule to the tree, you will circle the tree forever.

Because bridges can introduce infinite loops, I think a good idea for a corn maze would be to have a bridge connected to a edge which must be traversed - this would introduce the possibility of an endless loop which cannot be avoided.

Once we established the algorithm, all that needed to be done was create software to drive the robot while following the "rule". This introduced some real-world programs (such as over-steering, processing real time inputs and accounting for terrain imperfections), but overall was an achievable goal. We named the robot "Vroomin Koomen" in honor of one of the university's professors whom we all held in high regard.

Reasoning

What is the difference between reasoning and learning? I like to think of reasoning as "the next level" of learning. When we reason, we recall all of the pieces of information (evidence) we have which may be relevant to the topic at hand. The sources are varied and may not be tangible, like memories or past experiences. We consider what conclusions are possible, as well as the likelihood that each conclusion is possible based on the evidence. We assimilate several classifications of data, filter out non-relevant data maybe make some seemingly complex connections based on intuition or "gut instinct". We put all this together and then we draw higher level conclusions about the data.

Takeaway

Despite going off topic a bit, the most important point I am trying to make is that we need not fear artificial intelligence. The machine apocalypse is a long time from now. Think about this: we can't even get machines to predict the weather any better than by flipping a coin. But we are finding ways to use machine learning to process large amounts of data and drive decisions a number of industries - marketing, manufacturing, banking, pharmaceutical and pretty much everywhere else.