%% Lecture 2. % % READING DUE NEXT WEEK: Magrab, skim Chapter 1, read 2.1--2.5 % % HW: Magrab, % problem Section 2.3, exercise 2.3 (a,b) on page 79, and % problem Section 2.5, exercise 2.12 on page 81 % due at beginning of class next week. % turn in a print of of a script file with the commands used to solve % these problems. % time: 5-30 minutes. % in class next week, a closed book and notes quiz will ask for hand % written answers to 2 questions on creating vectors and matrices. % % LAST WEEK: I tried to demonstrate that matlab is a very powerful tool % that does math operations very well... with a focus on numerical % methods rather than symbolic computation (which is more the domain of % maple and mathematica). % % OPENING QUIZ: % 1. write (on paper) one matlab command that would make a list of % integers from 1 through 50 % 2. write (on paper) one matlab command that would 114 evenly spaced % numbers starting at 14 and ending at 22 % 3. write (on paper) one matlab command that would make a list that % looks like: [100 99 98 97 96 ... 1] % % TODAY: My goal is to completely work out a few specific examples % I hope that you enjoyed the Mandelbrot Set Laboratory. Like the % first lab, that had many steps that we went through % together... and some steps (some lines that you typed in) did % not have an obvious purpose until later in the lab. In this % lecture we will try to focus a little bit more on how to % organize our approach to a problem, as we teach a little bit % more syntax of matlab. % % So let's start today with a problem, and since so much focus in % technology development is based on the profits and returns, lets first % focus on money. Let's do an analysis of recent stock prices --- in % particular, lets find days when both google and yahoo's stock prices % went up. % % We could jump in here and start, but it is useful to have a strategy % in mind for how to proceed, and it is good to have a meta-strategy for % how to create a good strategy. % Meta Strategy: % 1. Specify the problem exactly. % 2. Define and do any programming necessary to get the input data. % 3. Then define the strategy for how to address the problem % % (why this? you should not work too hard to plan your approach to the % problem until you know what data you can get). % % Meta Planning: % % 1. the goal is "lets find days when both google and yahoo's stock prices % went up". More precisely, "Given the stock price data every day, % find which days the price of both yahoo and google went up". % 2. The input data: % So much is available online these days. We can get the data we % need from: % http://finance.yahoo.com/q/hp?s=AAPL % which has a link at the bottom to save the data to a % csv spreadsheet (csv == comma seperated values) % This is a very common, portable file format, and many % languages already have functions defined to read them. % In matlab, the function is: google = csvread('google.csv',1,1); yahoo = csvread('yahoo.csv',1,1); % important that this is in your "path" --- the current "relevant directory" % the 2,2 at the end says that we want to skip the first row and column, % so that we get just the matrix of numerical data. type "help csvread" % to get more information. % The "closing value" of the stock is in the fourth column, so lets % grab just that column and put it into a new variable: googleClose = google(:,4); % and do the same for yahoo. yahooClose = yahoo(:,4); % now, let's plot these plot(googleClose); hold on; plot(yahooClose); % did google really go down in this time? %... This array is arranged backwards in time.It'd be nice to fix that: googleFixed = googleClose(end:-1:1); yahooFixed = yahooClose(end:-1:1); % So, now we have our data values, % lets make a strategy for finding when they both rise. % 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. % Now we can implement these step by step. % we should also check that we get each step right before we go on. (!) % step 1? % 1. Convert the Google data into positive and negative percent changes. % percent change (in general, not in matlab terms) is: googleToday = googleFixed(2:end); googeeYesterday = googleFixed(1:end-1); googleChange = (today-yesterday)/yesterday googleChange = percentChange(googleFixed); % what will this look like for yahoo? % if they are so similar, good excuse to write a function! % why is writing a function good? %matlab function definition, in file "NAME.m" % function answer = NAME(arguments,...) % do stuff... % do stuff... % answer = something... % when that function is written, we can call it with: yahooChange = percentChange(yahooFixed); googleChange = percentChange(googleFixed); % Now, how can we tell when google is rising? %suggestion... googleRises = googleChange > 0; % googleChange(1:end)>0, googleChange(:) > 0 % and for yahoo? If it is similar, another chance for a function! yahooRises = yahooChange > 0; % then lets find when they both rise. rises = (yahooRises & googleRises) % gets printed days = find(rises) % gets printed; indexes of days. %%%% This is the end of what was asked for. But... now that we are %%%% here and have all this nice data... % How could we find days where both stocks fell? % how can we do this in just one line? % How can we display the correlations better? % tools for labelling plots xlabel ylabel % New commands today: % csvread % &, |, ~ % find