

# Understanding probability theory



Let's consider a simple example to illustrate the concept of probability. Suppose you have a fair six-sided die, and you want to know the probability of rolling a specific number, say 4. You can calculate this probability using the formula:

$$P(A) = \frac{\text{number of favorable outcomes}}{\text{total number of outcomes}}$$

In this case, there is only one favorable outcome (rolling a 4), and the total number of outcomes is 6. So, the probability of rolling a 4 is:

$$P(A) = \frac{1}{6}$$

## Exercise
Calculate the probability of rolling a 2 on a fair six-sided die.

### Solution
$$P(A) = \frac{1}{6}$$

In this example, the event A is the event of rolling a 2. The probability of this event is 1/6, which means that there is a 1 in 6 chance of rolling a 2.

# Getting started with Python and Matplotlib



Here's a simple Python script that prints "Hello, World!":

```python
print("Hello, World!")
```

To install Matplotlib, you can use the following command:

```bash
pip install matplotlib
```

Once installed, you can use Matplotlib to create plots in your Python scripts. For example, here's a script that creates a simple line plot:

```python
import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4]
y = [0, 2, 4, 6, 8]

plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Line plot example')
plt.show()
```

## Exercise
Create a script that generates a bar plot of the following data:

Fruits | Count

# Visualizing conditional probability using Matplotlib



Here's a script that creates a histogram of a random sample from a normal distribution:

```python
import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(0, 1, 1000)

plt.hist(data, bins=20, density=True)
plt.xlabel('Value')
plt.ylabel('Probability')
plt.title('Histogram example')
plt.show()
```

## Exercise
Create a script that generates a scatter plot of the following data:

X | Y

# Creating a simulation for conditional probability



Here's a script that creates a Monte Carlo simulation to estimate the value of pi:

```python
import numpy as np
import matplotlib.pyplot as plt

num_samples = 1000000

x = np.random.uniform(-1, 1, num_samples)
y = np.random.uniform(-1, 1, num_samples)

points_in_circle = np.sum(np.sqrt(x**2 + y**2) <= 1)

pi_estimate = 4 * points_in_circle / num_samples

print("Estimated value of pi:", pi_estimate)
```

## Exercise
Create a script that generates a Markov chain with the following transition matrix:

$$
\begin{bmatrix}
0.8 & 0.1 & 0.1 \\
0.3 & 0.6 & 0.1 \\
0.2 & 0.3 & 0.5
\end{bmatrix}
$$

### Solution
```python
import numpy as np

transition_matrix = np.array([[0.8, 0.1, 0.1],
                              [0.3, 0.6, 0.1],
                              [0.2, 0.3, 0.5]])

num_steps = 1000
current_state = np.random.choice([0, 1, 2])

states = [current_state]

for _ in range(num_steps):
    current_state = np.random.choice(3, p=transition_matrix[current_state])
    states.append(current_state)

print("Generated Markov chain:", states)
```

# Advanced visualization techniques



Here's a script that creates a 3D surface plot of a function:

```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, cmap='viridis')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('3D surface plot example')
plt.show()
```

## Exercise
Create a script that generates an animation of a sine wave using Matplotlib.

### Solution
```python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
line, = ax.plot(x, y)

def update(i):
    y = np.sin(x + i / 10.0)
    line.set_ydata(y)
    return line,

ani = animation.FuncAnimation(fig, update, frames=100, interval=50, blit=True)

plt.show()
```

# Applications of conditional probability in real-world scenarios



Here's a script that simulates a simple weather forecasting system using conditional probability:

```python
import numpy as np

# Probability of rain
P_rain = 0.6

# Probability of sunny given rain
P_sunny_given_rain = 0.1

# Probability of sunny given no rain
P_sunny_given_no_rain = 0.7

# Generate a random number between 0 and 1
random_number = np.random.uniform(0, 1)

# Determine if it will rain
if random_number < P_rain:
    # Determine if it will be sunny
    if np.random.uniform(0, 1) < P_sunny_given_rain:
        print("It will be sunny with rain.")
    else:
        print("It will rain.")
else:
    # Determine if it will be sunny
    if np.random.uniform(0, 1) < P_sunny_given_no_rain:
        print("It will be sunny without rain.")
    else:
        print("It will be overcast.")
```

## Exercise
Create a script that predicts the stock market using conditional probability.

### Solution
```python
import numpy as np

# Historical stock prices
stock_prices = [100, 105, 110, 115, 120]

# Probability of stock price increase
P_increase = 0.7

# Probability of stock price decrease
P_decrease = 0.3

# Generate a random number between 0 and 1
random_number = np.random.uniform(0, 1)

# Determine if the stock price will increase
if random_number < P_increase:
    print("The stock price will increase.")
else:
    print("The stock price will decrease.")
```

# Challenges and limitations of visualization and simulation



Here's a script that creates a scatter plot of the historical stock prices and the predicted stock prices:

```python
import numpy as np
import matplotlib.pyplot as plt

# Historical stock prices
stock_prices = [100, 105, 110, 115, 120]

# Predicted stock prices
predicted_prices = [105, 115, 125, 130, 135]

plt.scatter(stock_prices, stock_prices, label='Historical')
plt.scatter(stock_prices, predicted_prices, label='Predicted')
plt.xlabel('Historical Stock Prices')
plt.ylabel('Predicted Stock Prices')
plt.title('Stock Price Prediction')
plt.legend()
plt.show()
```

## Exercise
Discuss the challenges and limitations of the stock price prediction example.

### Solution
The stock price prediction example has several challenges and limitations:

- The prediction is based on historical data, which may not accurately represent future stock prices.
- The accuracy of the prediction depends on the quality of the historical data and the choice of prediction model.
- The example uses a simple linear model, which may not be suitable for predicting stock prices, which can be highly volatile and influenced by various factors.
- The example does not consider external factors such as economic indicators, market sentiment, or company-specific events.

# Conclusion and next steps

In this textbook, we have covered the fundamentals of probability theory, the basics of Python and Matplotlib, visualization and simulation techniques for conditional probability, and applications of conditional probability in real-world scenarios.

In the future, we can explore more advanced topics such as Bayesian inference, machine learning algorithms for probability estimation, and the use of big data and parallel computing for large-scale simulations. We can also delve deeper into specific applications of conditional probability, such as natural language processing, computer vision, and reinforcement learning.

By building on the knowledge gained in this textbook, you will be well-equipped to tackle complex problems in the fields of data analysis, machine learning, and artificial intelligence.