Tips and tricks: Difference between revisions
mNo edit summary |
No edit summary |
||
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
=== Step monitor === | === 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 | It is sometimes irritating when a calculation takes very long and nothing appears to happen. In such cases a step monitor may be useful to indicate 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</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>. | 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: | Open the the gretl script editor (File → Script files → New script) and paste the following code: | ||
<pre> | |||
set verbose off | |||
# 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 | |||
<code> | # This invokes the maximization routine | ||
matrix theta = {0, 0} # initial values for theta | |||
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 | |||
</pre> | |||
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. | |||
<code> | === "=" trick for eval === | ||
Sometimes you simply need to check the result of an expression in the Gretl console, such as 2+2. To do this you should use the <code>eval</code> command like this: | |||
<pre> | |||
? eval 2+2 | |||
4 | |||
</pre> | |||
Since without the command, gretl raises an error: | |||
<pre> | |||
? 2+2 | |||
Parse error at unexpected token '2' | |||
</pre> | |||
But <code>eval</code> is too long to type. You can use an <code>=</code> before the expression (as in a spreasheet) to get the same result as <code>eval</code>: | |||
<pre> | |||
? =2+2 | |||
4 | |||
</pre> | |||
< | === Backslash to split lines=== | ||
In some cases a line of code could be very long. Take for example the declaration of a matrix: | |||
<pre> | |||
matrix M = {1,2,3;4,5,6;7,8,9;10;11;12;13,14,15;16,17,18} | |||
</pre> | |||
It is difficult to read rows and columns. You can use the symbol <code>\</code> to split the line and improve readability. | |||
<pre> | |||
matrix M = { \ | |||
1,2,3; \ | |||
4,5,6; \ | |||
7,8,9; \ | |||
10,11,12; \ | |||
13,14,15; \ | |||
16,17,18} | |||
</pre> | |||
=== Execute portions of code === | |||
You can execute only a portion of code by highlighting it in the ''script editor'' window and running it with ''ctlr+r'' or the ''run'' button. | |||
<code> | === Custom ''Highlighting style'' === | ||
To use custom color highlighting in ''script window'' and '' console'' add the xml style file to <code>PATH/gtksourceview/</code>; where <code>PATH</code> is main Gretl directory. You can see it in ''Tools -> Preferences -> General''. | |||
After that, go to ''Tools -> Preferences -> Editor'' and select your file name in ''Highlighting style'' drop-down list. | |||
__FORCETOC__ | |||
Latest revision as of 21:51, 25 February 2023
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 to indicate 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:
set verbose off # 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 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.
"=" trick for eval
Sometimes you simply need to check the result of an expression in the Gretl console, such as 2+2. To do this you should use the eval
command like this:
? eval 2+2 4
Since without the command, gretl raises an error:
? 2+2 Parse error at unexpected token '2'
But eval
is too long to type. You can use an =
before the expression (as in a spreasheet) to get the same result as eval
:
? =2+2 4
Backslash to split lines
In some cases a line of code could be very long. Take for example the declaration of a matrix:
matrix M = {1,2,3;4,5,6;7,8,9;10;11;12;13,14,15;16,17,18}
It is difficult to read rows and columns. You can use the symbol \
to split the line and improve readability.
matrix M = { \ 1,2,3; \ 4,5,6; \ 7,8,9; \ 10,11,12; \ 13,14,15; \ 16,17,18}
Execute portions of code
You can execute only a portion of code by highlighting it in the script editor window and running it with ctlr+r or the run button.
Custom Highlighting style
To use custom color highlighting in script window and console add the xml style file to PATH/gtksourceview/
; where PATH
is main Gretl directory. You can see it in Tools -> Preferences -> General.
After that, go to Tools -> Preferences -> Editor and select your file name in Highlighting style drop-down list.