Published
Edited
Sep 17, 2021
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
assetNames = libra.sips
Insert cell
tokenY = 0 // position in libra.sips
Insert cell
tokenX = 3
Insert cell
trials = 1000
Insert cell
me = generateCopula(libra, "adffs")
Insert cell
Insert cell
Insert cell
function generateCopula(selfy, random) {
let ret = [];
let whichCorrelationMatrix = [];
// Do all of this for all copulas in document
selfy.U01.copula.forEach((copula) => {
if (copula.function == "GaussianCopula") {
// now get the cholesky factors
// get the global variable
let specifyCorrelationMatrix = copula.arguments.correlationMatrix.value;
let copulaArgs = copula.arguments.rng;
let randomMatrix = [];
for (let i = 0; i < copulaArgs.length; i++) {
console.log("caller ", copulaArgs[i]);
let val = generateRandom(copulaArgs[i], selfy.U01); // from U01/RNG
/* {
"counter": "PM_Index",
"entity": 1,
"varId": 6187319,
"seed3": 0,
"seed4": 0
} */
// let val = tempRando(1000);
//let val = [];

randomMatrix.push(val);
console.log("randomMatrix ", randomMatrix);
}

selfy.globalVariables.forEach((item, index) => {
if (item["name"] == specifyCorrelationMatrix) {
whichCorrelationMatrix = index;
} else {
let index = -1;
}
});

let thisCorrelationMatrix =
selfy.globalVariables[whichCorrelationMatrix].value.matrix;
let correlationMatrix = convertMx(thisCorrelationMatrix);

// Find the Cholesky Factors
//let lu = jStat.lu(correlationMatrix);
console.log("you are lower tri ", correlationMatrix);
let cho = jStat(jStat.cholesky(correlationMatrix));

//cho = cholesky(correlationMatrix, (lower = False)); // TODO: jstat?
console.log("Cholesky: \n", cho);

//cho = np.matrix(cho)
//Apply the Cholesky Factors to the randoms
// for (let i = 0; i < 1000; i++) {
// find out what rows of the hdr ret matrix to use based on what was asked for
let col = copula.copulaLayer.indexOf(random);
// TODO this part isn't completed yet. Need to pull in copula Layers)
//console.log("this is col ", col);
var corrSamples = cho.multiply(randomMatrix);
console.log("corrSamples ", corrSamples);
//console.log(randomMatrix);
//console.log("matrix mult ", corrSamples);
//let choSubSample = cho[(0, [col + 1, col])];
//console.log(choSubSample);
//let trial = randomMatrix[(1, [col + 2, i])];

// let invCdf = norm.ppf(trial).reshape(-1, col + 1);
// get HDRs from matrix
// let mMult = jStat.dot(invCdf, choSubSample);
// let val = norm.cdf(mMult);

//Convert to uniform correlated samples over 0,1 using normal CDF
var uniformCorrSamples = corrSamples.map(function (x) {
var normDist = jStat.normal(0, 1); //FTW Cholesky needs normal?

return normDist.cdf(x);
});

let val = uniformCorrSamples;
console.log(val);
ret.push(val);
// }
} else {
console.log(
"TypeError The function type for this copula is unsupported. "
);
}
});
// Add the result of the cholesky to the copula variable ??
self.copula = ret;
return ret;
}
Insert cell
Insert cell
xAxis = me[0][tokenX]
Insert cell
yAxis = me[0][tokenY]
Insert cell
jStat = require("jstat")
Insert cell
Insert cell
Insert cell
d3 = require("d3", "d3-array@^2.2")
Insert cell
Plotly = require("https://cdn.plot.ly/plotly-latest.min.js")
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more