COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### calculating returns

• Follow

```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
```
 0

```On Aug 3, 11:28=A0am, "Peter Smith" <fe0...@mail.wbs.ac.uk> wrote:
> Hi all
>
> I want to calculate squared returns from data using a formular similar to
>
> squaredreturn=3D(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=3D1 loop, so i cannot simply start th=
e loop from k>=3D2
>
> Can any1 offer any suggestions
> Many thanks

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

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

Note the dot before the ^.  This is essential.
```
 0
Reply mulgor (2841) 8/2/2010 11:33:45 PM

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

```TideMan <mulgor@gmail.com> wrote in message <c5309e34-e2ed-4e85-8d7a-2e418c8d0a1a@x20g2000pro.googlegroups.com>...
> On Aug 3, 11:28 am, "Peter Smith" <fe0...@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.
```
 0

```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

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
```
 0

4 Replies
241 Views

Similiar Articles:

7/30/2012 4:22:13 AM