{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# NumPy and Numerical Computing\n",
        "\n**Opening question:** Why are numerical arrays faster and more expressive than repeatedly updating Python lists?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "prices = np.array([100.0, 102.0, 101.0, 104.0])\n",
        "returns = prices[1:] / prices[:-1] - 1\n",
        "print(np.round(returns, 4))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. Each return compares adjacent prices. The output has one fewer observation than the price array.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "rng = np.random.default_rng(41202)\n",
        "asset_returns = rng.normal([0.0003, 0.0002], [0.012, 0.007], size=(5, 2))\n",
        "weights = np.array([0.6, 0.4])\n",
        "portfolio = asset_returns @ weights\n",
        "print(np.round(portfolio, 4))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. Matrix multiplication applies the same portfolio weights to every simulated observation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "prices = np.array([100.0, 102.0, 101.0, 104.0])\n",
        "print(np.round(returns, 4))\n",
        "import numpy as np\n",
        "rng = np.random.default_rng(41202)\n",
        "asset_returns = rng.normal([0.0003, 0.0002], [0.012, 0.007], size=(5, 2))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n[ 0.02 -0.0098 0.0297]\n```\n\n```text\n[-0.0097 0.004 0.0097 -0.0022 -0.002 ]\n```\n\n```text\n[ 0.02   -0.0098  0.0297]\n```\n\n```text\n[-0.0097  0.004   0.0097 -0.0022 -0.002 ]\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "source_derived": true,
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 8,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
