{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Weekly exercise 6: Simple NumPy exercises\n", "\n", "In this task you are practicing using NumPy in several applications." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1. Power of a matrix\n", "\n", "Write a function that would take in a matrix and a non-negative integer,\n", "check for that matrix is square, and return the power of the matrix computed through\n", "successive multiplication." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy as np\n", "\n", "def matrix_power(mtrx,n=1):\n", " # Write your code here\n", " pass\n", "\n", "A = np.array([[1,2,0],[0,2,3],[1,1,5]])\n", "print(matrix_power(A))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the tests to pass for your code (which are referred to as *unit tests*)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# Test the code above\n", "\n", "A = np.array([[1,2,0],[0,2,3],[1,1,5]])\n", "B = [[355,614,1806],[903,1565,4533],[1210,2113,6098]]\n", "eq=np.equal(matrix_power(A,5),B)\n", "if eq.all():\n", " print('Test 1 passed')\n", "else:\n", " print('Test 1 FAIL')\n", "\n", "\n", "A = [[1,2,0],[0,2,3],[1,1,5]]\n", "B = [[355,614,1806],[903,1565,4533],[1210,2113,6098]]\n", "eq=np.equal(matrix_power(A,5),B)\n", "if eq.all():\n", " print('Test 2 passed')\n", "else:\n", " print('Test 2 FAIL')\n", "\n", "\n", "try:\n", " matrix_power([1],4.5)\n", "except TypeError:\n", " print('Test 3 passed')\n", "except:\n", " print('Test 3 FAIL')\n", "else:\n", " print('Test 3 FAIL')\n", "\n", "try:\n", " matrix_power([1],-5)\n", "except ValueError:\n", " print('Test 4 passed')\n", "except:\n", " print('Test 4 FAIL')\n", "else:\n", " print('Test 4 FAIL')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2. Autoregressive model in matrix form\n", "\n", "Consider the AR(1) model\n", "\n", "$$\n", "y_t = a y_{t-1} + \\varepsilon, \\; \\varepsilon \\sim N(0, 1).\n", "$$\n", "\n", "We can represent it in the form\n", "\n", "$$\n", "Ay = \\varepsilon \\quad \\quad \\varepsilon \\sim N(0, 1)\n", "$$\n", "\n", "where $ A $ is\n", "\n", "$$\n", "A = \\begin{bmatrix} 1 & 0 & \\cdots & 0 & 0 \\cr\n", " -a & 1 & \\cdots & 0 & 0 \\cr\n", " \\vdots & \\vdots & \\cdots & \\vdots & \\vdots \\cr\n", " \\vdots & \\vdots & \\cdots & 1 & 0 \\cr\n", " 0 & 0 & \\cdots & -a & 1 \\end{bmatrix}\n", "$$\n", "\n", "and $ y $ and $ \\varepsilon $ are $ (T x 1) $ vectors\n", "\n", "Generate an AR(1) series with $ T=500 $ and $ \\alpha = 0.9 $\n", "using matrix algebra, and make a plot of $ y_t $.\n", "\n", "Hint: use NumPy.eye() with additional arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# replace @@@ by your code\n", "import numpy as np\n", "\n", "T = @@@\n", "α = @@@\n", "ɛ = np.random.randn(T)\n", "A = @@@\n", "y = @@@\n", "\n", "import matplotlib.pyplot as plt\n", "@@@" ] } ], "metadata": { "date": 1632755196.273495, "filename": "exercise06.rst", "kernelspec": { "display_name": "Python", "language": "python3", "name": "python3" }, "title": "Weekly exercise 6: Simple NumPy exercises" }, "nbformat": 4, "nbformat_minor": 4 }