help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: speed of octave interpreter, changed to vecorization


From: Henry F. Mollet
Subject: Re: speed of octave interpreter, changed to vecorization
Date: Tue, 27 Sep 2005 18:54:32 -0700
User-agent: Microsoft-Entourage/11.1.0.040913

I'm working on least square fitting. Below is for linear model function y =
b1 + b2*x. I can calculate the loss function (aka sum of squared residuals)

loss(i,j)=resid'*resid;

in parameter space using nested for loops but I'd like to calculate the loss
function with a meshgrid.  How can I do this? (I'd like to graph contour
plots of the loss function in parameter space.)

The 'for' loop for 101x101 =~10+4 calculations took
elapsed_time = 2.1996
cpu_time = 2.0928

My apparently feeble effort to use a meshgrid first failed (as per below)
and when I somehow was able to use a meshgrid (not shown) it took
elapsed_time = 0.50147
cpu_time = 0.30624
for only 3 out of the needed 101x101 = ~10+4 needed points. Thus there's no
point showing what I've tried.
Henry

% Nested for loops over b1 and b2 via integers i and j for calc of loss =
loss (b1,b2) in parameter space.

for i=1:101
    for j=1:101
    b1=0.0+(i-1)*(20.0-0.0)/100;
    b2=1.8+(j-1)*(2.2-1.8)/100;
    r=(Y(:)-b1-b2.*X(:));
    loss(i,j)=r'*r;
    end
end

% Now trying to calculate loss in parameter space using a meshgrid for a =
b1 and b = b2:
a=linspace(0,20,101);
b=linspace(1.8,2.2,101);
[aa,bb]= meshgrid (a,b);
LossViaMeshGrid = (Y(:) - aa - bb.*X(:))' * (Y(:) - aa - bb.*X(:));
%error: operator -: nonconformant arguments (op1 is 10x1, op2 is 101x101)
%error: evaluating binary operator `-' near line 67, column 24

% I can see that arguments of op1 and op2 don't agree but the number of data
points is a vector of length n, say, and the meshgrid has dimension mxm,
say. 



on 9/27/05 2:23 PM, Paul Thomas at address@hidden wrote:

> David,
> 
> It is also worth recalling the correspondence around that time about
> vectorization.  The slow tests can be speeded up by one or two orders of
> magnitude by replacing fortran77 style code with "vectorized" code.  I
> remind everybody of this excellent technical note from the other
> product, which applies just as well to octave:
> 
> http://www.mathworks.com/support/tech-notes/1100/1109.html
> 
> Paul T
> 
> 
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------
> 




-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]