{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "
\n", " \n", " \"QuantEcon\"\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Arrays, Tuples, Ranges, and Other Fundamental Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Arrays, Tuples, Ranges, and Other Fundamental Types](#Arrays,-Tuples,-Ranges,-and-Other-Fundamental-Types) \n", " - [Overview](#Overview) \n", " - [Array Basics](#Array-Basics) \n", " - [Operations on Arrays](#Operations-on-Arrays) \n", " - [Ranges](#Ranges) \n", " - [Tuples and Named Tuples](#Tuples-and-Named-Tuples) \n", " - [Nothing, Missing, and Unions](#Nothing,-Missing,-and-Unions) \n", " - [Exercises](#Exercises) \n", " - [Solutions](#Solutions) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> “Let’s be clear: the work of science has nothing whatever to do with consensus.\n", "> Consensus is the business of politics. Science, on the contrary, requires only\n", "> one investigator who happens to be right, which means that he or she has\n", "> results that are verifiable by reference to the real world. In science\n", "> consensus is irrelevant. What is relevant is reproducible results.” – Michael Crichton" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "In Julia, arrays and tuples are the most important data type for working with numerical data.\n", "\n", "In this lecture we give more details on\n", "\n", "- creating and manipulating Julia arrays \n", "- fundamental array processing operations \n", "- basic matrix algebra \n", "- tuples and named tuples \n", "- ranges \n", "- nothing, missing, and unions " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide-output": true }, "outputs": [], "source": [ "using InstantiateFromURL\n", "# optionally add arguments to force installation: instantiate = true, precompile = true\n", "github_project(\"QuantEcon/quantecon-notebooks-julia\", version = \"0.7.0\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hide-output": false }, "outputs": [], "source": [ "using LinearAlgebra, Statistics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Basics\n", "\n", "([See multi-dimensional arrays documentation](https://docs.julialang.org/en/v1/manual/arrays/))\n", "\n", "Since it is one of the most important types, we will start with arrays.\n", "\n", "Later, we will see how arrays (and all other types in Julia) are handled in a generic and extensible way." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Shape and Dimension\n", "\n", "We’ve already seen some Julia arrays in action" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10, 20, 30]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 2.0\n", " 3.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1.0, 2.0, 3.0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output tells us that the arrays are of types `Array{Int64,1}` and `Array{Float64,1}` respectively.\n", "\n", "Here `Int64` and `Float64` are types for the elements inferred by the compiler.\n", "\n", "We’ll talk more about types later.\n", "\n", "The `1` in `Array{Int64,1}` and `Array{Any,1}` indicates that the array is\n", "one dimensional (i.e., a `Vector`).\n", "\n", "This is the default for many Julia functions that create arrays" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "Array{Float64,1}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(randn(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Julia, one dimensional vectors are best interpreted as column vectors, which we will see when we take transposes.\n", "\n", "We can check the dimensions of `a` using `size()` and `ndims()`\n", "functions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ndims(a)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "size(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The syntax `(3,)` displays a tuple containing one element – the size along the one dimension that exists." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Array vs Vector vs Matrix\n", "\n", "In Julia, `Vector` and `Matrix` are just aliases for one- and two-dimensional arrays\n", "respectively" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Array{Int64, 1} == Vector{Int64}\n", "Array{Int64, 2} == Matrix{Int64}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vector construction with `,` is then interpreted as a column vector.\n", "\n", "To see this, we can create a column vector and row vector more directly" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3] == [1; 2; 3] # both column vectors" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Int64,2}:\n", " 1 2 3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1 2 3] # a row vector is 2-dimensional" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we’ve seen, in Julia we have both\n", "\n", "- one-dimensional arrays (i.e., flat arrays) \n", "- arrays of size `(1, n)` or `(n, 1)` that represent row and column vectors respectively \n", "\n", "\n", "Why do we need both?\n", "\n", "On one hand, dimension matters for matrix algebra.\n", "\n", "- Multiplying by a row vector is different to multiplying by a column vector. \n", "\n", "\n", "On the other, we use arrays in many settings that don’t involve matrix algebra.\n", "\n", "In such cases, we don’t care about the distinction between row and column vectors.\n", "\n", "This is why many Julia functions return flat arrays by default.\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Functions that Create Arrays\n", "\n", "We’ve already seen some functions for creating a vector filled with `0.0`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 0.0\n", " 0.0\n", " 0.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zeros(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This generalizes to matrices and higher dimensional arrays" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.0 0.0\n", " 0.0 0.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zeros(2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To return an array filled with a single value, use `fill`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 5.0 5.0\n", " 5.0 5.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill(5.0, 2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, you can create an empty array using the `Array()` constructor" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 6.90605e-310 6.90604e-310\n", " 6.90604e-310 5.4e-323" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Array{Float64}(undef, 2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The printed values you see here are just garbage values.\n", "\n", "(the existing contents of the allocated memory slots being interpreted as 64 bit floats)\n", "\n", "If you need more control over the types, fill with a non-floating point" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 0 0\n", " 0 0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill(0, 2, 2) # fills with 0, not 0.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or fill with a boolean type" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Bool,2}:\n", " 0 0\n", " 0 0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill(false, 2, 2) # produces a boolean matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating Arrays from Existing Arrays\n", "\n", "For the most part, we will avoid directly specifying the types of arrays, and let the compiler deduce the optimal types on its own.\n", "\n", "The reasons for this, discussed in more detail in [this lecture](../more_julia/generic_programming.html), are to ensure both clarity and generality.\n", "\n", "One place this can be inconvenient is when we need to create an array based on an existing array.\n", "\n", "First, note that assignment in Julia binds a name to a value, but does not make a copy of that type" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 2\n", " 2\n", " 3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2, 3]\n", "y = x\n", "y[1] = 2\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above, `y = x` simply creates a new named binding called `y` which refers to whatever `x` currently binds to.\n", "\n", "To copy the data, you need to be more explicit" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2, 3]\n", "y = copy(x)\n", "y[1] = 2\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, rather than making a copy of `x`, you may want to just have a similarly sized array" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 139779552174960\n", " 139779552174992\n", " 139779552175024" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2, 3]\n", "y = similar(x)\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use `similar` to pre-allocate a vector with a different size, but the same shape" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 139779779281392\n", " 139779779281264\n", " 139779779313760\n", " 0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2, 3]\n", "y = similar(x, 4) # make a vector of length 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which generalizes to higher dimensions" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 139779552352560 139779552352624\n", " 139779552352592 1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2, 3]\n", "y = similar(x, 2, 2) # make a 2x2 matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Manual Array Definitions\n", "\n", "As we’ve seen, you can create one dimensional arrays from manually specified data like so" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30\n", " 40" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10, 20, 30, 40]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In two dimensions we can proceed as follows" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×4 Array{Int64,2}:\n", " 10 20 30 40" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10 20 30 40] # two dimensional, shape is 1 x n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ndims(a)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 10 20\n", " 30 40" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10 20; 30 40] # 2 x 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You might then assume that `a = [10; 20; 30; 40]` creates a two dimensional column vector but this isn’t the case." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30\n", " 40" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10; 20; 30; 40]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ndims(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead transpose the matrix (or adjoint if complex)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4×1 Adjoint{Int64,Array{Int64,2}}:\n", " 10\n", " 20\n", " 30\n", " 40" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10 20 30 40]'" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ndims(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Indexing\n", "\n", "We’ve already seen the basics of array indexing" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10 20 30 40]\n", "a[end-1]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For 2D arrays the index syntax is straightforward" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.587870014565205" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = randn(2, 2)\n", "a[1, 1]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 0.587870014565205\n", " -0.6938604675588613" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1, :] # first row" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 0.587870014565205\n", " 0.758473576894037" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:, 1] # first column" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Booleans can be used to extract elements" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.5344 -0.199852\n", " -0.613174 0.394427" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = randn(2, 2)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Bool,2}:\n", " 1 0\n", " 0 1" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [true false; false true]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 0.5343997488684169\n", " 0.3944273890315965" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is useful for conditional extraction, as we’ll see below.\n", "\n", "An aside: some or all elements of an array can be set equal to one number using slice notation." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.0\n", " 0.0\n", " 0.0\n", " 0.0" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = zeros(4)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element view(::Array{Float64,1}, 2:4) with eltype Float64:\n", " 42.0\n", " 42.0\n", " 42.0" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:end] .= 42" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.0\n", " 42.0\n", " 42.0\n", " 42.0" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Views and Slices\n", "\n", "Using the `:` notation provides a slice of an array, copying the sub-array to a new array with a similar type." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b = [2, 4]\n", "a = " ] }, { "name": "stdout", "output_type": "stream", "text": [ "[1 4; 3 5]\n", "b = [2, 4]\n" ] } ], "source": [ "a = [1 2; 3 4]\n", "b = a[:, 2]\n", "@show b\n", "a[:, 2] = [4, 5] # modify a\n", "@show a\n", "@show b;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A **view** on the other hand does not copy the value" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b = [2, 4]\n", "a = [1 4; 3 5]\n", "b = [4, 5]\n" ] } ], "source": [ "a = [1 2; 3 4]\n", "@views b = a[:, 2]\n", "@show b\n", "a[:, 2] = [4, 5]\n", "@show a\n", "@show b;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the only difference is the `@views` macro, which will replace any slices with views in the expression.\n", "\n", "An alternative is to call the `view` function directly – though it is generally discouraged since it is a step away from the math." ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@views b = a[:, 2]\n", "view(a, :, 2) == b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As with most programming in Julia, it is best to avoid prematurely assuming that `@views` will have a significant impact on performance, and stress code clarity above all else.\n", "\n", "Another important lesson about `@views` is that they **are not** normal, dense arrays." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "typeof(b_slice) = Array{Int64,1}\n", "typeof(a) = Array{Int64,2}\n", "typeof(b) = SubArray{Int64,1,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}\n" ] } ], "source": [ "a = [1 2; 3 4]\n", "b_slice = a[:, 2]\n", "@show typeof(b_slice)\n", "@show typeof(a)\n", "@views b = a[:, 2]\n", "@show typeof(b);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The type of `b` is a good example of how types are not as they may seem.\n", "\n", "Similarly" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "Adjoint{Int64,Array{Int64,2}}" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1 2; 3 4]\n", "b = a' # transpose\n", "typeof(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To copy into a dense array" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1 2; 3 4]\n", "b = a' # transpose\n", "c = Matrix(b) # convert to matrix\n", "d = collect(b) # also `collect` works on any iterable\n", "c == d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Special Matrices\n", "\n", "As we saw with `transpose`, sometimes types that look like matrices are not stored as a dense array.\n", "\n", "As an example, consider creating a diagonal matrix" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Diagonal{Float64,Array{Float64,1}}:\n", " 1.0 ⋅ \n", " ⋅ 2.0" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = [1.0, 2.0]\n", "a = Diagonal(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the type is `2×2 Diagonal{Float64,Array{Float64,1}}`, which is not a 2-dimensional array.\n", "\n", "The reasons for this are both efficiency in storage, as well as efficiency in arithmetic and matrix operations.\n", "\n", "In every important sense, matrix types such as `Diagonal` are just as much a “matrix” as the dense matrices we have using (see the [introduction to types lecture](introduction_to_types.html) for more)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2a = [2.0 0.0; 0.0 4.0]\n", "b * a = " ] }, { "name": "stdout", "output_type": "stream", "text": [ "[0.02089371505446813 1.2357401141340265; 0.18216697618836442 1.8208847490854363]\n" ] } ], "source": [ "@show 2a\n", "b = rand(2,2)\n", "@show b * a;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example is in the construction of an identity matrix, where a naive implementation is" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.0 2.0\n", " 3.0 3.0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [1.0 2.0; 3.0 4.0]\n", "b - Diagonal([1.0, 1.0]) # poor style, inefficient code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whereas you should instead use" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.0 2.0\n", " 3.0 3.0" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [1.0 2.0; 3.0 4.0]\n", "b - I # good style, and note the lack of dimensions of I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While the implementation of `I` is a little abstract to go into at this point, a hint is:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "UniformScaling{Bool}" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(I)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a `UniformScaling` type rather than an identity matrix, making it much more powerful and general." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment and Passing Arrays\n", "\n", "As discussed above, in Julia, the left hand side of an assignment is a “binding” or a label to a value." ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Int64,2}:\n", " 1 2 3" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1 2 3]\n", "y = x # name `y` binds to whatever value `x` bound to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The consequence of this, is that you can re-bind that name." ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(x, y, z) = ([1 2 3], [2 3 4], [2 3 4])\n" ] } ], "source": [ "x = [1 2 3]\n", "y = x # name `y` binds to whatever `x` bound to\n", "z = [2 3 4]\n", "y = z # only changes name binding, not value!\n", "@show (x, y, z);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What this means is that if `a` is an array and we set `b = a` then `a` and `b` point to exactly the same data.\n", "\n", "In the above, suppose you had meant to change the value of `x` to the values of `y`, you need to assign the values rather than the name." ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(x, y, z) = ([2 3 4], [2 3 4], [2 3 4])\n" ] } ], "source": [ "x = [1 2 3]\n", "y = x # name `y` binds to whatever `x` bound to\n", "z = [2 3 4]\n", "y .= z # now dispatches the assignment of each element\n", "@show (x, y, z);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, you could have used `y[:] = z`.\n", "\n", "This applies to in-place functions as well.\n", "\n", "First, define a simple function for a linear map" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 5\n", " 11" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " return [1 2; 3 4] * x # matrix * column vector\n", "end\n", "\n", "val = [1, 2]\n", "f(val)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In general, these “out-of-place” functions are preferred to “in-place” functions, which modify the arguments." ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 5\n", " 11" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " return [1 2; 3 4] * x # matrix * column vector\n", "end\n", "\n", "val = [1, 2]\n", "y = similar(val)\n", "\n", "function f!(out, x)\n", " out .= [1 2; 3 4] * x\n", "end\n", "\n", "f!(y, val)\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This demonstrates a key convention in Julia: functions which modify any of the arguments have the name ending with `!` (e.g. `push!`).\n", "\n", "We can also see a common mistake, where instead of modifying the arguments, the name binding is swapped" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 139779872219056\n", " 139779872203472" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " return [1 2; 3 4] * x # matrix * column vector\n", "end\n", "\n", "val = [1, 2]\n", "y = similar(val)\n", "\n", "function f!(out, x)\n", " out = [1 2; 3 4] * x # MISTAKE! Should be .= or [:]\n", "end\n", "f!(y, val)\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The frequency of making this mistake is one of the reasons to avoid in-place functions, unless proven to be necessary by benchmarking." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In-place and Immutable Types\n", "\n", "Note that scalars are always immutable, such that" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = [1 2]\n", "y .-= 2 # y .= y .- 2, no problem\n", "\n", "x = 5\n", "# x .-= 2 # Fails!\n", "x = x - 2 # subtle difference - creates a new value and rebinds the variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In particular, there is no way to pass any immutable into a function and have it modified" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 2\n" ] } ], "source": [ "x = 2\n", "\n", "function f(x)\n", " x = 3 # MISTAKE! does not modify x, creates a new value!\n", "end\n", "\n", "f(x) # cannot modify immutables in place\n", "@show x;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is also true for other immutable types such as tuples, as well as some vector types" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(xdynamic) = [2, 4]\n", "f(xstatic) = " ] }, { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n", "xdynamic = [1, 2]\n", "g(xdynamic) = \"Success!\"\n", "xdynamic = [2, 4]\n" ] } ], "source": [ "using StaticArrays\n", "xdynamic = [1, 2]\n", "xstatic = @SVector [1, 2] # turns it into a highly optimized static vector\n", "\n", "f(x) = 2x\n", "@show f(xdynamic)\n", "@show f(xstatic)\n", "\n", "# inplace version\n", "function g(x)\n", " x .= 2x\n", " return \"Success!\"\n", "end\n", "@show xdynamic\n", "@show g(xdynamic)\n", "@show xdynamic;\n", "\n", "# g(xstatic) # fails, static vectors are immutable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operations on Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Methods\n", "\n", "Julia provides standard functions for acting on arrays, some of which we’ve\n", "already seen" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "length(a) = 3\n", "sum(a) = 0\n", "mean(a) = 0.0\n", "std(a) = " ] }, { "name": "stdout", "output_type": "stream", "text": [ "1.0\n", "var(a) = 1.0\n", "maximum(a) = 1\n", "minimum(a) = -1\n", "extrema(a) = (-1, 1)\n" ] }, { "data": { "text/plain": [ "(-1, 1)" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [-1, 0, 1]\n", "\n", "@show length(a)\n", "@show sum(a)\n", "@show mean(a)\n", "@show std(a) # standard deviation\n", "@show var(a) # variance\n", "@show maximum(a)\n", "@show minimum(a)\n", "@show extrema(a) # (mimimum(a), maximum(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To sort an array" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 0\n", " -1" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = sort(a, rev = true) # returns new array, original not modified" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 0\n", " -1" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = sort!(a, rev = true) # returns *modified original* array" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b == a # tests if have the same values" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b === a # tests if arrays are identical (i.e share same memory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matrix Algebra\n", "\n", "For two dimensional arrays, `*` means matrix multiplication" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×2 Array{Float64,2}:\n", " 1.0 1.0" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = ones(1, 2)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 1.0\n", " 1.0 1.0" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = ones(2, 2)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×2 Array{Float64,2}:\n", " 2.0 2.0" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * b" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×1 Array{Float64,2}:\n", " 2.0\n", " 2.0" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b * a'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To solve the linear system $ A X = B $ for $ X $ use `A \\ B`" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 1 2\n", " 2 3" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [1 2; 2 3]" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 1.0\n", " 1.0 1.0" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = ones(2, 2)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " -1.0 -1.0\n", " 1.0 1.0" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A \\ B" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " -1.0 -1.0\n", " 1.0 1.0" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inv(A) * B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although the last two operations give the same result, the first one is numerically more stable and should be preferred in most cases.\n", "\n", "Multiplying two **one** dimensional vectors gives an error – which is reasonable since the meaning is ambiguous.\n", "\n", "More precisely, the error is that there isn’t an implementation of `*` for two one dimensional vectors.\n", "\n", "The output explains this, and lists some other methods of `*` which Julia thinks are close to what we want." ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "hide-output": false }, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching *(::Array{Float64,1}, ::Array{Float64,1})\nClosest candidates are:\n *(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529\n *(!Matched::Adjoint{#s664,#s663} where #s663<:Union{DenseArray{T,2}, Base.ReinterpretArray{T,2,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, SubArray{T,2,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, Base.AbstractCartesianIndex},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}} where #s664, ::Union{DenseArray{S,1}, Base.ReinterpretArray{S,1,S1,A} where S1 where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, Base.ReshapedArray{S,1,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, SubArray{S,1,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, Base.AbstractCartesianIndex},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}}) where {T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64}, S} at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:106\n *(!Matched::Adjoint{#s664,#s663} where #s663<:LinearAlgebra.AbstractTriangular where #s664, ::AbstractArray{T,1} where T) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/triangular.jl:1971\n ...", "output_type": "error", "traceback": [ "MethodError: no method matching *(::Array{Float64,1}, ::Array{Float64,1})\nClosest candidates are:\n *(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529\n *(!Matched::Adjoint{#s664,#s663} where #s663<:Union{DenseArray{T,2}, Base.ReinterpretArray{T,2,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, Base.ReshapedArray{T,2,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, SubArray{T,2,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, Base.AbstractCartesianIndex},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}} where #s664, ::Union{DenseArray{S,1}, Base.ReinterpretArray{S,1,S1,A} where S1 where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, Base.ReshapedArray{S,1,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray}, SubArray{S,1,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, Base.AbstractCartesianIndex},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, Base.ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{Base.ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Union{Tuple{Vararg{Real,N} where N}, Tuple{AbstractUnitRange,Vararg{Any,N} where N}} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}}) where {T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64}, S} at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/matmul.jl:106\n *(!Matched::Adjoint{#s664,#s663} where #s663<:LinearAlgebra.AbstractTriangular where #s664, ::AbstractArray{T,1} where T) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/triangular.jl:1971\n ...", "", "Stacktrace:", " [1] top-level scope at In[74]:1" ] } ], "source": [ "ones(2) * ones(2) # does not conform, expect error" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead, you could take the transpose to form a row vector" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(2)' * ones(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, for inner product in this setting use `dot()` or the unicode `\\cdot`" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dot(ones(2), ones(2)) = 2.0\n", "ones(2) ⋅ ones(2) = 2.0\n" ] } ], "source": [ "@show dot(ones(2), ones(2))\n", "@show ones(2) ⋅ ones(2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix multiplication using one dimensional vectors similarly follows from treating them as\n", "column vectors. Post-multiplication requires a transpose" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 2.0\n", " 2.0" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = ones(2, 2)\n", "b * ones(2)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×2 Adjoint{Float64,Array{Float64,1}}:\n", " 2.0 2.0" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(2)' * b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the type of the returned value in this case is not `Array{Float64,1}` but rather\n", "`Adjoint{Float64,Array{Float64,1}}`.\n", "\n", "This is since the left multiplication by a row vector should also be a row-vector. It also hints\n", "that the types in Julia more complicated than first appears in the surface notation, as we will explore\n", "further in the [introduction to types lecture](introduction_to_types.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Elementwise Operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Algebraic Operations\n", "\n", "Suppose that we wish to multiply every element of matrix `A` with the corresponding element of matrix `B`.\n", "\n", "In that case we need to replace `*` (matrix multiplication) with `.*` (elementwise multiplication).\n", "\n", "For example, compare" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 2.0 2.0\n", " 2.0 2.0" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(2, 2) * ones(2, 2) # matrix multiplication" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 1.0\n", " 1.0 1.0" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(2, 2) .* ones(2, 2) # element by element multiplication" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a general principle: `.x` means apply operator `x` elementwise" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " -1.0 -1.0\n", " -1.0 -1.0" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = -ones(2, 2)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 1.0\n", " 1.0 1.0" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.^2 # square every element" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However in practice some operations are mathematically valid without broadcasting, and hence the `.` can be omitted." ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 2.0 2.0\n", " 2.0 2.0" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(2, 2) + ones(2, 2) # same as ones(2, 2) .+ ones(2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scalar multiplication is similar" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 1.0\n", " 1.0 1.0" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = ones(2, 2)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 2.0 2.0\n", " 2.0 2.0" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * A # same as 2 .* A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact you can omit the `*` altogether and just write `2A`.\n", "\n", "Unlike MATLAB and other languages, scalar addition requires the `.+` in order to correctly broadcast" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 0\n", " 1" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2]\n", "x .+ 1 # not x + 1\n", "x .- 1 # not x - 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Elementwise Comparisons\n", "\n", "Elementwise comparisons also use the `.x` style notation" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10, 20, 30]" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " -100\n", " 0\n", " 100" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [-100, 0, 100]" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element BitArray{1}:\n", " 0\n", " 0\n", " 1" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b .> a" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element BitArray{1}:\n", " 0\n", " 0\n", " 0" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a .== b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also do comparisons against scalars with parallel syntax" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " -100\n", " 0\n", " 100" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element BitArray{1}:\n", " 0\n", " 0\n", " 1" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b .> 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is particularly useful for *conditional extraction* – extracting the elements of an array that satisfy a condition" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " -0.6474097312061422\n", " -0.6001392953340913\n", " 0.6929032232542462\n", " -0.12570581205417036" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = randn(4)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element BitArray{1}:\n", " 1\n", " 1\n", " 0\n", " 1" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a .< 0" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " -0.6474097312061422\n", " -0.6001392953340913\n", " -0.12570581205417036" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[a .< 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Changing Dimensions\n", "\n", "The primary function for changing the dimensions of an array is `reshape()`" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30\n", " 40" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10, 20, 30, 40]" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 10 30\n", " 20 40" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = reshape(a, 2, 2)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 10 30\n", " 20 40" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that this function returns a view on the existing array.\n", "\n", "This means that changing the data in the new array will modify the data in the\n", "old one." ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[1, 1] = 100 # continuing the previous example" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 100 30\n", " 20 40" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 100\n", " 20\n", " 30\n", " 40" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To collapse an array along one dimension you can use `dropdims()`" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1×4 Array{Int64,2}:\n", " 1 2 3 4" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1 2 3 4] # two dimensional" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dropdims(a, dims = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The return value is an array with the specified dimension “flattened”." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Broadcasting Functions\n", "\n", "Julia provides standard mathematical functions such as `log`, `exp`, `sin`, etc." ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log(1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, these functions act *elementwise* on arrays" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.0\n", " 0.6931471805599453\n", " 1.0986122886681098\n", " 1.3862943611198906" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log.(1:4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we can get the same result as with a comprehension or more explicit loop" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.0\n", " 0.6931471805599453\n", " 1.0986122886681098\n", " 1.3862943611198906" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[ log(x) for x in 1:4 ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nonetheless the syntax is convenient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear Algebra\n", "\n", "([See linear algebra documentation](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/))\n", "\n", "Julia provides some a great deal of additional functionality related to linear operations" ] }, { "cell_type": "code", "execution_count": 107, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 1 2\n", " 3 4" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [1 2; 3 4]" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "-2.0" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "det(A)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tr(A)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " -0.3722813232690143\n", " 5.372281323269014" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvals(A)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rank(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ranges\n", "\n", "As with many other types, a `Range` can act as a vector." ] }, { "cell_type": "code", "execution_count": 112, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vector(a) = [10, 11, 12]\n" ] }, { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 9.0\n", " 20.0\n", " 33.0" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 10:12 # a range, equivalent to 10:1:12\n", "@show Vector(a) # can convert, but shouldn't\n", "\n", "b = Diagonal([1.0, 2.0, 3.0])\n", "b * a .- [1.0; 2.0; 3.0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ranges can also be created with floating point numbers using the same notation." ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.0:0.1:1.0" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 0.0:0.1:1.0 # 0.0, 0.1, 0.2, ... 1.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But care should be taken if the terminal node is not a multiple of the set sizes." ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maxval = 1.0\n", "minval = 0.0\n", "stepsize = 0.15\n", "a = minval:stepsize:maxval # 0.0, 0.15, 0.3, ...\n", "maximum(a) == maxval" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To evenly space points where the maximum value is important, i.e., `linspace` in other languages" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maxval = 1.0\n", "minval = 0.0\n", "numpoints = 10\n", "a = range(minval, maxval, length=numpoints)\n", "# or range(minval, stop=maxval, length=numpoints)\n", "\n", "maximum(a) == maxval" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuples and Named Tuples\n", "\n", "([See tuples](https://docs.julialang.org/en/v1/manual/functions/#Tuples-1) and [named tuples documentation](https://docs.julialang.org/en/v1/manual/functions/#Named-Tuples-1))\n", "\n", "We were introduced to tuples earlier, which provide high-performance immutable sets of distinct types." ] }, { "cell_type": "code", "execution_count": 116, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 1.0 and b = test\n" ] } ], "source": [ "t = (1.0, \"test\")\n", "t[1] # access by index\n", "a, b = t # unpack\n", "# t[1] = 3.0 # would fail as tuples are immutable\n", "println(\"a = $a and b = $b\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As well as **named tuples**, which extend tuples with names for each argument." ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "val1 = 1.0 and val1 = 1.0\n" ] } ], "source": [ "t = (val1 = 1.0, val2 = \"test\")\n", "t.val1 # access by index\n", "# a, b = t # bad style, better to unpack by name with @unpack\n", "println(\"val1 = $(t.val1) and val1 = $(t.val1)\") # access by name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While immutable, it is possible to manipulate tuples and generate new ones" ] }, { "cell_type": "code", "execution_count": 118, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "(val1 = 1.0, val2 = \"test\", val3 = 4, val4 = \"test!!\")" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2 = (val3 = 4, val4 = \"test!!\")\n", "t3 = merge(t, t2) # new tuple" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Named tuples are a convenient and high-performance way to manage and unpack sets of parameters" ] }, { "cell_type": "code", "execution_count": 119, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.30000000000000004" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(parameters)\n", " α, β = parameters.α, parameters.β # poor style, error prone if adding parameters\n", " return α + β\n", "end\n", "\n", "parameters = (α = 0.1, β = 0.2)\n", "f(parameters)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This functionality is aided by the `Parameters.jl` package and the `@unpack` macro" ] }, { "cell_type": "code", "execution_count": 120, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.30000000000000004" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Parameters\n", "\n", "function f(parameters)\n", " @unpack α, β = parameters # good style, less sensitive to errors\n", " return α + β\n", "end\n", "\n", "parameters = (α = 0.1, β = 0.2)\n", "f(parameters)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to manage default values, use the `@with_kw` macro" ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "paramgen() = (α = 0.1, β = 0.2)\n", "paramgen(α = 0.2) = (α = 0.2, β = 0.2)\n", "paramgen(α = 0.2, β = 0.5) = (α = 0.2, β = 0.5)\n" ] } ], "source": [ "using Parameters\n", "paramgen = @with_kw (α = 0.1, β = 0.2) # create named tuples with defaults\n", "\n", "# creates named tuples, replacing defaults\n", "@show paramgen() # calling without arguments gives all defaults\n", "@show paramgen(α = 0.2)\n", "@show paramgen(α = 0.2, β = 0.5);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An alternative approach, defining a new type using `struct` tends to be more prone to accidental misuse, and leads to a great deal of boilerplate code.\n", "\n", "For that, and other reasons of generality, we will use named tuples for collections of parameters where possible." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nothing, Missing, and Unions\n", "\n", "Sometimes a variable, return type from a function, or value in an array needs to represent the absence of a value rather than a particular value.\n", "\n", "There are two distinct use cases for this\n", "\n", "1. `nothing` (“software engineers null”): used where no value makes sense in a particular context due to a failure in the code, a function parameter not passed in, etc. \n", "1. `missing` (“data scientists null”): used when a value would make conceptual sense, but it isn’t available. \n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nothing and Basic Error Handling\n", "\n", "The value `nothing` is a single value of type `Nothing`" ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "Nothing" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "typeof(nothing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An example of a reasonable use of `nothing` is if you need to have a variable defined in an outer scope, which may or may not be set in an inner one" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 1.0\n", "f(1.0) = 1.0\n", "x was not set\n", "f(-1.0) = nothing\n" ] } ], "source": [ "function f(y)\n", " x = nothing\n", " if y > 0.0\n", " # calculations to set `x`\n", " x = y\n", " end\n", "\n", " # later, can check `x`\n", " if isnothing(x)\n", " println(\"x was not set\")\n", " else\n", " println(\"x = $x\")\n", " end\n", " x\n", "end\n", "\n", "@show f(1.0)\n", "@show f(-1.0);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While in general you want to keep a variable name bound to a single type in Julia, this is a notable exception.\n", "\n", "Similarly, if needed, you can return a `nothing` from a function to indicate that it did not calculate as expected." ] }, { "cell_type": "code", "execution_count": 124, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(-1.0) failed\n" ] } ], "source": [ "function f(x)\n", " if x > 0.0\n", " return sqrt(x)\n", " else\n", " return nothing\n", " end\n", "end\n", "x1 = 1.0\n", "x2 = -1.0\n", "y1 = f(x1)\n", "y2 = f(x2)\n", "\n", "# check results with isnothing\n", "if isnothing(y1)\n", " println(\"f($x2) successful\")\n", "else\n", " println(\"f($x2) failed\");\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an aside, an equivalent way to write the above function is to use the\n", "[ternary operator](https://docs.julialang.org/en/v1/manual/control-flow/index.html#man-conditional-evaluation-1),\n", "which gives a compact if/then/else structure" ] }, { "cell_type": "code", "execution_count": 125, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " x > 0.0 ? sqrt(x) : nothing # the \"a ? b : c\" pattern is the ternary\n", "end\n", "\n", "f(1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will sometimes use this form when it makes the code more clear (and it will occasionally make the code higher performance).\n", "\n", "Regardless of how `f(x)` is written, the return type is an example of a union, where the result could be one of an explicit set of types.\n", "\n", "In this particular case, the compiler would deduce that the type would be a `Union{Nothing,Float64}` – that is, it returns either a floating point or a `nothing`.\n", "\n", "You will see this type directly if you use an array containing both types" ] }, { "cell_type": "code", "execution_count": 126, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2-element Array{Union{Nothing, Float64},1}:\n", " 1.0\n", " nothing" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1.0, nothing]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When considering error handling, whether you want a function to return `nothing` or simply fail depends on whether the code calling `f(x)` is carefully checking the results.\n", "\n", "For example, if you were calling on an array of parameters where a priori you were not sure which ones will succeed, then" ] }, { "cell_type": "code", "execution_count": 127, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "4-element Array{Union{Nothing, Float64},1}:\n", " 0.31622776601683794\n", " nothing\n", " 1.4142135623730951\n", " nothing" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [0.1, -1.0, 2.0, -2.0]\n", "y = f.(x)\n", "\n", "# presumably check `y`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On the other hand, if the parameter passed is invalid and you would prefer not to handle a graceful failure, then using an assertion is more appropriate." ] }, { "cell_type": "code", "execution_count": 128, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " @assert x > 0.0\n", " sqrt(x)\n", "end\n", "\n", "f(1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, `nothing` is a good way to indicate an optional parameter in a function" ] }, { "cell_type": "code", "execution_count": 129, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No z given with 1.0\n", "z = 3.0 given with 1.0\n" ] } ], "source": [ "function f(x; z = nothing)\n", "\n", " if isnothing(z)\n", " println(\"No z given with $x\")\n", " else\n", " println(\"z = $z given with $x\")\n", " end\n", "end\n", "\n", "f(1.0)\n", "f(1.0, z=3.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An alternative to `nothing`, which can be useful and sometimes higher performance,\n", "is to use `NaN` to signal that a value is invalid returning from a function." ] }, { "cell_type": "code", "execution_count": 130, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "typeof(f(-1.0)) = Float64\n", "f(-1.0) == NaN = false\n", "isnan(f(-1.0)) = true\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " if x > 0.0\n", " return x\n", " else\n", " return NaN\n", " end\n", "end\n", "\n", "f(0.1)\n", "f(-1.0)\n", "\n", "@show typeof(f(-1.0))\n", "@show f(-1.0) == NaN # note, this fails!\n", "@show isnan(f(-1.0)) # check with this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in this case, the return type is `Float64` regardless of the input for `Float64` input.\n", "\n", "Keep in mind, though, that this only works if the return type of a function is `Float64`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exceptions\n", "\n", "(See [exceptions documentation](https://docs.julialang.org/en/v1/manual/control-flow/index.html#Exception-Handling-1))\n", "\n", "While returning a `nothing` can be a good way to deal with functions which may or may not return values, a more robust error handling method is to use exceptions.\n", "\n", "Unless you are writing a package, you will rarely want to define and throw your own exceptions, but will need to deal with them from other libraries.\n", "\n", "The key distinction for when to use an exceptions vs. return a `nothing` is whether an error is unexpected rather than a normal path of execution.\n", "\n", "An example of an exception is a `DomainError`, which signifies that a value passed to a function is invalid." ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "DomainError(-1.0, \"sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).\")" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# throws exception, turned off to prevent breaking notebook\n", "# sqrt(-1.0)\n", "\n", "# to see the error\n", "try sqrt(-1.0); catch err; err end # catches the exception and prints it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example you will see is when the compiler cannot convert between types." ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "InexactError(:Int64, Int64, 3.12)" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# throws exception, turned off to prevent breaking notebook\n", "# convert(Int64, 3.12)\n", "\n", "# to see the error\n", "try convert(Int64, 3.12); catch err; err end # catches the exception and prints it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If these exceptions are generated from unexpected cases in your code, it may be appropriate simply let them occur and ensure you can read the error.\n", "\n", "Occasionally you will want to catch these errors and try to recover, as we did above in the `try` block." ] }, { "cell_type": "code", "execution_count": 133, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.0 + 1.0im" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " try\n", " sqrt(x)\n", " catch err # enters if exception thrown\n", " sqrt(complex(x, 0)) # convert to complex number\n", " end\n", "end\n", "\n", "f(0.0)\n", "f(-1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Missing\n", "\n", "(see [“missing” documentation](https://docs.julialang.org/en/v1/manual/missing/))\n", "\n", "The value `missing` of type `Missing` is used to represent missing value in a statistical sense.\n", "\n", "For example, if you loaded data from a panel, and gaps existed" ] }, { "cell_type": "code", "execution_count": 134, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "5-element Array{Union{Missing, Float64},1}:\n", " 3.0\n", " missing\n", " 5.0\n", " missing\n", " missing" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [3.0, missing, 5.0, missing, missing]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A key feature of `missing` is that it propagates through other function calls - unlike `nothing`" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "missing + 1.0 = missing\n", "missing * 2 = missing\n", "missing * \"test\" = missing\n", "f(missing) = missing\n", "mean(x) = missing\n" ] } ], "source": [ "f(x) = x^2\n", "\n", "@show missing + 1.0\n", "@show missing * 2\n", "@show missing * \"test\"\n", "@show f(missing); # even user-defined functions\n", "@show mean(x);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The purpose of this is to ensure that failures do not silently fail and provide meaningless numerical results.\n", "\n", "This even applies for the comparison of values, which" ] }, { "cell_type": "code", "execution_count": 136, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x == missing = missing\n", "x === missing = true\n", "ismissing(x) = true\n" ] } ], "source": [ "x = missing\n", "\n", "@show x == missing\n", "@show x === missing # an exception\n", "@show ismissing(x);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Where `ismissing` is the canonical way to test the value.\n", "\n", "In the case where you would like to calculate a value without the missing values, you can use `skipmissing`." ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "hide-output": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mean(x) = missing\n", "mean(skipmissing(x)) = 2.6666666666666665\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "coalesce.(x, 0.0) = [1.0, 0.0, 2.0, 0.0, 0.0, 5.0]\n" ] } ], "source": [ "x = [1.0, missing, 2.0, missing, missing, 5.0]\n", "\n", "@show mean(x)\n", "@show mean(skipmissing(x))\n", "@show coalesce.(x, 0.0); # replace missing with 0.0;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As `missing` is similar to R’s `NA` type, we will see more of `missing` when we cover `DataFrames`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1\n", "\n", "This exercise uses matrix operations that arise in certain problems,\n", "including when dealing with linear stochastic difference equations.\n", "\n", "If you aren’t familiar with all the terminology don’t be concerned – you can\n", "skim read the background discussion and focus purely on the matrix exercise.\n", "\n", "With that said, consider the stochastic difference equation\n", "\n", "\n", "\n", "$$\n", "X_{t+1} = A X_t + b + \\Sigma W_{t+1} \\tag{1}\n", "$$\n", "\n", "Here\n", "\n", "- $ X_t, b $ and $ X_{t+1} $ are $ n \\times 1 $ \n", "- $ A $ is $ n \\times n $ \n", "- $ \\Sigma $ is $ n \\times k $ \n", "- $ W_t $ is $ k \\times 1 $ and $ \\{W_t\\} $ is iid with zero mean and variance-covariance matrix equal to the identity matrix \n", "\n", "\n", "Let $ S_t $ denote the $ n \\times n $ variance-covariance matrix of $ X_t $.\n", "\n", "Using the rules for computing variances in matrix expressions, it can be shown from [(1)](#equation-ja-sde) that $ \\{S_t\\} $ obeys\n", "\n", "\n", "\n", "$$\n", "S_{t+1} = A S_t A' + \\Sigma \\Sigma' \\tag{2}\n", "$$\n", "\n", "It can be shown that, provided all eigenvalues of $ A $ lie within the unit circle, the sequence $ \\{S_t\\} $ converges to a unique limit $ S $.\n", "\n", "This is the **unconditional variance** or **asymptotic variance** of the stochastic difference equation.\n", "\n", "As an exercise, try writing a simple function that solves for the limit $ S $ by iterating on [(2)](#equation-ja-sde-v) given $ A $ and $ \\Sigma $.\n", "\n", "To test your solution, observe that the limit $ S $ is a solution to the matrix equation\n", "\n", "\n", "\n", "$$\n", "S = A S A' + Q\n", "\\quad \\text{where} \\quad Q := \\Sigma \\Sigma' \\tag{3}\n", "$$\n", "\n", "This kind of equation is known as a **discrete time Lyapunov equation**.\n", "\n", "The [QuantEcon package](http://quantecon.org/quantecon-jl)\n", "provides a function called `solve_discrete_lyapunov` that implements a fast\n", "“doubling” algorithm to solve this equation.\n", "\n", "Test your iterative method against `solve_discrete_lyapunov` using matrices\n", "\n", "$$\n", "A =\n", "\\begin{bmatrix}\n", " 0.8 & -0.2 \\\\\n", " -0.1 & 0.7\n", "\\end{bmatrix}\n", "\\qquad\n", "\\Sigma =\n", "\\begin{bmatrix}\n", " 0.5 & 0.4 \\\\\n", " 0.4 & 0.6\n", "\\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2\n", "\n", "Take a stochastic process for $ \\{y_t\\}_{t=0}^T $\n", "\n", "$$\n", "y_{t+1} = \\gamma + \\theta y_t + \\sigma w_{t+1}\n", "$$\n", "\n", "where\n", "\n", "- $ w_{t+1} $ is distributed `Normal(0,1)` \n", "- $ \\gamma=1, \\sigma=1, y_0 = 0 $ \n", "- $ \\theta \\in \\Theta \\equiv \\{0.8, 0.9, 0.98\\} $ \n", "\n", "\n", "Given these parameters\n", "\n", "- Simulate a single $ y_t $ series for each $ \\theta \\in \\Theta $\n", " for $ T = 150 $. Feel free to experiment with different $ T $. \n", "- Overlay plots of the rolling mean of the process for each $ \\theta \\in \\Theta $,\n", " i.e. for each $ 1 \\leq \\tau \\leq T $ plot \n", "\n", "\n", "$$\n", "\\frac{1}{\\tau}\\sum_{t=1}^{\\tau}y_T\n", "$$\n", "\n", "- Simulate $ N=200 $ paths of the stochastic process above to the $ T $,\n", " for each $ \\theta \\in \\Theta $, where we refer to an element of a particular\n", " simulation as $ y^n_t $. \n", "- Overlay plots a histogram of the stationary distribution of the final\n", " $ y^n_T $ for each $ \\theta \\in \\Theta $. Hint: pass `alpha`\n", " to a plot to make it transparent (e.g. `histogram(vals, alpha = 0.5)`) or\n", " use `stephist(vals)` to show just the step function for the histogram. \n", "- Numerically find the mean and variance of this as an ensemble average, i.e.\n", " $ \\sum_{n=1}^N\\frac{y^n_T}{N} $ and\n", " $ \\sum_{n=1}^N\\frac{(y_T^n)^2}{N} -\\left(\\sum_{n=1}^N\\frac{y^n_T}{N}\\right)^2 $. \n", "\n", "\n", "Later, we will interpret some of these in [this lecture](../tools_and_techniques/lln_clt.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3\n", "\n", "Let the data generating process for a variable be\n", "\n", "$$\n", "y = a x_1 + b x_1^2 + c x_2 + d + \\sigma w\n", "$$\n", "\n", "where $ y, x_1, x_2 $ are scalar observables, $ a,b,c,d $ are parameters to estimate, and $ w $ are iid normal with mean 0 and variance 1.\n", "\n", "First, let’s simulate data we can use to estimate the parameters\n", "\n", "- Draw $ N=50 $ values for $ x_1, x_2 $ from iid normal distributions. \n", "\n", "\n", "Then, simulate with different $ w $\n", "* Draw a $ w $ vector for the `N` values and then `y` from this simulated data if the parameters were $ a = 0.1, b = 0.2 c = 0.5, d = 1.0, \\sigma = 0.1 $.\n", "* Repeat that so you have `M = 20` different simulations of the `y` for the `N` values.\n", "\n", "Finally, calculate order least squares manually (i.e., put the observables\n", "into matrices and vectors, and directly use the equations for\n", "[OLS](https://en.wikipedia.org/wiki/Ordinary_least_squares) rather than a package).\n", "\n", "- For each of the `M=20` simulations, calculate the OLS estimates for $ a, b, c, d, \\sigma $. \n", "- Plot a histogram of these estimates for each variable. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4\n", "\n", "Redo Exercise 1 using the `fixedpoint` function from `NLsolve` [this lecture](julia_by_example.html).\n", "\n", "Compare the number of iterations of the NLsolve’s Anderson Acceleration to the handcoded iteration used in Exercise 1.\n", "\n", "Hint: Convert the matrix to a vector to use `fixedpoint`. e.g. `A = [1 2; 3 4]` then `x = reshape(A, 4)` turns it into a vector. To reverse, `reshape(x, 2, 2)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1\n", "\n", "Here’s the iterative approach" ] }, { "cell_type": "code", "execution_count": 138, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "compute_asymptotic_var (generic function with 1 method)" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function compute_asymptotic_var(A, Σ;\n", " S0 = Σ * Σ',\n", " tolerance = 1e-6,\n", " maxiter = 500)\n", " V = Σ * Σ'\n", " S = S0\n", " err = tolerance + 1\n", " i = 1\n", " while err > tolerance && i ≤ maxiter\n", " next_S = A * S * A' + V\n", " err = norm(S - next_S)\n", " S = next_S\n", " i += 1\n", " end\n", " return S\n", "end" ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.5 0.4\n", " 0.4 0.6" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [0.8 -0.2;\n", " -0.1 0.7]\n", "\n", "Σ = [0.5 0.4;\n", " 0.4 0.6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that all eigenvalues of $ A $ lie inside the unit disc." ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "0.9" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(abs, eigvals(A))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s compute the asymptotic variance" ] }, { "cell_type": "code", "execution_count": 141, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.671228 0.633476\n", " 0.633476 0.858874" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "our_solution = compute_asymptotic_var(A, Σ)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let’s do the same thing using QuantEcon’s `solve_discrete_lyapunov()` function and check we get the same result." ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "hide-output": true }, "outputs": [], "source": [ "using QuantEcon" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "hide-output": false }, "outputs": [ { "data": { "text/plain": [ "3.883245447999784e-6" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "norm(our_solution - solve_discrete_lyapunov(A, Σ * Σ'))" ] } ], "metadata": { "date": 1590087458.61993, "download_nb": 1, "download_nb_path": "https://julia.quantecon.org/", "filename": "fundamental_types.rst", "filename_with_path": "getting_started_julia/fundamental_types", "kernelspec": { "display_name": "Julia 1.4.1", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.1" }, "title": "Arrays, Tuples, Ranges, and Other Fundamental Types" }, "nbformat": 4, "nbformat_minor": 2 }