Re: [R] portfolio optimization in R

2014-11-20 Thread Boris Steipe
Indeed.
Start here: 
http://www.r-bloggers.com/three-tips-for-posting-good-questions-to-r-help-and-stack-overflow/

and then read this: 
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

Then put some comments into your code about what you think it should do. Oh, 
and clean up the formatting.
Hope this helps.
B.



On Nov 19, 2014, at 11:29 PM, Esra Ulasan  wrote:

> Dear Sir/Madam,
> 
> I am a PhD candidate and writing my dissertation about portfolio optimization 
> in R. However, I have some problems with the codes. It always give the 
> dimension error. Could you help me to fix it?
> 
> Yours sincerely,
> 
> Here are the codes:
> 
> optimization <- function(x) {
>  mean <- colMeans(x)
>  names(mean) <-assets.names
>  p <- ncol(x)
>  n <- nrow(x)
>  M <- as.integer(p)
>  S <- cov(x)
>  invS<- solve(S)
>  u <- rep(1,p)
> 
> #w <- matrix(rep(0,p), nrow=p)
> #r <- seq(0,length=p); w <- cbind (diag(450));#sum of 1 
> #constraint <- cbind(diag(450), rep(0, 450));  #non-negativity constraint
>  a <- matrix(rep(0,4), nrow=2)
>  a[1,1] <- u%*%invS%*%u; 
>  a[1,2] <- a[2,1] <- u%*%invS%*%mean
>  a[2,2] <-mean%*%invS%*%mean 
>  d <- a[1,1]*a[2,2]-a[1,2]*a[1,2] 
>  f <- (invS%*%(a[2,2]*u-a[1,2]*mean))/d 
>  g <- (invS%*%(-a[1,2]*u+a[1,1]*mean))/d
>  rho <- 0.5
>  r <- seq(0, rho,length= M)
>  w <- matrix(rep(0, p*M), nrow=p)
>  for(m in 1:M) w[,m] <- f+r[,m]*g
>  #w <- f+(rho*g)
>  w[,m] >=0
>  sum([,m])==1
>  s <- sqrt(a[1,1]*((r-a[1,2]/a[1,1])^2)/d+ 1/a[1,1]) 
>  ss <- sqrt(diag(S))
>  names(ss) <- assets.name
>  minp <- c(sqrt(1/a[1,1]), a[1,2]/a[1,1])
>  wmimp<- f+(a[1,2]/a[1,1])*g; 
>  tanp <- c(sqrt(a[2,2])/a[1,2], a[2,2]/a[1,2]) 
>  wtanp <- f+(a[2,2]/a[1,2])*g; 
>  return(solve(s, r, ss, p, minp, tanp, wminp, wtanp))
> }
> result=optimization(x)
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Portfolio Optimization

2012-01-13 Thread Enrico Schumann


I would be biased towards using a heuristic, for instance Threshold 
Accepting (TA), for solving such a problem. (TA is implemented in 
package NMOF. Disclosure: I am the author of that package.) But you will 
not find a ready-to-use solution there.


(1) you need an objective function, ie, a function that maps a given 
vector of holdings (and data like your scenario matrix) into a real 
number; the better the portfolio, the lower the number.


(2) For TA, you need a so-called neighbourhood function. That is a 
function that changes one portfolio vector into another, by changing 
some elements. Examples for simple neighbourhoods are in the package 
vignettes. Do you have a budget constraint? If yes, and you want to work 
with integers, I would suggest using a "cash" variable. (See, eg, 
Algorithm 3 in http://www.swissfinanceinstitute.ch/rp20.pdf )


(3) The constraints can, at least in a first round, be included through 
penalties.



Regards,
Enrico

PS. There is a mailing list dedicated to finance-with-R questions, and 
you may get better answers there.


https://stat.ethz.ch/mailman/listinfo/r-sig-finance



--
Enrico Schumann
Lucerne, Switzerland
http://nmof.net/


Am 13.01.2012 17:06, schrieb Sal Pellettieri:

Hi,

I'm an R newbie and I've been struggling with a optimization problem for
the past couple of days now.

Here's the problem - I have a matrix of expected payouts from different
stock option strategies. Each column in my matrix represents a different
stock and each row represents the return to the strategy given a certain
market move. So the rows are not a time series of percentage returns but a
dollar payout in different expected scenarios, i.e.

Expected Return Matrix (ER) =   stock1   stock2   stockn
scenario1   $  $
   $
scenario2   $  $
   $
scenario3   $  $
   $
...

I want to create an optimal portfolio of these strategies by applying a
vector of weights. The weights will be the number of contracts of each to
buy and won't be a percentage weighting. There are a few constraints I need
it comply with:

- The weights have to be integers
- The minimum portfolio return (ER* Weights) across the scenarios has to
be greater than some negative number I specify
- There has to be a certain minimum number of stocks in the portfolio so
length(weights)>some number I specify.

Any help is GREATLY appreciated since I have tried so many different
functions and packages. Even if someone can just lead me to the correct
function to use that would be a great help as I've looked at optim,
solveLP, ROI package and many others.


Thanks,
S



__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] portfolio optimization problem - use R

2008-07-21 Thread José Augusto Jr.
You could try the fPortfolio package.

Wish helps.

jamaj

2008/7/21, fzp2008 <[EMAIL PROTECTED]>:
>
> How to use R to solve the optimisaton problem
>
> Minimize:
> ½*w^T*omega*w+mu^T*w+c^T(w-w0) for w>w0 long position
> ½*w^T*omega*w+mu^T*w-c^T(w-w0) for w
> W: is the update weight of portfolio
> Wo is the initial weight of portfolio
>
> Omega is the variance covariance matrix
>
> mu is the vector of return rate of stocks in the portfolio
>
> C is the vector coefficient of transaction cost
>
> Is it a quandratic programming problem? Then how to write the objective
> function? Or any other method to solve this?
>
> --
> View this message in context: 
> http://www.nabble.com/portfolio-optimization-problem---use-R-tp18570399p18570399.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.