MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Speed of repeated matrix calculation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49783] Re: [mg49717] Speed of repeated matrix calculation
  • From: Gregory Lypny <gregory.lypny at videotron.ca>
  • Date: Sat, 31 Jul 2004 03:14:02 -0400 (EDT)
  • References: <200407291143.HAA10250@smc.vnet.net> <opsbxiicf5iz9bcq@monster.cox-internet.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Hello Dr. Bob,

I'm using version 5.0.0.

The repeated quadratic form (portfolio variance calculations) is quick 
at .43 seconds.  My exact notation is

varianceRp = Table[{x[[i, All]].V.x[[i, All]]}, {i, 1, totalObs}],

where totalObs is 7,320.  The problem is with that pesky repeated dot 
product (portfolio return calculations).  I use

Rp = Table[{x[[i, All]].r[[i, All]]}, {i, 1, totalObs}] or

Rp2 = Table[{x[[i]].r[[i]]}, {i, 1, totalObs}], and, in either case, it 
takes 128.99 seconds!

Do you think it's the format of my data.  Here are the first five rows 
of x and r:

x
\!\({{3\/10, 3\/10, 2\/5}, {0.3183977497466464`, 0.3638831425675959`, \
0.3177190712974436`}, {0.15984884693034268`, 0.6393953877213707`, \
0.20075558631757812`}, {0.28134163384933736`, 0.422012450774006`, \
0.29664596320473446`}, {0.2333851448354868`, 0.33340734976498115`, \
0.4332074073777712`}}\)

r
{{0.0613669, 0.120612, 0.111651}, {0.0371958,
     0.0875982, 0.297405}, {-0.0165332, 0.141302,
     0.242225}, {0.0174065, -0.0236906, 0.201915}, {0.0281807, 
0.00599887,
     0.227316}}

Any thoughts?

	Regards,

		Gregory

On Jul 29, 2004, at 10:28 PM, DrBob wrote:

> Here are three ways to do it, with timings for a smaller problem:
>
> n=1000;
> x=Array[f,{n,3}];
> v=Array[h,{3,3}];
> Timing[a=Tr[x.v.Transpose[x],List];]
> Timing[b=Table[x[[i]].v.x[[i]],{i,1,n}];]
> Timing[c=(#.v.#&)/@x;]
> a == b == c
>
> {5.594 Second,Null}
>
> {0.031 Second,Null}
>
> {0.032 Second,Null}
>
> True
>
> The first method clearly isn't competitive. Eliminating it and 
> increasing the problem size, I get:
>
> n=7000;
> x=Array[f,{n,3}];
> v=Array[h,{3,3}];
> Timing[b=Table[x[[i]].v.x[[i]],{i,1,n}];]
> Timing[c=(#.v.#&)/@x;]
> b == c
>
> {0.234 Second,Null}
>
> {0.203 Second,Null}
>
> True
>
> For the other problem, the following methods are very fast.
>
> n=7000;
> x=Array[f,{n,3}];
> y=Array[g,{n,3}];
> v=Array[h,{3,3}];
> Timing[a=Table[x[[i]].y[[i]],{i,1,n}];]
> Timing[b=Inner[Dot,dum@@x,dum@@y,List];]
> a == b
>
> {0.093 Second,Null}
>
> {0.063 Second,Null}
>
> True
>
> But the first is really the same as your method; are you using an 
> older version of Mathematica, perhaps?
>
> Bobby
>
> On Thu, 29 Jul 2004 07:43:39 -0400 (EDT), Gregory Lypny 
> <gregory.lypny at videotron.ca> wrote:
>
>> Hello everyone,
>>
>> Just curious to know whether I'm doing this repeated matrix 
>> calculation
>> efficiently by using the Table command.
>>
>> I've got matrices x and y, which are both 7000x3, and a matrix V which
>> is 3x3.
>>
>> If I create a 7000x1 vector, q, whose elements are equal to each of 
>> the
>> row quadratic forms x.V.x, I can do it by creating a table.
>>
>> q = Table[{x[[i, All]].V.x[[i, All]]}, {i, 1, 7000}];
>>
>> The calculation is done in a split second on a G4 iBook.
>>
>> However, if I use Table to create a 7000x1 vector, d, of the dot
>> products of the rows of x and y, the calculation takes more than two
>> minutes!
>>
>> d = Table[{x[[i, All]].y[[i, All]]}, {i, 1, 7000}];
>>
>> Why the big difference in time?  Is there a better way?
>>
>> 	Greg
>>
>>
>>
>
>
>
> -- 
> DrBob at bigfoot.com
> www.eclecticdreams.net
>


  • Prev by Date: Re: ListDensityPlot (solution, and critical comment))
  • Next by Date: Re: Question on Compile[]
  • Previous by thread: Re: Speed of repeated matrix calculation
  • Next by thread: Re: Speed of repeated matrix calculation