`).join('');
};
const renderCharts = (metrics) => {
const chartContexts = {
gauge: document.getElementById('risk-gauge-chart').getContext('2d'),
contribution: document.getElementById('contribution-chart').getContext('2d'),
};
Object.values(charts).forEach(chart => chart.destroy());
const riskColor = metrics.riskLevel === 'Low' ? '#16a34a' :
metrics.riskLevel === 'Elevated' ? '#facc15' :
metrics.riskLevel === 'High' ? '#f97316' : '#dc2626';
charts.gauge = new Chart(chartContexts.gauge, {
type: 'doughnut',
data: {
datasets: [{
data: [metrics.probability, 100 - metrics.probability],
backgroundColor: [riskColor, '#e5e7eb'],
borderWidth: 0,
}]
},
options: {
rotation: -90,
circumference: 180,
cutout: '70%',
plugins: { tooltip: { enabled: false } }
}
});
document.getElementById('risk-gauge-label').textContent = `${metrics.probability.toFixed(1)}%`;
document.getElementById('risk-gauge-label').className = `text-4xl font-bold mt-[-50px] ${`risk-${metrics.riskLevel.split(' ')[0].toLowerCase()}`}`;
charts.contribution = new Chart(chartContexts.contribution, {
type: 'bar',
data: {
labels: metrics.factorScores.map(f => f.label),
datasets: [{
label: 'Contribution to Risk Score',
data: metrics.factorScores.map(f => f.contribution),
backgroundColor: '#4f46e5',
}]
},
options: {
indexAxis: 'y',
scales: { x: { beginAtZero: true, title: { display: true, text: 'Weighted Risk Points'} } },
plugins: { legend: { display: false } }
}
});
};
const renderAnalysis = (metrics) => {
const container = document.getElementById('analysis-output');
let outlook;
if (metrics.riskLevel === 'Low') {
outlook = `The model predicts a low probability of a significant financial risk event in the next 12 months. Macroeconomic indicators appear stable. The ${metrics.keyFactor.toLowerCase()} is the largest, yet still minor, contributor to the overall risk score. Continued monitoring is advised, but immediate systemic risks appear contained.`;
} else if (metrics.riskLevel === 'Elevated') {
outlook = `The model indicates an elevated risk of a financial event. While not a certainty, conditions are showing signs of stress. The primary driver is the ${metrics.keyFactor.toLowerCase()}. This suggests a heightened chance of market volatility or a minor economic downturn. Increased caution and portfolio review are recommended.`;
} else if (metrics.riskLevel === 'High') {
outlook = `The model predicts a high probability of a financial risk event. Multiple indicators are flashing warning signs, with the ${metrics.keyFactor.toLowerCase()} being the most significant concern. This scenario aligns with conditions that have historically preceded recessions or major market corrections. Defensive positioning and risk mitigation strategies should be strongly considered.`;
} else { // Severe
outlook = `The model predicts a severe and imminent risk of a major financial event. A confluence of negative indicators, led by the ${metrics.keyFactor.toLowerCase()}, points towards significant systemic instability. The probability is high for a recession, a market crash, or a credit event. Maximum caution and capital preservation strategies are paramount.`;
}
container.innerHTML = `
Risk Prediction Analysis
Predicted Outlook: ${metrics.riskLevel} Risk
${outlook}
Factor Breakdown
The total risk score of ${metrics.totalScore.toFixed(0)} out of 100 was calculated based on the following weighted contributions:
-
${metrics.factorScores.map(f => `
- ${f.label}: Contributed ${f.contribution.toFixed(1)} points to the total score. `).join('')}
