lecture one: fundamental of computational economics
DESCRIPTION
Lecture notes for the course: Fundamental of Computational EconomicsTRANSCRIPT
Lecture One: Fundamental of Computational Economics
Lecture One: Fundamental ofComputational Economics
Li, Xihao∗
Department of Economics and Social Sciences (DiSES)
Universita Politecnica delle Marche
October, 2013
∗Department of Economics and Social Sciences (DiSES), Universita Politecnica delle Marche, Piazzale
Martelli 8, 60121 Ancona, Italy. Email: [email protected].
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Introduction: I
Goal of Lecture One: to have general idea about how touse computational tools to work out problems.
Tools:General tools: Matlab, GNU Octave
Special purpose tools: e.g., statistical package: Eview,Sdata, R
We use GNU Octave for demonstration!Main textbook: (”Scientific Computing with MATLABand Octave”, Quarteroni Alfio, Saleri Fausto, GervasioPaola 3rd ed., 2010)
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Introduction: I
Goal of Lecture One: to have general idea about how touse computational tools to work out problems.
Tools:General tools: Matlab, GNU Octave
Special purpose tools: e.g., statistical package: Eview,Sdata, R
We use GNU Octave for demonstration!
Main textbook: (”Scientific Computing with MATLABand Octave”, Quarteroni Alfio, Saleri Fausto, GervasioPaola 3rd ed., 2010)
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Introduction: I
Goal of Lecture One: to have general idea about how touse computational tools to work out problems.
Tools:General tools: Matlab, GNU Octave
Special purpose tools: e.g., statistical package: Eview,Sdata, R
We use GNU Octave for demonstration!Main textbook: (”Scientific Computing with MATLABand Octave”, Quarteroni Alfio, Saleri Fausto, GervasioPaola 3rd ed., 2010)
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
ABC of GNU Octave
GNU Octave (Octave in short) is free, under GNU GeneralPublic License.
Learning by doing!
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: I
Clean up the working environment:
> clear all;
> clc;
display format in Octave:
> a = 1/7;
> a
format long:
> format long
> a
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: II
epsilon, the roundoff error:
> eps
finite scope of float number:
> realmin
> realmax
Infinity representation:
> Inf
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: III
Numercial computation: computer is NOT the same asMath!
Example:
> a = 1; b=1; while a+b ∼ = a; b=b/2; end> b
Another example:
> x = 1.e-15;> ((1+x)-1)/x
NaN:
> 0/0
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: IV
Complex number:
> z = complex(1,1)
Consider abs(z) ( cos( angle(z) ) + i * sin( angle(z) ) ):
modulus of complex number:
> abs(z)
angle of complex number:
> angle(z)
Compare with 45 degree of angle, pi/4:
> angle z = pi/4
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: V
graphical presentation:
> compass(z)
real and imaginary part:
> real(z)
> imag(z)
conjuecture of z:
> conj(z)
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: VI
Matrices:
> A = [ 1,2; 3, 4 ]
> A complex = [ complex(1,1), 2; 3, 4 ]
zero matrix:
> B = zeros(3,3)
identity matrix:
> id = eye(2, 2)
Note the dimension of matrix, try A * B
determinant of matrix:
> det(A)Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: VII
inverse of matrix:
> inv(A)
try complex matrix:
> det(A complex)
> inv(A complex)
singular matrix:
> A sing = [1,0; 3, 0]
> det(A sing)
> inv(A sing)
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: VIII
diagonal matrix:
> v = [1:2:5]
> dv = diag(v)
transpose of matrix:
> A’
Hermitian of complex matrix:
> A complex’
transpose of diagonal matrix:
> dv’
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: IX
vectors:
> v = zeros(10,1)
> v’
> size(v)
> size(v’)
unit vector:
> vone = ones(10,1)
random number vector:
> vrand = rand(100,1);
> plot(vrand)Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: X
dot product:
> vrand = rand(10,1)
> dot(vrand, vone)
dot product with unit vector is equivalent to sum:
> sum(vrand)
Euclidean norm of vector:
> norm(vrand)
> norm(vone)
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: XI
element-by-element operation .∗, .\, .∧:
> vrand .∗ ones(10,1)
> vrand .∗ ones(10,1) - vrand
Question: does it work with: vrand * ones(10,1)?
Question: vrand .∗ ones(10,1) - vrand is the same asvrand .∗ ones(10,1) .− vrand ?
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: XII
simple way to write a function:
> func = ’x.∧ 2 + 2 * x + 1’;
standard way to write a function:
> function y = fun (x); y = x.∧2 + 2 * x + 1 + 10; endfunction
plot functions:
> lim = [-7,5];> figure;> subplot(2,1,1); fplot(func, lim); legend(”x.∧2 + 2 * x + 1”);> hold on;> subplot(2,1,2); fplot(’fun’, lim, ’-r’); legend(”x.∧2 + 2 * x +1 + 10”);> hold off;
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: XII
another way to plot functions:
> xx = [-7:0.1:5];> v func = xx.∧2 + 2 * xx + 1;> v fun = xx.∧2 + 2 * xx + 1 + 10;
> figure;> subplot(2,1,1); plot(xx, v func, ’c’, ’Linewidth’, 5);legend(”x.∧2 + 2 * x + 1”);> hold on;> subplot(2,1,2); plot(xx, v fun, ’-r’, ’Linewidth’, 5);legend(”x.∧2 + 2 * x + 1 + 10”);> hold off;
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Action: XIII
numerical solution of f(x) = 0:
> function y = f (x)> y = x.∧2 - 2 * x - 3;> endfunction
find f(x) = 0:
> fzero(’f’, -2)
> fzero(’f’, 4)
> fzero(’f’, [-2,0])
Question: What if fzero(’f’, [-2,4])? Why?
Li, Xihao Lecture Notes
Lecture One: Fundamental of Computational Economics Introduction First Tour Exercise
Exercise
Try the aforementioned questions.
Read Chapter 1 of textbook for introduction.
Li, Xihao Lecture Notes
Install GNU Octave in Windows I
The complete instruction is here:http://wiki.octave.org/Octave_for_Windows
To install Octave-3.6.4-mingw + octaveforge pkgs:
download:1 binary file ”Octave3.6.4 gcc4.6.2 20130329.7z”2 package file ” Octave3.6.4 gcc4.6.2 pkgs 20130331.7z”
extract these two files to the path: ” C:/Octave/”
copy file ”octave3.6.4 gcc4.6.2.lnk” and”octave3.6.4 gcc4.6.2 docs.lnk” to any convenient lo-cation and edit its properties
Li, Xihao Lecture Notes
Install GNU Octave in Windows IIlaunch Octave console and and execute the follow-ing commands:> pkg rebuild -auto> pkg rebuild -noauto ad> pkg rebuild -noauto nan> pkg rebuild -noauto gsl> pkg rebuild -auto javarestart Octave.
(optional) install ”Notepad++” as editor:download Notepad++ from http://notepad-plus-plus.org/ and install it on your system
edit ” C:/Octave//share /octave /site /m /startup /octaverc”:
uncomment the line:EDITOR(’C:/Program Files/Notepad++/notepad++.exe’);
Li, Xihao Lecture Notes
Install GNU Octave in Windows IIIadd the following lines:> edit (”editor”, sprintf (”%s %%s”, EDITOR ()))> edit mode async
Li, Xihao Lecture Notes