Machine Learning-Driven Cross-Asset Risk Correlation Tool

Machine Learning-Driven Cross-Asset Risk Correlation Tool

Analyze the risk correlation between a primary asset and other market assets.

Analysis Parameters

This is the core asset of your portfolio you want to analyze.

Choose the asset to measure the risk relationship against.

The lookback period for the correlation analysis.

Awaiting Analysis

Select your assets and click "Analyze Correlation" to view the results.

${corrText}

${betaText}

`; } /** * Creates or updates the Chart.js scatter plot. */ function createOrUpdateChart(data, primaryLabel, secondaryLabel) { const ctx = chartCanvas.getContext('2d'); if (correlationChart) { correlationChart.destroy(); } correlationChart = new Chart(ctx, { type: 'scatter', data: { datasets: [{ label: 'Monthly Returns', data: data, backgroundColor: 'rgba(79, 70, 229, 0.6)', borderColor: 'rgba(79, 70, 229, 1)', }] }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { title: { display: true, text: `${primaryLabel} Monthly Return (%)`, font: { weight: 'bold' } }, ticks: { callback: val => val + '%' } }, y: { title: { display: true, text: `${secondaryLabel} Monthly Return (%)`, font: { weight: 'bold' } }, ticks: { callback: val => val + '%' } } }, plugins: { legend: { display: false }, tooltip: { callbacks: { label: (context) => `(${context.parsed.x.toFixed(2)}%, ${context.parsed.y.toFixed(2)}%)` } } } } }); } // --- Helper functions for calculation (based on simulated data) --- function sum(arr) { return arr.reduce((acc, val) => acc + val, 0); } function mean(arr) { return sum(arr) / arr.length; } function stdDev(arr) { const mu = mean(arr); return Math.sqrt(sum(arr.map(x => (x - mu) ** 2)) / arr.length); } function covariance(arr1, arr2) { const mean1 = mean(arr1); const mean2 = mean(arr2); let covar = 0; for (let i = 0; i < arr1.length; i++) { covar += (arr1[i] - mean1) * (arr2[i] - mean2); } return covar / arr1.length; } function calculateCorrelation(arr1, arr2) { return covariance(arr1, arr2) / (stdDev(arr1) * stdDev(arr2)); } function calculateBeta(arr1, arr2) { return covariance(arr1, arr2) / (stdDev(arr1) ** 2); } /** * Handles the PDF generation process. * Adheres to Specification II.C. */ async function generatePDF() { const contentToPrint = document.getElementById('pdf-content'); if (!contentToPrint || resultsContainer.classList.contains('hidden')) { alert("Please run an analysis first to generate results."); return; } pdfBtn.disabled = true; pdfSpinner.classList.remove('hidden'); pdfBtn.querySelector('span').textContent = 'Generating...'; try { const canvas = await html2canvas(contentToPrint, { scale: 2, backgroundColor: '#ffffff' }); const imgData = canvas.toDataURL('image/png'); const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgProps = pdf.getImageProperties(imgData); const imgRatio = imgProps.height / imgProps.width; let imgWidth = pdfWidth - 20; let imgHeight = imgWidth * imgRatio; if (imgHeight > pdfHeight - 20) { imgHeight = pdfHeight - 20; imgWidth = imgHeight / imgRatio; } const x = (pdfWidth - imgWidth) / 2; const y = 10; pdf.addImage(imgData, 'PNG', x, y, imgWidth, imgHeight); const primaryTicker = primaryAssetSelect.value; const secondaryTicker = secondaryAssetSelect.value; pdf.save(`Risk-Correlation-Analysis-${primaryTicker}-vs-${secondaryTicker}.pdf`); } catch (error) { console.error("Error generating PDF:", error); alert("An error occurred while generating the PDF."); } finally { pdfBtn.disabled = false; pdfSpinner.classList.add('hidden'); pdfBtn.querySelector('span').textContent = 'Download Results as PDF'; } } });
Scroll to Top