lstsq.rst
author Nishanth <nishanth@fossee.in>
Thu, 07 Oct 2010 14:10:32 +0530
changeset 232 da873a5ac918
parent 221 7cd975ff5f0d
permissions -rw-r--r--
made the tuples.rst into new template form
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
     1
.. Author              : Nishanth
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
     2
   Internal Reviewer 1 : Puneeth
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
     3
   Internal Reviewer 2 : 
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
     4
   External Reviewer   :
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
     5
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     6
Hello friends and welcome to the tutorial on Least Square Fit
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     7
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     8
{{{ Show the slide containing title }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     9
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    10
{{{ Show the slide containing the outline slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    11
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    12
In this tutorial, we shall look at generating the least square fit line for a
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    13
given set of points.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    14
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    15
First let us have a look at the problem.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    16
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    17
{{{ Show the slide containing problem statement. }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    18
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    19
We have an input file generated from a simple pendulum experiment.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    20
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    21
It contains two columns of data. The first column is the length of the
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    22
pendulum and the second is the corresponding time period of the pendulum.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    23
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    24
As we know, the square of time period of a pendulum is directly proportional to
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    25
its length, we shall plot l vs t^2 and verify this. 
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    26
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    27
#[Puneeth:] removed the explanation about loadtxt and unpack
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    28
 option. It's been done in another LO already. simple dependency 
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    29
 should work?
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    30
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    31
To read the input file and parse the data, we are going to use the
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    32
loadtxt function.  Type 
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    33
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    34
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    35
    l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    36
    l
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    37
    t
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    38
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    39
We can see that l and t are two sequences containing length and time values
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    40
correspondingly.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    41
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    42
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
    43
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    44
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    45
    tsq = t * t
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    46
    plot(l, tsq, 'bo')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    47
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    48
{{{ switch to the plot window }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    49
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    50
#[Puneeth:] Moved explanation of least square fit here. seems more
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    51
apt. 
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    52
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    53
We can see that there is a visible linear trend, but we do not get a
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    54
straight line connecting them. We shall, therefore, generate a least
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    55
square fit line.
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    56
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    57
{{{ show the slide containing explanation on least square fit }}}
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    58
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    59
As shown in the slide, we are first going to generate the two matrices
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    60
tsq and A. Then we are going to use the ``lstsq`` function to find the
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    61
values of m and c.
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    62
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    63
let us now generate the A matrix with l values.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    64
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
    65
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
    66
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    67
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    68
    inter_mat = array((l, ones_like(l)))
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    69
    inter_mat
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    70
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    71
We see that we have intermediate matrix. Now we need the transpose. Type
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    72
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    73
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    74
    A = inter_mat.T
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    75
    A
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    76
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    77
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
    78
Type
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    79
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    80
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    81
    result = lstsq(A, tsq)
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    82
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    83
The result is a sequence of values. The first item in this sequence,
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
    84
is the matrix p i.e., the values of m and c. Hence, 
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    85
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    86
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    87
    m, c = result[0]
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    88
    m
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    89
    c
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    90
139
9e67c055a413 added a newline before :: so that a colon does not appear in html
nishanth
parents: 135
diff changeset
    91
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
    92
::
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    93
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    94
    tsq_fit = m * l + c
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    95
    plot(l, tsq, 'bo')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    96
    plot(l, tsq_fit, 'r')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    97
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    98
We get the least square fit of l vs t^2
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    99
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   100
{{{ Pause here and try out the following exercises }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   101
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   102
%% 2 %% change the label on y-axis to "y" and save the lines of code
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   103
        accordingly
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   104
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   105
{{{ continue from paused state }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   106
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   107
{{{ Show summary slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   108
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   109
This brings us to the end of the tutorial.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   110
we have learnt
135
7bc03b5096f9 corrected the rst syntax
nishanth
parents: 132
diff changeset
   111
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   112
 * how to generate a least square fit
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   113
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   114
{{{ Show the "sponsored by FOSSEE" slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   115
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   116
#[Nishanth]: Will add this line after all of us fix on one.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   117
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   118
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   119
Hope you have enjoyed and found it useful.
195
e8a251048213 Comments and changes for lstsq script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 139
diff changeset
   120
Thank you
221
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   121
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   122
Questions
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   123
=========
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   124
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   125
 1. What does ones_like([1, 2, 3]) produce
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   126
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   127
   a. array([1, 1, 1])
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   128
   #. [1, 1, 1]
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   129
   #. [1.0, 1.0, 1.0]
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   130
   #. Error
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   131
   
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   132
 2. What does ones_like([1.2, 3, 4, 5]) produce
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   133
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   134
   a. [1.2, 3, 4, 5]
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   135
   #. array([1.0, 1.0, 1.0, 1.0])
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   136
   #. array([1, 1, 1, 1])
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   137
   #. array([1.2, 3, 4, 5])
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   138
7cd975ff5f0d Added questions
Nishanth <nishanth@fossee.in>
parents: 195
diff changeset
   139
 3. What is the shape of the