Hi there,
it's been a while since my last post...how was the gretl conference in Berlin?)
Today I'm concerned with an optimization problem.
I have the following goal:
Find a linear combination of input series in such a way that
- the resulting series is used to calculate the difference to another exogenous series
- the variance of the 'difference-series' shall be minimized
- the coefficients are weights for the input series
- there are 2 groups of weights and in each group the weights sum up to 1 and are all positive
(In a next step I also restrict the coeffs so that the weights of the two groups are identical).
I manged to use gretls numerical methods, somehow. Code see below.
However the results are not as good as the Excel Solver solutions: gretl gives a small but not the minimal variance.
I want to use gretl since stability analysis is much easier (and I prefer gretl for such tasks at any rate :-).
Is it a necessary to use analytical derivatives?
At the moment I'm not sure how to implement them in the optimization procedure...
I also tried to use the simann helping step, but this function ignores all restrictions already implemented and did not produce valuable results.
I'd be happy to receive some advice.
Cheers
Leon
<hansl>
function scalar fn_Dif_min_Vola_sharesTo100_2gr (matrix *x, list lXlist, \
scalar nGr1, series sComp , series *sProg, series *sDiff)
scalar nParams = rows(x)
x[nGr1] = 1-sumc(x[1:nGr1-1])
if nParams-nGr1<>1
x[nParams] = 1-sumc(x[nGr1+1:nParams-1])
endif
x = (x.<0)? 0 : x
x = (x.>1)? 1: x
series sProg = lincomb(lXlist, x)
series sDiff = sProg-sComp
scalar ret = -sd(sDiff)
return ret
end function
function scalar fn_Dif_min_Vola_equal_2gr (matrix *x, list lXlist,\
scalar nGr1, series sComp , series *sProg, series *sDiff)
scalar nParams = rows(x)
x[nGr1] = 1-sumc(x[1:nGr1-1])
# shares of 1st group to use for 2nd
x[nGr1+1:nParams] = x[1:nGr1]
x = (x.<0)? 0: x
x = (x.>1)? 1: x
series sProg = lincomb(lXlist, x)
series sDiff = sProg-sComp
scalar ret = -sd(sDiff)
return ret
end function
# not run
list xList1 = Input1 Input2 Input3 Input4
list xList2 = Input5 Input6 Input7 Input8
list lData = xList1 xList2
matrix mParams = {0.25; 0.25; 0.25; 0.25; 0.25; 0.25; 0.25; 0.25} # nx1 matrix
x = mParams
series y = lincomb(lData, mParams)
series sDiff = y- sExo
# simann produced incorrect values, since the 0<=x<=1 of the function is ignored
#u = simann(&x, fn_Dif_min_Vola_sharesTo100_2gr(&x, lData, 4,sExo, &y, &sDiff), 100)
#print x
u = BFGSmax(&x, fn_Dif_min_Vola_sharesTo100_2gr(&x, lData, 4, sExo, &y, &sDiff))
print x
<hansl>