-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiversification_tool_type1_odd.html
More file actions
360 lines (313 loc) · 12.7 KB
/
diversification_tool_type1_odd.html
File metadata and controls
360 lines (313 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Diversification Tool (2 Assets + Cash)</title>
<style>
:root{
--bg:#0b1220; --card:#111a2e; --card2:#0f1830;
--text:#e9eefc; --muted:#a9b6d6; --line:rgba(255,255,255,0.10);
--accent:#7aa2ff; --accent2:#8be9fd;
--good:#51cf66; --bad:#ff6b6b; --warn:#ffd43b;
}
*{box-sizing:border-box}
body{margin:0; font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; background:var(--bg); color:var(--text);}
.wrap{max-width:1100px; margin:24px auto; padding:0 16px;}
h1{font-size:20px; margin:0 0 6px; font-weight:800;}
p{margin:0 0 14px; color:var(--muted); line-height:1.35;}
.grid{display:grid; grid-template-columns:1fr; gap:14px;}
@media(min-width:900px){ .grid{grid-template-columns: 1.05fr 0.95fr;} }
.card{
background:var(--card); border:1px solid var(--line);
border-radius:16px; padding:14px;
box-shadow: 0 10px 28px rgba(0,0,0,0.25);
}
.card h2{font-size:15px; margin:0 0 10px; font-weight:750;}
.row{display:grid; grid-template-columns: 1fr 1fr; gap:10px; margin-bottom:10px;}
label{display:block; font-size:12px; color:var(--muted); margin-bottom:6px;}
input{
width:100%; padding:10px 10px; border-radius:12px;
border:1px solid rgba(255,255,255,0.12);
background: rgba(0,0,0,0.18);
color:var(--text);
outline:none;
}
input:focus{border-color: rgba(122,162,255,0.7); box-shadow:0 0 0 3px rgba(122,162,255,0.15);}
.btnRow{display:grid; grid-template-columns: 1fr 1fr; gap:10px; margin-top:10px;}
button{
cursor:pointer; border:0; border-radius:12px;
padding:10px 12px; font-weight:800;
background:var(--accent); color:#08122e;
}
button.secondary{background:rgba(255,255,255,0.10); color:var(--text); border:1px solid var(--line);}
button:hover{filter:brightness(1.05);}
.mono{font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;}
.note{font-size:12px; color:var(--muted); line-height:1.35; margin-top:8px;}
hr{border:0; border-top:1px solid var(--line); margin:12px 0;}
table{width:100%; border-collapse: collapse; margin-top:8px;}
th, td{padding:8px; border-bottom:1px solid var(--line); text-align:right;}
th:first-child, td:first-child{text-align:left;}
th{font-size:12px; color:var(--muted); font-weight:700;}
.kpi{
display:grid; grid-template-columns:1fr; gap:10px;
}
@media(min-width:900px){ .kpi{grid-template-columns: 1fr 1fr;} }
.kpiBox{
background:var(--card2); border:1px solid var(--line);
border-radius:14px; padding:10px;
}
.kpiTitle{font-size:12px; color:var(--muted); margin-bottom:6px;}
.kpiVal{font-size:18px; font-weight:900;}
.pill{display:inline-block; padding:3px 8px; border-radius:999px; font-size:12px; border:1px solid transparent; margin-left:8px;}
.pill.good{color:var(--good); border-color:rgba(81,207,102,0.35); background:rgba(81,207,102,0.10);}
.pill.bad{color:var(--bad); border-color:rgba(255,107,107,0.35); background:rgba(255,107,107,0.10);}
.warn{display:none; margin-top:10px; padding:10px; border-radius:14px;
border:1px solid rgba(255,212,59,0.35); background:rgba(255,212,59,0.10); color:var(--text);}
.matrix{
display:grid;
grid-template-columns: 120px 1fr 1fr;
gap:8px;
align-items:center;
margin-top:8px;
}
.mHead{font-size:12px; color:var(--muted); font-weight:800; text-align:right; padding-right:6px;}
.mCorner{font-size:12px; color:var(--muted); font-weight:800;}
.mRowLab{font-size:12px; color:var(--muted); font-weight:800; text-align:right; padding-right:6px;}
.smallInput{padding:9px 10px;}
</style>
</head>
<body>
<div class="wrap">
<h1>Diversification Tool — Type 1 (odd-numbered users)</h1>
<p>Initial endowments for this version: cash = 12, Asset A = 16 units, Asset B = 2 units.</p>
<div class="grid">
<div class="card">
<h2>Current holdings</h2>
<div class="row">
<div>
<label>Risk aversion γ</label>
<input id="gamma" type="number" step="0.0001" value="0.06" />
</div>
<div>
<label> </label>
<button id="computeBtn">Compute</button>
</div>
</div>
<div class="row">
<div>
<label>Current cash (c)</label>
<input id="c" type="number" step="0.01" value="12" />
</div>
<div>
<label>Current units of Asset A (x_A)</label>
<input id="xA" type="number" step="0.01" value="16" />
</div>
</div>
<div class="row">
<div>
<label>Current units of Asset B (x_B)</label>
<input id="xB" type="number" step="0.01" value="2" />
</div>
<div>
<label> </label>
<button class="secondary" id="resetHoldingsBtn">Reset current holdings to initial (c0, x0)</button>
</div>
</div>
<div class="btnRow">
<button class="secondary" id="clearHypBtn">Clear hypothetical trades & prices (set to 0)</button>
<div></div>
</div>
<hr/>
<h2>Hypothetical trades</h2>
<div class="note">
Enter quantities (units) and prices. Empty price cells are treated as <span class="mono">0</span>.
Buys reduce cash; sells increase cash.
</div>
<div style="margin-top:10px;">
<div class="note" style="font-weight:800; color:var(--accent2); margin-bottom:6px;">Quantities (units)</div>
<div class="matrix">
<div class="mCorner"></div>
<div class="mHead">Buy</div>
<div class="mHead">Sell</div>
<div class="mRowLab">Asset A</div>
<input class="smallInput" id="qA_buy" type="number" step="0.01" value="0" />
<input class="smallInput" id="qA_sell" type="number" step="0.01" value="0" />
<div class="mRowLab">Asset B</div>
<input class="smallInput" id="qB_buy" type="number" step="0.01" value="0" />
<input class="smallInput" id="qB_sell" type="number" step="0.01" value="0" />
</div>
</div>
<div style="margin-top:12px;">
<div class="note" style="font-weight:800; color:var(--accent2); margin-bottom:6px;">Prices ($ per unit)</div>
<div class="matrix">
<div class="mCorner"></div>
<div class="mHead">Buy</div>
<div class="mHead">Sell</div>
<div class="mRowLab">Asset A</div>
<input class="smallInput" id="pA_buy" type="number" step="0.01" placeholder="0" value="0" />
<input class="smallInput" id="pA_sell" type="number" step="0.01" placeholder="0" value="0" />
<div class="mRowLab">Asset B</div>
<input class="smallInput" id="pB_buy" type="number" step="0.01" placeholder="0" value="0" />
<input class="smallInput" id="pB_sell" type="number" step="0.01" placeholder="0" value="0" />
</div>
</div>
<div id="warnBox" class="warn"></div>
</div>
<div class="card">
<h2>Outputs</h2>
<div class="kpi">
<div class="kpiBox">
<div class="kpiTitle">Baseline utility (at initial endowments)</div>
<div class="kpiVal" id="U0">—</div>
</div>
<div class="kpiBox">
<div class="kpiTitle">Current utility (at entered holdings)</div>
<div class="kpiVal" id="Uc">—</div>
</div>
<div class="kpiBox">
<div class="kpiTitle">Current performance = U(current) − U(baseline)</div>
<div class="kpiVal" id="Perf">—</div>
</div>
<div class="kpiBox">
<div class="kpiTitle">Hypothetical improvement = U(post-trade) − U(current)</div>
<div class="kpiVal" id="Imp">—</div>
</div>
</div>
<hr/>
<h2>Post-trade holdings implied by hypothetical trades</h2>
<table>
<thead>
<tr>
<th>Scenario</th>
<th>x_A</th>
<th>x_B</th>
<th>Cash c</th>
<th>U</th>
</tr>
</thead>
<tbody id="tableBody"></tbody>
</table>
</div>
</div>
</div>
<script>
const MU = [1, 1];
const SIGMA = [
[2/3, -1/3],
[-1/3, 2/3]
];
const BASELINE = { c0: 12, xA0: 16, xB0: 2 };
function getNum(id){
const el = document.getElementById(id);
const v = (el.value ?? "").toString().trim();
if (v === "") return 0;
const n = Number(v);
return Number.isFinite(n) ? n : 0;
}
function fmt(x){
if (!Number.isFinite(x)) return "—";
return x.toLocaleString(undefined, { maximumFractionDigits: 6 });
}
function quadFormSigma(xA, xB){
const s11 = SIGMA[0][0], s12 = SIGMA[0][1], s22 = SIGMA[1][1];
return (xA*xA*s11) + (2*xA*xB*s12) + (xB*xB*s22);
}
function utility(xA, xB, c, gamma){
const meanPart = xA*MU[0] + xB*MU[1] + c;
const riskPart = (gamma/2) * quadFormSigma(xA, xB);
return meanPart - riskPart;
}
function updateHoldingsToBaseline(){
document.getElementById("c").value = BASELINE.c0;
document.getElementById("xA").value = BASELINE.xA0;
document.getElementById("xB").value = BASELINE.xB0;
}
function clearHypotheticals(){
["qA_buy","qA_sell","qB_buy","qB_sell","pA_buy","pA_sell","pB_buy","pB_sell"].forEach(id=>{
document.getElementById(id).value = 0;
});
}
function compute(){
const warnBox = document.getElementById("warnBox");
warnBox.style.display = "none";
warnBox.innerHTML = "";
const gamma = getNum("gamma");
const U0 = utility(BASELINE.xA0, BASELINE.xB0, BASELINE.c0, gamma);
const c = getNum("c");
const xA = getNum("xA");
const xB = getNum("xB");
const Uc = utility(xA, xB, c, gamma);
const qA_buy = getNum("qA_buy");
const qA_sell = getNum("qA_sell");
const qB_buy = getNum("qB_buy");
const qB_sell = getNum("qB_sell");
const pA_buy = getNum("pA_buy");
const pA_sell = getNum("pA_sell");
const pB_buy = getNum("pB_buy");
const pB_sell = getNum("pB_sell");
const dA = qA_buy - qA_sell;
const dB = qB_buy - qB_sell;
const xA2 = xA + dA;
const xB2 = xB + dB;
const cashChange = -(qA_buy*pA_buy + qB_buy*pB_buy) + (qA_sell*pA_sell + qB_sell*pB_sell);
const c2 = c + cashChange;
const Up = utility(xA2, xB2, c2, gamma);
const perf = Uc - U0;
const imp = Up - Uc;
const warnings = [];
if (c2 < 0) warnings.push(`Post-trade cash is negative: c' = ${fmt(c2)}.`);
if (xA2 < 0) warnings.push(`Post-trade x_A is negative (short): x_A' = ${fmt(xA2)}.`);
if (xB2 < 0) warnings.push(`Post-trade x_B is negative (short): x_B' = ${fmt(xB2)}.`);
if (warnings.length){
warnBox.style.display = "block";
warnBox.innerHTML = "<b>Note:</b><br/>" + warnings.map(w => `• ${w}`).join("<br/>");
}
document.getElementById("U0").textContent = fmt(U0);
document.getElementById("Uc").textContent = fmt(Uc);
const perfEl = document.getElementById("Perf");
perfEl.innerHTML = `${fmt(perf)}${perf>=0 ? ` <span class="pill good">≥ 0</span>` : ` <span class="pill bad">< 0</span>`}`;
const impEl = document.getElementById("Imp");
impEl.innerHTML = `${fmt(imp)}${imp>=0 ? ` <span class="pill good">≥ 0</span>` : ` <span class="pill bad">< 0</span>`}`;
const body = document.getElementById("tableBody");
body.innerHTML = `
<tr>
<td>Baseline (initial)</td>
<td>${fmt(BASELINE.xA0)}</td>
<td>${fmt(BASELINE.xB0)}</td>
<td>${fmt(BASELINE.c0)}</td>
<td><b>${fmt(U0)}</b></td>
</tr>
<tr>
<td>Current (entered)</td>
<td>${fmt(xA)}</td>
<td>${fmt(xB)}</td>
<td>${fmt(c)}</td>
<td><b>${fmt(Uc)}</b></td>
</tr>
<tr>
<td>Post-trade (hypothetical)</td>
<td>${fmt(xA2)}</td>
<td>${fmt(xB2)}</td>
<td>${fmt(c2)}</td>
<td><b>${fmt(Up)}</b></td>
</tr>
`;
}
document.getElementById("computeBtn").addEventListener("click", compute);
document.getElementById("resetHoldingsBtn").addEventListener("click", ()=>{
updateHoldingsToBaseline();
compute();
});
document.getElementById("clearHypBtn").addEventListener("click", ()=>{
clearHypotheticals();
compute();
});
["gamma","c","xA","xB","qA_buy","qA_sell","qB_buy","qB_sell","pA_buy","pA_sell","pB_buy","pB_sell"]
.forEach(id => document.getElementById(id).addEventListener("input", compute));
updateHoldingsToBaseline();
clearHypotheticals();
compute();
</script>
</body>
</html>