Published
Edited
Nov 20, 2019
2 forks
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mkrRollingBeta = {
return dailyCloses[selectedIdx].map((close, idx) => {
let cleaned = dailyPercentageChanges[selectedIdx]
let btc = btcPercentChange.slice(-cleaned.length)
if (idx < betaLookback) return 0
let startIdx = idx - betaLookback
return calculateBeta(cleaned.slice(startIdx, idx), btc.slice(startIdx, idx))
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mkrForwardChange = getForwardChange(dailyPercentageChanges[selectedIdx], dailyCloses[selectedIdx])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mkrForwardChangeClean = betaChart.map(data => [data.beta, data.forwardChange]).filter(x => !isNaN(x[0]))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {batExpectedReturn} from '@bgits/crypto-protocol-expected-returns'
Insert cell
Insert cell
Insert cell
Insert cell
permutatedRiskReturnsChartNoBat = vz(permutatedRiskReturnsNoBat)
.y('returns')
.x('portfolioStd')
.detail('details', 'n')
.width(900)
.height(600)
.plot();
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
permutatedRiskReturnsNoBat = {
return [
['DAI', 'MKR', 'SNT', 'ETH'],
['DAI', 'SNT', 'ETH', 'MKR'],
['MKR', 'DAI','ETH', 'SNT'],
['SNT', 'ETH', 'MKR','DAI']
]
.map(calculatePortfolios)
.reduce((pv,cv) => [...pv, ...cv.riskReturns],[])
}
Insert cell
Insert cell
Insert cell
mkrCollateralTypes = ['SNT', 'ETH', 'GNT', 'DGD', 'ZRX', 'BAT', 'OMG', 'REP']
Insert cell
makerCDPCovariances = coorelationMatrix(mkrCollateralTypes)
Insert cell
cdpKeys = Object.keys(makerCDPCovariances)
Insert cell
makerAvgCovariances = mkrCollateralTypes.map(sym => {
const values = cdpKeys.filter(key => key.includes(sym)).map(k => makerCDPCovariances[k])
return {[sym]: simpleStatistics.mean(values)}
})
Insert cell
calculatePortfolios = (syms = []) => {
const coorelations = coorelationMatrix(syms)
const weights = combinationSum(syms.length, 100).map(x => x.map(x => x/100))
const returns = weights.map(comb => simpleStatistics.sum(comb.map((weight,i) => weight * forwardExpectedReturn[syms[i]])))
//TODO look into use PMPT - sortino/ downside volatility only
const stds = syms.map(ticker => {
const pctChanges = getSymbolPercentageChanges(ticker).slice(-sharpeLookback)
const std = simpleStatistics.standardDeviation(pctChanges)
return std
})
const weightedStds = weights.map(comb => simpleStatistics.sum(comb.map((weight, i) => (weight * stds[i])**2)))
const weightedCors = weights.map(weights => {
let cors = []
for(let i=0;i<syms.length;i++){
for(let j=i+1;j<syms.length;j++){
const first = syms[i]
const second = syms[j]
const key = `${first}_${second}`
const cor = coorelations[key]
const result = 2*weights[i]*weights[j]*stds[i]*stds[j]*cor
cors.push(result)
}
}
return simpleStatistics.sum(cors)
})
const portfolioStds = weightedStds.map((std, i) => Math.sqrt(std + weightedCors[i]))
const details = weights
.map(w => w.map((w, i) => `${syms[i]}: ${w}`))
.map(w => w.reduce((pv,cv) => `${pv} ${cv}`))
.map((w,i) => `${w} sharpe: ${(returns[i] - riskFreeRate) / portfolioStds[i]}`)
const riskReturns = portfolioStds.map((p,i) => ({ portfolioStd: p, returns: returns[i], details: details[i]}))
return { weights,returns, portfolioStds, stds, weightedStds, coorelations, weightedCors, riskReturns, details }
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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