From: Peter Smith on
Hi all

I want to calculate squared returns from data using a formular similar to

squaredreturn=(log(lastprice(k)-lastprice(k-1)))^2

but obviously there is a problem since there is no data for lastprice(0).
In addition i am already in a for k=1 loop, so i cannot simply start the loop from k>=2

Can any1 offer any suggestions
Many thanks
From: TideMan on
On Aug 3, 11:28 am, "Peter Smith" <fe0...(a)mail.wbs.ac.uk> wrote:
> Hi all
>
> I want to calculate squared returns from data using a formular similar to
>
> squaredreturn=(log(lastprice(k)-lastprice(k-1)))^2
>
> but obviously there is a problem since there is no data for lastprice(0).
> In addition i am already in a for k=1 loop, so i cannot simply start the loop from k>=2
>
> Can any1 offer any suggestions
> Many thanks

Well, your squaredreturn means nothing at k=1, so you're better to set
it to NaN.

But don't do it in the loop:
squaredreturn=(log(lastprice(2:end)-lastprice(1:end-1))).^2;
squaredreturn=[NaN;squaredreturn];

Note the dot before the ^. This is essential.
From: Peter Smith on
Also please note i cannot use ln(lastprice(k+1)-lastprice(k)) as eventually it reaches a point where there is no k+1
From: someone on
TideMan <mulgor(a)gmail.com> wrote in message <c5309e34-e2ed-4e85-8d7a-2e418c8d0a1a(a)x20g2000pro.googlegroups.com>...
> On Aug 3, 11:28 am, "Peter Smith" <fe0...(a)mail.wbs.ac.uk> wrote:
> > Hi all
> >
> > I want to calculate squared returns from data using a formular similar to
> >
> > squaredreturn=(log(lastprice(k)-lastprice(k-1)))^2
> >
> > but obviously there is a problem since there is no data for lastprice(0).
> > In addition i am already in a for k=1 loop, so i cannot simply start the loop from k>=2
> >
> > Can any1 offer any suggestions
> > Many thanks
>
> Well, your squaredreturn means nothing at k=1, so you're better to set
> it to NaN.
>
> But don't do it in the loop:
> squaredreturn=(log(lastprice(2:end)-lastprice(1:end-1))).^2;

% You can also use the built-in MATLAB function DIFF
% which will do exactly the same thing:

squaredreturn=(log(diff(lastprice))).^2;

% Slightly slower perhaps, but somewhat simplier?

> squaredreturn=[NaN;squaredreturn];
>
> Note the dot before the ^. This is essential.
From: Jan Simon on
Dear Peter,

> Also please note i cannot use ln(lastprice(k+1)-lastprice(k)) as eventually it reaches a point where there is no k+1

What about the trivial solution:
n = length(lastprice);
for k = 1:n
if k < n
x = ln(lastprice(k+1) - lastprice(k));
end
end

If a certain line should not be caclulated in the loop for a certain counter value, simply omit the calculation for this certain counter value.

Kind regards, Jan