Conditional Calculations or Loop
1 view (last 30 days)
Show older comments
I've got a question about how Matlab works with conditional formulas. I have a dataset imported that contains tickers, dates, and returns (each as separate arrays). What I'd like to calculate eventually are large rolling period medians applying various filters to my datasets. To get started, I'm wondering if someone can point me in the right direction.
What I'd like to do is calculate the product of my (MyReturns) array for each ticker in my tickers array(MyTickers) and a certain time period (MyDates). Once I have all the product’s calculated, I’d like to calculate the median of that dataset and write that value to a separate array for future use and move on to another time period. My Data looks like this (see below)-
Say I want to calculate the Product of MyReturns for MyDates = 1/1 and 1/2. Then find the median of the returns and store that value. How would I go about writing a formula or a loop to do that calculation within Matlab?
Thanks a lot! Brian
MyTickers MyDates MyReturns
GOOG 1/1/2011 1.01
GOOG 1/2/2011 1.02
GOOG 1/3/2011 0.99
GOOG 1/4/2011 0.98
GOOG 1/5/2011 1
AAPL 1/1/2011 1.03
AAPL 1/2/2011 1.01
AAPL 1/3/2011 1.02
AAPL 1/4/2011 0.99
AAPL 1/5/2011 0.98
IBM 1/1/2011 1
IBM 1/2/2011 1.01
IBM 1/3/2011 1.01
IBM 1/4/2011 0.99
IBM 1/5/2011 0.99
0 Comments
Accepted Answer
Fangjun Jiang
on 1 Nov 2011
All you need is to properly index and select the data from the array. I don't understand what do you mean by "Product" though.
MyTickers={'GOOG','GOOG','GOOG','AAPL','AAPL','IBM','IBM'}';
MyDates={'1/1/2011','1/2/2011','1/3/2011','1/1/2011','1/2/2011','1/2/2011','1/3/2011'}';
MyReturns=[1.01 1.02 0.99 1.03 1.04 0.8 0.7];
MyNumDates=datenum(MyDates);
Index1=strcmp(MyTickers,'GOOG');
Index2=MyNumDates>=datenum('1/1/2011') & MyNumDates<datenum('1/3/2011');
Index=Index1 & Index2;
SelectData=MyReturns(Index)
prod(SelectData)
4 Comments
Fangjun Jiang
on 2 Nov 2011
I don't understand. In the example, MyTickers etc. only has a few elements. If you have hundreds of elements, the code will work the same and return whatever data that meets the criteria.
More Answers (0)
See Also
Categories
Find more on Categorical Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!