Lecture 4 amended and corrected PowerPoint PPT Presentation

presentation player overlay
1 / 34
About This Presentation
Transcript and Presenter's Notes

Title: Lecture 4 amended and corrected


1
Lecture 4 (amended and corrected)
  • Divide and conquer
  • Dot product

2
Last time yahoo/google correlation
  • Start with 'yahoo.csv' and 'google.csv', stock
    prices per day for 2 stocks
  • Data is in dollars
  • Want to find what days both prices rose

3
Strategy
  • 1. Convert the Google data into positive and
    negative percent changes.
  • 2. Convert the Yahoo data into positive and
    negative percent changes.
  • 3. Find out when Google rose.
  • 4. Find out when Yahoo rose.
  • 5. Find out when they both rose.

4
Steps
  • Really only three steps compute percent changes,
    find rises, and match-up rises.

5
Google percentage price change
  • change 100 (today-yesterday) / yesterday
  • In matlab
  • googleChange
  • 100(google(2end) google(1end-1))
  • ./ google(1end-1)
  • Changes for Yahoo will look very similar.

6
Write a procedure!
  • function res percentChange(pricelist)
  • for a list of n prices, compute the n-1
    percentage
  • changes in price from previous day.
  • res 100(priceList(2end) priceList(1end-1))
  • ./ priceList(1end-1)
  • Alternative
  • today pricelist(2end)
  • Yesterday pricelist(1end-1)
  • res 100(today yesterday) ./ yesterday

7
Strategy
  • Convert the Google data into positive and
    negative percent changes.
  • function compareStocks()
  • yahoo csvread(yahoo.csv)
  • google csvread(google.csv)
  • yahooChanges percentChange(yahoo)
  • googleChanges percentChange(google)
  • more to come

8
Strategy
  • 1. Convert the Google data into positive and
    negative percent changes.
  • 2. Convert the Yahoo data into positive and
    negative percent changes.
  • 3. Find out when Google rose.
  • 4. Find out when Yahoo rose.
  • 5. Find out when they both rose.

9
When did google rise?
  • Google price rose on days when googleRises gt 0.
  • Lets get a list of 1s and 0s for rise or
    didnt.
  • Easy
  • googleRises (googleChanges gt 0)

10
Write another function
  • function res findRises(data)
  • report which elements of data represent price
  • rises
  • res (data gt 0)

11
Main procedure, revised
  • function compareStocks()
  • yahoo csvread(yahoo.csv)
  • google csvread(google.csv)
  • yahooChanges percentChange(yahoo)
  • googleChanges percentChange(google)
  • yahooRises findRises(yahooChanges)
  • googleRises findRises(googleChanges)
  • rises (yahooRises googleRises) gets printed
  • days find(rises) gets printed indexes of
    days.
  • Note the single-ampersand () is elementwise
    and, which you should use is something
    slightly different, which we wont need.

12
Logical operations
  • If p and q are logicals, then AND and
    OR.
  • P Q is True only of p is True AND q is True.
  • P Q is true if
  • p is True or
  • Q is True or
  • Both are true
  • p is not p not true gt false not false gt
    true.
  • , , and all operate elementwise on arrays.

13
Example of multiple returns
  • Compute both the greatest common divisor and the
    least common multiple of n and k.
  • function g, m both(n, k)
  • compute gcd and lcm of n and k.
  • g gcd(n,k)
  • m lcm(n,k)

14
New topic matrix products
  • Useful in economic computations, all sorts of
    scientific modeling, etc.
  • Surprisingly easy
  • Impress your friends by saying that you learned
    to compute tensor products for tensors of type
    (1,1). ?

15
First step an example
  • Read in two files
  • Number of computers sold by each of several
    manufacturers
  • Average computer price for each manufacturer
  • computers csvread('computers.csv')
  • prices csvread('prices.csv')
  • What is total revenue?
  • totalmoney1 sum(computers . prices)
  • Can also be written
  • totalmoney1 dot(computers, prices)

16
Dot product
  • 10 20 60
  • 200 500 100
  • 200010000 6000 18000
  • The dot product mutliplies corresponding
    elements of two vectorsand then sums up
  • Vectors must have same number of elements!
  • Also called inner product

17
Exercises
  • Whats the dot product of ones(1,12) and
    ones(1,12)? (ans 12)
  • The dot product of 2 4 5 and -1 0 1? (ans 3)
  • The dot product of 1 1 and -1 1? (ans 0)

18
Vectors and arrows
  • We can interpret a vector like 2 3 as
    representing an arrow from the origin (0,0) to
    the point (2, 3)

19
Deep Geometric Truth
  • If the dot product of u and v is zero, and both u
    and v are nonzero, then theyre perpendicular!

20
Algebraic fact
  • Dot product follows the same rules as ordinary
    products, when they make sense.
  • Example
  • v DOT (u w) v DOT u v DOT w.

21
Recall our arrow picture
  • We can interpret a vector like 2 3 as
    representing an arrow from the origin (0,0) to
    the point (2, 3)

22
Length of a vector
  • Pythagoras says h2 22 32, so
  • h sqrt(4 9).
  • In general
  • length of v sqrt(dot(v,v))

h
3
2
23
Matrix Product
  • If A is n x k, and B is k x p, then AB is an n
    x p matrix.

p
B
k
A
C
n
k
24
Matrix Product
  • (i,j) entry of AB is dot product of ith row of A
    with jth column of B

Col j
p
B
k
Row i
C
n
k
25
Example
0 1 1 4 2 0 1 1 0 0 1 1
B
2 1 3 2 0 7
A
26
Properties of matrix multiply
  • A(BC) AB AC
  • (AB)C AC BC
  • (AB)C A(BC)
  • The identity matrix I ( eye(n)) has the
    property that I A A (where A is n x k)
  • For some n x n matrices, A, theres an inverse
    a matrix B with AB BA I.
  • For others, theres not.
  • Matlab can find B inv(A).
  • Never use this! (or rarely)

27
Systems of equations and matrix multiplication
  • The equations
  • 2x 3y 6
  • x 4y 5
  • can be rewritten as
  • 2 3x 6
  • 1 -4y 5
  • i.e., in the form Ax b.


28
Solving systems
  • Matlab can solve systems like Ax b
  • Write x A\b.
  • Alternative x inv(A) b MUCH
    SLOWER

29
Other cases
  • You might have more equations than unknowns
  • 3 2x 5
  • 1 -3y 2
  • 4 7 3
  • Matlab will find x and y with Ax b as small a
    vector as possible.

30
Other cases (II)
  • You might have FEWER equations than unknowns
  • Matlab will find the smallest possible solution
    vector.

31
Other cases (III)
  • You might want to solve Ax b, and A is not
    invertiblematlab will tell you so with an
    error/warning.

32
New topic LOOPS
  • Waited this long because we generally avoid them
    in Matlab code
  • Some of you have seen a for-loop in lab already

33
for loops
  • u 0
  • for t 15
  • u u t
  • end
  • U will end up being 1 2 3 4 5.
  • Execute the steps inside the loop over and over,
    each time with t having a new value from the list
    of possibilities

34
Problem
  • Sum up the primes from 1 to 100.
  • Solution (one of MANY solutions)
  • total 0
  • for i 1100
  • if (isprime(i))
  • total total i
  • end
  • end
  • total do this to make the total print out
Write a Comment
User Comments (0)
About PowerShow.com