Investing in financial products involves risk to your capital.

Asset Classes

Free investment financial education

More Campus Resources

Useful Tools and Information

Language

Multilingual content from IBKR

The Brass Tacks of Julia Programming – Part II

Posted May 25, 2022 at 10:50 am
Anshul Tayal
QuantInsti

See Part I for instructions on basic arithmetic operations.

Working with matrices

A matrix is an array of numbers represented as a vector of vectors. Here’s how you can create a matrix.

matrix_1 = [[1 2 3 ]; [4 5 6]]

Here’s what it looks like:

Output:

2×3 Matrix{Int64}:
1 2  3
4 5  6

Let’s look at various other operations in a matrix.

# Size of a matrix
print("This size of the matrix is" , size(matrix_1))

# Matrix
matrix_2 = [[2 2 0 ]; [2 7 9]]

#Transpose of a matrix
matrix_2'

# Accessing matrix elements
matrix_2[2,3]
matrix_2[1:4]
Output:

The size of the matrix is (2, 3)

2×3 Matrix{Int64}:
2 2  0
2 7  9

9

4-element Vector{Int64}:
2
2
2
7

Let’s add and multiply matrices now.

matrix_2 + matrix_1
matrix_3 = [[2 3] ; [5 6] ; [4 5]]

# Multiplying matrices
matrix_1 * matrix_3
The output of addition is -
2×3 Matrix{Int64}:
3 4   3
6 12  15

The output of multiplication is -
2×2 Matrix{Int64}:
24  30
57  72

Performing the elementwise operation in Julia using the “.” symbol. Any operation followed by “.” will perform. Here’s an example of taking the log of each element of a matrix. It is equivalent to the map() function in Python

# element wise operation.

log.(matrix_1)
Output:

2×3 Matrix{Float64}:
0.0 0.693147  1.09861
1.38629 1.60944   1.79176

Let’s create a matrix with zeros and ones. You can use the function zeros() and ones() to do that.

zero = zeros(3,4)
one = ones(2,3)
Output:

3×4 Matrix{Float64}:
0.0 0.0  0.0  0.0
0.0 0.0  0.0  0.0
0.0 0.0  0.0  0.0

2×3 Matrix{Float64}:
1.0 1.0  1.0
1.0 1.0  1.0

Let’s see how to generate random values.

rand_1 = rand(5)

# Random values in 2D
rand_2 = rand(5, 2)

# Random values in 3D
rand_3 = rand(3, 2, 2)
Output:

5-element Vector{Float64}:
0.24942793903668203
0.8454642816997531
0.8538602946052574
0.21154126925067507
0.07873330355017316

5×2 Matrix{Float64}:
0.706975 0.935989
0.494324 0.988657
0.994824 0.943832
0.44183 0.404011
0.802337 0.124313

3×2×2 Array{Float64, 3}:
[:, :, 1] =
0.443337 0.813706
0.958816 0.53124
0.203751 0.951473

[:, :, 2] =
0.125259 0.761672
0.440907 0.0505402
0.161741 0.672215

Loops

A loop helps in performing the same task repeatedly.  There are multiple types of loops used in programming . Mostly, they are:

• For loop – This type of loop is used when the no. of iterations to be run are known beforehand.
• While loop – This type of loop is used when the no. of iterations depends on a condition.

Let’s see how to create them in Julia.

## While loop

var = 0
while var < 10
var += 1
println(var)
end
Output:

1
2
3
4
5
6
7
8
9
10
## For loop

# Method 1
for i in 1:10
println(i)
end

# Method 2
colleagues = ["Vivek", "Jay", "Mario", "Udisha"]
for colleague in colleagues
println(colleague)
end

# Method 3
c = [i+j for i in 1:3, j in 1:5]
Output:

The output of method 1 -
1
2
3
4
5
6
7
8
9
10

The output of method 2 -
Vivek
Jay
Mario
Udisha

The output of method 3 -
3×5 Matrix{Int64}:
2 3  4  5  6
3 4  5  6  7
4 5  6  7  8

There are multiple methods of performing the same operations in any programming language. Feel free to take your own approach.

Conditional statements

Let’s move on to the conditional statements. These are if-else conditional statements where you want to perform an operation when a certain event occurs. Here’s the control flow.

Here’s the syntax –

if condition 1
println(“Statements if condition 1 is true”)
else
println(“Statements if condition 1 is false”)
end

Here’s how you can do it.

var_1 = 31
var_2 = 45

# Method 1
if var_1 > var_2
println("\$var_1 is larger than \$var_2")
elseif var_2 > var_1
println("\$var_2 is larger than \$var_1")
else
println("None")
end

# Method 2
# condition ? True:False
(var_1 > var_2) ? var_1 : var_2

# Method 3
# && is logical "and" operator
(var_1 > var_2) && [22] println("\$var_1 is larger than \$var_2")
(var_2 > var_1) && println("\$var_2 is larger than \$var_1")
Output:

45 is larger than 31

45

false

45 is larger than 31

Functions

Functions are a set of instructions written together so that they can be used multiple times without writing the code repeatedly.

Creating functions also helps debug the code easily as you can look at individual blocks (functions) and figure out the one with the error.

Here’s how we can write functions in Julia:

# Method 1
function power_5(var_1)
return var_1^5
end

# Calling the function
power_5(4)

# Method 2
power_5(x) = x^5
power_5(4)
Output:

1024
1024

Stay tuned for the next installment in which Anshul Tayal will show us how to use R and Python code in Julia.