How to group financial time series objects in Matlab - matlab

I want to group 2 or more financial time series objects of different length.
Specifically, I have FTS a and b, a is shorter than b, and how can I get a time series c, so that c has two sub series a and b, and all the missing records in a is filled with null values. Thank you.

Sounds like the MERGE function is what you're looking for.

Related

Splitting a table in MATLAB by Sensor ID

I have a large table in MATLAB which contains over 1000 rows of data, in two columns. Column 1 is the ID of the sensor which gathered the data, and column two is the data itself (in this case a voltage).
I have been able to sort my table to gather all the data for sensors together. So, all the data from Sensor 1 is in rows 1 to 100, the data for Sensor 2 is in rows 101 to 179, the data for Sensor 3 is in rows 180 to 310, and so on. In other words, the number of rows which contain data for a given sensor is never the same.
Now, I want to split this main table into separate tables for each sensor ID, and I am having trouble figuring out a way to do it. I imagine I could do it with a loop, where my I cycle through the various IDs, but that doesn't seem like a very, MATLAB way of doing it.
What would be an efficient way to complete this task? Or would a loop really be the only way?
I have attached a small screenshot of some of my data.
The screenshot you shared shows a 1244x1 structure array with 2 fields but the question describes a table. You could convert the structure array to a table using,
T = struct2table(S); % Assuming S is the name of your structure
Whether the variable is a structure or table, it's better to not separate the variable and to use indexing instead. For example, assuming the variable is a table, you can compute the mean voltage for sensor1 using,
mean(T.reported_voltage(strcmp(T.sensor_id,'Sensor1')))
and you could report the mean of all groups using,
groupsummary(T,'sensor_id', 'mean')
or
splitapply(#mean,T.reported_voltage,findgroups(T.sensor_id))
But if you absolutely must break apart and tidy, well-organized table, you can do so by splitting the table into sub-tables stored within a cell array using,
unqSensorID = unique(T.sensor_id);
C = arrayfun(#(id){T(strcmp(T.sensor_id, id),:)},unqSensorID)
In this case the for loop is fine because (I guess) there aren't that many different sensors and your code will likely spend most of its time processing the data anyway - the loop won't give you a significant overhead.
Assuming your table is called t, the following should do what you want.
unique_sensors = unique(t.sensor_id)
for i = 1:length(unique_sensors)
sensor_data = t(t.sensor_id == unique_sensors(i), :);
% save or do some processing on this data
end

Removing non-matching dates from two time series in matlab

I have two time series x and y which roughly cover the same period of time. The data is in daily form however there are some days that have data in one dataset but no data in the other. I wish to use matlab to create two data-sets of equal size with matching dates. Essentially I wish to remove the days that don't have data in both x and y. Is there a simple way to do this? Thanks.
You could use an inner join see help join if you are able to convert your timeseries into datasets. If not you could use the ismember function, but this time you should do it only on the dates.
Something like this will work:
a = {'2015-01-01', '2015-02-02', '2015-03-03'};
b = {'2015-01-01', '2015-03-03', '2015-04-04'};
newA = a(ismember(a,b));
newB = b(ismember(b,a));

Apply a function iteratively to pieces of arrays

So I have this cute pretty function which requires two input: price vector and date vector:
function [tsve, timeint, dbin,adjtsve, stringdate,rvavg,rv ] = TSVE( price,datetime )
Now I have the vectors price and datetime covering a long period let say 20days and I want to compute the previous function over each day. The number of observations for each day is random but I can easily manage to find the last observation of the day.
date=floor(datetime);
[a, b, c]=unique(date,'legacy'); <----This gives me the index for the last obs of the day
[~, nbin]=histc(date,a,1); <----I was thinking about accumarray so I made this, but I am not sure I can use it on 2 values simultaneously
So I was thinking, is there a way to run a function (iteratively) on each piece of the two vectors? or should I split my two vectors into pieces and run the function on each piece?
Thank you all in advance

Time series generation matlab

I have an excel worksheet with several entries of column data. The data is arranged in pairs such that the first column contains dates and the second contains time series data corresponding to that date. So for example time series 1 will be in columns A and B where is is the dates and B is the data. Column C is blank before columns D and E contain the entries for time series 2 and so on and so forth...
How do I merge these into a single file in Matlab where the dates match up? Specifically I would want the first column to contain the dates and the other columns to contain the data. I have tried to do this with fts and merge functions but so far failed..
You could grab the dates like this: dates = [raw{:,1}]' and the data like this data = reshape([raw{:, 2:3:end}]', size(raw,1), []); to get normal matlab matrices in case you want to manipulate them in matlab.
Otherwise if you just want to send them straight back to excel then:
data = [raw(:,1) reshape(raw(:, 2:3:end)];
xlswrite(...blablafilename_etc..., data);
But in this case you should have use a VBA macro :/

Perl - determining the intersection of several numeric ranges

I would like to be able to load long list of positive integer ranges and create a new "summary" range list that is the union of the intersections of each pairs of ranges. And, I want to do this in Perl. For example:
Sample ranges: (1..30) (45..90) (15..34) (92..100)
Intersection of ranges: (15..30)
The only way I could think of was using a bunch of nested if statements to determine the starting point of sample A, sample B, sample C, etc. and figure out the overlap this way, but it's not possible to do that with hundreds of sample, each containing numerous ranges.
Any suggestions are appreciated!
The first thing you should do when you need to do some thing is take a look at CPAN to see what tools are available of if someone has solved your problem for you already.
Set::IntSpan and Set::IntRange are on the first page of results for "set" on CPAN.
What you want is the union of the intersection of each pair of ranges, so the algorithm is as follows:
Create an empty result set.
Create a set for each range.
For each set in the list,
For each later set in the list,
Find the intersection of those two sets.
Find the union of the result set and this intersection. This is the new result set.
Enumerate the elements of the result set.
I don't have code to share, but I would expand each range into hash, or use a Set module, and then use intersection operations on the sets.