Conditional Value at Risk (CVaR) Analyzer

Enter inputs to see results.

Confidence Level: ${data.confidenceLevel.toFixed(2)}% | Time Horizon: ${data.timeHorizon} days

Simulations: ${data.simulations}

VaR: $${data.results.varValue} (${data.results.varPercent}%) | CVaR: $${data.results.cvarValue} (${data.results.cvarPercent}%)

Tail Losses (Worst ${Math.min(100, data.results.tailLosses.length)} Scenarios)

${data.results.tailLosses.map(item => ` `).join('')}
Simulation Loss (USD) Final Portfolio Value (USD)
${item.simulation} $${item.loss.toFixed(2)} $${item.finalValue.toFixed(2)}

Recommendations:

${data.recommendations}

`; resultsTable.innerHTML = tableHtml; } // Sort Table window.sortTable = function (n) { const table = document.querySelector('.results-table'); if (!table) return; let rows, switching = true, i, x, y, shouldSwitch, dir = 'asc', switchcount = 0; while (switching) { switching = false; rows = table.rows; for (i = 1; i < (rows.length - 1); i++) { shouldSwitch = false; x = rows[i].getElementsByTagName('TD')[n]; y = rows[i + 1].getElementsByTagName('TD')[n]; let xVal = x.innerHTML.toLowerCase().replace('$', ''); let yVal = y.innerHTML.toLowerCase().replace('$', ''); if (n === 0) { xVal = parseInt(xVal) || 0; yVal = parseInt(yVal) || 0; } else { xVal = parseFloat(xVal) || 0; yVal = parseFloat(yVal) || 0; } if (dir === 'asc') { if (xVal > yVal) { shouldSwitch = true; break; } } else if (dir === 'desc') { if (xVal < yVal) { shouldSwitch = true; break; } } } if (shouldSwitch) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; switchcount++; } else if (switchcount === 0 && dir === 'asc') { dir = 'desc'; switching = true; } } }; // Tab Navigation window.openTab = function (tabId) { const tabs = document.querySelectorAll('.tab-content'); const tabLinks = document.querySelectorAll('.tab-link'); tabs.forEach(tab => tab.classList.remove('active')); tabLinks.forEach(link => link.classList.remove('active')); const activeTab = document.getElementById(tabId); if (activeTab) { activeTab.classList.add('active'); const activeLink = document.querySelector(`.tab-link[onclick="openTab('${tabId}')"]`); if (activeLink) activeLink.classList.add('active'); } }; // Download PDF window.downloadPDF = function () { const resultsTable = document.getElementById('results-table'); const pdfContent = resultsTable.querySelector('.pdf-content'); if (!resultsTable || !pdfContent || !pdfContent.querySelector('.results-table')) { alert('No results available to download. Please calculate results first.'); return; } // Check if pdfMake and vfs are available if (typeof pdfMake === 'undefined' || typeof pdfMake.vfs === 'undefined') { alert('PDF generation library failed to load. Please check your internet connection and try again.'); console.error('pdfMake or vfs is not defined. Library may have failed to load.'); return; } try { // Extract table data const rows = []; const headers = ['Simulation', 'Loss (USD)', 'Final Portfolio Value (USD)']; rows.push(headers.map(header => ({ text: header, style: 'tableHeader' }))); const tableRows = pdfContent.querySelectorAll('.results-table tbody tr'); if (tableRows.length === 0) { throw new Error('No table rows found in results.'); } tableRows.forEach(row => { const cells = row.querySelectorAll('td'); const rowData = Array.from(cells).map(cell => ({ text: cell.textContent })); rows.push(rowData); }); // Extract summary data const summaryData = Array.from(pdfContent.querySelectorAll('p')).map(p => p.textContent); const recommendations = pdfContent.querySelector('.summary p')?.textContent || 'N/A'; // Define PDF document structure const docDefinition = { content: [ { text: 'Conditional Value at Risk (CVaR) Analysis Report', style: 'header' }, { text: '\n' }, { text: summaryData.join('\n'), style: 'body' }, { text: '\n' }, { text: 'Tail Losses', style: 'subheader' }, { table: { headerRows: 1, widths: ['auto', 'auto', 'auto'], body: rows }, layout: { fillColor: function (rowIndex) { return (rowIndex % 2 === 0 && rowIndex > 0) ? '#f2f2f2' : null; }, hLineColor: '#ddd', vLineColor: '#ddd' } }, { text: '\n' }, { text: 'Recommendations', style: 'subheader' }, { text: recommendations, style: 'body' } ], styles: { header: { fontSize: 18, bold: true, alignment: 'center', color: '#333' }, subheader: { fontSize: 14, bold: true, color: '#333' }, tableHeader: { bold: true, fontSize: 12, color: 'white', fillColor: '#007BFF' }, body: { fontSize: 10, color: '#333' } }, defaultStyle: { fontSize: 10, color: '#333' }, pageMargins: [40, 60, 40, 60] }; // Generate and download PDF pdfMake.createPdf(docDefinition).download('CVaR_Analysis_Report.pdf'); } catch (err) { console.error('PDF generation failed:', err.message, err.stack); alert('Failed to generate PDF due to an internal error. Please ensure results are valid and try again.'); } }; });
Scroll to Top