fPortfolio minimum variance optimisation under constraints

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

fPortfolio minimum variance optimisation under constraints

pierre.lequeux-2

I have when running an optimisation under constraints to derive a set of
currency hedge ratios using the fPortfolio package.  My portfolio is made
of 1 domestic asset (GBP) with a fix weight of 60% and 4 other non-domestic
assets for which I have for each a currency hedged and currency un-hedged
series with a box constraint summing up to the country weight in my
benchmark (respectively 20% for US 10% for Euro, 5% for Japan and 5% for
Switzerland). What I want to do is to find the portfolio that minimise the
variance (and also the one that maximise the risk return ratio). then the
proportionality of weights between the hedge and un-hedged asset will be my
hedge ratio.  The code below is in respect of the minimum variance
portfolio. I have coded a few lines and this seems to work most of the time
but in some occasion I am returned with a vector solution where of weights
are equal to zero. So I am obviously doing something wrong. It may be
something trivial,  possibly the initial settings of the portfolio spec or
the constraints,.but I can t seem to find what it is and running out of
ideas. I would be really thankful  if someonee could take the time to
peruse through the few below lines or point me towarda solution. I have
tried the StackOverflow forum but did not get any answer so far.  I have
attached  my code  and a sample of the data in the below.
                                                                                                                                                     
                GBP-U         USD-U         EUR-U        JPY-U         CHF-U         USD-H         EUR-H         JPY-H        CHF-H                  
 2016-05-13  0.0013866228 -0.0004015032  0.0023176509  0.004868271  0.0230163636 -0.0046939993  0.0065432710  0.0146679154  0.022040377              
 2016-05-20  0.0027094242 -0.0063831791 -0.0126106089 -0.006146676 -0.0158351590  0.0031237903  0.0045931268  0.0173505502  0.008964457              
 2016-05-27  0.0181190929  0.0140661262  0.0099806390 -0.005023782  0.0206840583  0.0223892121  0.0282165690  0.0048453909  0.033676661              
 2016-06-03 -0.0102711128  0.0081653966  0.0133194563  0.031814348  0.0092257881  0.0009701383 -0.0161380212 -0.0101505102 -0.016812484              
 2016-06-10 -0.0144639135  0.0160062120 -0.0152727781  0.008429249  0.0015983898 -0.0021260494 -0.0229890046 -0.0054785155 -0.027628141              
 2016-06-17 -0.0152295990 -0.0183599416 -0.0266861641 -0.039472434 -0.0286395252 -0.0112888146 -0.0217678087 -0.0589147494 -0.026415992              
 2016-06-24  0.0200253356  0.0317549413  0.0346147227  0.029164922  0.0401491058 -0.0166789670  0.0006268938 -0.0379679346  0.004386684              
 2016-07-01  0.0696777854  0.0620232077  0.0754752233  0.065465564  0.0704656764  0.0314514909  0.0433540129  0.0379248977  0.041482495              
 2016-07-08  0.0016753950  0.0369097752  0.0076117981  0.007834512  0.0070165833  0.0130392554 -0.0084350439 -0.0354380321 -0.006380199              
 2016-07-15  0.0118593292 -0.0037643046  0.0058939005  0.028278185 -0.0020681743  0.0144418213  0.0257127861  0.0888542585  0.015778875              
 2016-07-22  0.0092918745  0.0130552307  0.0086318585  0.001398931  0.0062780458  0.0067460116  0.0077586182  0.0070486894  0.004685162              
 2016-07-29 -0.0004646661 -0.0094042916  0.0124831676  0.025141288  0.0018086154 -0.0002180359  0.0040494413 -0.0046691781 -0.006745367              
 2016-08-05  0.0101311910  0.0160368778  0.0045395343 -0.014712256  0.0081973627  0.0040861320  0.0006639525 -0.0289135714  0.007984259              
 2016-08-12  0.0177313054  0.0125451367  0.0377475690  0.050109303  0.0322171465  0.0007342288  0.0192744848  0.0332886266  0.014216897              
 2016-08-19 -0.0086591802 -0.0121869847 -0.0131592756 -0.018946998 -0.0182591489 -0.0003056645 -0.0156054036 -0.0176744155 -0.020491089              
 2016-08-26 -0.0032629188 -0.0112907806 -0.0099742141 -0.028672792 -0.0178773544 -0.0066076557  0.0061594585 -0.0078421386  0.005324054              
 2016-09-02  0.0082322936 -0.0063800658 -0.0012434593  0.010783580  0.0006709931  0.0054516933  0.0145201246  0.0429479722  0.015518353              
 2016-09-09 -0.0172703615 -0.0222186855 -0.0031692051  0.014054238  0.0031503483 -0.0243009678 -0.0119551411  0.0001827415 -0.004089906              
 2016-09-16 -0.0105444978  0.0257113060 -0.0072348498 -0.002332981 -0.0002596633  0.0054847746 -0.0203191336 -0.0263452760 -0.015320125              
 2016-09-23  0.0294827487  0.0150126367  0.0358026791  0.041563858  0.0298489405  0.0121889694  0.0268098637  0.0263689159  0.017033142              
 2016-09-30 -0.0010620124  0.0010033756 -0.0058345738 -0.025319672 -0.0159143301  0.0014163678 -0.0060494835 -0.0215285310 -0.014212323              
 2016-10-07  0.0213392520  0.0351900327  0.0426486765  0.047394294  0.0333505051 -0.0072195144  0.0034453853  0.0210516899 -0.002539864              
 2016-10-14 -0.0050982217  0.0095672058  0.0009866843  0.004577971  0.0035956430 -0.0102205657  0.0020303997 -0.0035136735 -0.002926098              
 2016-10-21  0.0002873986  0.0004041448 -0.0029951394  0.012861688 -0.0135120432  0.0038727554  0.0087025204  0.0127856995 -0.006358163              
 2016-10-28 -0.0039917961 -0.0029823347  0.0078312950  0.013806532 -0.0053670019 -0.0070482290 -0.0052953175  0.0188646173 -0.014826428              
                                                                                                                                                     



                                                                                                                                                     
 require(fPortfolio)                                                                                                                                  
 spec <- portfolioSpec()                                                                                                                              
 mydata <- as.timeSeries(mydata)                                                                                                                      
                                                                                                                                                     
 cons <- c("eqsumW['GBP-U']=0.6" ,"eqsumW[c('USD-U','USD-H')]=0.20","eqsumW[c('EUR-U','EUR-H')]=0.10","eqsumW[c('JPY-U','JPY-H')]=0.05","eqsumW[c    
 ('CHF-U','CHF-H')]=0.05")                                                                                                                            
                                                                                                                                                     
 minvariancePortfolio(list(mu=apply(mydata,2,function(x) mean(x)),sigma=cov(mydata)), spec = spec , constraints = cons)                              
                                                                                                                                                     





when running the above lines on the data I get the following:
                                                                           
 Title:                                                                    
  MV Minimum Variance Portfolio                                            
  Estimator:         covEstimator                                          
  Solver:            solveRquadprog                                        
  Optimize:          minRisk                                                
  Constraints:       LongOnly                                              
                                                                           
 Portfolio Weights:                                                        
 GBP-U USD-U EUR-U JPY-U CHF-U USD-H EUR-H JPY-H CHF-H                      
     0     0     0     0     0     0     0     0     0                      
                                                                           
 Covariance Risk Budgets:                                                  
 GBP-U USD-U EUR-U JPY-U CHF-U USD-H EUR-H JPY-H CHF-H                      
                                                                           
                                                                           
 Target Return and Risk:                                                    
 [1] 0                                                                      
                                                                           
 Description:                                                              
  Mon Oct 31 11:26:35 2016 by user: 43951663                                
                                                                           




Clearly this is not right as I have a solution when I optimise this in
excel I get the following when I solve to minimise the portfolio risk:

                                                                                                 
     Constraints                                                                                  
 GBP 0.6000                                                                                      
 USD 0.2000                                                                                      
 EUR 0.1000                                                                                      
 JPY 0.0500                                                                                      
 CHF 0.0500                                                                                      
                                                                                                 
 Portfolio   1.0000                                                                              
                                                                                                 
                                                                                                 
 Weights                                                                                          
 GBP-U   USD-U   EUR-U   JPY-U   CHF-U   USD-H   EUR-H   JPY-H   CHF-H                            
 0.6000  0.0000  0.0000  0.0423  0.0000  0.2000  0.1000  0.0077  0.0500                          
                                                                                                 
 Portfolio Stdev 0.01562                                                                          
 Portfolio Mean  0.00424                                                                          
                                                                                                 





Kind regards



Pierre



************************************************************
HSBC Bank plc
Registered Office: 8 Canada Square, London E14 5HQ
Registered in England - Number 14259
Authorised by the Prudential Regulation Authority and regulated by the
Financial Conduct Authority and the Prudential Regulation Authority
************************************************************



-----------------------------------------
SAVE PAPER - THINK BEFORE YOU PRINT!

This E-mail is confidential.  

It may also be legally privileged. If you are not the addressee you may not copy,
forward, disclose or use any part of it. If you have received this message in error,
please delete it and all copies from your system and notify the sender immediately by
return E-mail.

Internet communications cannot be guaranteed to be timely secure, error or virus-free.
The sender does not accept liability for any errors or omissions.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: fPortfolio minimum variance optimisation under constraints

Michael Weylandt
Hi Pierre,

I'm willing to take a look, but could you format the data in a way
that would make it a bit easier to use?

The dput() function can produce a representation which is safe for
emailing and can be read directly back into R.

Cheers,
Michael

On Tue, Nov 1, 2016 at 4:19 AM,  <[hidden email]> wrote:

>
> I have when running an optimisation under constraints to derive a set of
> currency hedge ratios using the fPortfolio package.  My portfolio is made
> of 1 domestic asset (GBP) with a fix weight of 60% and 4 other non-domestic
> assets for which I have for each a currency hedged and currency un-hedged
> series with a box constraint summing up to the country weight in my
> benchmark (respectively 20% for US 10% for Euro, 5% for Japan and 5% for
> Switzerland). What I want to do is to find the portfolio that minimise the
> variance (and also the one that maximise the risk return ratio). then the
> proportionality of weights between the hedge and un-hedged asset will be my
> hedge ratio.  The code below is in respect of the minimum variance
> portfolio. I have coded a few lines and this seems to work most of the time
> but in some occasion I am returned with a vector solution where of weights
> are equal to zero. So I am obviously doing something wrong. It may be
> something trivial,  possibly the initial settings of the portfolio spec or
> the constraints,.but I can t seem to find what it is and running out of
> ideas. I would be really thankful  if someonee could take the time to
> peruse through the few below lines or point me towarda solution. I have
> tried the StackOverflow forum but did not get any answer so far.  I have
> attached  my code  and a sample of the data in the below.
>
>                 GBP-U         USD-U         EUR-U        JPY-U         CHF-U         USD-H         EUR-H         JPY-H        CHF-H
>  2016-05-13  0.0013866228 -0.0004015032  0.0023176509  0.004868271  0.0230163636 -0.0046939993  0.0065432710  0.0146679154  0.022040377
>  2016-05-20  0.0027094242 -0.0063831791 -0.0126106089 -0.006146676 -0.0158351590  0.0031237903  0.0045931268  0.0173505502  0.008964457
>  2016-05-27  0.0181190929  0.0140661262  0.0099806390 -0.005023782  0.0206840583  0.0223892121  0.0282165690  0.0048453909  0.033676661
>  2016-06-03 -0.0102711128  0.0081653966  0.0133194563  0.031814348  0.0092257881  0.0009701383 -0.0161380212 -0.0101505102 -0.016812484
>  2016-06-10 -0.0144639135  0.0160062120 -0.0152727781  0.008429249  0.0015983898 -0.0021260494 -0.0229890046 -0.0054785155 -0.027628141
>  2016-06-17 -0.0152295990 -0.0183599416 -0.0266861641 -0.039472434 -0.0286395252 -0.0112888146 -0.0217678087 -0.0589147494 -0.026415992
>  2016-06-24  0.0200253356  0.0317549413  0.0346147227  0.029164922  0.0401491058 -0.0166789670  0.0006268938 -0.0379679346  0.004386684
>  2016-07-01  0.0696777854  0.0620232077  0.0754752233  0.065465564  0.0704656764  0.0314514909  0.0433540129  0.0379248977  0.041482495
>  2016-07-08  0.0016753950  0.0369097752  0.0076117981  0.007834512  0.0070165833  0.0130392554 -0.0084350439 -0.0354380321 -0.006380199
>  2016-07-15  0.0118593292 -0.0037643046  0.0058939005  0.028278185 -0.0020681743  0.0144418213  0.0257127861  0.0888542585  0.015778875
>  2016-07-22  0.0092918745  0.0130552307  0.0086318585  0.001398931  0.0062780458  0.0067460116  0.0077586182  0.0070486894  0.004685162
>  2016-07-29 -0.0004646661 -0.0094042916  0.0124831676  0.025141288  0.0018086154 -0.0002180359  0.0040494413 -0.0046691781 -0.006745367
>  2016-08-05  0.0101311910  0.0160368778  0.0045395343 -0.014712256  0.0081973627  0.0040861320  0.0006639525 -0.0289135714  0.007984259
>  2016-08-12  0.0177313054  0.0125451367  0.0377475690  0.050109303  0.0322171465  0.0007342288  0.0192744848  0.0332886266  0.014216897
>  2016-08-19 -0.0086591802 -0.0121869847 -0.0131592756 -0.018946998 -0.0182591489 -0.0003056645 -0.0156054036 -0.0176744155 -0.020491089
>  2016-08-26 -0.0032629188 -0.0112907806 -0.0099742141 -0.028672792 -0.0178773544 -0.0066076557  0.0061594585 -0.0078421386  0.005324054
>  2016-09-02  0.0082322936 -0.0063800658 -0.0012434593  0.010783580  0.0006709931  0.0054516933  0.0145201246  0.0429479722  0.015518353
>  2016-09-09 -0.0172703615 -0.0222186855 -0.0031692051  0.014054238  0.0031503483 -0.0243009678 -0.0119551411  0.0001827415 -0.004089906
>  2016-09-16 -0.0105444978  0.0257113060 -0.0072348498 -0.002332981 -0.0002596633  0.0054847746 -0.0203191336 -0.0263452760 -0.015320125
>  2016-09-23  0.0294827487  0.0150126367  0.0358026791  0.041563858  0.0298489405  0.0121889694  0.0268098637  0.0263689159  0.017033142
>  2016-09-30 -0.0010620124  0.0010033756 -0.0058345738 -0.025319672 -0.0159143301  0.0014163678 -0.0060494835 -0.0215285310 -0.014212323
>  2016-10-07  0.0213392520  0.0351900327  0.0426486765  0.047394294  0.0333505051 -0.0072195144  0.0034453853  0.0210516899 -0.002539864
>  2016-10-14 -0.0050982217  0.0095672058  0.0009866843  0.004577971  0.0035956430 -0.0102205657  0.0020303997 -0.0035136735 -0.002926098
>  2016-10-21  0.0002873986  0.0004041448 -0.0029951394  0.012861688 -0.0135120432  0.0038727554  0.0087025204  0.0127856995 -0.006358163
>  2016-10-28 -0.0039917961 -0.0029823347  0.0078312950  0.013806532 -0.0053670019 -0.0070482290 -0.0052953175  0.0188646173 -0.014826428
>
>
>
>
>
>  require(fPortfolio)
>  spec <- portfolioSpec()
>  mydata <- as.timeSeries(mydata)
>
>  cons <- c("eqsumW['GBP-U']=0.6" ,"eqsumW[c('USD-U','USD-H')]=0.20","eqsumW[c('EUR-U','EUR-H')]=0.10","eqsumW[c('JPY-U','JPY-H')]=0.05","eqsumW[c
>  ('CHF-U','CHF-H')]=0.05")
>
>  minvariancePortfolio(list(mu=apply(mydata,2,function(x) mean(x)),sigma=cov(mydata)), spec = spec , constraints = cons)
>
>
>
>
>
>
> when running the above lines on the data I get the following:
>
>  Title:
>   MV Minimum Variance Portfolio
>   Estimator:         covEstimator
>   Solver:            solveRquadprog
>   Optimize:          minRisk
>   Constraints:       LongOnly
>
>  Portfolio Weights:
>  GBP-U USD-U EUR-U JPY-U CHF-U USD-H EUR-H JPY-H CHF-H
>      0     0     0     0     0     0     0     0     0
>
>  Covariance Risk Budgets:
>  GBP-U USD-U EUR-U JPY-U CHF-U USD-H EUR-H JPY-H CHF-H
>
>
>  Target Return and Risk:
>  [1] 0
>
>  Description:
>   Mon Oct 31 11:26:35 2016 by user: 43951663
>
>
>
>
>
> Clearly this is not right as I have a solution when I optimise this in
> excel I get the following when I solve to minimise the portfolio risk:
>
>
>      Constraints
>  GBP 0.6000
>  USD 0.2000
>  EUR 0.1000
>  JPY 0.0500
>  CHF 0.0500
>
>  Portfolio   1.0000
>
>
>  Weights
>  GBP-U   USD-U   EUR-U   JPY-U   CHF-U   USD-H   EUR-H   JPY-H   CHF-H
>  0.6000  0.0000  0.0000  0.0423  0.0000  0.2000  0.1000  0.0077  0.0500
>
>  Portfolio Stdev 0.01562
>  Portfolio Mean  0.00424
>
>
>
>
>
>
> Kind regards
>
>
>
> Pierre
>
>
>
> ************************************************************
> HSBC Bank plc
> Registered Office: 8 Canada Square, London E14 5HQ
> Registered in England - Number 14259
> Authorised by the Prudential Regulation Authority and regulated by the
> Financial Conduct Authority and the Prudential Regulation Authority
> ************************************************************
>
>
>
> -----------------------------------------
> SAVE PAPER - THINK BEFORE YOU PRINT!
>
> This E-mail is confidential.
>
> It may also be legally privileged. If you are not the addressee you may not copy,
> forward, disclose or use any part of it. If you have received this message in error,
> please delete it and all copies from your system and notify the sender immediately by
> return E-mail.
>
> Internet communications cannot be guaranteed to be timely secure, error or virus-free.
> The sender does not accept liability for any errors or omissions.
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.