Computing Value at Risk and Conditional Value at Risk (Expected Shortfall) with R
<p>I am new to R and have some questions with regard to computing VaR and CVaR with the Portfolio Analytics package.</p>

<p>I have historical return data in a csv file. For example as follows:</p>

<pre><code>    time         return of stock1    return of stock2   return of stock3
    2016-01-01  -0.6184181087       -0.126394629        0.2537283152
    2016-01-02  -0.1738764113       -0.8100882955       -0.3162766513
    2016-01-03  -0.8229430174       -0.5007778048       -0.880233143
    ...         ...                 ...                 ...
</code></pre>

<p>(I) I want to compute the value at risk and conditional value at risk of this portfolio with equal weights (and later with different weights). I want to use the historical data.
(I do <strong>not</strong> want to make an assumption about the probability distribution-especially not asssume a Gaussian distribution.) So e.g. for the VaR I basically want to find the percentile.</p>

<p>(II) I want to optimize the weights by optimizing with the CVaR. ( again by the historical method).</p>

<p>I have following questions:</p>

<ol>
<li><p>If I use the VaR function and choose method = c("historical"), does it compute the respective percentile of my historical data?</p></li>
<li><p>With the help of some online tutorials, I have tried (but not completely understood) the following so far:</p>

<pre><code> *Loading data into file*
 testData_return &lt;- Return.read(filename = "myTest.csv", frequency = "d",format = "%Y-%m-%d", header = TRUE)

 *Create the portfolio specification object*
  Wcons &lt;- portfolio.spec(assets = colnames(testData_return))

 * Add box constraints *
  Wcons &lt;- add.constraint(portfolio = Wcons, type='box', min=0, max=1)  
  Wcons &lt;- add.constraint( portfolio=Wcons, type = "weight_sum", min_sum=0.99, max_sum=1.01)         * why can't I put min_sum=1, max_sum=1 ? *

  * Add an objective * 
  ObjSpec = add.objective(portfolio = Wcons, type = "risk", name = "VaR", arguments=list(p=0.95), enabled=TRUE)


* value of the objective function * 
constrained_objective(w=rep(1/3,3), R=testData_return,portfolio = ObjSpec)
</code></pre>

<p>gives me  "VaR 0.5707718".</p></li>
</ol>

<p>To test this result I chose the VaR function</p>

<pre><code>    VaR(testData_return, p = 0.95, method = c("historical"), clean = c("none"), portfolio_method = c("single")) 
</code></pre>

<p>this returns me as a results:</p>

<pre><code>      "stock1     stock2     stock3
      VaR -0.8836453 -0.9002575 -0.9151286"
</code></pre>

<p>Hence, I do not understand why I get different results and I cannot understand why I get three results for the last line.</p>

<p>In order to test further, I used python as follows:</p>

<p>I merged the returns by adding up the returns for each day and dividing by 3, yielding a csvfile with one column.</p>

<pre><code>    -0.1636948075
    -0.433413786
    -0.7346513217
    ...
</code></pre>

<p>Then I have computed the 95th percentile (same as above) of these returns.</p>

<pre><code>    percentile = 0.95

    * fname is the name of my file with the one column of data *
    returns_values = np.loadtxt(fname, skiprows=0, usecols=[0], unpack=True)

     print(np.percentile(returns_values, percentile))
</code></pre>

<p>This gives me -0.74321324 as a result.</p>

<p>So, I do not understand why this result is substantially different to my first results (-0.74 != 0.57 ). </p>

<p>Also, I do not understand why my second approach gives me a VaR for each individual stock.</p>