lstsq.rst
author amit
Wed, 22 Sep 2010 20:01:59 +0530
changeset 183 c66ee1743d25
parent 139 9e67c055a413
child 195 e8a251048213
permissions -rw-r--r--
Merging changes with Nishant's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Least Square Fit
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     2
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     4
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     6
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     7
In this tutorial, we shall look at generating the least square fit line for a
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     8
given set of points.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     9
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    10
First let us have a look at the problem.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    11
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    12
{{{ Show the slide containing problem statement. }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    13
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    14
We have an input file generated from a simple pendulum experiment.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    15
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    16
It contains two columns of data. The first column is the length of the
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    17
pendulum and the second is the corresponding time period of the pendulum.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    18
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    19
As we know, the square of time period of a pendulum is directly proportional to
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    20
its length, we shall plot l vs t^2 and verify if the proportionality is linear.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    21
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    22
If it is not linear, we shall generate a least square fit line.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    23
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    24
{{{ show the slide containing explanation on least square fit }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    25
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    26
As shown in the slide, we are first going to generate the two matrices tsq and
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    27
A. Then we are going to use the =lstsq= function to find the values of m and c.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    28
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    29
To read the input file and parse the data, we are going to loadtxt function.
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    30
Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    31
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    32
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    33
    data = loadtxt("/home/fossee/pendulum.txt")
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    34
    data
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    35
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    36
As you can see, data is a sequence containing 90 records. Each record contains
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    37
two values. The first is length and second is time period. But what we need is 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    38
two sequences. One sequence containing all the length values and one containing
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    39
all the time values.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    40
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    41
Hence we have to use the unpack option with loadtxt. It unpacks the data into
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    42
 sequences depending on the structure of data.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    43
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    44
Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    45
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    46
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    47
    l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    48
    l
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    49
    t
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    50
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    51
We can see that l and t are two sequences containing length and time values
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    52
correspondingly.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    53
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    54
Let us first plot l vs t^2. Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    55
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    56
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    57
    tsq = t * t
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    58
    plot(l, tsq, 'bo')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    59
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    60
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    61
{{{ switch to the plot window }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    62
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    63
We can see that there is a visible linear trend.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    64
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    65
let us now generate the A matrix with l values.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    66
We shall first generate a 2 x 90 matrix with the first row as l values and the
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    67
second row as ones. Then take the transpose of it. Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    68
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    69
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    70
    inter_mat = array((l, ones_like(l)))
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    71
    inter_mat
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    72
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    73
We see that we have intermediate matrix. Now we need the transpose.Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    74
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    75
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    76
    A = inter_mat.T
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    77
    A
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    78
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    79
Now we have both the matrices A and tsq. We only need to use the =lstsq=
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    80
Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    81
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    82
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    83
    result = lstsq(A, tsq)
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    84
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    85
The result is a sequence of values. The first item is the matrix p or in simple
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    86
words, the values of m and c. Hence, 
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    87
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    88
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    89
    m, c = result[0]
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    90
    m
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    91
    c
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    92
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    93
Now that we have m and c, we need to generate the fitted values of t^2. Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    94
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    95
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    96
    tsq_fit = m * l + c
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    97
    plot(l, tsq, 'bo')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    98
    plot(l, tsq_fit, 'r')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    99
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   100
We get the least square fit of l vs t^2
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   101
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   102
{{{ Pause here and try out the following exercises }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   103
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   104
%% 2 %% change the label on y-axis to "y" and save the lines of code
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   105
        accordingly
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   106
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   107
{{{ continue from paused state }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   108
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   109
{{{ Show summary slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   110
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   111
This brings us to the end of the tutorial.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   112
we have learnt
135
7bc03b5096f9 corrected the rst syntax
nishanth
parents: 132
diff changeset
   113
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   114
 * how to use loadtxt to read files
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   115
 * how to generate a least square fit
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   116
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   117
{{{ Show the "sponsored by FOSSEE" slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   118
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   119
#[Nishanth]: Will add this line after all of us fix on one.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   120
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   121
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   122
Hope you have enjoyed and found it useful.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   123
Thankyou
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   124
 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   125
.. Author              : Nishanth
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   126
   Internal Reviewer 1 : 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   127
   Internal Reviewer 2 : 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   128
   External Reviewer   :