Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #4855 > unrolled thread
| Started by | kristoph <kristophs.post@web.de> |
|---|---|
| First post | 2011-09-10 11:29 +0000 |
| Last post | 2011-09-13 11:27 +0000 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
help to make code run faster (mathematica v8.01) kristoph <kristophs.post@web.de> - 2011-09-10 11:29 +0000
Re: help to make code run faster (mathematica v8.01) Ray Koopman <koopman@sfu.ca> - 2011-09-11 11:33 +0000
Re: help to make code run faster (mathematica v8.01) Ray Koopman <koopman@sfu.ca> - 2011-09-12 08:29 +0000
Re: help to make code run faster (mathematica v8.01) kristoph <kristophs.post@web.de> - 2011-09-13 11:27 +0000
| From | kristoph <kristophs.post@web.de> |
|---|---|
| Date | 2011-09-10 11:29 +0000 |
| Subject | help to make code run faster (mathematica v8.01) |
| Message-ID | <j4fhn1$2em$1@smc.vnet.net> |
Hi
I'm running out of options in order to make my code run faster. I do
appreciate any help. I programmed a function named tStat that
basically sums over two compiled functions. Although, I run the
function parallel it is still rather slow. Thanks in advance for help.
Here is what I mean:
(*the following 5 lines is just random input data to test the
function*)
resp=RandomReal[10,250];
reg=RandomReal[1,250];
des=DesignMatrix[Table[{reg[[i]],resp[[i]]},{i,1,Length[reg]}],x,x];
fit=LinearModelFit[{des,resp}];
h=1.06 StandardDeviation[reg] Length[reg]^(-1/5);
(*the two compiled functions which are inputs for the function tStat*)
epanKern=Compile[{u},
If[Abs[u]<1,3/4 (1-u^2),0]
];
val1=Compile[{{rk,_Real},{rj,_Real},{dk,_Real},{dj,_Real},
{band,_Real}},
rk rj epanKern[(dk-dj)/band]];
(*the following function is rather slow*)
tStat[data_,band_,residuals_,leg_]:=Module[{k,j,res=0,var=0},
res=ParallelSum[val1[residuals[[k]],residuals[[j]],data[[k]],data[[j]],band],
{k,1,leg},{j,k+1,leg}];
2 res
];
(*executing the function*)
tStat[reg,h,fit["FitResiduals"],Length[reg]]//AbsoluteTiming
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-09-11 11:33 +0000 |
| Message-ID | <j4i6a7$c0a$1@smc.vnet.net> |
| In reply to | #4855 |
On Sep 10, 4:29 am, kristoph <kristophs.p...@web.de> wrote:
> Hi
>
> I'm running out of options in order to make my code run faster. I do
> appreciate any help. I programmed a function named tStat that
> basically sums over two compiled functions. Although, I run the
> function parallel it is still rather slow. Thanks in advance for help.
> Here is what I mean:
>
> (*the following 5 lines is just random input data to test the
> function*)
>
> resp=RandomReal[10,250];
> reg=RandomReal[1,250];
> des=DesignMatrix[Table[{reg[[i]],resp[[i]]},{i,1,Length[reg]}],x,x];
> fit=LinearModelFit[{des,resp}];
> h=1.06 StandardDeviation[reg] Length[reg]^(-1/5);
>
> (*the two compiled functions which are inputs for the function tStat*)
>
> epanKern=Compile[{u},
> If[Abs[u]<1,3/4 (1-u^2),0]
> ];
>
> val1=Compile[{{rk,_Real},{rj,_Real},{dk,_Real},{dj,_Real},
> {band,_Real}},
> rk rj epanKern[(dk-dj)/band]];
>
> (*the following function is rather slow*)
>
> tStat[data_,band_,residuals_,leg_]:=Module[{k,j,res=0,var=0},
> res=ParallelSum[val1[residuals[[k]],residuals[[j]],data[[k]],data[[j]],band],
> {k,1,leg},{j,k+1,leg}];
> 2 res
> ];
>
> (*executing the function*)
> tStat[reg,h,fit["FitResiduals"],Length[reg]]//AbsoluteTiming
Think in terms of vectors:
1.5 Times @@ Transpose@Subsets[fit["FitResiduals"],{2}] .
Clip[1. - ((Subtract @@ Transpose@Subsets[reg,{2}])/h)^2, {0.,1.}]
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-09-12 08:29 +0000 |
| Message-ID | <j4kftd$kic$1@smc.vnet.net> |
| In reply to | #4863 |
On Sep 11, 4:33 am, Ray Koopman <koop...@sfu.ca> wrote:
>
> Think in terms of vectors:
>
> 1.5 Times @@ Transpose@Subsets[fit["FitResiduals"],{2}] .
> Clip[1. - ((Subtract @@ Transpose@Subsets[reg,{2}])/h)^2, {0.,1.}]
This is about 10% faster. There are several variants of this that are
just about as fast, so experiment a little.
(-1.5/h^2) Times @@ Transpose@Subsets[fit["FitResiduals"],{2}] .
Clip[(Subsets[reg,{2}].{1.,-1.})^2 - h^2, {-h^2,0.}]
[toc] | [prev] | [next] | [standalone]
| From | kristoph <kristophs.post@web.de> |
|---|---|
| Date | 2011-09-13 11:27 +0000 |
| Message-ID | <j4nemt$53s$1@smc.vnet.net> |
| In reply to | #4863 |
On 11 Sep., 13:33, Ray Koopman <koop...@sfu.ca> wrote:
> On Sep 10, 4:29 am, kristoph <kristophs.p...@web.de> wrote:
>
>
>
>
>
>
>
>
>
> > Hi
>
> > I'm running out of options in order to make my code run faster. I do
> > appreciate any help. I programmed a function named tStat that
> > basically sums over two compiled functions. Although, I run the
> > function parallel it is still rather slow. Thanks in advance for help.
> > Here is what I mean:
>
> > (*the following 5 lines is just random input data to test the
> > function*)
>
> > resp=RandomReal[10,250];
> > reg=RandomReal[1,250];
> > des=DesignMatrix[Table[{reg[[i]],resp[[i]]},{i,1,Length[reg]}],x,x];
> > fit=LinearModelFit[{des,resp}];
> > h=1.06 StandardDeviation[reg] Length[reg]^(-1/5);
>
> > (*the two compiled functions which are inputs for the function tStat*)
>
> > epanKern=Compile[{u},
> > If[Abs[u]<1,3/4 (1-u^2),0]
> > ];
>
> > val1=Compile[{{rk,_Real},{rj,_Real},{dk,_Real},{dj,_Real},
> > {band,_Real}},
> > rk rj epanKern[(dk-dj)/band]];
>
> > (*the following function is rather slow*)
>
> > tStat[data_,band_,residuals_,leg_]:=Module[{k,j,res=0,var=0},
> > res=ParallelSum[val1[residuals[[k]],residuals[[j]],data[[k]],data[[j]=
],band ],
> > {k,1,leg},{j,k+1,leg}];
> > 2 res
> > ];
>
> > (*executing the function*)
> > tStat[reg,h,fit["FitResiduals"],Length[reg]]//AbsoluteTiming
>
> Think in terms of vectors:
>
> 1.5 Times @@ Transpose@Subsets[fit["FitResiduals"],{2}] .
> Clip[1. - ((Subtract @@ Transpose@Subsets[reg,{2}])/h)^2, {0.,1.}]
I tried vectors but not as nice and inspiring as you proposed. Thank
you very much.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web