### Optimization Problem gretl vs Excel Solver

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