From: Sean Douglas on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <acb556c3-dfaf-4395-bb0f-19659c615c04(a)j9g2000vbp.googlegroups.com>...
> Sean:
> Simply adjust your kernel:
>
> % Get smoothing kernel for the convolution.
> % Make it half zeros, a 1 for the center, and half 1's on the right.
> % Remember convolution flips the kernel prior to shifting and
> multiplying.
> % That's why the 1's are on the right (future) and not on the left
> (past).
> halfWindowWidth = 74;
> kernel = [zeros(halfWindowWidth,1); 1; ones(halfWindowWidth, 1)] /
> (halfWindowWidth + 1);
>

> % Kernel is now 149 long - 74 0's, a 1, and 75 1's for a total of 75
> that will be averaged together.


Thanks, I have been trying to figure this out since you sent it me. I am really having trouble understanding your last statement and code. Can you please tell me what is the meaning of (halfwindowwidth,1) if you give that maybe it will help clear this up a little. I can not find anything on "halfwindowwidth"?
thanks
From: ImageAnalyst on
Sean:
Every element in the window gets multiplied by the corresponding
element in the kernel (flipped left to right if using conv() but not
if using filter()). Then for the output array, the pixel
corresponding to the center pixel is replaced by the sum of all those
products. So if I had 149 1's in the kernel, the output pixel
corresponding to the element #75 would get replaced by the sum of all
149 elements of the original vector. But you wanted the average, so
to get the average you'd have to divide by 149 so you'd get this:
(output at 75) = ( (element 1)*1 + (element 2)*1 + ....... (element
149)*1) / 149;
So that gives the average in the window doesn't it?

But what if you didn't want the "future" in your stock series to be
included in the average. You wanted to look only backwards, or to the
left in the vector when you're summing your products. So what you
want is then this:
(output at 75) = ( (element 1)*1 + (element 2)*1 + ....... (element
75)*1 + (element 76)*0 + (element 77)*0 + ...(element 149)*0) / 75;
This time we're dividing by 75 to get the average because there are
only 75 elements that get summed.

You want a kernel that's 1 for the center day, and 1 for all 74 other
"prior" days, and 0 for all 74 future days. So we need a kernel
that's [1 1 1 1 1.....1 1 1 1 0 0 0 0 0 ...0] with 75 ones and 74
zeros. (That last 1 I showed is the center element.) Usually you
have an odd number of elements in a kernel so that the kernel is
symmetric and centered on the output pixel. So in this case you have
74 pixels on either side, plus a 1 in the center giving a total of
75. The total width is 149 but the half width is 74 (well, it's half
not counting the center element). Now the only issue is that (for
reasons I won't go into here) the kernel in a convolution gets flipped
right to left before multiplying and summing. So if you want ones on
the left half, you have to pass in a kernel with ones on the right
half.

I hope that explains it.
-ImageAnalyst