所以我想构造一个不同权重的移动时间平均值,用于不同的月份。参见analysis/filtering-data.html中的filter函数,其中b=每个月中的天数,a=一年中的天数。
然而,问题是,时间序列是每个月的一系列温度(我想为每一组可能的年份建立一个年平均温度,例如,每年从3月到2月)。使用这种方法,每个窗口的第一个月将加权为31/365,而不论第一个月是1月还是6月。
在这种情况下,标准过滤算法将无法工作。还有别的选择吗?
结合闰年的解决方案也不错,但对于初始解决方案来说并不是必要的。
发布于 2013-08-18 14:38:18
加权平均值定义为sum(x .* weights) / sum(weights)
。如果您想以一种移动平均的方式计算这个值,我想您可以这样做(未经测试):
moving_sum = @(n, x) filter(ones(1,n), 1, x);
moving_weighted_avg = moving_sum(12, temperature .* days_per_month) ...
./ moving_sum(12, days_per_month);
如果temperature
是一个月温度向量,而days_per_month
包含相应月份的实际天数,那么在闰年情况下,这甚至是可行的。
编辑回复评论
您可以这样重构days_per_month
:
start_year = 2003;
start_month = 10;
nmonth = 130;
month_offset = 0:nmonth - 1;
month = mod(start_month + month_offset - 1, 12) + 1;
year = start_year + floor((start_month + month_offset - 1) / 12);
days_in_month = eomday(year, month);
disp([month_offset; year; month; days_in_month]') %print table to check
https://stackoverflow.com/questions/18302678
复制