Re: One-liners' performance
- To: mathgroup at smc.vnet.net
- Subject: [mg40746] Re: One-liners' performance
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Wed, 16 Apr 2003 01:34:53 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <b7geb0$dk4$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
and with
Lh1L = Function[{\[CapitalPi], \[Mu], \[Sigma], \[Xi]0, Y},
Module[{\[Nu], \[Zeta]1, \[Zeta]2, \[Zeta], Lo, \[Xi]o},
\[Nu] = Dimensions[\[CapitalPi]][[1]];
\[Xi] = Flatten[\[Xi]0];
\[Xi]1 = {};
\[Eta] =
Transpose[
Table[PDF[
NormalDistribution[\[Mu][[i, 1]], Sqrt[\[Sigma][[i,
1]]]],
Y], {i, 1, \[Nu]}]];
xx = Transpose[FoldList[({\[Xi]o, Lo} = #1;
\[Xi]o = Flatten[{\[Xi]o}.\[CapitalPi]];
\[Zeta] = \[Xi]o*#2;
\[Xi]o = Plus @@ \[Zeta];
{\[Zeta]/\[Xi]o, Lo + Log[\[Xi]o]}) &, {\[Xi],
0.}, \[Eta]]];
Ps = xx[[1]];
xx[[2, -1]]]];
that what you call a one-liner, is 25 % faster
Regards
Jens
Kyriakos Chourdakis wrote:
>
> Dear all,
>
> Having read the discussion about one-liners, and the
> suggestions that overall the code is typically faster
> if such an approach is taken, I decided to alter some
> of my existing code. The code computes the likelihood
> for regime switching models.
>
> I run some comparisons, and found that the
> ``one-liner'' code is consistenly slower than the code
> that loops using Do[]. My first guess is that my
> understanding of the one-liner code might be wrong,
> and I would appreciate any feedback.
>
> The code is given below: First a sample is generated,
> and likelihoods are computed. Then, 20 samples are
> computed. For a sample of 3000, the Do[] version is
> about 30% faster than the Fold[] one.
>
> I apologize for the messy Greeks.
>
> K.
>
> Quit[];
> << "Statistics`NormalDistribution`"
>
> (* the parameter vector *)
> theta = {0.05, 0.05, 0.5, -0.5, 1., 4.};
>
> (* Simulate one regime switching path *)
> Sample :=
> Module[
> {p11 = 1 - theta[[1]], p22 = 1 - theta[[2]]},
> StTF = NestList[If[#1, Random[] < p11, Random[] > p22]
> & , Random[] > 0.5, 3000];
> ST = (If[#1, 1, 0] & ) /@ StTF;
> STm = (If[#1, theta[[3]], theta[[4]]] & ) /@ StTF;
> STs = (If[#1, theta[[5]], theta[[6]]] & ) /@ StTF;
> n = Length[ST];
> STm + Sqrt[STs]*RandomArray[NormalDistribution[], {n}]
> - STs/2
> ];
>
> (* The original code *)
> Lh = Function[
> {\[CapitalPi], \[Mu], \[Sigma], \[Xi]0, Y},
> Module[
> {\[Nu], \[Zeta]1, \[Zeta]2, \[Zeta]},
> \[Nu] = Dimensions[\[CapitalPi]][[1]];
> \[Xi] = Transpose[\[Xi]0];
> \[Xi]1 = {};
> \[Eta] =
> Transpose[Table[PDF[NormalDistribution[\[Mu][[i,1]],
> Sqrt[\[Sigma][[i,1]]]], Y], {i, 1, \[Nu]}]];
> Do[
> \[Zeta]1 = Flatten[\[Xi][[-1]]];
> \[Zeta]2 = \[Eta][[i]];
> \[Zeta] = \[Zeta]1*\[Zeta]2;
> \[Zeta]0 = \[Zeta]/Plus @@ Flatten[\[Zeta]];
> \[Xi]1 = {\[Xi]1, \[Zeta]0};
> \[Xi] = {\[Xi], {\[Zeta]0} . \[CapitalPi]},
> {i, Length[Y]}
> ];
> \[Xi]1 = Partition[Flatten[\[Xi]1], \[Nu]];
> \[Xi] = Partition[Flatten[\[Xi]], \[Nu]];
> Plus @@ Log[Apply[Plus, Delete[\[Xi], -1]*\[Eta],
> {1}]]
> ]
> ];
>
> (* The one-liner type code *)
> Lh1L = Function[
> {\[CapitalPi], \[Mu], \[Sigma], \[Xi]0, Y},
> Module[
> {\[Nu], \[Zeta]1, \[Zeta]2, \[Zeta]},
> \[Nu] = Dimensions[\[CapitalPi]][[1]];
> \[Xi] = Flatten[\[Xi]0];
> \[Xi]1 = {};
> \[Eta] =
> Transpose[Table[PDF[NormalDistribution[\[Mu][[i,1]],
> Sqrt[\[Sigma][[i,1]]]], Y], {i, 1, \[Nu]}]];
> xx = Transpose[FoldList[
> Module[{Lo, \[Xi]o, \[Zeta]},
> {\[Xi]o, Lo} = #1;
> \[Xi]o = Flatten[{\[Xi]o} . \[CapitalPi]];
> \[Zeta] = \[Xi]o*#2;
> \[Xi]o = Plus @@ \[Zeta];
> {\[Zeta]/\[Xi]o, Lo + Log[\[Xi]o]}] & ,
> {\[Xi], 0.}, \[Eta]]];
> Ps = xx[[1]];
> xx[[2,-1]]
> ]
> ];
>
> (* Some wrappers *)
> ToEst[x_, Y_] := Lh[
> {{1 - x[[1]], x[[1]]}, {x[[2]], 1 - x[[2]]}},
> {{x[[3]]}, {x[[4]]}},
> {{x[[5]]}, {x[[6]]}},
> {{x[[2]]/(x[[1]] + x[[2]])}, {x[[1]]/(x[[1]] +
> x[[2]])}},
> Y]
> ToEst1L[x_, Y_] := Lh1L[
> {{1 - x[[1]], x[[1]]}, {x[[2]], 1 - x[[2]]}},
> {{x[[3]]}, {x[[4]]}},
> {{x[[5]]}, {x[[6]]}},
> {{x[[2]]/(x[[1]] + x[[2]])}, {x[[1]]/(x[[1]] +
> x[[2]])}},
> Y]
>
> (* 20 simulations *)
> Do[
> Y = Sample;
> ET = Timing[ToEst[theta, Y]][[1]];
> ET1L = Timing[ToEst1L[theta, Y]][[1]];
> Print[{ET, ET1L, ET - ET1L}],
> {20}]
>
> __________________________________________________
> Yahoo! Plus
> For a better Internet experience
> http://www.yahoo.co.uk/btoffer