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

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.

Related

How to define date column and plot that date column as x axis in R studio

I have half-hourly time series data; 48 data points in a day. The total time period starts from 4/6/2018 (i.e day/month/year) to 2/12/2018. I want to create a time series object. I am using ts function.
However, I am unable to define the frequency. As I know, frequency=1 for yearly data, but unable to define in my half-hourly data. How to solve this problem using ts object or there is any other way to do it?
After creating a time series object (i.e. date column), I want to plot it with the corresponding data series.
How to plot my result (in R object) on Y-axis and time period on the X-axis?
R <- function(x){
return(FDWhittle(x, method="discrete", sdf.method="multitaper"))
}
plot(R[,i],type="l",col = "Black", xlab="Time",
ylab="Return",main=names(R)[,i])
When I am plotting by using the above code, the results in the object "R" is coming on the Y-axis, and some values like (100, 200, 300--------) are coming on the X-axis. I want to get the time period on my X axis, but unable to define the intra-day date column having 48 data points in a day.

How to get monthly totals from linearly interpolated data

I am working with a data set of 10,000s of variables which have been repeatedly measured since the 1980s. The first meassurements for each variable are not on the same date and the variables are irregularly measured - sometimes measurements are only a month apart, in a small number of cases they are decades apart.
I want to get the change in each variable per month.
So far I have a cell of dates of measurements,and interpolated rates of change between measurements (each cell represents a single variable in either, and I've only posted the first 5 cells in each array)
DateNumss= {[736614;736641;736669] [736636;736666] 736672 [736631;736659;736685] 736686}
LinearInterpss={[17.7777777777778;20.7142857142857;0] [0.200000000000000;0] 0 [2.57142857142857;2.80769230769231;0]}
How do I get monthly sums of the interpolated change in variable?
i.e.
If the first measurement for a variable is made on the January 1st, and the linearly interpolated change between that an the next measurement is 1 per day; and the next measurement is on Febuary the 5th and the corresponding linearly interpolated change is 2; then January has a total change of 1*31 (31 days at 1) and febuary has a total change of 1*5+2*23 (5 days at 1, 23 days at 2).
You would need the points in the serial dates that correspond with the change of a month.
mat(:,1)=sort(repmat(1980:1989,[1,12]));
mat(:,2)=repmat(1:12,[1,size(mat,1)/12]);
mat(:,3)=1;
monthseps=datenum(mat);
This gives you a list of all 120 changes of months in the eighties.
Now you want, for each month, the change per day, and sum it. If you take the original data it is easier, since you can just interpolate each day's value using matlab. If you only have the "LinearInterpss" you need to map it on the days using interp1 with the method 'previous'.
for ct = 2:length(monthseps)
days = monthseps(ct-1):(monthseps(ct)-1); %days in the month
%now we need each day assigned a certain change. This value depends on your "LinearInterpss". interp1 with method 'previous' searches LineairInterpss for the last value.
vals = interp1(DateNumss,LinearInterpss,days,'previous');
sum(vals); %the sum over the change in each day is the total change in a month
end

How to draw time based graphs using ios-charts

I'm trying to draw a temperature graph using iso-charts where the x axis data would be set from a server timestamp but the labels would be readable text.
For instance the graph x-axis label would start at Monday 00:00 and end Tuesday 12pm but the LineChartDataSet would be a collection of temperature (y-axis) and timestamps for the x
To display the timestamp I have a custom valueFormatter set as follow (which works great)
lineChartView.xAxis.valueFormatter = timestampXAxisFormatter() //converts timestamp to Date string
My question: The LineChartDataSet seems to be indexed based which is causing some trouble: if I have 4 data points such as (9am, 10), (9:15am, 11), (12pm, 15), (1pm, 16) the 4 points are set in the chart at regular intervals (I was expecting 2 points to be on the left side of the graph and then last 2 points on the right side) - Is there a way to have a data set that is based on the x value instead of the index?
I saw ChartData has an init that takes an array of NSObjects but then it converts it to Strings...Thanks in advance for any suggestions you may have!
There is no good way to solve it, as you figured out the x axis is index based.
You have two options:
insert many x values between each real x value, like between 9:00 and 9:15, you manually insert 9:01, 9:02, ..., 9:14, but don't add any entry at these values, just ignore it and continue. ios-charts will skip if no entry found and go to next. This will works fine, if you don't have a large number of values to insert. I tried ~1000 values, the performance is acceptable.
you create your own chart, using two y axis, one as x axis and one as y axis, so the distances to 0 point are calculated by value. However this requires you understand the ios-chart logic deeply. If you succeed, you are more than welcome to file a PR.

Plotting multiple datasets in MATLAB

I have voltage and current signals from multiple days. The time vector is in seconds of the day (SOD), and the voltage and current vectors are in volts and amps respectively. However, the vector data from each day is different lengths. For example Mondays data might be 1x100000 for both time and voltage/current, and Tuesdays might be 1x50000 for both time and voltage/current. I was asked to plot the different days of data on the same figure for comparison purposes. I have tried using the plot(x1,y1,x2,y2) method but that obviously didn't work due to different vector lengths. I tried interpolating to the larger data set, but then realized that I will get all NaNs on the result since there is no overlap in time. I ran out of ideas and am desperately in need of help.
EDIT:
I guess I forgot to mention that somehow I would like to overlay them one on top of the other in the same figure and not using a subplot.
It sounds like you want a data vector of length n to span, I'm guessing, 24 hours = 86400 seconds, for any n (e.g. n=100000 or n=50000). Assuming the original data is uniformly sampled, this should do the trick:
x1=linspace(0,86400,length(x1));
x2=linspace(0,86400,length(x2));
plot(x1,y1,'r-',x2,y2,'b-');
If it is not uniformly sampled, we can still make it work:
t1=linspace(0,86400,length(x1));
t2=linspace(0,86400,length(x2));
newy1 = spline(x1,y1,t1);
newy2 = spline(x2,y2,t2);
plot(t1,newy1,'r-',t2,newy2,'b-');

Finding rolling z-score in Matlab

I want to calculate z-score of the current point in cross-sectional time-series data based on standard deviation over the last 10 days and simple moving average over the last 10 days. I can't use the z-score function in Matlab as it looks forward to calculate the z-score. Currently my solution is
for i=11:length(equity.(1))
z(i) = (x(i)-mean(x(i-10:i))/std(x(i-10:i);
end
but issue is that i want to do this for the entire dataset at once. Is there a way to handle the entire matrix at once and calculate z-score for a given look back period (10 days in my case).
Whether this is in fact more efficient or not I don't know, but one way (im2col requires the image processing toolbox):
data = 1:40; %dummy data
% presuming "ten days" means day of interest + 9 days back
n = 10;
data2 = im2col(data,[1,n],'sliding');
%mean/std for each column:
dmean = mean(data2);
dstd = std(data2);
z = (data(n:end)-dmean)./dstd;
You might also try this from the file exchange.