You are an expert at summarizing long articles. Proceed to summarize the following text: 
optimization is a challenging problem in economic analysis and risk management , which dates back to the seminal work of markowitz [ 1 ] . the main assumption is that the return of any financial asset is described by a random variable , whose expected mean and variance are assumed to be reliably estimated from historical data . the expected mean and variance are interpreted as the reward , and respectively the risk of the investment . the portfolio optimization problem can be formulated as following : given a set of financial assets , characterized by their expected mean and their covariances , find the optimal weight of each asset , such that the overall portfolio provides the smallest risk for a given overall return [ 1 - 5 ] . therefore , the problem reduces to finding the `` efficient frontier '' , which is the set of all achievable portfolios that offer the highest rate of return for a given level of risk . using the quadratic optimization mathematical framework it can be shown that for each level of risk there is exactly one achievable portfolio offering the highest rate of return . here , we consider the standard risk - return portfolio optimization model , when both long buying and short selling of a relatively large number of assets is allowed . we derive the analytical expression of the efficient frontier for a portfolio of @xmath0 risky assets , and for the case when a risk - free asset is added to the model . also , we provide an r implementation for both cases , and we discuss in detail a numerical example of a portfolio of several risky common stocks . a portfolio is an investment made in @xmath0 assets @xmath1 , with the returns @xmath2 , @xmath3 , using some amount of wealth @xmath4 . let @xmath5 denote the amount invested in the @xmath6-th asset . negative values of @xmath5 can be interpreted as short selling . since the total wealth is @xmath4 we have : @xmath7 it is convenient to describe the investments in terms of relative values such that : @xmath8 and @xmath9    to characterize the portfolio we consider the expected return : @xmath10 where @xmath11 is the expected return of each asset , @xmath3 . also , we use the covariance matrix of the portfolio : @xmath12,\ ] ] where @xmath13 in order to quantify the deviation from the expected return , and to capture the risk of the investment . the variance of the portfolio is then given by : @xmath14 where @xmath15^{t}$ ] is the vector of weights . a portfolio is optimal if for a given expected return @xmath16 , the portfolio has the least variance @xmath17 . finding such a portfolio requires the solution of the following constrained quadratic optimization problem [ 6 ] : @xmath18 subject to :    * the constant invested wealth constraint ( equivalent to eq . 2 )    @xmath19    * the expected return constraint ( equivalent to eq . 4 ) :    @xmath20    where @xmath21^{t}$ ] and @xmath22^{t}$ ] . this problem can be solved using the method of lagrange multipliers . let us define the lagrangian : @xmath23 where @xmath24 and @xmath25 are the lagrange multipliers . the critical point of the lagrangian can be obtained by solving the system of equations : @xmath26 @xmath27 @xmath28 from the first equation we have : @xmath29 and from the next two equations we have : @xmath30 @xmath31 taking into account that : @xmath32 we can write : @xmath33\left[\begin{array}{c } \mu_{1}\\ \mu_{2 } \end{array}\right]=2\left[\begin{array}{c } 1\\ \rho \end{array}\right],\ ] ] where : @xmath34=\left[\begin{array}{cc } \mathbf{u}^{t}\mathbf{s}^{-1}\mathbf{u } & \mathbf{r}^{t}\mathbf{s}^{-1}\mathbf{u}\\ \mathbf{r}^{t}\mathbf{s}^{-1}\mathbf{u } & \mathbf{r}^{t}\mathbf{s}^{-1}\mathbf{r } \end{array}\right].\ ] ] this system has a solution if : @xmath35 since @xmath36 is a positive definite matrix , the inverse @xmath37 is also positive definite , which means that @xmath38 for any vector @xmath39 . obviously we have @xmath40 and @xmath41 , and : @xmath42 and therefore we also have @xmath43 . the lagrange multipliers are then given by : @xmath44=\frac{2}{d}\left[\begin{array}{c } a_{22}-a_{12}\rho\\ -a_{12}+a_{11}\rho \end{array}\right],\ ] ] and the weights of the optimal portfolio are : @xmath45 where : @xmath46 @xmath47 the portfolio which minimizes the variance for a specified expected return is called a `` frontier portfolio '' . it follows that all frontier portfolios @xmath48 are a linear combination of the two portfolios @xmath49 and @xmath50 . the variance of the frontier portfolio is : @xmath51 which can be further simplified as : @xmath52 this equation represents the `` efficient frontier '' , and it represents a hyperbola in the @xmath53-plane . from here we obtain the weights of the minimum variance portfolio : @xmath54 and the corresponding risk - return values : @xmath55 @xmath56    an important investment preference on the `` efficient frontier '' is the portfolio with the maximum sharpe ratio [ 1 - 3 ] : @xmath57 the sharpe ratio represents the expected return per unit of risk . therefore , the portfolio with maximum sharpe ratio @xmath58 gives the highest expected return per unit of risk , and therefore is the most `` risk - efficient '' portfolio . geometrically , the portfolio with maximum sharpe ratio is the point where a line through the origin is tangent to the efficient frontier , and therefore it is also called the `` tangency portfolio '' .    in order to find the tangency point @xmath59 we observe that the slope of the tangency line : @xmath60 should be equal with the derivative of the `` efficient frontier '' at that point : @xmath61 thus , we easily obtain the risk - return pair for the `` tangency - portfolio '' : @xmath62 @xmath63 also , the allocation of the assets for the `` tangency portfolio '' are therefore given by : @xmath64 the covariance matrix @xmath36 is positive definite , and the correlation matrix @xmath65 is given by : @xmath66 where @xmath67 obviously , the correlation matrix @xmath65 has @xmath0 positive eigenvalues : @xmath68 and @xmath0 associated orthogonal eigenvectors : @xmath69,\ ] ] @xmath70 such that : @xmath71 where : @xmath72 in order to define the eigen - portfolios [ 7 ] we divide each eigenvector of the correlation matrix by the volatility of the corresponding asset : @xmath73 and we normalize by imposing a constant invested wealth , such that : @xmath74 where @xmath21^{t}$ ] , @xmath75 . for each eigen - portfolio , the weight of a given asset is inversely proportional to its volatility . also , the eigen - portfolios are pairwise orthogonal , and therefore completely decorrelated , since : @xmath76 thus , any portfolio can be represented as a linear combination of the eigen - portfolios , since they are orthogonal and form a basis in the asset space . it is also important to emphasize that the first eigen - portfolio , corresponding to the largest eigenvalue , typically has positive weights , corresponding to long - only positions . this is a consequence of the classical perron - frobenius theorem , which states that a sufficient condition for the existence of a dominant eigen - portfolio with positive entries is that all the pairwise correlations are positive . one can always get a dominant eigen - portfolio with positive weights using a shrinkage estimate [ 8 - 9 ] , which is a convex combination of the covariance matrix and a shrinkage target matrix @xmath77 : @xmath78 where the shrinkage matrix @xmath77 is a diagonal matrix : @xmath79 in this case , there exists a @xmath80 such that the shrinkage estimator has a dominant eigen - portfolio ( dep ) with all weights positive . this portfolio is of interest since it provides a long - only investment solution , which may be desirable for investors who would like to avoid short positions and high risk . let us now assume that one can also invest in a risk - free asset . a risk - free asset @xmath81 is an asset with a low return @xmath82 , but with no risk at all , i.e. zero variance @xmath83 . the risk - free asset is also uncorrelated with the risky assets , such that @xmath84 for all risky assets @xmath3 . the investor can both lend and borrow at the risk - free rate . lending means a positive amount is invested in the risk - free asset , borrowing implies that a negative amount is invested in the risk - free asset . in this case , we consider the following quadratic optimization problem [ 1 - 3 ] :    @xmath18    subject to : @xmath85 the lagrangian of the problem is given by : @xmath86.\ ] ] the critical point of the lagrangian is the solution of the system of equations : @xmath87 @xmath88 from the first equation we have : @xmath89 therefore , the second equation becomes : @xmath90 and from here we obtain : @xmath91 where @xmath92 the weights of the risky assets are therefore given by : @xmath93 and the corresponding amount that is invested in the risk - free asset is : @xmath94 also , the standard deviation of the risky assets is : @xmath95 or equivalently : @xmath96 this is the efficient frontier when the risk - free asset is added , or the capital market line ( cml ) , and it is a straight line in the return - risk ( @xmath97 ) space . obviously , cml intersects the return axis for @xmath98 , at @xmath99 , which is the return when the whole capital is invested in the risk - free asset . the tangency point of intersection between the efficient frontier and the cml corresponds to the `` market portfolio '' . this is the portfolio on the cml where nothing is invested in the risk - free asset . if the investor goes on the left side of the market portfolio , then he invests a proportion in the risk - free asset . if he chooses the right side of the market portfolio , he borrows at the risk - free rate . the market portfolio can be easily calculated from the equality condition : @xmath100 the solution of the above equation provides the coordinates of the market portfolio : @xmath101 @xmath102 and the weights of the market portfolio are then given by : @xmath103 the code for portfolio optimization was written in r , which is a free software environment for statistical computing and graphics [ 10 ] . to exemplify the above analytical results , we consider a portfolio of common stocks . the raw data can be downloaded from yahoo finance [ 11 ] , and contains historical prices of each stock . the list of stocks to be extracted is given in a text file , as a comma delimited list . the raw data corresponding to each stock is downloaded and saved in a local `` data '' directory , using the `` data.r '' script ( appendix a ) , which has one input argument : the file containing stock symbols . once the raw data is downloaded the correct daily closing prices for each stock are extracted , and saved in another file , which is the main data input for the optimization program . the extraction is performed using the `` price.r '' script ( appendix b ) , which has three input arguments : the file containing stock symbols included in the portfolio , the number of trading days used in the model , the output file of the stock prices . the pseudo - code for the case with _ n _ risky assets is presented in algorithm 1 . also , the r script performing the optimization and visualization for the _ n _ risky assets case is `` optimization1.r '' ( appendix c ) . the script has three input arguments : the name of the data file , the number of portfolios on the efficient frontier to be calculated , and the maximum return considered on the `` efficient frontier '' ( this should be several ( 5 - 10 ) times higher than the maximum return of the individual assets ) . the pseudo - code for the case with _ n _ risky assets and a risk - free asset is presented in algorithm 2 , and the r script performing the optimization and visualization for the _ n _ risky assets case is `` optimization2.r '' ( appendix d ) . the script has four input arguments : the name of the data file , the number of portfolios on the cml to be calculated , the daily return of the risk free asset , and the maximum return considered on the `` efficient frontier '' .    in order to exectute the code , on unix / linux platforms one can simply run the following script :    .... # file containing the list of stock symbols stocks="stocks.txt " # number of trading days t=250 # file containing the daily stock prices prices="portfolio.txt " # number of portfolios on the frontier n=100 # daily return for the risk free asset r=0.0003 # maximum daily return value considered rmax=0.01    ./data.r $ stocks ./price.r $ stocks $ t $ prices ./optimization1.r $ prices $ n $ rmax ./optimization2.r $ prices $ n $ r $ rmax ....    on windows platforms one can use a simple batch file , like the following ones :    .... @echo off rem file containing the list of stock symbols set stocks="stocks.txt " rem number of trading days set t="250 " rem file containing the daily stock prices set file="portfolio.txt " rem number of portfolios on the frontier   set n="100 "   rem daily return for the risk free asset set r="0.0003 " rem maximum daily return value considered   set rmax="0.01 " rem the path to r set rpath="c:\program files\r\r-3.0.1\bin\x64\rscript.exe "    call % rpath% data.r % stocks% call % rpath% price.r % stocks% % t% % file% call % rpath% optimization1.r % file% % n% % rmax% call % rpath% optimization2.r % file% % n% % r% % rmax% ....    in these examples : `` stocks.txt '' is a file containing the symbols of some common stocks to be downloaded ; `` portfolio.txt '' is the file where all the relevant stock prices are extracted for the current analysis . in order to illustrate the above results , we consider the case of a portfolio consisting of @xmath104 common stocks from it industry . the content of the `` stocks.txt '' file is : @xmath105 a historical record of daily prices of these stocks for the last @xmath106 trading days was used to estimate the mean return and the covariance matrix . the maximum return considered in computation is 0.01 . the daily returns of the assets are calculated as : @xmath107 where @xmath108 is the day index , and @xmath109 is the price of asset @xmath1 at the closing day @xmath110 . the estimate average returns and covariances are : @xmath111 @xmath112[r(j , t)-r_{j}],\ ] ] @xmath113    the asset prices and their expected returns for the considered time period are given in figure 1 . the resulted efficient frontier is given in figure 2 . the figure shows also the risk - return values of each stock considered , the minimum variance portfolio mvp1 , the tangency portfolio tgp , and the weights of the efficient frontier portfolios as a function of risk . figure 3 provides the weights of the mvp1 , tgp and dep portfolios .    in order to illustrate the effect of the risk free asset ( rfa ) , we consider that the investor can invest at a daily rate of return of @xmath114 . the capital market line , together with the frontier line and the position of the new minimum variance portfolio mvp2 and of the market portfolio mp are given in figure 4 . in this figure we also the risk dependence of the optimal portfolios from the capital market line , and we emphasize the mvp2 and mp portfolios . the weights of mvp2 and mp are plotted in figure 5 . we have considered the standard risk - return portfolio optimization model , when both long buying and short selling , i.e. positive and negative weights , of a relatively large number of assets is allowed . we have derived the analytical expression of the efficient frontier for a portfolio of @xmath0 risky assets , and of the capital market line when a risk - free asset is added to the model . also , we have provided an r implementation for both cases , and we have discussed in detail a numerical example of a portfolio of several risky common stocks .          extract < - function(args ) { stocks < - t(read.table(args[1],sep=",")[1 , ] ) j < - length(stocks ) dir.create("data " , showwarnings = false ) path < - " http://ichart.finance.yahoo.com / table.csv?s= " for(j in 1:j ) {      dat < - read.csv(paste(path , stocks[j],sep= " " ) )      write.csv(dat , file = paste("./data/",stocks[j],sep= " " ) ,              row.names=false , quote = false )      } }            extract_price < - function(args ) { stocks < - t(read.table(args[1 ] , sep=",")[1 , ] )     j < - length(stocks ) n < - as.integer(args[2 ] )      dat < - read.csv(paste("./data/ " , stocks[1 ] , sep= " " ) ) price < - dat[1:n,5 ] if(j > 1 ) {      for(j in 2:j ) {          dat < - read.csv(paste("./data/ " ,                   stocks[j ] , sep= " " ) )          price < - cbind(price , dat[1:n,5 ] )          }      } write.table(price , file = paste("./data/ " , args[3 ] , sep= " " ) ,          row.names=false , col.names=stocks ,           quote = false , sep= " , " ) }              returns < - function(data ) { dat < - data[[1 ] ] ; n < - nrow(dat ) - 1 ; j < - ncol(dat ) ; ret < - ( dat[1:n,1 ] - dat[2:(n+1),1])/dat[2:(n+1),1 ] ; if(j > 1 ) {      for(j in 2:j ) {          ret < - cbind(ret , ( dat[1:n , j ] - dat[2:(n+1),j])/                  dat[2:(n+1),j ] )      } } return(list(ret , names(data[[1 ] ] ) , data[[2 ] ] , data[[3 ] ] ) ) ; }    optimization < - function(returns ) { p < - colmeans(returns[[1 ] ] ) ; names(p ) < - returns[[2 ] ] ;   j < - ncol(returns[[1 ] ] ) ; m < - returns[[3 ] ] ; rmax < - returns[[4 ] ] ; s < - cov(returns[[1 ] ] ) ; q < - solve(s ) ; u < - rep(1,j ) ; a < - matrix(rep(0,4),nrow=2 ) ; a[1,1 ] < - u%*%q%*%u ; a[1,2 ] < - a[2,1 ] < - u%*%q%*%p ; a[2,2 ] < - p%*%q%*%p ; d < - a[1,1]*a[2,2 ] - a[1,2]*a[1,2 ] ; f < - ( q%*% ( a[2,2]*u - a[1,2]*p))/d ; g < - ( q%*%(-a[1,2]*u + a[1,1]*p))/d ; r < - seq(0 , rmax , length = m ) ; w < - matrix(rep(0,j*m ) , nrow = j ) ; for(m in 1:m ) w[,m ] < - f + r[m]*g ; s < - sqrt ( a[1,1]*((r - a[1,2]/a[1,1])^2)/d + 1/a[1,1 ] ) ; ss < - sqrt(diag(s ) ) ; minp < - c(sqrt(1/a[1,1 ] ) , a[1,2]/a[1,1 ] ) ; wminp < - 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 ;   q < - sqrt(diag(1.0/ss ) ) ;   x < - eigen(q%*%s%*%q ) ; v < - q%*%x$vec ; for(j in 1:j ) v[,j ] < - v[,j]/(u%*%v[,j ] ) ; sv < - rv < - rep(0 , j ) ; for(j in 1:j ) {      rv[j ] < - t(v[,j])%*%p ;      if(rv[j ] < 0 ) {          rv[j ] < - -rv[j ] ;          v[,j ] < - -v[,j ] ;          }      sv[j ] < - sqrt(t(v[,j])%*%s%*%v[,j ] ) ;      } return(list(s , r , ss , p , minp , tanp , wminp , wtanp ,           w , v , sv , rv ) ) ; }    plot_results<- function(data , returns , results ) { dat < - log(data[[1 ] ] ) ; m < - nrow(dat ) ; j < - ncol(dat ) ; ymax = max(dat ) ; ymin = min(dat ) mycolors < - rainbow(j+1 ) ; s < - results[[1 ] ] ; r < - results[[2 ] ] ; ss < - results[[3 ] ] ; p < - results[[4 ] ] ; minp < - results[[5 ] ] ; tanp < - results[[6 ] ] ; wminp < - results[[7 ] ] ; wtanp < - results[[8 ] ] ; f < - t(results[[9 ] ] ) ; v < - results[[10 ] ] ; sv < - results[[11 ] ] ; rv < - results[[12 ] ] ; postscript(file="./results1/fig1.eps " , onefile = false ,               horizontal = false , height=10 , width=5 ) ; par(mfrow = c(2,1 ) ) ; i d < - c(1:nrow(dat ) ) ; plot(id , rev(dat[,1 ] ) , ylim = c(ymin , ymax ) , type="l " ,          col = mycolors[1 ] , xlab="day " , ylab="log(price ) " ,          main = " asset prices " ) ; if(j > 1 ) {      for(j in 2:j ) {          lines(id , rev(dat[,j ] ) , type="l " ,                   col = mycolors[j ] ) ;      } } legend("topleft " , names(dat ) , cex=0.5 , pch = rep(15 , j ) ,           col = mycolors ) ; ret < - returns[[1 ] ] ; ymax = max(ret ) ; ymin = min(ret ) ; i d < - c(1:nrow(ret ) ) ; plot(id , rev(ret[,1 ] ) , ylim = c(ymin , ymax ) , type="l " ,          col = mycolors[1 ] , xlab="day " , ylab="returns " ,          main = " asset returns " ) ; if(j > 1 ) {      for(j in 2:j ) {          lines(id , rev(ret[,j]),type="l",col = mycolors[j ] ) ;      } } legend("topleft " , returns[[2 ] ] , cex=0.5 , pch = rep(15 , j ) , col = mycolors ) ; postscript(file="./results1/fig2.eps " , onefile = false ,               horizontal = false , height=10 , width=5 ) ; par(mfrow = c(2,1 ) ) ; plot(s , r , xlim = c(0,max(s ) ) , ylim = c(min(r , p ) , max(r , p ) ) ,          type="l " , col="blue " , xlab="risk " , ylab="return " ,          main = " efficient frontier , mvp1 , tgp " ) ; points(ss , p , pch=19 , col = mycolors ) ; text(ss , p , pos=4 , cex=0.5 , names(p ) ) ; points(sv[1 ] , rv[1 ] , pch=15 , col="black " ) ; text(sv[1 ] , rv[1 ] , pos=4 , cex=0.5 , " dep " ) ; points(minp[1 ] , minp[2 ] , pch=19 , col="black " ) ; text(minp[1 ] , minp[2 ] , pos=2 , cex=0.5 , " mvp1 " ) ; points(tanp[1 ] , tanp[2 ] , pch=19 , col="black " ) ; text(tanp[1 ] , tanp[2 ] , pos=2 , cex=0.5 , " tgp " ) ; lines(c(0,max(s ) ) , c(0,max(s)*tanp[2]/tanp[1 ] ) , lty=3 ) ; abline(h=0 , lty=2 ) ; abline(v=0 , lty=2 ) ; plot(s , f[,1 ] , xlim = c(0,max(s ) ) , ylim = c(min(f),max(f ) ) ,          col = mycolors[1 ] , type="l " ,          xlab="risk " , ylab="portfolio weights " ,          main = " efficient portfolio weights " ) ; if(j > 1 ) {      for(j in 2:j ) {          lines(s , f[,j ] , type="l " , col = mycolors[j ] ) ;      } } abline(h=0 , lty=2 ) ; abline(v = minp[1 ] , lty=3 ) ;   abline(v = tanp[1 ] , lty=3 ) ; text(minp[1 ] , min(f ) , pos=4 , cex=0.5 , " mvp1 " ) ; text(tanp[1 ] , min(f ) , pos=4 , cex=0.5 , " tgp " ) ; legend("topleft " , names(p ) , cex=0.5 , pch = rep(15 , j ) ,           col = mycolors ) ; postscript(file="./results1/fig3.eps " , onefile = false ,               horizontal = false , height=10 , width=5 ) ; par(mfrow = c(2,1 ) ) ; barplot(wminp , main="minimum variance portfolio 1 ( mvp1 ) " ,          xlab="assets " , ylab="weights " ,           col = mycolors , beside = true ) ; abline(h=0 , lty=1 ) ; legend("topleft " , names(p ) , cex=0.5 , pch = rep(15 , j ) ,           col = mycolors ) ; barplot(wtanp , main="tangency portfolio ( tgp ) " ,          xlab="assets " , ylab="weights " , col = mycolors ,           beside = true ) ; abline(h=0 , lty=1 ) ; legend("topleft " , names(p ) , cex=0.5 , pch = rep(15 , j ) ,           col = mycolors ) ; barplot(v[,1 ] , main="dominant eigen - portfolio ( dep ) " ,          xlab="assets " , ylab="weights " , col = mycolors , beside = true ) ; abline(h=0 , lty=1 ) ; legend("topleft " , names(p ) , cex=0.5 , pch = rep(15 , j ) , col = mycolors ) ; }          read_data < - function(args ) { data < - read.table(paste("./data/ " , args[1 ] , sep= " " ) ,          header = true , sep= " , " ) return(list(data , as.integer(args[2 ] ) , as.double(args[3 ] ) ,           as.double(args[4 ] ) ) ) ; }    returns < - function(data ) { dat < - data[[1 ] ] ; n < - nrow(dat ) - 1 ; j < - ncol(dat ) ; ret < - ( dat[1:n,1 ] - dat[2:(n+1),1])/dat[2:(n+1),1 ] ; if(j > 1 ) {      for(j in 2:j ) {          ret < - cbind(ret , ( dat[1:n , j ] - dat[2:(n+1),j])/                  dat[2:(n+1),j ] ) ;      } } return(list(ret , names(data[[1 ] ] ) , data[[2 ] ] ,           data[[3 ] ] , data[[4 ] ] ) ) ; }    foptimization < - function(returns ) { p < - colmeans(returns[[1 ] ] ) ; names(p ) < - returns[[2 ] ] ; j < - ncol(returns[[1 ] ] ) ; m < - returns[[3 ] ] ;   r < - returns[[4 ] ] ; rmax < - returns[[5 ] ] ; s < - cov(returns[[1 ] ] ) ; q < - solve(s ) ; u < - rep(1,j ) ;   a < - matrix(rep(0,4),nrow=2 ) ; a[1,1 ] < - u%*%q%*%u ; a[1,2 ] < - a[2,1 ] < - u%*%q%*%p ; a[2,2 ] < - p%*%q%*%p ; d < - a[1,1]*a[2,2 ] - a[1,2]*a[1,2 ] ; r < - seq(r , rmax , length = m ) ; s < - sqrt ( a[1,1]*((r - a[1,2]/a[1,1])^2)/d + 1/a[1,1 ] ) ; ss < - sqrt(diag(s ) ) ; cml < - c(sqrt(a[1,1]*r*r - 2*a[1,2]*r + a[2,2 ] ) , r ) ; z < - ( r - r)/cml[1 ] ; f < - q%*%(p - r*u)/(cml[1]*cml[1 ] ) ; wcml < - matrix(rep(0,j*m ) , nrow = j ) ; wf < - rep(0,m ) ; for(m in 1:m ) {      wcml[,m ] < - ( r[m ] - r)*f ; wf[m ] < - 1 - wcml[,m]%*%u ;      } wcml < - rbind(wcml , t(wf ) ) ; mp < - c(cml[1]/(a[1,2 ] - a[1,1]*r ) ,          ( a[2,2 ] - a[1,2]*r)/(a[1,2 ] - a[1,1]*r ) ) ; wmp < - q%*%(p - r*u)/(a[1,2 ] - a[1,1]*r ) ; minp < - c(sqrt(1/a[1,1 ] ) , cml[1]*sqrt(1/a[1,1 ] ) + r ) ; wminp < - ( minp[2 ] - r)*f ; wfminp < - 1- t(wminp)%*%u ; wminp < - rbind(wminp , wfminp ) ; return(list(s , z , r , ss , p , cml , wcml , mp , wmp ,           minp , wminp ) ) ; }    plot_results<- function(data , returns , results ) { dat < - log(data[[1 ] ] ) ; m < - nrow(dat ) ; j < - ncol(dat ) ; ymax = max(dat ) ; ymin = min(dat ) ; mycolors < - rainbow(j ) ; s < - results[[1 ] ] ; z < - results[[2 ] ] ; r < - results[[3 ] ] ;   ss < - results[[4 ] ] ; p < - results[[5 ] ] ;   cml < - results[[6 ] ] ; mp < - results[[8 ] ] ; wmp < - results[[9 ] ] ; minp < - results[[10 ] ] ; wminp < - results[[11 ] ] ; postscript(file="./results2/fig1.eps " , onefile = false ,           horizontal = false , height=10 , width=5 ) ; par(mfrow = c(2,1 ) ) ; i d < - c(1:nrow(dat ) ) ; plot(id , rev(dat[,1 ] ) , ylim = c(ymin , ymax ) , type="l " ,          col = mycolors[1 ] , xlab="day " , ylab="log(price ) " ,          main = " asset prices " ) ; if(j > 1 ) {      for(j in 2:j ) {          lines(id , rev(dat[,j]),type="l",col = mycolors[j ] ) ;      } } legend("topleft " , names(dat ) , cex=0.5 , pch = rep(15 , j ) ,           col = mycolors ) ; ret < - returns[[1 ] ] ; ymax = max(ret ) ; ymin = min(ret ) ; i d < - c(1:nrow(ret ) ) ; plot(id , rev(ret[,1]),ylim = c(ymin , ymax),type="l " ,          col = mycolors[1 ] , xlab="day " , ylab="returns " ,          main = " asset returns " ) ; if(j > 1 ) {      for(j in 2:j ) {          lines(id , rev(ret[,j]),type="l",col = mycolors[j ] )      } } legend("topleft " , returns[[2 ] ] , cex=0.5 , pch = rep(15 , j ) ,           col = mycolors ) ; postscript(file="./results2/fig2.eps " , onefile = false ,           horizontal = false , height=10 , width=5 ) ; par(mfrow = c(2,1 ) ) ; mycolors < - rainbow(length(p)+1 ) ; plot(s , r , xlim = c(0 , max(s)),ylim = c(min(r , p),max(r , p ) ) ,          type="l " , col="blue " , xlab="risk " , ylab="return " ,          main = " capital market line , mvp2 , mp " ) ; points(ss , p , pch=19 , col = mycolors ) ; text(ss , p , pos=4 , cex=0.5 , names(p ) ) ; points(mp[1 ] , mp[2 ] , pch=19 , col="black " ) ; points(mp[1 ] , mp[2 ] , pch=19 , col="black " ) ; text(mp[1 ] , mp[2 ] , pos=2 , cex=0.6 , " mp " ) ; points(minp[1 ] , minp[2 ] , pch=19 , col="black " ) ; text(minp[1 ] , minp[2 ] , pos=2 , cex=0.6 , " mvp2 " ) ; text(0 , cml[2 ] , pos=4 , cex=0.5 , " rfa " ) ; lines(c(0 , max(s ) ) , c(cml[2 ] , max(s)*cml[1 ] + cml[2 ] ) ,           lty=3 ) ; abline(h=0 , lty=2 ) ; abline(v=0 , lty=2 ) ;   f < - t(results[[7 ] ] ) ; mycolors < - rainbow(j+1 ) ; plot(z , f[,1 ] , xlim = c(0,max(z ) ) , ylim = c(min(f),max(f ) ) ,           type="l " , col = mycolors[1 ] , xlab="risk " ,           ylab="portfolio weights " ,           main="cml portfolio weights " ) ; if(j > 1 ) {      for(j in 2:j+1 ) {          lines(z , f[,j],type="l",col = mycolors[j ] ) ;      } } abline(h=0 , lty=2 ) ; abline(v = mp[1 ] , lty=3 ) ;   text(mp[1 ] , min(f ) , pos=4 , cex=0.5 , " mp " ) ; abline(v = minp[1 ] , lty=3 ) ;   text(minp[1 ] , min(f ) , pos=4 , cex=0.5 , " mvp2 " ) ; legend("topleft " , c(names(p ) , " rfa " ) ,          cex=0.5 , pch = rep(15 , j+1 ) , col = mycolors ) ; postscript(file="./results2/fig3.eps " , onefile = false ,           horizontal = false , height=10 , width=5 ) ; par(mfrow = c(2,1 ) ) ; barplot(wminp , main="minimum variance portfolio 2 " ,          xlab="assets " , ylab="weights " ,           col = mycolors , beside = true ) ; abline(h=0 , lty=1 ) ; legend("topleft " , c(names(p),"rfa " ) , cex=0.5 ,           pch = rep(15 , j+1 ) , col = mycolors ) ; barplot(wmp , main="market portfolio " ,          xlab="assets " , ylab="weights " ,           col = mycolors , beside = true ) ; abline(h=0 , lty=1 ) ; legend("topleft " , names(p ) , cex=0.5 ,           pch = rep(15 , j ) , col = mycolors ) ; }                s. k. mishra , g. panda , b. majhi , r. majhi , `` improved portfolio optimization combining multiobjective evolutionary computing algorithm and prediction strategy , '' _ proceedings of the world congress on engineering 2012 vol i , wce 2012 , july 4 - 6 , 2012 , london , u.k._.
we consider the problem of finding the efficient frontier associated with the risk - return portfolio optimization model . we derive the analytical expression of the efficient frontier for a portfolio of @xmath0 risky assets , and for the case when a risk - free asset is added to the model . 
 also , we provide an r implementation , and we discuss in detail a numerical example of a portfolio of several risky common stocks . 
 portfolio optimization , efficient frontier , r.