{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Week 4: Predicting the Weather" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The opener for Week 4 discusses an example in which tomorrow's weather is predicted from today's weather. This is done by assigning probabilities to how today's weather transitions to tomorrow's weather." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "The data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Transition" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "The transition matrix, $ P $" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "P = np.matrix( '0.4, 0.3, 0.1; \\\n", " 0.4, 0.3, 0.6; \\\n", " 0.2, 0.4, 0.3' )\n", "\n", "print( P )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Today is cloudy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vector $ x $ consists of three components: \n", "$$ \\left( \\begin{array}{c} \\chi_s \\\\ \\chi_c \\\\ \\chi_r \\end{array} \\right). $$\n", "If we want to use $ x^{(0)} $ to express that on day $ 0 $ it is cloudy, we set\n", "$$ \\left( \\begin{array}{c} \\chi_s^{(0)} \\\\ \\chi_c^{(0)} \\\\ \\chi_r^{(0)} \\end{array} \\right) = \n", "\\left( \\begin{array}{r} 0 \\\\ 1 \\\\ 0 \\end{array} \\right). $$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x0 = np.matrix( '0; \\\n", " 1; \\\n", " 0' )\n", "print( x0 )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Predicting the weather a week from today" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The weather for day $ 1 $ is predicted by $ x^{(1)} = P x^{(0)} $. Notice that if P and x0 are numpy matrices, then P * x0 computes the product of matrix P times vector x0 ." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x1 = P * x0 \n", "print( x1 )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predict the weather for day 2 from day 1." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x2 = P * x1\n", "print( x2 )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predict the weather for day 3 from day 2." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predict the weather for day 4 from day 3." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predict the weather for day 5 from day 4." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predict the weather for day 6 from day 5." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Predict the weather for day 7 from day 6." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What are the probabilities it is sunny, cloudy, rainy on day 7 (given that it was cloudy on day 0)? (Just think about this...)" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Predicting the next 20 days" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is best done with a loop, where $ x $ simply takes on the next value everytime through the loop." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = x0\n", "for i in range( 21 ): # you saw range( n ) in Week 1\n", " print( 'Day', i ) # print the predictions for day i\n", " print( x )\n", " x = P * x # compute the predictions for day i+1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Predicting the weather for a year from now" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify the above loop to compute the prediction for Day 365." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare Day 365 to Day 20. Notice that they are almost the same. Go back and start with x0 = \"today is sunny\". Go back and start with x0 = \"today is rainy\" Notice that no matter what the weather is today, eventually the prediction becomes \"the typical forecast for this location\"." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }