|
| 1 | +/** |
| 2 | + * Validation: Simulation loop math against geometric series formula |
| 3 | + */ |
| 4 | + |
| 5 | +// Test 1: Loop leverage calculation |
| 6 | +function testLoopLeverage() { |
| 7 | + const F = 0.9; |
| 8 | + const capital = 1000; |
| 9 | + const price = 0.40; |
| 10 | + const loops = 10; |
| 11 | + |
| 12 | + // Simulate loop |
| 13 | + let total = 0; |
| 14 | + let usd = capital; |
| 15 | + for (let i = 0; i < loops; i++) { |
| 16 | + const tokens = usd / price; |
| 17 | + total += tokens; |
| 18 | + usd = tokens * F * price; // CORRECT formula |
| 19 | + if (usd < 1) break; |
| 20 | + } |
| 21 | + |
| 22 | + // Expected (geometric series) |
| 23 | + const multiplier = (1 - Math.pow(F, loops)) / (1 - F); |
| 24 | + const expected = (capital / price) * multiplier; |
| 25 | + |
| 26 | + console.log('=== LOOP LEVERAGE TEST ==='); |
| 27 | + console.log(`Capital: $${capital}, Price: ${price}, F: ${F}, Loops: ${loops}`); |
| 28 | + console.log(''); |
| 29 | + console.log('Simulated:'); |
| 30 | + console.log(` Total tokens: ${total.toFixed(0)}`); |
| 31 | + console.log(` Leverage: ${(total * price / capital).toFixed(2)}x`); |
| 32 | + console.log(''); |
| 33 | + console.log('Expected (formula):'); |
| 34 | + console.log(` Total tokens: ${expected.toFixed(0)}`); |
| 35 | + console.log(` Leverage: ${(expected * price / capital).toFixed(2)}x`); |
| 36 | + console.log(` Multiplier: ${multiplier.toFixed(2)}x capital`); |
| 37 | + console.log(''); |
| 38 | + |
| 39 | + const error = Math.abs(total - expected) / expected; |
| 40 | + const pass = error < 0.001; |
| 41 | + console.log(pass ? `PASS: ${(error * 100).toFixed(4)}% error` : `FAIL: ${(error * 100).toFixed(2)}% error`); |
| 42 | + console.log(''); |
| 43 | + |
| 44 | + return pass; |
| 45 | +} |
| 46 | + |
| 47 | +// Test 2: Runway validation |
| 48 | +function testRunway() { |
| 49 | + const F = 0.9; |
| 50 | + const R = 0.10; // 10% APR |
| 51 | + const YEAR = 365 * 24 * 3600; |
| 52 | + |
| 53 | + // Runway formula: (1 - F) / (F * R) * year |
| 54 | + const runway = ((1 - F) / (F * R)) * YEAR; |
| 55 | + const safeRunway = runway * 0.95; |
| 56 | + |
| 57 | + console.log('=== RUNWAY VALIDATION TEST ==='); |
| 58 | + console.log(`F: ${F}, R: ${R} (${(R * 100).toFixed(0)}% APR)`); |
| 59 | + console.log(''); |
| 60 | + console.log(`Runway: ${(runway / 3600).toFixed(1)} hours`); |
| 61 | + console.log(`Safe runway (95%): ${(safeRunway / 3600).toFixed(1)} hours`); |
| 62 | + console.log(''); |
| 63 | + |
| 64 | + // Verify debt at runway |
| 65 | + const tau = runway / YEAR; |
| 66 | + const debt = F * (1 + R * tau); |
| 67 | + console.log('Verification:'); |
| 68 | + console.log(` Debt at runway: ${debt.toFixed(4)} (should be ~1.00)`); |
| 69 | + console.log(` Error: ${((debt - 1) * 100).toFixed(2)}%`); |
| 70 | + console.log(''); |
| 71 | + |
| 72 | + const pass = Math.abs(debt - 1) < 0.001; |
| 73 | + console.log(pass ? 'PASS' : 'FAIL'); |
| 74 | + console.log(''); |
| 75 | + |
| 76 | + return pass; |
| 77 | +} |
| 78 | + |
| 79 | +// Test 3: Loop count formula |
| 80 | +function testLoopCount() { |
| 81 | + const F = 0.9; |
| 82 | + |
| 83 | + // NEW: N = log(0.1) / log(F) to reach 90% of max |
| 84 | + const loops_new = Math.ceil(Math.log(0.1) / Math.log(F)); |
| 85 | + |
| 86 | + // Verify: what leverage do we actually get? |
| 87 | + const maxLeverage = 1 / (1 - F); |
| 88 | + const actualMultiplier = (1 - Math.pow(F, loops_new)) / (1 - F); |
| 89 | + const target = maxLeverage * 0.9; |
| 90 | + |
| 91 | + console.log('=== LOOP COUNT TEST ==='); |
| 92 | + console.log(`F: ${F}`); |
| 93 | + console.log(`Max leverage: ${maxLeverage.toFixed(2)}x capital`); |
| 94 | + console.log(`Target (90%): ${target.toFixed(2)}x capital`); |
| 95 | + console.log(''); |
| 96 | + console.log('NEW formula:'); |
| 97 | + console.log(` Loops: ${loops_new}`); |
| 98 | + console.log(` Actual multiplier: ${actualMultiplier.toFixed(2)}x`); |
| 99 | + console.log(` vs target: ${((actualMultiplier / target - 1) * 100).toFixed(1)}% diff`); |
| 100 | + console.log(''); |
| 101 | + |
| 102 | + const error = Math.abs(actualMultiplier - target) / target; |
| 103 | + const pass = error < 0.15; // Allow 15% error (since we're rounding loops) |
| 104 | + console.log(pass ? `PASS: ${(error * 100).toFixed(1)}% error` : `FAIL: ${(error * 100).toFixed(1)}% error`); |
| 105 | + console.log(''); |
| 106 | + |
| 107 | + return pass; |
| 108 | +} |
| 109 | + |
| 110 | +// Run all tests |
| 111 | +console.log('\n'); |
| 112 | +console.log('╔════════════════════════════════════════╗'); |
| 113 | +console.log('║ SDK SIMULATION VALIDATION SUITE ║'); |
| 114 | +console.log('╚════════════════════════════════════════╝'); |
| 115 | +console.log('\n'); |
| 116 | + |
| 117 | +const results = { |
| 118 | + 'Loop leverage': testLoopLeverage(), |
| 119 | + 'Runway validation': testRunway(), |
| 120 | + 'Loop count': testLoopCount(), |
| 121 | +}; |
| 122 | + |
| 123 | +console.log(''); |
| 124 | +console.log('═══════════════════════════════════════'); |
| 125 | +console.log('FINAL RESULTS'); |
| 126 | +console.log('═══════════════════════════════════════'); |
| 127 | + |
| 128 | +let passed = 0; |
| 129 | +let failed = 0; |
| 130 | + |
| 131 | +for (const [name, result] of Object.entries(results)) { |
| 132 | + console.log(`${name}: ${result ? 'PASS' : 'FAIL'}`); |
| 133 | + if (result) passed++; |
| 134 | + else failed++; |
| 135 | +} |
| 136 | + |
| 137 | +console.log(''); |
| 138 | +console.log(`${passed}/${passed + failed} tests passed`); |
| 139 | +console.log(''); |
| 140 | + |
| 141 | +process.exit(failed > 0 ? 1 : 0); |
0 commit comments