{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Monte Carlo simulation for portfolio optimization",
"width": 300,
"height": 300,
"data": {
"sequence": {
"start": 0,
"stop": 10000,
"step": 1,
"as": "index"
}
},
"transform": [
{
"calculate": "random()",
"as": "r1"
},
{
"calculate": "random()",
"as": "r2"
},
{
"calculate": "random()",
"as": "r3"
},
{
"calculate": "random()",
"as": "r4"
},
{
"calculate": "random()",
"as": "r5"
},
{
"calculate": "datum.r1 + datum.r2 + datum.r3 + datum.r4 + datum.r5",
"as": "total"
},
{
"calculate": "datum.r1 / datum.total",
"as": "w1"
},
{
"calculate": "datum.r2 / datum.total",
"as": "w2"
},
{
"calculate": "datum.r3 / datum.total",
"as": "w3"
},
{
"calculate": "datum.r4 / datum.total",
"as": "w4"
},
{
"calculate": "datum.r5 / datum.total",
"as": "w5"
},
{
"calculate": "0.05 * datum.w1 + 0.08 * datum.w2 + 0.12 * datum.w3 + 0.07 * datum.w4 + 0.10 * datum.w5",
"as": "return"
},
{
"calculate": "sqrt(pow(0.12 * datum.w1, 2) + pow(0.15 * datum.w2, 2) + pow(0.20 * datum.w3, 2) + pow(0.18 * datum.w4, 2) + pow(0.22 * datum.w5, 2) + 2 * 0.05 * 0.12 * 0.15 * datum.w1 * datum.w2 + 2 * 0.08 * 0.12 * 0.20 * datum.w1 * datum.w3 + 2 * 0.03 * 0.12 * 0.18 * datum.w1 * datum.w4 + 2 * 0.06 * 0.12 * 0.22 * datum.w1 * datum.w5 + 2 * 0.10 * 0.15 * 0.20 * datum.w2 * datum.w3 + 2 * 0.07 * 0.15 * 0.18 * datum.w2 * datum.w4 + 2 * 0.09 * 0.15 * 0.22 * datum.w2 * datum.w5 + 2 * 0.12 * 0.20 * 0.18 * datum.w3 * datum.w4 + 2 * 0.14 * 0.20 * 0.22 * datum.w3 * datum.w5 + 2 * 0.11 * 0.18 * 0.22 * datum.w4 * datum.w5)",
"as": "risk"
},
{
"calculate": "(datum.return - 0.02) / datum.risk",
"as": "sharpe_ratio"
}
],
"layer": [
{
"mark": {
"type": "point",
"filled": true,
"opacity": 0.3
},
"encoding": {
"x": {
"field": "risk",
"type": "quantitative",
"title": "Risk (Standard Deviation)",
"scale": {
"domain": [
0.05,
0.2
]
}
},
"y": {
"field": "return",
"type": "quantitative",
"title": "Expected Return",
"scale": {
"domain": [
0.05,
0.12
]
}
},
"color": {
"field": "sharpe_ratio",
"type": "quantitative",
"title": "Sharpe Ratio"
}
}
},
{
"transform": [
{
"window": [
{
"op": "rank",
"as": "rank"
}
],
"sort": [
{
"field": "sharpe_ratio",
"order": "descending"
}
]
},
{
"filter": "datum.rank == 1"
}
],
"mark": {
"type": "point",
"color": "red",
"size": 100
},
"encoding": {
"x": {
"field": "risk",
"type": "quantitative"
},
"y": {
"field": "return",
"type": "quantitative"
},
"tooltip": [
{
"field": "w1",
"title": "Weight 1",
"format": ".2f"
},
{
"field": "w2",
"title": "Weight 2",
"format": ".2f"
},
{
"field": "w3",
"title": "Weight 3",
"format": ".2f"
},
{
"field": "w4",
"title": "Weight 4",
"format": ".2f"
},
{
"field": "w5",
"title": "Weight 5",
"format": ".2f"
},
{
"field": "return",
"title": "Return",
"format": ".2f"
},
{
"field": "risk",
"title": "Risk",
"format": ".2f"
},
{
"field": "sharpe_ratio",
"title": "Sharpe Ratio",
"format": ".2f"
}
]
}
}
]
}