<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project Portfolio Management Simulator</title>
<style>
body{font-family:Arial,Helvetica,sans-serif;margin:20px;background:#f5f7fa;color:#333}
table{border-collapse:collapse;width:100%;margin:15px 0}
th,td{border:1px solid #ccc;padding:6px 8px;text-align:center}
th{background:#eee}
button{padding:8px 16px;font-size:15px;cursor:pointer}
.over-budget{background:#ffcece}
#summary{margin-top:20px;line-height:1.6}
canvas{border:1px solid #ccc;margin-top:10px}
</style>
</head>
<body>
<h1>Project Portfolio Management Simulator</h1>
<p>Select projects, set a budget, then run a Monte-Carlo simulation to see possible outcomes.</p>
<label>Portfolio Budget: $<input type="number" id="budget" value="800000" step="10000"></label>
<table id="projTable">
<thead>
<tr>
<th>Select</th>
<th>Project</th>
<th>Cost ($)</th>
<th>Expected Benefit ($)</th>
<th>Risk (0-1)</th>
<th>Strategic Alignment (1-5)</th>
</tr>
</thead>
<tbody></tbody>
</table>
<button id="runBtn">Run Simulation</button>
<div id="summary"></div>
<canvas id="hist" width="600" height="300"></canvas>
<script>
// ---- Project Data ----
const projects=[
{name:"Mobile App Revamp", cost:200000, benefit:450000, risk:0.25, align:5},
{name:"Cloud Migration", cost:300000, benefit:650000, risk:0.35, align:4},
{name:"AI Chatbot", cost:150000, benefit:350000, risk:0.40, align:3},
{name:"Legacy System Upgrade", cost:220000, benefit:300000, risk:0.20, align:4},
{name:"Market Expansion LATAM", cost:400000, benefit:900000, risk:0.45, align:5},
{name:"Cybersecurity Overhaul", cost:180000, benefit:320000, risk:0.15, align:5},
{name:"Data Warehouse", cost:250000, benefit:500000, risk:0.30, align:4},
{name:"E-commerce Optimization", cost:210000, benefit:480000, risk:0.28, align:4}
];
// ---- Build Table ----
const tbody=document.querySelector("#projTable tbody");
projects.forEach((p,i)=>{
const tr=document.createElement("tr");
tr.innerHTML=`<td><input type="checkbox" data-i="${i}"></td>
<td>${p.name}</td>
<td>${p.cost.toLocaleString()}</td>
<td>${p.benefit.toLocaleString()}</td>
<td>${p.risk}</td>
<td>${p.align}</td>`;
tbody.appendChild(tr);
});
// ---- Simulation ----
const ITER=1000;
document.getElementById("runBtn").onclick=()=>{
const sel=[...document.querySelectorAll('input[type="checkbox"]:checked')].map(cb=>+cb.dataset.i);
const budget=+document.getElementById("budget").value;
let cost=0, alignSum=0;
sel.forEach(i=>{cost+=projects[i].cost; alignSum+=projects[i].align;});
// highlight over-budget rows
tbody.querySelectorAll("tr").forEach((tr,idx)=>tr.classList.toggle("over-budget",sel.includes(idx)&&cost>budget));
// Monte-Carlo
const results=[];
for(let k=0;k<ITER;k++){
let benefit=0;
sel.forEach(i=>{
if(Math.random()>projects[i].risk) benefit+=projects[i].benefit;
});
results.push(benefit-cost);
}
const avg=results.reduce((a,b)=>a+b,0)/ITER;
const pos=results.filter(v=>v>0).length/ITER*100;
const worst=Math.min(...results);
const best=Math.max(...results);
document.getElementById("summary").innerHTML=
`<h3>Portfolio Summary</h3>
Total Cost: $${cost.toLocaleString()} (${cost<=budget?"✅ within":"❌ over"} budget)<br>
Selected Projects: ${sel.length}<br>
Avg. Strategic Alignment: ${sel.length?(alignSum/sel.length).toFixed(2):"N/A"}<br><br>
<h3>Simulation (${ITER} runs)</h3>
Avg. Net Return: $${avg.toFixed(0)}<br>
Chance of Positive Return: ${pos.toFixed(1)}%<br>
Best-case: $${best.toLocaleString()} | Worst-case: $${worst.toLocaleString()}`;
drawHist(results);
};
// ---- Histogram ----
function drawHist(data){
const cvs=document.getElementById("hist"),ctx=cvs.getContext("2d");
ctx.clearRect(0,0,cvs.width,cvs.height);
if(!data.length) return;
const bins=20, w=cvs.width, h=cvs.height, min=Math.min(...data), max=Math.max(...data), step=(max-min)/bins;
const hist=new Array(bins).fill(0);
data.forEach(v=>{let b=Math.floor((v-min)/step);if(b===bins)b--;hist[b]++;});
const maxFreq=Math.max(...hist);
const barW=w/bins;
hist.forEach((f,i)=>{
const barH=f/maxFreq*(h-20);
ctx.fillStyle="#4caf50";
ctx.fillRect(i*barW+1,h-barH,barW-2,barH);
});
ctx.fillStyle="#000";ctx.fillText("Net Return Distribution",10,15);
}
</script>
</body>
</html>