With sleeping this thing to play the Bayesian inferred that the Bayes

Source: Internet
Author: User
Tags jupyter notebook
The basic idea of Bayesian inference is to "make fewer mistakes" by using more data. The inference process is straightforward: we first have an initial belief, known as a priori, that can be revised and updated when we have additional information. Although we do not know that this process is Bayesian inference, but we have been using this technique.
For example, we may initially think we have a 50% chance of getting a promotion at the end of the quarter. If we get positive feedback from the manager then we're going to adjust the possibility of the promotion, and conversely, if we mess up the coffee machine, we might have to lower the probability. As we continue to get more information, we can constantly revise our estimates to approximate the "right" answer. The above intuitive behavior can be formalized using the following simple but powerful formula called the Bayes rule:

The one on the left of the equal sign is called a posteriori, and we read the conditional probability that event a occurs after a given event B occurs. On the right side of the equal sign, p (A) is called a priori, or is called our initial belief in the probability of occurrence of event A, p (b| A) is called likelihood (also a conditional probability), which can be obtained from the data, and P (B) is a normalized constant so that the sum of probability distributions is 1. The general form of the Bayes rule described in statistical language is that the posterior probability equals likelihood multiplied by priori divided by normalized constants. This short equation leads to the whole field of Bayesian inference, and also gives people an effective way of reasoning about the world.

Although A and b in the equation are very good placeholders, they do not help us understand how to use the concept well. In order to achieve this goal, we can apply the Bayes law to solve a real-world problem.

▌ Case


One of the problems I've been working on in my own life is sleep patterns. I've collected more than two months of data from my own Garmin Vivosmart smart watch, which shows when I fall asleep and get up. In the previous article, I used the Markov chain Monte Carlo (Markov Chain Monte Carlo, MCMC) method to calculate the probability that I would fall asleep at a given time. The final model shows that the most likely distribution of sleep is a function of time (MCMC is an approximate method), as shown in the following figure.




This is the probability that I fall asleep only considering the time factor. What if we knew the time and we knew the rest of the evidence. If I knew my bedroom light was on, it would change the probability of my sleeping. This is where we can use the Bayes rule to update our estimates. For a specific time, if we know the information about my bedroom light, then we can use the probability distribution above as our priori and then apply the Bayes formula:




The equal sign to the left is a posteriori, that is, given the condition of my bedroom light (on or off), the probability of my falling asleep. If we do not have additional information to provide, then the probability of sleep at a given time will be our transcendental, P (sleeping), or as our estimate. For example, at 10:00 in the evening, I fell asleep with a priori probability of 27.34%. If we really have more information, then we can use likelihood to update this probability, P (bedroom light |sleep), which may be obtained from observational data. According to my habits, I know the probability of my bedroom light on when I'm Asleep is 1%. That




The probability of my light turning off when I'm asleep is 1–0.01 = 0.99 (I use a minus sign (-) here to indicate the opposite). This is because the sum of the conditional probability distributions is 1. If we knew I was asleep, my bedroom light must be on or off in one of two situations.


The last term of the equation is normalized constant p (light). This is the full probability that my lamp is open. My light is on. Two things are happening: I'm asleep or I'm awake. So if we know the transcendental probability that I'm asleep, we can compute the normalized constants like this:




The full probability of my light being open considered two possibilities, I fell asleep and my light was on, and I was awake and my light was on. (P (-sleep) = 1-p (sleep) is the probability that I am awake)


The probability that my lamp was open when I was awake, P (light |-sleep), was also determined from the observational data. In my case, I knew that if I was awake, my bedroom light would be open for about a 80% of the way (it also means that if I was awake, 20% of my lights would be closed).


Using the full probability that my light is on, the Bayes formula can be written as:




This formula shows the probability of my falling asleep when my light is on. If my lights go out, then we can put the P (light| ...) in the formula. All replaced by P (-light| ...).


There are only a few words in the equation, let's see how we can use them with numbers.


According to the formula, we calculate the probability of falling asleep at 10:30 in the evening when we know my bedroom light is on. First, we calculate the prior probability of my falling asleep based on time, and the result is 73.9%. This transcendental provides a good starting point for our estimates, but we can improve this estimate by adding information about my bedroom lights. Now that I know that my bedroom light is on, we can fill in the Bayes formula with the relevant numbers:




The bedroom light is on. This knowledge has largely changed our estimate of the probability of my falling asleep, from 70% to 3.42%. This change shows the power of the Bayes Law: by introducing more information we can update our initial estimates. Although we may do this on a regular basis, using formal formulas to think about this process can allow us to renew our beliefs in a rigorous way.


Let's try another example. Suppose it's 9:45 in the evening and my bedroom lights are out. Starting with a priori probability of 0.1206, try to solve this example.


Instead of making manual inferences every time, I wrote some Python code to do the above calculations, and you can play with the code in this Jupyter notebook.


The code outputs the following answers:



time:09:45:00 PM Light is off.
The prior probability of sleep:12.06%
The updated probability of sleep:40.44%


We have once again found that additional information has changed our estimates. Now, if my sister is going to call me at 9:45 in the evening, and she somehow knows that my bedroom light is on, she can use this equation to determine if I'm going to answer the phone (assuming I always answer the phone when I'm awake). Who says you can't use statistics in your daily life?


It is helpful to see numerical results, but data visualization can explain the problem more clearly. When communicating ideas with others, if they cannot be understood directly through equations, then I always try to introduce images to explain them. Below, we can use the extra data to visualize the transcendental and conditional probability distributions that I fell asleep in.





When my bedroom light is on, the curve moves to the right, meaning that I fall asleep at a certain point in time with a low probability. Similarly, if my lights went out, the curve moved to the left. It is difficult to understand the concept of a statistical concept, but the illustration shows exactly why we use the Bayes rule. If we want to make fewer mistakes in this world, then the extra information should change our beliefs, and Bayesian inference is using a systematic approach to update our estimates.


▌ to use more evidence


Why do I have to stop here in my bedroom light? We can use as much information as possible in the model, and the model will continue to be more accurate (as long as the data tells us useful information about the situation). For example, if I know that my phone is charging at 95% in the case of my sleep, then we can add that knowledge to our model.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.