title: Constraining norms with inequalities
body: <p>I have time-series data for N stocks.</p>
  
  <p><code>sample.data&lt;-replicate(10,rnorm(1000))</code>, where each column shows the returns of different stocks over time.</p>
  
  <p>I am trying to construct a portfolio weight vector to minimize the variance of the returns.</p>
  
  <p>the objective function:</p>
  
  <pre><code>min w^{T}\sum w
  s.t. e_{n}^{T}w=1
  \left \| w \right \|\leq C
  </code></pre>
  
  <p>where w is the vector of weights, <code>\sum</code> is the covariance matrix, <code>e_{n}^{T}</code> is a vector of ones, <code>C</code> is a constant. Where the second constraint (<code>\left \| w \right \|</code>) is an inequality constraint. </p>
  
  <p>I used the following code to solve this problem:</p>
  
  <pre><code>library(Rsolnp) 
   gamma&lt;-1  
  fn&lt;-function(x) {cov.Rt&lt;-cov(sample.data); return(t(x)%*%cov.Rt%*%x)}   #OBJECTIVE FUNCTION TO MINIMIZE 
  eqn&lt;-function(x){one.vec&lt;-matrix(1,ncol=10,nrow=1); return(one.vec%*%x)} #EQUALITY CONSTRAINT
  constraints&lt;-1   #EQUALITY CONSTRAINT
  ineq&lt;-function(x){one.vec&lt;-matrix(1,ncol=10,nrow=1); #INEQUALITY CONSTRAINT
  z1&lt;-one.vec%*%abs(x)
  return(z1)  
      }   
  
  uh&lt;-gamma #UPPER BOUND
  lb&lt;-0 #LOWER BOUND
  x0&lt;-matrix(0,10,1) #STARTING PARAMETER VECTOR (NOT SURE WHAT STARTING VALUES TO PUT HERE)
  
  sol1&lt;-solnp(x0,fun=fn,eqfun=eqn,eqB=constraints, ineqfun=ineq,ineqUB=gamma,ineqLB=lb)
  </code></pre>
  
  <p>When running this code I get the following error message:</p>
  
  <pre><code>Error in solve.default(cz,tol = 1e-25) : system is computationally singular: reciprocal condition number = 0 In addition: There were 50 warnings (use warnings() to see the first 50)
  
  warnings()
  1: In cbind(temp, funv): number of rows of result is not a multiple of vector length
  </code></pre>
  
  <p>Any ideas what I might be doing wrong?
  Is there a problem with the starting parameter vector <code>x0</code>?</p>