Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
RamamAgarwal authored Jul 23, 2024
1 parent b147ab5 commit 299f3b2
Showing 1 changed file with 197 additions and 0 deletions.
197 changes: 197 additions & 0 deletions Most Likely Weather Sequence.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "20424303",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def viterbi_algorithm(S, Pi, E, P, Y):\n",
" K = len(S) # Length of state space will give K\n",
" T = len(Y) # Length of observation sequence will give T\n",
" \n",
" # Initialize matrices for Viterbi probabilities and paths\n",
" viterbi_prob = np.zeros((K, T))\n",
" viterbi_path = np.zeros((K, T))\n",
" \n",
" # Part I: Initialization\n",
" for i in range(K):\n",
" viterbi_prob[i][0] = Pi[i] * E[i][Y[0]]\n",
" viterbi_path[i][0] = 0\n",
" \n",
" # Part II: Compute Viterbi probabilities and path\n",
" for j in range(1, T):\n",
" for i in range(K):\n",
" viterbi_prob[i, j] = max(E[i][Y[j]] * P[k][i] * viterbi_prob[k, j-1] for k in range(K))\n",
" viterbi_path[i, j] = np.argmax([E[i][Y[j]] * P[k][i] * viterbi_prob[k, j-1] for k in range(K)])\n",
" \n",
" # Part III: Re-track the most likely path\n",
" x = []\n",
" x_t = np.argmax(viterbi_prob[:, T-1])\n",
" x.append(x_t)\n",
" for j in range(T-1, 0, -1):\n",
" x_prev = int(viterbi_path[int(x_t), j])\n",
" x.append(x_prev)\n",
" x_t = x_prev\n",
" \n",
" # Reverse path to get the correct order\n",
" x=x[::-1]\n",
" print(viterbi_prob)\n",
" print(viterbi_path)\n",
" \n",
" return x, np.max(viterbi_prob[:, T-1])\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4526f268",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"def forward_algorithm(S, Pi, E, P, Y, Pii=0):\n",
" K = len(S)\n",
" T = len(Y)\n",
" \n",
" forward_prob = np.zeros((K, T))\n",
" \n",
" # Part I: Initialization\n",
" for i in range(K):\n",
" forward_prob[i][0] = Pi[i] * E[i][Y[0]]\n",
" \n",
" # Part II: Recursion\n",
" for j in range(1, T):\n",
" for i in range(K):\n",
" forward_prob[i, j] = sum(forward_prob[k, j-1] * P[k][i] * E[i][Y[j]] for k in range(K))\n",
" \n",
" # Part III: Termination\n",
" total_prob = np.sum(forward_prob[:, T-1])\n",
" return total_prob\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6a25233e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.800000e-01 1.120000e-02 9.600000e-04 2.400000e-04 4.800000e-04\n",
" 1.536000e-04 4.915200e-05 1.966080e-06 1.179648e-06 4.718592e-08]\n",
" [5.000000e-02 4.800000e-02 1.200000e-02 3.000000e-03 3.000000e-04\n",
" 4.320000e-05 9.216000e-06 7.372800e-06 7.372800e-07 2.654208e-07]\n",
" [1.600000e-01 2.240000e-02 2.880000e-03 7.200000e-04 3.600000e-04\n",
" 7.680000e-05 2.457600e-05 3.932160e-06 8.847360e-07 9.437184e-08]]\n",
"[[0. 0. 1. 1. 1. 0. 0. 0. 1. 0.]\n",
" [0. 2. 1. 1. 1. 2. 2. 2. 1. 2.]\n",
" [0. 0. 1. 1. 1. 0. 0. 0. 1. 0.]]\n",
"Most likely weather sequence: ['sunny', 'cloudy', 'cloudy', 'cloudy', 'rainy', 'rainy', 'sunny', 'cloudy', 'sunny', 'cloudy']\n"
]
}
],
"source": [
"# Given data\n",
"S = ['rainy', 'cloudy', 'sunny']\n",
"Pi = [0.35, 0.25, 0.4]\n",
"P = [\n",
" [0.4, 0.2, 0.4],\n",
" [0.2, 0.5, 0.3],\n",
" [0.3, 0.6, 0.1]\n",
"]\n",
"E = [\n",
" [0.8, 0.1, 0.1],\n",
" [0.2, 0.5, 0.3],\n",
" [0.4, 0.2, 0.4]\n",
"]\n",
"#activities = ['read', 'read', 'shop', 'play', 'shop']\n",
"activities = ['read', 'shop', 'play', 'play', 'read', 'play', 'shop', 'shop', 'shop']\n",
"#observation_indices = [0, 0, 2, 1, 2] # Convert activities to observation indices(read->0,play->1,shop->2)\n",
" \n",
"# (a) Find the most likely weather sequence\n",
"most_likely_weather_sequence, _ = viterbi_algorithm(S, Pi, E, P, observation_indices)\n",
"print(\"Most likely weather sequence:\", [S[state] for state in most_likely_weather_sequence])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "50b47d13",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.00000000e-02 2.36250000e-01 7.44187500e-02 2.34419062e-02\n",
" 8.20466719e-04 2.81302875e-04 1.32915608e-04 2.29678171e-04\n",
" 8.03873600e-06 2.17045872e-05]\n",
" [1.80000000e-01 2.30400000e-02 4.25250000e-02 1.33953750e-02\n",
" 6.32931469e-03 1.21522842e-03 2.33323857e-04 2.98654536e-05\n",
" 6.20131063e-05 7.93767760e-06]\n",
" [3.50000000e-01 2.16000000e-02 1.41750000e-02 5.10300000e-03\n",
" 3.75070500e-03 1.77220811e-03 3.40263958e-04 2.79988628e-05\n",
" 3.21549440e-05 7.44157275e-06]]\n",
"[[0. 2. 0. 0. 0. 2. 2. 2. 0. 2.]\n",
" [0. 1. 0. 0. 0. 1. 1. 1. 0. 1.]\n",
" [0. 1. 0. 1. 1. 1. 1. 1. 0. 1.]]\n",
"Most likely sequence of aircraft control laws: ['direct', 'normal', 'normal', 'normal', 'alternate', 'alternate', 'direct', 'normal', 'direct', 'normal']\n",
"0.0011761669647830317\n"
]
}
],
"source": [
"# Given data\n",
"S = ['normal', 'alternate', 'direct']\n",
"Pi = [0.2, 0.3, 0.5]\n",
"P = [\n",
" [0.35, 0.45, 0.2],\n",
" [0.28, 0.32, 0.4],\n",
" [0.75, 0.1, 0.15]\n",
"]\n",
"E = [\n",
" [0.1, 0.9],\n",
" [0.6, 0.4],\n",
" [0.7, 0.3]\n",
"]\n",
"pitch_data = ['up', 'down', 'down', 'down', 'up', 'up', 'up', 'down', 'up', 'down']\n",
"#pitch_data = ['up', 'down']\n",
"\n",
"# Convert pitch data to observation indices\n",
"observation_indices = [0 if pitch == 'up' else 1 for pitch in pitch_data]\n",
"\n",
"# Implement Viterbi algorithm\n",
"most_likely_control_sequence, _ = viterbi_algorithm(S, Pi, E, P, observation_indices)\n",
"print(\"Most likely sequence of aircraft control laws:\", [S[state] for state in most_likely_control_sequence])\n",
"print(forward_algorithm(S,Pi,E,P,observation_indices,Pii=0))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 299f3b2

Please sign in to comment.