# An Introductory Example¶

Contents

## Overview¶

We’re now ready to start learning the Julia language itself

### Level¶

Our approach is aimed at those who already have at least some knowledge of programming — perhaps experience with Python, MATLAB, R, C or similar

In particular, we assume you have some familiarity with fundamental programming concepts such as

- variables
- loops
- conditionals (if/else)

If you have no such programming experience, then one option is to try Python first

Python is a great first language and there are many introductory treatments

Otherwise, just dive in and see how you go...

### Approach¶

In this lecture we will write and then pick apart small Julia programs

At this stage the objective is to introduce you to basic syntax and data structures

Deeper concepts—how things work—will be covered in later lectures

Since we are looking for simplicity the examples are a little contrived

### Set Up¶

We assume that you’ve worked your way through our getting started lecture already

For this lecture, we recommend that you work in a Jupyter notebook, as described here

### Other References¶

The definitive reference is Julia’s own documentation

The manual is thoughtfully written but also quite dense (and somewhat evangelical)

The presentation in this and our remaining lectures is more of a tutorial style based around examples

## Example: Plotting a White Noise Process¶

To begin, let’s suppose that we want to simulate and plot the white noise process \(\epsilon_0, \epsilon_1, \ldots, \epsilon_T\), where each draw \(\epsilon_t\) is independent standard normal

In other words, we want to generate figures that look something like this:

This is straightforward using Plots.jl, which was discussed in our set up lecture

Fire up a Jupyter notebook and enter the following in a cell

```
using Plots
ts_length = 100
epsilon_values = randn(ts_length)
plot(epsilon_values, color="blue")
```

Let’s break this down and see how it works

### Importing Functions¶

The effect of the statement `using Plots`

is to make all the names exported by the `Plots`

module available in the global scope

If you prefer to be more selective you can replace `using Plots`

with `import Plots: plot`

Now only the `plot`

function is accessible

Since our program uses only the plot function from this module, either would have worked in the previous example

### Arrays¶

The function call `epsilon_values = randn(ts_length)`

creates one of the
most fundamental Julia data types: an array

```
julia> typeof(epsilon_values)
Array{Float64,1}
julia> epsilon_values
100-element Array{Float64,1}:
-0.908823
-0.759142
-1.42078
0.792799
0.577181
1.74219
-0.912529
1.06259
0.5766
-0.0172788
-0.591671
-1.02792
...
-1.29412
-1.12475
0.437858
-0.709243
-1.96053
1.31092
1.19819
1.54028
-0.246204
-1.23305
-1.16484
```

The information from `typeof()`

tells us that `epsilon_values`

is an array of 64 bit floating point values, of dimension 1

Julia arrays are quite flexible — they can store heterogeneous data for example

```
julia> x = [10, "foo", false]
3-element Array{Any,1}:
10
"foo"
false
```

Notice now that the data type is recorded as `Any`

, since the array contains mixed data

The first element of `x`

is an integer

```
julia> typeof(x[1])
Int64
```

The second is a string

```
julia> typeof(x[2])
ASCIIString (constructor with 2 methods)
```

The third is the boolean value `false`

```
julia> typeof(x[3])
Bool
```

Notice from the above that

- array indices start at 1 (unlike Python, where arrays are zero-based)
- array elements are referenced using square brackets (unlike MATLAB and Fortran)

Julia contains many functions for acting on arrays — we’ll review them later

For now here’s several examples, applied to the same list `x = [10, "foo", false]`

```
julia> length(x)
3
julia> pop!(x)
false
julia> x
2-element Array{Any,1}:
10
"foo"
julia> push!(x, "bar")
3-element Array{Any,1}:
10
"foo"
"bar"
julia> x
3-element Array{Any,1}:
10
"foo"
"bar"
```

The first example just returns the length of the list

The second, `pop!()`

, pops the last element off the list and returns it

In doing so it changes the list (by dropping the last element)

Because of this we call `pop!`

a **mutating method**

It’s conventional in Julia that mutating methods end in `!`

to remind the user that the function has other effects beyond just returning a value

The function `push!()`

is similar, expect that it appends its second argument to the array

### For Loops¶

Although there’s no need in terms of what we wanted to achieve with our program, for the sake of learning syntax let’s rewrite our program to use a for loop

```
using Plots
ts_length = 100
epsilon_values = Array(Float64, ts_length)
for i in 1:ts_length
epsilon_values[i] = randn()
end
plot(epsilon_values, color="blue")
```

Here we first declared `epsilon_values`

to be an empty array for storing 64 bit floating point numbers

The `for`

loop then populates this array by successive calls to `randn()`

- Called without an argument,
`randn()`

returns a single float

Like all code blocks in Julia, the end of the for loop code block (which is just one line here) is indicated by the keyword `end`

The word `in`

from the for loop can be replaced by symbol `=`

The expression `1:ts_length`

creates an **iterator** that is looped over — in this case the integers from `1`

to `ts_length`

Iterators are memory efficient because the elements are generated on the fly rather than stored in memory

In Julia you can also loop directly over arrays themselves, like so

```
words = ["foo", "bar"]
for word in words
println("Hello $word")
end
```

The output is

```
Hello foo
Hello bar
```

### While Loops¶

The syntax for the while loop contains no surprises

```
using Plots
ts_length = 100
epsilon_values = Array(Float64, ts_length)
i = 1
while i <= ts_length
epsilon_values[i] = randn()
i = i + 1
end
plot(epsilon_values, color="blue")
```

The next example does the same thing with a condition and the `break`

statement

```
using Plots
ts_length = 100
epsilon_values = Array(Float64, ts_length)
i = 1
while true
epsilon_values[i] = randn()
i = i + 1
if i > ts_length
break
end
end
plot(epsilon_values, color="blue")
```

### User-Defined Functions¶

For the sake of the exercise, let’s now go back to the `for`

loop but restructure our program so that generation of random variables takes place within a user-defined function

```
using Plots
function generate_data(n)
epsilon_values = Array(Float64, n)
for i = 1:n
epsilon_values[i] = randn()
end
return epsilon_values
end
ts_length = 100
data = generate_data(ts_length)
plot(data, color="blue")
```

Here

`function`

is a Julia keyword that indicates the start of a function definition`generate_data`

is an arbitrary name for the function`return`

is a keyword indicating the return value

#### A Slightly More Useful Function¶

Of course the function `generate_data`

is completely contrived

We could just write the following and be done

```
ts_length = 100
data = randn(ts_length)
plot(data, color="blue")
```

Let’s make a slightly more useful function

This function will be passed a choice of probability distribution and respond by plotting a histogram of observations

In doing so we’ll make use of the Distributions package

```
julia> Pkg.add("Distributions")
```

Here’s the code

```
using Plots
using Distributions
function plot_histogram(distribution, n)
epsilon_values = rand(distribution, n) # n draws from distribution
histogram(epsilon_values)
end
lp = Laplace()
plot_histogram(lp, 500)
```

The resulting figure looks like this

Let’s have a casual discussion of how all this works while leaving technical details for later in the lectures

First, `lp = Laplace()`

creates an instance of a data type defined
in the Distributions module that represents the Laplace distribution

The name `lp`

is bound to this object

When we make the function call `plot_histogram(lp, 500)`

the code in the body
of the function `plot_histogram`

is run with

- the name
`distribution`

bound to the same object as`lp`

- the name
`n`

bound to the integer`500`

#### A Mystery¶

Now consider the function call `rand(distribution, n)`

This looks like something of a mystery

The function `rand()`

is defined in the base library such that `rand(n)`

returns `n`

uniform random variables on \([0, 1)\)

```
julia> rand(3)
3-element Array{Float64,1}:
0.856817
0.981502
0.510947
```

On the other hand, `distribution`

points to a data type representing the Laplace distribution that has been defined in a third party package

So how can it be that `rand()`

is able to take this kind of object as an
argument and return the output that we want?

The answer in a nutshell is **multiple dispatch**

This refers to the idea that functions in Julia can have different behavior depending on the particular arguments that they’re passed

Hence in Julia we can take an existing function and give it a new behavior by defining how it acts on a new type of object

The interpreter knows which function definition to apply in a given setting by looking at the types of the objects the function is called on

In Julia these alternative versions of a function are called **methods**

## Exercises¶

### Exercise 1¶

Recall that \(n!\) is read as “\(n\) factorial” and defined as \(n! = n \times (n - 1) \times \cdots \times 2 \times 1\)

In Julia you can compute this value with `factorial(n)`

Write your own version of this function, called `factorial2`

, using a for loop

### Exercise 2¶

The binomial random variable \(Y \sim Bin(n, p)\) represents

- number of successes in \(n\) binary trials
- each trial succeeds with probability \(p\)

Using only `rand()`

from the set of Julia’s built in random number
generators (not the Distributions package), write a function `binomial_rv`

such that `binomial_rv(n, p)`

generates one draw of \(Y\)

Hint: If \(U\) is uniform on \((0, 1)\) and \(p \in (0,1)\), then the expression `U < p`

evaluates to `true`

with probability \(p\)

### Exercise 3¶

Compute an approximation to \(\pi\) using Monte Carlo

For random number generation use only `rand()`

Your hints are as follows:

- If \(U\) is a bivariate uniform random variable on the unit square \((0, 1)^2\), then the probability that \(U\) lies in a subset \(B\) of \((0,1)^2\) is equal to the area of \(B\)
- If \(U_1,\ldots,U_n\) are iid copies of \(U\), then, as \(n\) gets large, the fraction that falls in \(B\) converges to the probability of landing in \(B\)
- For a circle, area = pi * radius^2

### Exercise 4¶

Write a program that prints one realization of the following random device:

- Flip an unbiased coin 10 times
- If 3 consecutive heads occur one or more times within this sequence, pay one dollar
- If not, pay nothing

Once again use only `rand()`

as your random number generator

### Exercise 5¶

Simulate and plot the correlated time series

The sequence of shocks \(\{\epsilon_t\}\) is assumed to be iid and standard normal

Set \(T=200\) and \(\alpha = 0.9\)

### Exercise 6¶

Plot three simulated time series, one for each of the cases \(\alpha=0\), \(\alpha=0.8\) and \(\alpha=0.98\)

In particular, you should produce (modulo randomness) a figure that looks as follows

(The figure illustrates how time series with the same one-step-ahead conditional volatilities, as these three processes have, can have very different unconditional volatilities.)