{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Global constant\n", "YEARS_END = 26" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The switch probability matrix `P`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "P = np.array([[0.70, 0.15, 0.15],\n", " [0.20, 0.80, 0.15],\n", " [0.10, 0.05, 0.70]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The proportion `X` of customers currently in each store" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "X = np.array([[0.15], \n", " [0.20], \n", " [0.65]])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initial:\n", "[[0.15]\n", " [0.2 ]\n", " [0.65]]\n" ] } ], "source": [ "%precision 4\n", "\n", "print('Initial:')\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initially ...\n", "\n", "Customers in Store A: 15.00%\n", "Customers in Store B: 20.00%\n", "Customers in Store C: 65.00%\n" ] } ], "source": [ "print('Initially ...')\n", "print()\n", "print(f'Customers in Store A: {X[0][0]:.2%}')\n", "print(f'Customers in Store B: {X[1][0]:.2%}')\n", "print(f'Customers in Store C: {X[2][0]:.2%}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate the Markov chain" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "After year 1:\n", "[[0.2325]\n", " [0.2875]\n", " [0.48 ]]\n", "\n", "After year 2:\n", "[[0.2779]\n", " [0.3485]\n", " [0.3736]]\n", "\n", "After year 3:\n", "[[0.3028]\n", " [0.3904]\n", " [0.3067]]\n", "\n", "After year 4:\n", "[[0.3166]\n", " [0.4189]\n", " [0.2645]]\n", "\n", "After year 5:\n", "[[0.3241]\n", " [0.4381]\n", " [0.2378]]\n", "\n", "After year 6:\n", "[[0.3283]\n", " [0.451 ]\n", " [0.2208]]\n", "\n", "After year 7:\n", "[[0.3305]\n", " [0.4596]\n", " [0.2099]]\n", "\n", "After year 8:\n", "[[0.3318]\n", " [0.4652]\n", " [0.203 ]]\n", "\n", "After year 9:\n", "[[0.3325]\n", " [0.469 ]\n", " [0.1985]]\n", "\n", "After year 10:\n", "[[0.3329]\n", " [0.4715]\n", " [0.1957]]\n", "\n", "After year 11:\n", "[[0.3331]\n", " [0.4731]\n", " [0.1938]]\n", "\n", "After year 12:\n", "[[0.3332]\n", " [0.4742]\n", " [0.1926]]\n", "\n", "After year 13:\n", "[[0.3333]\n", " [0.4749]\n", " [0.1919]]\n", "\n", "After year 14:\n", "[[0.3333]\n", " [0.4753]\n", " [0.1914]]\n", "\n", "After year 15:\n", "[[0.3333]\n", " [0.4756]\n", " [0.1911]]\n", "\n", "After year 16:\n", "[[0.3333]\n", " [0.4758]\n", " [0.1909]]\n", "\n", "After year 17:\n", "[[0.3333]\n", " [0.476 ]\n", " [0.1907]]\n", "\n", "After year 18:\n", "[[0.3333]\n", " [0.476 ]\n", " [0.1906]]\n", "\n", "After year 19:\n", "[[0.3333]\n", " [0.4761]\n", " [0.1906]]\n", "\n", "After year 20:\n", "[[0.3333]\n", " [0.4761]\n", " [0.1905]]\n", "\n", "After year 21:\n", "[[0.3333]\n", " [0.4761]\n", " [0.1905]]\n", "\n", "After year 22:\n", "[[0.3333]\n", " [0.4762]\n", " [0.1905]]\n", "\n", "After year 23:\n", "[[0.3333]\n", " [0.4762]\n", " [0.1905]]\n", "\n", "After year 24:\n", "[[0.3333]\n", " [0.4762]\n", " [0.1905]]\n", "\n", "After year 25:\n", "[[0.3333]\n", " [0.4762]\n", " [0.1905]]\n" ] } ], "source": [ "for year in range(1, YEARS_END):\n", " X = P@X\n", " print()\n", " print(f'After year {year}:')\n", " print(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## At the end ..." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After 25 years ...\n", "\n", "Customers in Store A: 33.33%\n", "Customers in Store B: 47.62%\n", "Customers in Store C: 19.05%\n" ] } ], "source": [ "print(f'After {YEARS_END - 1} years ...')\n", "print()\n", "print(f'Customers in Store A: {X[0][0]:.2%}')\n", "print(f'Customers in Store B: {X[1][0]:.2%}')\n", "print(f'Customers in Store C: {X[2][0]:.2%}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## After about 19 years, the percentages of customers in the stores reached a steady state (to 4 decimal places)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }