Skip to content

Commit

Permalink
Adjust all cvar functions to receive weights and alphatest on argumen…
Browse files Browse the repository at this point in the history
…ts and project build update
  • Loading branch information
prcolaco committed Nov 23, 2019
1 parent 3cb52cf commit c58778d
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 275 deletions.
4 changes: 3 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
'target_name': 'riskjs',
'sources': [
'src/RiskJS.cpp',
'src/ptf_mc_var.cpp',
'src/CVaRHistorical.cpp',
'src/CVaRMonteCarlo.cpp',
'src/CVaRVarianceCovariance.cpp',
'src/compute_returns_eigen.cpp',
'src/instrument.cpp',
'src/path.cpp',
Expand Down
22 changes: 22 additions & 0 deletions src/CVaR.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Portfolio VaR through MC simulation
// from test_ptf_mc_var.cpp

#pragma once


#include <string>
#include <vector>


namespace RiskJS {

// Vectors types for function arguments
typedef std::vector<std::string> priceRow;
typedef std::vector<priceRow> priceData;
typedef std::vector<double> weightData;

double CVaRMonteCarlo(priceData rawPrices, weightData weights, double alphatest);
double CVaRHistorical(priceData rawPrices, weightData weights, double alphatest);
double CVaRVarianceCovariance(priceData rawPrices, weightData weights, double alphatest);

} // namespace RiskJS
40 changes: 16 additions & 24 deletions src/CVaRHistorical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <Eigen/Dense>

#include "ptf_mc_var.h"
#include "CVaR.h"

#include "compute_returns_eigen.h"
#include "compute_var.h"
Expand All @@ -27,17 +27,17 @@ using namespace Eigen;
using namespace std;


double CVaRHistorical(priceData data) {
double CVaRHistorical(priceData rawPrices, weightData weights, double alphatest) {
// Remove lines with missing values
size_t n(data.size() - 1);
size_t m(data[0].size() - 1);
size_t n(rawPrices.size() - 1);
size_t m(rawPrices[0].size() - 1);

Mat _prices;
_prices.resize(m,Vec(n));

for(size_t i = 1;i < n+1;++i){
for(size_t j = 1;j < data[i].size();++j){
string tmp = data[i][j];
for(size_t j = 1;j < rawPrices[i].size();++j){
string tmp = rawPrices[i][j];
if(tmp.empty()){
_prices[j-1][i-1] = 99999.;
}
Expand All @@ -47,10 +47,10 @@ double CVaRHistorical(priceData data) {
}
}

vector<string> indexNames(data[0].size() - 1);
vector<string> indexNames(rawPrices[0].size() - 1);

for(size_t i = 1;i < data[0].size();++i){
indexNames[i-1] = data[0][i];
for(size_t i = 1;i < rawPrices[0].size();++i){
indexNames[i-1] = rawPrices[0][i];
}

// Remove missing values to compute trailling returns
Expand All @@ -68,29 +68,21 @@ double CVaRHistorical(priceData data) {
std::shared_ptr<ComputeReturn> cr(new ComputeReturn(prices,1,windowsize,true));

// create portfolio
double a = double(1./m);
std::vector<double> weights{a,a}; //initialization. Equi-weighted asset for mere convenience
Ptf _ptf;
for(size_t i = 0;i < m;++i){
Ptf _ptf;
for(size_t i = 0;i < m;++i){
shared_ptr<Instrument> instrument(new DeltaOne());
auto p = std::make_pair(i,instrument);
_ptf.push_back(p);
}
shared_ptr<Portfolio> ptf(new Portfolio(_ptf, weights, cr, false, 1.e+07));
_ptf.push_back(p);
}
shared_ptr<Portfolio> ptf(new Portfolio(_ptf, weights, cr, false, 1.e+07));

HistoricalVaR model;

double alphatest = .95; // set alpha level, example 0.9 for 95% CVaR

// CVaRhistorical
model.setAlpha(1.0-alphatest);
double CVaRHistorical = model(0,ptf->getReturns());
cout << "CVaRHistorical: " << CVaRHistorical << endl;
double CVaRHistorical = model(0,ptf->getReturns());

return CVaRHistorical;

}

}


}
136 changes: 36 additions & 100 deletions src/CVaRMonteCarlo.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
//test_ptf_mc_var.cpp
// Portfolio VaR through MC simulation
// from test_ptf_mc_var.cpp

//test portfolio VaR through MC simulation

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include<memory>

#include <Eigen/Dense>

#include "CVaR.h"

#include "compute_returns_eigen.h"
#include "compute_var.h"
#include "path.h"
Expand All @@ -23,136 +20,75 @@
#include <boost/math/special_functions/erf.hpp>
using boost::math::erfc_inv;

using namespace Eigen;
using namespace std;

void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
//https://www.gamedev.net/topic/444193-c-how-to-load-in-a-csv-file/
{
std::string csvLine;
// read every line from the stream
while( std::getline(input, csvLine) )
{
std::istringstream csvStream(csvLine);
std::vector<std::string> csvColumn;
std::string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while( std::getline(csvStream, csvElement, ',') )
{
csvColumn.push_back(csvElement);
}
output.push_back(csvColumn);
}
}


int main()
{

try{

// Read daily price history for crypto assets, in USD
//Date,EOS,DAPP
//2018-11-19,3.5,0.5
//2018-11-20,3.6,0.6

std::fstream file("/home/gg/VaR/test/data.csv", ios::in);
if(!file.is_open())
{
std::cout << "File not found!\n";
return 1;
}
// typedef to save typing for the following object
typedef std::vector< std::vector<std::string> > csvVector;
csvVector csvData;

readCSV(file, csvData);
namespace RiskJS {

//test
for(size_t i = 0;i < 5; ++i){
for(size_t j = 0;j < csvData[i].size();++j){
cout << csvData[i][j] << '\t';

}
using namespace Eigen;
using namespace std;

cout << endl;
}
cout << endl;

double CVaRMonteCarlo(priceData rawPrices, weightData weights, double alphatest) {
// Remove lines with missing values

size_t n(csvData.size() - 1);
size_t m(csvData[0].size() - 1);
size_t n(rawPrices.size() - 1);
size_t m(rawPrices[0].size() - 1);

Mat _prices;
_prices.resize(m,Vec(n));

for(size_t i = 1;i < n+1;++i){
for(size_t j = 1;j < csvData[i].size();++j){
std::string tmp = csvData[i][j];
for(size_t j = 1;j < rawPrices[i].size();++j){
string tmp = rawPrices[i][j];
if(tmp.empty()){
_prices[j-1][i-1] = 99999.;
}
else{
_prices[j-1][i-1] = std::stod(tmp);
_prices[j-1][i-1] = stod(tmp);
}
}
}

std::vector<std::string> indexNames(csvData[0].size() - 1);
vector<string> indexNames(rawPrices[0].size() - 1);

for(size_t i = 1;i < csvData[0].size();++i){
indexNames[i-1] = csvData[0][i];
for(size_t i = 1;i < rawPrices[0].size();++i){
indexNames[i-1] = rawPrices[0][i];
}

//Remove missing values to compute trailling returns
//Asynchornous time series. Shift to the next value
// Remove missing values to compute trailling returns
// Asynchornous time series. Shift to the next value
Mat prices;
prices.resize(m,Vec(0));

for(size_t i = 0;i < _prices.size();++i){
for(size_t j = 0;j < _prices[i].size();++j){
if(!((_prices[i][j] == 99999) || (_prices[i][j] == 0)))
prices[i].push_back(_prices[i][j]);
}
}
for(size_t i = 0;i < _prices.size();++i){
for(size_t j = 0;j < _prices[i].size();++j){
if(!((_prices[i][j] == 99999) || (_prices[i][j] == 0)))
prices[i].push_back(_prices[i][j]);
}
}
unsigned int windowsize = prices[0].size()-2;
std::shared_ptr<ComputeReturn> cr(new ComputeReturn(prices,1,windowsize,true));

// create portfolio
double a = double(1./m);
std::vector<double> weights{a,a}; //initialization. Equi-weighted asset for mere convenience
Ptf _ptf;
for(size_t i = 0;i < m;++i){
shared_ptr<Instrument> instrument(new DeltaOne());
auto p = std::make_pair(i,instrument);
_ptf.push_back(p);
}
shared_ptr<Portfolio> ptf(new Portfolio(_ptf, weights, cr, false, 1.e+07));
Ptf _ptf;
for(size_t i = 0;i < m;++i){
shared_ptr<Instrument> instrument(new DeltaOne());
auto p = std::make_pair(i,instrument);
_ptf.push_back(p);
}
shared_ptr<Portfolio> ptf(new Portfolio(_ptf, weights, cr, false, 1.e+07));

HistoricalVaR model;

double alphatest = .95; // set alpha level, example 0.9 for 95% CVaR

// CVaRMonteCarlo
// Simulate crypto rtn using AR(1)xGARCH(1,1) through brute force Monte-Carlo
std::vector<AR1xGARCH11> processes(2);
// CVaRMonteCarlo
// Simulate crypto rtn using AR(1)xGARCH(1,1) through brute force Monte-Carlo
std::vector<AR1xGARCH11> processes(2);
processes[0] = AR1xGARCH11(); // first crypto asset AR1xGARCH11( 0.,0.5,omega, alpha=.24, beta=.76); note beta, alpha, omega correspond to python arch_model
processes[1] = AR1xGARCH11(); // 2nd crypto asset
Path1x1 process;
rng _rng;
VaRPtfMCCompute<HistoricalVaR, AR1xGARCH11> VaRMonteCarlo(ptf,model, processes, _rng, 1.0-alphatest);
double CVaRMonteCarlo = VaRMonteCarlo.computeVaR();
cout << "CVaRMonteCarlo: " << CVaRMonteCarlo << endl;

return 0;

} catch (const std::exception& e) { // caught by reference to base
std::cout << " a standard exception was caught, with message '"
<< e.what() << "'\n";
}

return CVaRMonteCarlo;
}


}
38 changes: 15 additions & 23 deletions src/CVaRVarianceCovariance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <Eigen/Dense>

#include "ptf_mc_var.h"
#include "CVaR.h"

#include "compute_returns_eigen.h"
#include "compute_var.h"
Expand All @@ -27,17 +27,17 @@ using namespace Eigen;
using namespace std;


double CVaRVarianceCovariance(priceData data) {
double CVaRVarianceCovariance(priceData rawPrices, weightData weights, double alphatest) {
// Remove lines with missing values
size_t n(data.size() - 1);
size_t m(data[0].size() - 1);
size_t n(rawPrices.size() - 1);
size_t m(rawPrices[0].size() - 1);

Mat _prices;
_prices.resize(m,Vec(n));

for(size_t i = 1;i < n+1;++i){
for(size_t j = 1;j < data[i].size();++j){
string tmp = data[i][j];
for(size_t j = 1;j < rawPrices[i].size();++j){
string tmp = rawPrices[i][j];
if(tmp.empty()){
_prices[j-1][i-1] = 99999.;
}
Expand All @@ -47,10 +47,10 @@ double CVaRVarianceCovariance(priceData data) {
}
}

vector<string> indexNames(data[0].size() - 1);
vector<string> indexNames(rawPrices[0].size() - 1);

for(size_t i = 1;i < data[0].size();++i){
indexNames[i-1] = data[0][i];
for(size_t i = 1;i < rawPrices[0].size();++i){
indexNames[i-1] = rawPrices[0][i];
}

// Remove missing values to compute trailling returns
Expand All @@ -68,29 +68,21 @@ double CVaRVarianceCovariance(priceData data) {
std::shared_ptr<ComputeReturn> cr(new ComputeReturn(prices,1,windowsize,true));

// create portfolio
double a = double(1./m);
std::vector<double> weights{a,a}; //initialization. Equi-weighted asset for mere convenience
Ptf _ptf;
for(size_t i = 0;i < m;++i){
Ptf _ptf;
for(size_t i = 0;i < m;++i){
shared_ptr<Instrument> instrument(new DeltaOne());
auto p = std::make_pair(i,instrument);
_ptf.push_back(p);
}
shared_ptr<Portfolio> ptf(new Portfolio(_ptf, weights, cr, false, 1.e+07));
_ptf.push_back(p);
}
shared_ptr<Portfolio> ptf(new Portfolio(_ptf, weights, cr, false, 1.e+07));

HistoricalVaR model;

double alphatest = .95; // set alpha level, example 0.9 for 95% CVaR

// CVaRVarianceCovariance
// as currently used in VIGOR
double CVaRVarianceCovariance = -100.0 * std::log(1.0 + (((std::exp(-1.0*(std::pow(-1.0*std::sqrt(2.0)*erfc_inv(2.0*alphatest),2.0))/2.0)/(std::sqrt(2.0*M_PI)))/(1.0-alphatest)) * (ptf->getPtfSdev()/100)));
cout << "CVaRVarianceCovariance: " << CVaRVarianceCovariance << endl;

return CVaRVarianceCovariance;

}

}


}
Loading

0 comments on commit c58778d

Please sign in to comment.