0

我有一个问题,在 Matlab 中,我有一个包含 20 年每日数据 (X) 的向量和一个相关日期 (DATES) 的向量。为了找到每年每日数据的平均值,我使用以下脚本:

A = fints(DATES,X);                          %convert to financial time series
B = toannual(A,'CalcMethod', 'SimpAvg');     %calculate average value per year
C = fts2mat(B);                              %Convert fts object to vector

C 是一个包含 20 个值的向量。显示 20 年中每一年的每日数据的平均值。到目前为止,一切都很好..现在我正在尝试做同样的事情,但不是每年计算平均值,而是每年计算标准值,但似乎没有函数“toannual”这样的选项。

关于如何做到这一点的任何想法?

先感谢您

4

1 回答 1

1

I'm assuming that X is the financial information and it is an even distribution across each year. You'll have to modify this if that isn't the case. Just to clarify, by even distribution, I mean that if there are 20 years and X has 200 values, each year has 10 values to it.

You should be able to do something like this:

num_years = length(C);
span_size = length(X)/num_years;
for n = 0:num_years-1
    std_dev(n+1,1) = std(X(1+(n*span_size):(n+1)*span_size));
end

The idea is that you simply pass the date for the given year (the day to day values) into matlab's standard deviation function. That will return the std-dev for that year. std_dev should be a column vector that correlates 1:1 with your C vector of yearly averages.


unique_Dates = unique(DATES) %This should return a vector of 20 elements since you have 20 years.
std_dev = zeros(size(unique_Dates)); %Just pre allocating the standard deviation vector.
for n = 1:length(unique_Dates)
    std_dev(n) = std(X(DATES==unique_Dates(n)));
end

Now this is assuming that your DATES matrix is passable to the unique function and that it will return the expected list of dates. If you have the dates in a numeric form I know this will work, I'm just concerned about the dates being in a string form.

In the event they are in a string form you can look at using regexp to parse the information and replace matching dates with a numeric identifier and use the above code. Or you can take the basic theory behind this and adapt it to what works best for you!

于 2012-08-01T14:38:25.233 回答