{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Simulation\n", "\n", "Functions\n", "\n", "`np.random.standard_normal`, `np.random.standard_t`, `np.random.RandomState`\n", "\n", "### Exercise 5\n", "\n", "Simulate 100 standard Normal random variables\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:26.957782Z", "iopub.status.busy": "2023-09-28T12:33:26.957782Z", "iopub.status.idle": "2023-09-28T12:33:27.127917Z", "shell.execute_reply": "2023-09-28T12:33:27.127392Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.74853182 0.00570743 -1.43625788 0.83781064 0.04405833 0.46523284\n", " 0.10429603 -0.58350807 1.23537454 1.2828819 ]\n" ] } ], "source": [ "import numpy as np\n", "\n", "x = np.random.standard_normal(100)\n", "print(x[:10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 6\n", "\n", "Simulate 100 random variables from a $N\\left(.08,.2^{2}\\right)$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.127917Z", "iopub.status.busy": "2023-09-28T12:33:27.127917Z", "iopub.status.idle": "2023-09-28T12:33:27.135532Z", "shell.execute_reply": "2023-09-28T12:33:27.135532Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.35717346 -0.08418996 -0.29114298 -0.01254201 -0.15909042 -0.14751249\n", " -0.4147758 0.13698652 -0.05896361 -0.22702702]\n" ] } ], "source": [ "x = np.random.normal(0.08, 0.20, 100)\n", "print(x[:10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 7\n", "\n", "Simulate 100 random variables from a Students t with 8 degrees of freedom\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.137046Z", "iopub.status.busy": "2023-09-28T12:33:27.137046Z", "iopub.status.idle": "2023-09-28T12:33:27.143130Z", "shell.execute_reply": "2023-09-28T12:33:27.143130Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1.7635763 4.91532809 -0.25849491 0.3148048 0.62468202 -0.25901158\n", " -1.65966825 1.80346246 2.00472374 -0.35256962]\n" ] } ], "source": [ "x = np.random.standard_t(8, size=100)\n", "print(x[:10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 8\n", "\n", "Simulate 100 random variables from a Students t with 8 degrees of freedom with a mean of 8% and a volatility of 20%. Note: $V\\left[X\\right]=\\frac{\\nu}{\\nu-2}$ when $X\\sim t_{\\nu}$." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.143130Z", "iopub.status.busy": "2023-09-28T12:33:27.143130Z", "iopub.status.idle": "2023-09-28T12:33:27.151159Z", "shell.execute_reply": "2023-09-28T12:33:27.151159Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.05936668 -0.05321954 0.20630707 0.14163083 -0.01681002 -0.54887556\n", " 0.18339363 -0.27534791 0.07396293 0.00630069]\n" ] } ], "source": [ "e = np.random.standard_t(8, size=100)\n", "e = e / np.sqrt(8 / (8 - 2))\n", "x = 0.08 + 0.2 * e\n", "print(x[:10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Exercise 9\n", "\n", "Simulate two identical sets of 100 standard normal random variables by resetting the random number generator." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.153170Z", "iopub.status.busy": "2023-09-28T12:33:27.153170Z", "iopub.status.idle": "2023-09-28T12:33:27.159989Z", "shell.execute_reply": "2023-09-28T12:33:27.159989Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0.]\n" ] } ], "source": [ "# Legacy method for any NumPy\n", "rs = np.random.RandomState(30092019)\n", "# Modern method, NumPy >= 1.17\n", "rs = np.random.default_rng(30092019)\n", "\n", "x1 = rs.standard_normal(100)\n", "\n", "# rs2 = np.random.RandomState(30092019)\n", "rs2 = np.random.default_rng(30092019)\n", "\n", "x2 = rs2.standard_normal(100)\n", "print(x1 - x2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.159989Z", "iopub.status.busy": "2023-09-28T12:33:27.159989Z", "iopub.status.idle": "2023-09-28T12:33:27.168327Z", "shell.execute_reply": "2023-09-28T12:33:27.168327Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0.]\n" ] } ], "source": [ "# When using the legacy RandomState\n", "rs = np.random.RandomState(30092019)\n", "state = rs.get_state()\n", "x1 = rs.standard_normal(100)\n", "rs.set_state(state)\n", "x2 = rs.standard_normal(100)\n", "print(x1 - x2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2023-09-28T12:33:27.168327Z", "iopub.status.busy": "2023-09-28T12:33:27.168327Z", "iopub.status.idle": "2023-09-28T12:33:27.176217Z", "shell.execute_reply": "2023-09-28T12:33:27.176217Z" }, "jupyter": { "outputs_hidden": false }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0.]\n" ] } ], "source": [ "# If using the modern way\n", "rs = np.random.default_rng(30092019)\n", "state = rs.bit_generator.state\n", "x1 = rs.standard_normal(100)\n", "rs.bit_generator.state = state\n", "x2 = rs.standard_normal(100)\n", "print(x1 - x2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Exercise 10\n", "\n", "Repeat exercise 7 using only `standard_normal`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.176217Z", "iopub.status.busy": "2023-09-28T12:33:27.176217Z", "iopub.status.idle": "2023-09-28T12:33:27.184669Z", "shell.execute_reply": "2023-09-28T12:33:27.184669Z" }, "pycharm": { "is_executing": false, "name": "#%%" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.00851377 -0.12160312 0.25513281 0.00612633 -0.01818945 0.12300775\n", " -0.00469669 0.0382072 0.0040533 -0.02505603]\n" ] } ], "source": [ "# Modern way to get a reproducible random generator\n", "rs = np.random.default_rng(30092019)\n", "\n", "df = 8\n", "\n", "num = rs.standard_normal(100)\n", "denom = rs.standard_normal((100, df))\n", "denom = np.sum(denom**2, 1)\n", "denom = denom / df\n", "\n", "e = num / np.sqrt(denom)\n", "\n", "e = e / np.sqrt(df / (df - 2))\n", "x = 0.08 + 0.2 * e\n", "\n", "print(x[:10])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2023-09-28T12:33:27.184669Z", "iopub.status.busy": "2023-09-28T12:33:27.184669Z", "iopub.status.idle": "2023-09-28T12:33:28.207143Z", "shell.execute_reply": "2023-09-28T12:33:28.207143Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kurtosis: 4.505665233546498\n", "Theoretical Value: 4.5\n" ] } ], "source": [ "from scipy import stats\n", "\n", "reps = 1000000\n", "num = rs.standard_normal(reps)\n", "denom = rs.standard_normal((reps, df))\n", "denom = np.sum(denom**2, 1)\n", "denom = denom / df\n", "\n", "e = num / np.sqrt(denom)\n", "print(f\"Kurtosis: {3 + stats.kurtosis(e)}\")\n", "print(f\"Theoretical Value: {3 + 6 / (df - 4)}\")" ] } ], "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" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 4 }