How does AI work? Part 1
Tags: ai, ml, math, machinelearning
Published: 2023-01-25 19:58:42

---
series: How does AI work?
---

## Part 1 — A gentle introduction

![Brain](https://raw.githubusercontent.com/santisbon/reference/main/assets/img/ai-brain.webp)
###### Image credit: [Seanbatty](https://pixabay.com/en/users/Seanbatty-5097598/)

When most people hear the term Artificial Intelligence they think of the Terminator movies or the general notion of machines that can “think” the way a biological brain does. It’s safe to say that we don’t need to worry about robots enslaving humankind for the foreseeable future but chances are you’re still curious about what exactly is AI used for in real life and how it works. Some of the foundations of AI include:

* Search problems — Used in games, among other areas.
* Constraint satisfaction — Some examples could be scheduling a university’s offerings for all teachers, courses, rooms, and equipment or designing a factory floor layout.
* Logic and reasoning — Planning the sequence of actions that will achieve a goal such as a plan to take cargo shipments to their destinations in an efficient way.
* Inference — Using probability to answer questions given the available evidence.

Building on these foundations we can create systems and applications for computer vision, natural language processing, voice user interfaces, and self-driving cars. Many of these domains have something in common: pattern recognition through time. And we need to do this in noisy environments, meaning we don’t have access to the true information we’re after (such as the word someone meant to say) but instead we can only make inferences from the data available to our sensors (such as an audio signal).

For this type of problems we want to identify some basic units that combine in sequences to form larger units. These can be sounds that form words, which in turn form sentences. Or image sequences that combine to depict sign language words and sentences. It is this last application that we’ll use to dive deeper into AI. We’ll see how we can go about building an American Sign Language recognizer. But first, we’ll go through a simpler example.

---

A very widely used technique for identifying signals is something called a **Hidden Markov Model** or HMM.

There are different ways to represent HMMs so let’s start with a simple one. Let’s say you’re spending several days inside a lab with no windows, working really hard and you’d like to know whether it’s raining. Not being able to look outside, the only evidence you have access to is whether the advisor coming in every day has an umbrella with her. Let’s designate each day as a **state**, which could be rainy or not. Similarly, we’ll say the umbrella is the **evidence**, which is the presence or absence of an umbrella. 

We’ll use arrows to show when a node in our diagram influences another. For example, whether it rains today tells us something about the likelihood of getting rain tomorrow and rain influences the probability that the advisor will bring an umbrella that day. So for days 1, 2, and 3 we have:


![HHM](https://sketchviz.com/@santisbon/57ab49924c97e9c3a668086624109ebc/ae435b948d07da76cbae88ddc7f113aa87e0137b.sketchy.png)


###### Figure 1. HMM for rainy days

State zero at the far left side will be useful to us for bookkeeping purposes later so don’t worry about it for now. Remember when we said our observations were noisy? The fact that it rained today doesn’t guarantee that it will tomorrow and the fact that it rained doesn’t guarantee that the advisor will bring an umbrella either. She can forget it at home or she could bring it on a day when it turns out it doesn’t rain after all. That’s where probabilities come in.

Let’s now look at the probabilities. We’re interested in the probability of: 
* Rain on a given day or time {% katex inline %}t{% endkatex %}, 
* Rain on the next time {% katex inline %}t+1{% endkatex %}, and 
* Rain given that it rained the previous day. 

On the table below we can see how the second row tells us that the probability that it doesn’t rain ({% katex inline %}-r{% endkatex %}) tomorrow given that it rained ({% katex inline %}+r{% endkatex %}) today is 0.3, or 30%.

<table>
<tr>
<th>{% katex inline %}R_t{% endkatex %}</th>
<th>{% katex inline %}R_{t+1}{% endkatex %}</th>
<th>{% katex inline %}P(R_{t+1} | R_t){% endkatex %}</th></tr>
<tr>
<td>{% katex inline %}+r{% endkatex %}</td>
<td>{% katex inline %}+r{% endkatex %}</td>
<td>{% katex inline %}0.7{% endkatex %}</td>
</tr>
<tr>
<td>{% katex inline %}+r{% endkatex %}</td>
<td>{% katex inline %}-r{% endkatex %}</td>
<td>{% katex inline %}0.3{% endkatex %}</td>
</tr>
<tr>
<td>{% katex inline %}-r{% endkatex %}</td>
<td>{% katex inline %}+r{% endkatex %}</td>
<td>{% katex inline %}0.3{% endkatex %}</td>
</tr>
<tr>
<td>{% katex inline %}-r{% endkatex %}</td>
<td>{% katex inline %}-r{% endkatex %}</td>
<td>{% katex inline %}0.7{% endkatex %}</td>
</tr>
</table>
###### Figure 2. Probability distribution for Rain given the previous day’s conditions

Similarly, we can have the following probability distribution for the advisor bringing an umbrella given the weather conditions on day {% katex inline %}t{% endkatex %}. For example, there’s a 90% probability of seeing the advisor bring an umbrella when it rained and a 10% probability of her not bringing an umbrella on a rainy day.

<table>
<tr>
<th>{% katex inline %}R_t{% endkatex %}</th>
<th>{% katex inline %}U_t{% endkatex %}</th>
<th>{% katex inline %}P(U_t | R_t){% endkatex %}</th>
</tr>
<tr>
<td>{% katex inline %}+r{% endkatex %}</td>
<td>{% katex inline %}+u{% endkatex %}</td>
<td>{% katex inline %}0.9{% endkatex %}</td>
</tr>
<tr>
<td>{% katex inline %}+r{% endkatex %}</td>
<td>{% katex inline %}-u{% endkatex %}</td>
<td>{% katex inline %}0.1{% endkatex %}</td>
</tr>
<tr>
<td>{% katex inline %}-r{% endkatex %}</td>
<td>{% katex inline %}+u{% endkatex %}</td>
<td>{% katex inline %}0.2{% endkatex %}</td>
</tr>
<tr>
<td>{% katex inline %}-r{% endkatex %}</td>
<td>{% katex inline %}-u{% endkatex %}</td>
<td>{% katex inline %}0.8{% endkatex %}</td>
</tr>
</table>
###### Figure 3. Probability distribution for Umbrella given Rain

Notice how we don’t really know whether it rains or not but we can make intelligent inferences based on the available evidence, namely the presence or absence of an umbrella. Now, From the arrows in figure 1 we can see that variables are not directly affected by all the other variables, only by the ones at the other end of an incoming arrow. This is important because we’ll think of the **probability of an event as something that happens given an event on which it depends**. For example, the probability that there’s an umbrella on day 2 given that it rained on day 2, or the probability that it rained on day 2 given that it did not rain on day 1.

As each day goes by and we see if there is an umbrella that day, we alternate between two events: **incorporating new evidence** into our knowledge and accounting for the **passage of time**.

Let’s say we observed an umbrella on day 1 and on day 2. What’s the probability that it rained on day 2? Before you keep on reading try to think about what information you would need to make that calculation.

Let’s settle on a nomenclature to use. We’ll call the **rain nodes** in Figure 1 our state variables {% katex inline %}X{% endkatex %} and the **umbrella nodes** our evidence variables {% katex inline %}e{% endkatex %}. They will all be indexed by day or time frame {% katex inline %}t{% endkatex %}. Also, we’ll refer to our belief about variable {% katex inline %}X{% endkatex %} at time {% katex inline %}t{% endkatex %} as {% katex inline %}B(X_t){% endkatex %} **after** seeing that day’s evidence. Our belief **before** seeing the day’s evidence will be {% katex inline %}B'(X_t){% endkatex %}. The day following {% katex inline %}t{% endkatex %} will of course be {% katex inline %}t+1{% endkatex %}. To wrap things up let’s clarify what we mean by a belief: the probability of an event given the available evidence.

This way our belief about variable {% katex inline %}X{% endkatex %} at time {% katex inline %}t+1{% endkatex %} is the probability of {% katex inline %}X{% endkatex %} at time {% katex inline %}t+1{% endkatex %} given evidence {% katex inline %}1{% endkatex %} through {% katex inline %}t{% endkatex %}. This is expressed like this:

{% katex %}B'(X_{t+1})=P(X_{t+1} | e_{1:t}){% endkatex %}
###### Belief before seeing the evidence

Feel free to zoom in or out on your browser (*command +, command -*) to see the formulas comfortably.

> :bulb: We'll be using [Bayes’ Theorem](https://en.wikipedia.org/wiki/Bayes%27_theorem) for calculating the probability of an event given that another event happened:
> 
> 1) {% katex inline %}P(A|B)={P(B|A)P(A) \over P(B)}{% endkatex %}

> :information_source: Where did that come from? It can be derived from the conditional probability that tells us in how many cases do both events {% katex inline %}A{% endkatex %} and {% katex inline %}B{% endkatex %} happen out of the cases where {% katex inline %}B{% endkatex %} happens:
>
> 2) {% katex inline %}P(A|B)={P(A \cap B) \over P(B)}{% endkatex %}
>
> Similarly, {% katex inline %}P(B|A)={P(A \cap B) \over P(A)}{% endkatex %}
>
> which means
>
> 3) {% katex inline %}P(A \cap B)=P(B|A)P(A){% endkatex %}
>
> and substituting in the expression for conditional probability yields Bayes' Theorem:
> {% katex %}P(A|B)={P(B|A)P(A) \over P(B)}{% endkatex %}

All the operations going forward are based on Bayes’ Theorem to derive the probability of an event given another and go down the path that leads us to the variables we’re interested in. Figuring out this path takes some intuition but we can also just try all the ways to play with it until we get to where we want to go. 

**After the evidence** for time frame {% katex inline %}t+1{% endkatex %} **comes in**, we want the **probability of {% katex inline %}X{% endkatex %} including the new evidence**, that is, **the evidence at {% katex inline %}t+1{% endkatex %} given all the previous evidence**. 

So from Bayes' Theorem (2) now we have:

{% katex %}P(X_{t+1}|(e_{t+1}|e_{1:t}))={P(X_{t+1},(e_{t+1}|e_{1:t})) \over P(e_{t+1}|e_{1:t})}{% endkatex %}

###### Probability of the variable after the evidence comes in

At this point we can see that nothing on the denominator depends on the {% katex inline %}X{% endkatex %} variable so we can get rid of it with the understanding that this will no longer be an equality but instead the term on the left will be ***proportional*** to the term on the right and that’s what the new {% katex inline %}\propto{% endkatex %} symbol means:

{% katex %}P(X_{t+1}|(e_{t+1}|e_{1:t})) \propto P(X_{t+1},(e_{t+1}|e_{1:t})){% endkatex %}

{% katex %}P(X_{t+1}|(e_{t+1}|e_{1:t})) \propto P(X_{t+1},e_{t+1}|e_{1:t}){% endkatex %}

And by Bayes' Theorem (3):

{% katex %}P(X_{t+1}|(e_{t+1}|e_{1:t})) \propto P(((e_{t+1}|X_{t+1}),X_{t+1})|e_{1:t}){% endkatex %}

{% katex %}P(X_{t+1}|(e_{t+1}|e_{1:t})) \propto P(e_{t+1}|X_{t+1},e_{1:t})P(X_{t+1}|e_{1:t}){% endkatex %}

Now take a look at *Figure 1* again. All the previous evidence is independent of the new evidence given the state variable. That is, the old evidence doesn’t affect the new one except indirectly through the {% katex inline %}X{% endkatex %} variable at {% katex inline %}t+1{% endkatex %}. In other words:

{% katex %}e_{1:t} &#10987; e_{t+1}|X_{t+1}{% endkatex %}

###### Conditional independence. Note that the independence symbol ⫫ has higher precedence than the conditional symbol |

That means we can eliminate the old evidence from the expression after the conditional bar. That leaves us with our final formula for calculating the probability that it’s raining today given all the evidence now available:

{% katex %}P(X_{t+1}|e_{1:t+1}) \propto P(e_{t+1}|X_{t+1})P(X_{t+1}|e_{1:t}){% endkatex %}

Finally we’re ready to calculate the probability distribution over the **Rain variable at day 2** when all we have is:
- The observation that **the advisor brought an umbrella on days 1 and 2**.
- The probability distribution table for Rain given Rain the day before.
- The probability distribution table for Umbrella given Rain.

Since at {% katex inline %}t=0{% endkatex %} we have no evidence yet, let’s assume that there’s a 50% chance of rain on the first day. From then on, we can use our formula to derive the probabilities as we gather evidence every day.

![New evidence](https://raw.githubusercontent.com/santisbon/reference/main/assets/img/rain2.webp)

![Pasage of time](https://raw.githubusercontent.com/santisbon/reference/main/assets/img/rain3.webp)

So, rounding to three decimal places we find that if we saw the advisor bring an umbrella on days 1 and 2, the probability that it’s raining on day 2 is {% katex inline %}0.883{% endkatex %} or {% katex inline %}88.3\%{% endkatex %}. That implies a probability of {% katex inline %}11.7\%{% endkatex %} that it’s **not** raining on day 2.

![Prediction](https://raw.githubusercontent.com/santisbon/reference/main/assets/img/rain4.webp)

That’s it! This wraps up our introduction to applications of Artificial Intelligence. Next, we’ll use a slightly more complex model to use probabilistic inference to recognize American Sign Language given video frame data.