How do I loop a portfolio return calculation for multiple periods in matlab? - matlab

I have the following data/sets (simplified version to make the example clear):
3*10 matrix of stocks identified by a number for a given period (3 stocks (my portfolio) in rows * by 10 days (in columns) named indexBest.
10*10 matrix of returns for each period for each security in my universe named dailyret (my universe of stocks is 10).
I want to create a loop where I am able to calculate the sum returns of each portfolio for each period into one matrix ideally 1*10 or 10*1 (returns * date or vice versa).
I have done this for a single period for a portfolio, see below: but I want to be able to automate this process instead of doing all this 10* over for each portfolio for each period. Please help!
Portfolio_1_L= indexBest(:,1); %helps me get the portfolio constituents for period one (3 stocks basically).
Returns_1_L= dailyret(Portfolio_1(:,1));%to calculate returns of the portfolio for period 1 I have referenced the extraction of returns to the portfolio constituents.
Sum_ret_Port_1_L=sum(Returns_1_L); %sum return of the portfolio for period one
How do I loop this process for all other 9 periods?

Use a for loop and use the index variable in place of hardcoded 1 in your example code. Also index the output to store the result for each day.
for day = 1:10
Portfolio_1_L = indexBest(:,day);
Returns_1_L = dailyret(Portfolio_1_L);
Sum_ret_Port_1_L(day) = sum(Returns_1_L);
end

Related

Function which finds temperatures for a given month from data

I'm having some issues creating a function with the following parameters:
Ndata = extperiod(data, year, month,time)
The data is a table with 3 columns, which from left to right are:
year/month/date, time, temperature
My goal is to create a function which can extract a time and a year/month, irrespective of the date and find it's corresponding temperature.
I need to avoid using for loops
I've been advised to use floor and find, where floor(YYYYMMDD/100) = YYYY*100 + MM, which I somehow want to integrate to my function.
I've previously found a way to extract all temperatures from the data for a given day, as follows:
k = find(data(:,1)==19750101);
data(k(1):k(end),3)
I'm trying to incorporate this method, but I think that the hint "floor(YYYYMMDD/100)" throws me a of a little.
I have tried with find(data(:,1)==floor(YYYYMMDD/100)), where I would think that I'd be given all dates with a specific year and month. For example:
find( data(:,1) == floor(19660101/100) )
I thought this would give me all points in the column vector where the value is 196601. But it doesn't.
What could I try differently?
From your explanation, your want to get all temperature for a given month, no matter time and day.
So you want to find dates that are comprised in the range [YYYYMM ; YYYY{MM+1}[ or [YYYYMM ; {YYYY+1}01[ in the case of selecting December.
Recall that you store the complete date in your table. So you need to apply your operator floor to both sides of your query, not only on the query value, because no date is floor(YYYYMMDD/100)!
As a result, try the following:
find( floor(data(:,1)/100) == floor(19660101/100) )

How do I take an n-day average of data in Matlab to match another time series?

I have daily time series data and I want to calculate 5-day averages of that data while also retrieving the corresponding start date for each of the 5-day averages. For example:
x = [732099 732100 732101 732102 732103 732104 732105 732106 732107 732108];
y= [1 5 3 4 6 2 3 5 6 8];
Where x and y are actually size 92x1.
Firstly, how do I compute the 5-day mean when this time series data is not divisible by 5? Ultimately, I want to compute the 'jumping mean', where the average is not computed continuously (e.g., June 1-5, June 6-10, and so on).
I've tried doing the following:
Pentad_avg = mean(reshape(y(1:90),5,[]))'; %manually adjusted to be divisible by 5
Pentad_dt = x(1:5:90); %select every 5th day for time
However, Pentad_dt gives me dates 01-Jun-2004 and 06-Jun-2004 as output. And, that brings me to my second point.
I am looking to find 5-day averages for x and y that correspond to 5-day averages of another time series. This second time series has 5-day averaged data starting from 15-Jun-2004 until 29-Aug-2004 (instead of starting at 01-Jun-2004). Ultimately, how do I align the dates and 5-day averages between these two time series?
Synchronization between two time series can be accomplished using the timeseries object. Placing your data into an object allows Matlab to intelligently process it. The most useful thing is adds for your usage is the synchronize method.
You'll want to make sure to properly set the time vector on each of the timeseries objects.
An example of what this might look like is as follows:
ts1 = timeseries(y,datestr(x));
ts2 = timeseries(OtherData,OtherTimes);
[ts1 ts2] = synchronize(ts1,ts2,'Uniform','Interval',5);
This should return to you each timeseries aligned to be with the same times. You could also specify a specific time vector to align a timeseries to using the resample method.

Group of consecutive minimum values within data - MATLAB

I have daily river flow data for 1975-2009 and I am asked to find the 7 consecutive days within each year that have the smallest flows.
Any advice how to start this? I've only been using MATLAB for a couple weeks.
Thanks!
You could convolve the data with ones(1,7) and look for the minimum, which will yield the starting day of your dry period:
[~,startingDay] = min(conv(flow,ones(1,7),'valid'))
(This is basically a moving average filter without the normalization).
Loop through the years to get each year's result.
Start by finding cumulative sum with cumsum. The difference between cumulative sums 7 days apart will give you the total for those 7 days. Then pick the minimum of those.
a = cumsum(flow);
b = a(8:end) - a(1:end-7);
[m,i] = min(b);
Here m holds the smallest total over 7 consecutive days, and i is a vector of indices telling you when they occurred.

Extracting values in a matrix based on values of another matrix without a for loop in Octave

I'd like to know if there's a way to operate on the values of one matrix, based separatedly on the values of each row from another matrix, without using a for loop.
One specific example below.
data is a ~500k row matrix with three columns: the first is a list of hours/dates (I have them as serial numbers) covering 24 hours a day for several years, the second column is a location ID, and the third one is the electricity cost in that location.
hours has two columns: the first one is a list of dates/hours as a serial number and the second one is a certain upper limit specific for that hour.
What I need to do is find, for every date and hour in hours, the largest value of electrical cost that is below the upper limit set in hours, and save the ID of the corresponding location in a third column of hours if the id is unique, or zero if there's no location that satisfy the condition or if there's more than one.
A symplified version of the code I'm using:
#Add a third column for the hours matrix
ids=zeros(rows(hours),1)
hours=horzcat(hours,ids)
for i=1:rows(hour)
#Get the data for all locations in that hour
idx=(data(:,1)==hour(i,1) )
hourlydata=(data(idx,:))
#Get the ID for the maximum below the limit in that hour
idx=(hourlydata(:,3)<hour(i,2))
idlimit=(hourlydata(idx,3)==max(hourlydata(idx,3))
dataid=hourlydata(idlimit,2)
#Check if the data exists and is unique
if(rows(dataid)==1)
id=dataid(1,1)
else
id=0
endif
#Save the ID
hour(i,3)=id
endfor
Is there any way to do this without using the for loop?
(I already optimized the data matrix to make it as small as possible, but it's still pretty big, so I might encounter memory constraints when trying to implement a solution)
You can work with arrayfun. Suppose your data is
data = [datenum('2014-01-17'), 1, 13;datenum('2014-01-18'), 2, 7]
hours = [datenum('2014-01-17'), 17; datenum('2014-01-18'), 3]
Then define a selector function
function id = select( d, limit, data )
idx = (data(:,1)==d );
hourlydata = (data(idx,:));
idx = (hourlydata(:,3)<limit);
idlimit = (hourlydata(idx,3)==max(hourlydata(idx,3)));
dataid=hourlydata(idlimit,2);
if length(dataid)==1
id=dataid(1,1)
else
id=0
end
end
Now we find a vector the same size as hours containing the id
arrayfun(#(d, limit) select(d, limit, data), hours(:,1), hours(:,2))
ans =
1
0
You can easily merge this vector with hours.
Now i doubt that this is much faster, but no loop. Works with MATLAB, haven't checked with Octave.

Interactive Report - aggregate sum of multiple columns from one table multiply by values from another Table

I have a challenge in Oracle Apex - I would to sum multiple columns to give 3 extra rows namely points, Score, %score. There are more columns but I'm only choosing a few for now.
Below is an example structure of my data:
Town | Sector | Outside| Inside |Available|Price
Roy-----Formal----0----------0----------1------0
Kobus --Formal----0 ---------0--------- 1------0
Wika ---Formal----0----------0--------- 1------0
Mevo----Formal----1----------1----------1------0
Hoch----Formal----1----------1----------1------1
Points------------2----------2----------5------1
Score------------10---------10---------10------10
%score-----------20---------20---------50------10
Each column has a constant weighting (which serves as a factor and it can change depending on the areas). In this case, the weighting for the areas are in the first row of the sector Formal:
Sector |Outside| Inside |Available|Price
Formal----1----------1 ----------1-----1
Informal--1----------0 ----------2-----1
I tried using the aggregate sum function in apex but it wont work because I need the factor in the other table. This is where my challenge began.
To compute the rows below the report
points = sum per column * weighting factor per column
Score = sum of no of shops visited (in this case its 5) * weighting factor per column
% score = points/Score * 100
The report should display as described above. With the new computed rows below.
I kindly ask anyone to assist me with this challenge as I have tried searching for solutions but haven't come across any.
Thanks a lot for your support in advance!!