moving average and errors - Matlab -
i have series of data x,y , trying find moving average. x data numbers integers 1 100 while y data numbers 0.01 1 , have standard deviation y_dev (which derive because experiment repeated several times). trying find moving average using 20 closest neighbors (using matlab):
num_data=length(x) mov_average=y i=11,num_data-10 % leave data in edges same ind1(i)=i-10 ind2(i)=i+10 mov_average(i)=mean(y(ind1(i):ind2(i))); end
the above way derives moving average not know how use standard deviation have each y data point because data points have larger standard deviations others means not reliable others (so weigh less). how can include standard deviation each data point in above calculation?
thank you.
say have vector a
. way of writing mean(a)
weighted average a*wts'
, wts = ones(1,numel(a))/numel(a)
. in case, have a = y(ind1(i):ind2(i))
.
it sounds you're wanting use weighted moving average, weights wts
no longer identical, chosen using standard deviation of corresponding values.
assuming vector sd
holds standard deviations, here's 1 way of doing this:
num_data=length(x) mov_average=y i=11,num_data-10 ind1(i)=i-10 ind2(i)=i+10 sds = 1./sd(ind1(i):ind2(i)); % smaller sd -> larger weight wts = sds./sum(sds); % weights should sum 1 mov_average(i) = y*wts'; end
here, values smaller standard deviations contribute larger weights.
an alternative idea calculate simple moving average of both y
, standard deviations sd
, , plot them alongside 1 another.
wts = ones(1,10)/10; y_mean = conv(y, wts, 'valid'); % moving avg of y y_lb = y + conv(sd, wts, 'valid'); % moving avg of lower bound on y y_ub = y - conv(sd, wts, 'valid'); % moving avg of upper bound on y
this has advantage of being more statistically interpretable choosing weights function of standard deviations.
Comments
Post a Comment