Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Displaying Intermediate Iterations As They are Calculated

Subject: Displaying Intermediate Iterations As They are Calculated

From: Brian

Date: 19 Jan, 2012 14:15:10

Message: 1 of 3

I teach a graduate course in econometrics and have my students write their optimization code without using the optimization toolbox. For example they code out the Newton Raphson iterative procedure for estimating the parameters of a particular likelihood function. In the code I have developed, the iteration information is only shown in the command window once the iterations are completed. Is there any way to display each iteration individually as that particular iteration is completed. Here is the code that does the estimation. Remember I am not using the Optimization toolbox and in addition I am a new Matlab user.

                %******* Begin do loop *********
   while ((crit > critic_limit) & (iter<= iter_limit));
      z = Grad(b0,func_name,numobs); % Numerical Gradients
      H = z'*z; % Compute approx. to Hessian
      g = sum(z)'; % Gradient vector (K x 1)
      db = inv(H)*g; % Compute full step adjustment
      if do_step == 1; % Use a variable step length
       s = 2; % Reset base step length
       li1 = 0; li2 = 1; % ** Intialize LLF's under 2 step lengths
       while (li1 < li2) && (s >=.2); % ** Loop to determine step length
          s = s/2;
          li1 = sum(feval(func_name,(b0 + s*db))); %Eval. LLF at higher SL
          li2 = sum(feval(func_name,(b0 + s*db/2)));%Eval. LLF at lower SL
       end
     end
      betas = b0 + s*db; % Update parameter values
      diary on; % Turn on output to file
      fprintf('i = %3.0f\r', iter); % Print iteration number
      fprintf('LLF = %8.4f\r', sum(feval(func_name,b0))); % Print current LLF
      fprintf('step = %5.4f\r', s); % Print "current" step length
      fprintf('Paramaters \r'); % Print current paramater estimate
      vv=1; % Reset a counter to 1
     if length(betas)>7; % Do we have more than 7 parameters?
      numloop=floor(length(betas)./7); % How many group of 7 parameters?
            resid=length(betas)-numloop*7; % How many parameters
                                           % after the last group of 7
      while vv<=numloop; % Number of groups of 7
     disp(b0((vv-1)*7+1:vv*7,:)'); % Display 7 parameters
     vv=vv+1; % Increment numloop counter
            end
            if resid>0; % After the last group of 7 print out remaining
                disp(b0((vv-1)*7+1:rows(b0),:)');
                disp(' ')
                disp(' ')
            end
        else
         disp(b0'); % Print out all paramatrs if less than 7
        end
      iter = iter + 1; % Update iteration count
      crit = abs((betas-b0)./b0)./s; % Eval. size adjusted % parm change
      b0 = betas; % Replace old parm vector w/new
   end

Subject: Displaying Intermediate Iterations As They are Calculated

From: g.heath@verizon.net

Date: 20 Jan, 2012 05:07:53

Message: 2 of 3

On Jan 19, 9:15 am, "Brian " <bwgo...@wisc.edu> wrote:
> I teach a graduate course in econometrics and have my students write their optimization code without using the optimization toolbox.  For example they code out the Newton Raphson iterative procedure for estimating the parameters of a particular likelihood function.  In the code I have developed, the iteration information is only shown in the command window once the iterations are completed.  Is there any way to display each iteration individually as that particular iteration is completed.  Here is the code that does the estimation.  Remember I am not using the Optimization toolbox and in addition I am a new Matlab user.
>
>                 %******* Begin do loop *********
>    while ((crit > critic_limit) & (iter<= iter_limit));
>       z = Grad(b0,func_name,numobs);  % Numerical Gradients
>       H = z'*z;                       % Compute approx. to Hessian
>       g = sum(z)';                    % Gradient vector (K x 1)
>       db = inv(H)*g;                  % Compute full step adjustment
>       if do_step == 1;                % Use a variable step length
>        s = 2;                         % Reset base step length
>        li1 = 0; li2 = 1;              % ** Intialize LLF's under 2 step lengths
>        while (li1 < li2) && (s >=.2); % ** Loop to determine step length
>           s = s/2;
>           li1 = sum(feval(func_name,(b0 + s*db)));  %Eval. LLF at higher SL
>           li2 = sum(feval(func_name,(b0 + s*db/2)));%Eval. LLF at lower SL
>        end
>      end
>       betas = b0 + s*db;               % Update parameter values
>       diary on;                        % Turn on output to file
>       fprintf('i = %3.0f\r', iter);    % Print iteration number
>       fprintf('LLF = %8.4f\r', sum(feval(func_name,b0))); % Print current LLF
>       fprintf('step = %5.4f\r', s);    % Print "current" step length
>       fprintf('Paramaters \r');        % Print current paramater estimate
>       vv=1;                            % Reset a counter to 1
>         if length(betas)>7;            % Do we have more than 7 parameters?
>                 numloop=floor(length(betas)./7); % How many group of 7 parameters?
>             resid=length(betas)-numloop*7; % How many parameters
>                                            % after the last group of 7
>                 while vv<=numloop;             % Number of groups of 7
>                         disp(b0((vv-1)*7+1:vv*7,:)');  % Display 7 parameters
>                         vv=vv+1;                   % Increment numloop counter
>             end
>             if resid>0;     % After the last group of 7 print out remaining
>                 disp(b0((vv-1)*7+1:rows(b0),:)');
>                 disp(' ')
>                 disp(' ')
>             end
>         else
>          disp(b0');              % Print out all paramatrs if less than 7
>         end
>       iter = iter + 1;               % Update iteration count
>       crit = abs((betas-b0)./b0)./s; % Eval. size adjusted % parm change
>       b0 = betas;                    % Replace old parm vector w/new
>    end

If you remove a semicolon, you will get the resulting calculations
printed to the command line.

Hope this helps.

Greg

Subject: Displaying Intermediate Iterations As They are Calculated

From: ImageAnalyst

Date: 20 Jan, 2012 14:40:48

Message: 3 of 3

On Jan 20, 12:07 am, g.he...@verizon.net wrote:
> On Jan 19, 9:15 am, "Brian " <bwgo...@wisc.edu> wrote:
> If you remove a semicolon, you will get the resulting calculations
> printed to the command line.
>
> Hope this helps.
>
> Greg

-----------------------------------------------------
You can also say "echo on" if you want the code statement to also be
sent out to the command window. That can make it kind of cluttered
though.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us