Octave Programming and Linear Algebra
Octave Programming and Linear Algebra
Basic Math
Examples given in Octave code
Victor K Miclovich
vicmiclovich@{ugandasoft, gmail}.com
December 5, 2009
2
Preface
I would like to welcome you to this tutorial courtesy of GuruPrevails. We
want to make Machine learning a really wonderful experience and I hope
these tutorials shall give you great satisfaction.
Good luck and have fun!
Victor K Miclovich
December, 2009
Uganda
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1 Introduction 5
1.1 Starting Octave . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.1 Using Octave like a calculator . . . . . . . . . . . . . . 5
1.1.2 Inbuilt functions . . . . . . . . . . . . . . . . . . . . . 6
1.1.3 Some constants to take note of. . . . . . . . . . . . . . 6
3 Probability Theory 13
3.1 Simple terminology . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.1 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.2 Probability space or Sample space . . . . . . . . . . . 13
3.1.3 Probability . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.4 Review of basic Probability concepts . . . . . . . . . . 13
3.1.5 Law of Total Probability . . . . . . . . . . . . . . . . . 14
3.1.6 Baye’s Theorem . . . . . . . . . . . . . . . . . . . . . 14
3.2 Where Probability occurs in Octave . . . . . . . . . . . . . . 14
3.3 Functions that are probabilistic in nature . . . . . . . . . . . 15
4 What next? 17
4.1 Alternatives that we are going to use . . . . . . . . . . . . . . 17
4.1.1 What makes Python different from Octave/MatLab? . 17
3
4 CONTENTS
Chapter 1
Introduction
Addition
octave:1> 3 + 4
=>7
This is a simple example... and almost any kind of operand4 will work.
1
Don’t worry if you don’t have strong UNIX background... similar things can get done
in Windows
2
Windows users should look for Octave under the Programs menu of their machine;
install Octave if you don’t have it.
3
Windows users will probably click under the programs menu... and a terminal should
pop out.
4
In the above example, the operands are 3 and 4
5
6 CHAPTER 1. INTRODUCTION
Subtraction
octave:1> 5 - 2
=>3
Multiplication
octave:1> 5 * 2
=>10
5
trigonometric functions usually take radians an input... e.g. 90◦ is π
2
Chapter 2
2.1 Matrix(-ces)
2.1.1 Representing a matrix
A matrix is just an array of numbers. These numbers could be real numbers,
integers or some other system of numbers or numerics.
Mathematical representation of an m × n matrix
a1,1 a1,2 ··· a1,n
a2,1 a2,2 ··· a2,n
Am,n = .
.. .. ..
.. . . .
am,1 am,2 · · · am,n
In Octave I show several types of matrices
Row matrix
octave:1>A = [3,4,5]
=>
3 4 5
Column matrix
octave:2>A = [3; 4; 5]
=>
3
4
5
A 2 × 3 matrix
octave:3>A = [1, 2, 3; 4, 5, 6;]
=>
7
8 CHAPTER 2. LINEAR ALGEBRA BASICS
1 2 3
4 5 6
or octave:3>A = [ [1, 2, 3]; [4, 5, 6]]
=>
1 2 3
4 5 6
A 3 × 3 matrix
Please, don’t get confused by my use of #; this is put inside your program
to help you read and understand the logic of your program.
Scalar products
2.1. MATRIX(-CES) 9
octave:1> newA = 2 * A;
octave:2># remember to always give A some values. You can test
other values...
octave:3># A is a variable; a place in computer memory where we
stor the values octave:4># in this case those values are entries/elements
of an array
octave:5> newA
=>
2 4 6
8 10 12
14 16 18
Take note of my use of newA; this is called variable... and the line
newA = 2 * A computes a value and puts it under that name... so I can
use newA anywhere in my program. Please read the Octave manual to get
other concepts in programming using the Octave language. Don’t forget to
PRACTICE
Determinants
Determinants are usually calculated for square matrices n × n; you might
turn up with some nasty errors if you work with singular or m × n matrices
where m 6= n
octave:1>A = [2,3,4];[3,15,20];[7,8,1];
octave:1>det(A)
=> -203.00
1
Mathematical definition of a Transpose of a matrix: Given an m × n matrix, it’s
transpose is just that matrix with rows interchanged for columns... an n × m;
[A]Tij = [A]ji
10 CHAPTER 2. LINEAR ALGEBRA BASICS
2.2 Assignment
You are going to test the following functions; study the math and octave
code too!
2. Using the above vectors, get the cross product, ~v ×~u [Hint: cross(x,y)
gives ~x × ~y ]
1. ~u + ~v is in V 4
2. c~u is in V 5
3. ~u + ~v = ~v + ~u
4. u + v = v + u
2
if ~
u and ~v are objects in V and c is a scalar, then we can in some way, define ~v + ~
u
and c~ u or c~v .
3
we use objects here because vector has 2 things its describing: orientation and
length/size... which makes the term sound more ”important”.
4
closed under addition
5
closed under scalar multiplication
2.3. VECTOR SPACES 11
5. ~u + (~v + w)
~ = (~u + ~v ) + w
~
11. 1 × ~u = ~u
From your knowledge of the previous sections, implement these axioms for
the following vectors... if this isn’t enough you can set your questions :)
(a)
~v = (2, 7)
octave:1> v = [-13; -25]
Use the constants c = 3, 5, 6, k = 3, 5, 6 And test using the Octave
interpreter6
6
another name for the terminal that you see on running Octave
12 CHAPTER 2. LINEAR ALGEBRA BASICS
Chapter 3
Probability Theory
3.1.3 Probability
To find the probability of an event A
n(A)
P (A) =
n(S)
At times we may need to find the complement of some probability A0 , that
event that does not occur.
P (A0 ) = 1 − P (A)
13
14 CHAPTER 3. PROBABILITY THEORY
P (A ∩ B) = P (A|B) × P (B)
For some extra credit and work... please re-familiarize yourselves with the
probability tree... I won’t do that here...
Probability trees make calculations of conditional probabilities easy... con-
ditional probabilities occur a lot with decision making algorithms, recursive
depth search and many other algorithms you are gonna encounter in your
Machine learning careerer.
P (B) = P (B ∩ A) + P (B ∩ A0 )
= P (A)P (B|A) + P (A0 )P (B|A0 )
P (A)P (B|A)
P (A|B) =
P (A)P (B|A) + P (A0 )P (B|A0 )
1
The equation above is the denominator of Baye’s Theorem
2
timers
3.3. FUNCTIONS THAT ARE PROBABILISTIC IN NATURE 15
What next?
• There’s a lot more fun stuff... like libraries that let you play with the
web; you can implement features like web scrappers, SPAM filters,
signal analysis, training algorithms, etc.
During the course, we shall look at python and more math in detail.
17