Tips and tricks: Difference between revisions
(new page) |
m (minor touch-up) |
||
Line 3: | Line 3: | ||
It is sometimes irritating when a calculation takes very long and nothing appears to happen. In such cases a step monitor may be useful that indicates the state of the calculations. It is a simple gimmick. | It is sometimes irritating when a calculation takes very long and nothing appears to happen. In such cases a step monitor may be useful that indicates the state of the calculations. It is a simple gimmick. | ||
Take, as an example, a modification of the Listing 37.1: Finding the minimum of the Rosenbrock function with the BFGSmax routine given in Gretl's user guide. The <code>printf | Take, as an example, a modification of the Listing 37.1: Finding the minimum of the Rosenbrock function with the BFGSmax routine given in Gretl's user guide. The <code>printf</code> command prints the values of x and y and ends with a carriage return. The command <code>flush</code> induces printing at each step. To simulate a time consuming routine, a delay of .003 has been inserted by giving the command <code>sleep(.003)</code>. | ||
Open the the gretl script editor (File -> Script files ->new script) and paste the following code: | |||
<code># This defines the function to be maximized</code> | <code># This defines the function to be maximized</code> | ||
Line 24: | Line 24: | ||
<code>end function</code> | <code>end function</code> | ||
Line 42: | Line 43: | ||
<code>printf "\ntheta: %8.4f \n", theta</code> | <code>printf "\ntheta: %8.4f \n", theta</code> | ||
Now run the script (push the gears button or Ctrl+R). The successive estimates of x and y will be monitored. Other information can easily be monitored as well, the value of the criterion <code>-(1-x)^2 - 100 * (y - x^2)^2</code> for instance, by simply including it in the <code>printf</code> command in the Rosenbrook function. | |||
Now run the script (push the gears button or Ctrl+R). The successive estimates of x and y will be monitored. Other information can easily be monitored as well, the value of the criterion <code>-(1-x)^2 - 100 * (y - x^2)^2</code> for instance, by simply including it in the <code>printf</code> command in the Rosenbrook function. | Now run the script (push the gears button or Ctrl+R). The successive estimates of x and y will be monitored. Other information can easily be monitored as well, the value of the criterion <code>-(1-x)^2 - 100 * (y - x^2)^2</code> for instance, by simply including it in the <code>printf</code> command in the Rosenbrook function. |
Revision as of 14:43, 30 December 2022
Step monitor
It is sometimes irritating when a calculation takes very long and nothing appears to happen. In such cases a step monitor may be useful that indicates the state of the calculations. It is a simple gimmick.
Take, as an example, a modification of the Listing 37.1: Finding the minimum of the Rosenbrock function with the BFGSmax routine given in Gretl's user guide. The printf
command prints the values of x and y and ends with a carriage return. The command flush
induces printing at each step. To simulate a time consuming routine, a delay of .003 has been inserted by giving the command sleep(.003)
.
Open the the gretl script editor (File -> Script files ->new script) and paste the following code:
# This defines the function to be maximized
function scalar Rosenbrock( const matrix param "parameters" )
scalar x = param[1]
scalar y = param[2]
printf "x =%7.4f, y =%7.4f \r",x,y # the information printed at each step
flush # this induces immediate printing
sleep(.003) # this simulates a time-consuming process
return -(1-x)^2 - 100 * (y - x^2)^2
end function
# This invokes the maximization routine
matrix theta = {0, 0} # initial values for theta
set verbose off
set max_verbose off
M = BFGSmax(&theta, Rosenbrock(theta) )
printf " \r" # destroy the last step message
flush # flush it
printf "\ntheta: %8.4f \n", theta
Now run the script (push the gears button or Ctrl+R). The successive estimates of x and y will be monitored. Other information can easily be monitored as well, the value of the criterion -(1-x)^2 - 100 * (y - x^2)^2
for instance, by simply including it in the printf
command in the Rosenbrook function.
Now run the script (push the gears button or Ctrl+R). The successive estimates of x and y will be monitored. Other information can easily be monitored as well, the value of the criterion -(1-x)^2 - 100 * (y - x^2)^2
for instance, by simply including it in the printf
command in the Rosenbrook function.