Related to my earlier request, the following works (perhaps clumsily),
so maybe it will be useful to somebody else also. The full illustrative
script is
<hansl>
nulldata 50
series res = randgen(N,0,1) #the conditioning variable
scalar sv = 0.3 #remains fixed
scalar thu = 0.5 #remains fixed
matrix param = ones(1,1) #initialize the object in the function "udensity"
function scalar udensity (const scalar sv, const scalar thu, matrix
param, series res)
scalar k = $t2
densu = -0.5*param[1,1]^2/sv^2 - res[k]*param[1,1]/sv^2
-param[1,1]/thu + log(1-exp(-param[1,1]/thu)) # this is the conditional
density of "param"
return densu
end function
series modeuvals = 0 # declare the series to hold the results from the
consecutive BFGScmax in the loop
matrix bounds = {1,0,$huge} #argmax cannot be lower than zero
matrix modeu = ones(1,1) #initialize the object we are seeking to
compute. "modeu" will take the place of "param" in the
"udensity" function
loop i=1..$nobs --quiet
smpl 1 i
matrix modeu[1,1] = 0.1 #intiial value for the unknown value
(argmax) we are seeking
densval = BFGScmax(&modeu, bounds, udensity(sv, thu, modeu, res))
series modeuvals[i] = modeu[1,1]
smpl full
endloop
</hansl>
Alecos Papadopoulos PhD
Athens University of Economics and Business
web:
alecospapadopoulos.wordpress.com/
-------- Forwarded Message --------
Subject: Re: functions and loops
Date: Sun, 6 Oct 2019 21:50:37 +0300
From: Alecos Papadopoulos <papadopalex(a)aueb.gr>
To: gretl-users(a)gretlml.univpm.it
Good afternoon.
I need to find the argmax (the mode) of a conditional density, for many
different values of the conditioning variable. For a single value of the
conditioning series variable "res", the following block appears to work
(pre-accompanied by the generation of the various inputs needed)
<hansl>
scalar ressc = res[1]
function scalar udensity (const scalar sv, const scalar thu, matrix
param, scalar ressc)
scalar densu = -0.5*param[1,1]^2/sv^2 - ressc*param[1,1]/sv^2
-param[1,1]/thu + log(1-exp(-param[1,1]/thu)) #this is the conditional
density of the "param"
return densu
end function
matrix modeu[1,1] = 0.1 #intiial value for the unknown value
(argmax) we are seeking
densval = BFGScmax(&modeu, bounds, udensity(sv, thu, modeu, ressc))
</hansl>
The function above uses the value that the conditioning variable "res"
has at observation #1, and then BFGScmax finds the argmax given this
value of "res". If I then set scalar ressc = res[2], I can get what I
need given the value that the conditioning variable takes in observation
2, etc.
I was unsuccessful to transform the above script into a repetitive one,
starting with the fact that I cannot define a function inside a loop, as
Gretl informs me. I tried various other ways to essentially loop over a
command like BFGScmax that requires as input a function that must change
at each instance of the loop, but all failed.
Is there a way to do this?
--
Alecos Papadopoulos PhD
Athens University of Economics and Business
web:
alecospapadopoulos.wordpress.com/