-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonte-carlo.js
More file actions
155 lines (139 loc) Β· 4.41 KB
/
monte-carlo.js
File metadata and controls
155 lines (139 loc) Β· 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
import { fileURLToPath } from 'url'
import { playHand } from './index.js'
import * as bettingStrategies from './betting.js'
function parseOdds (str) {
const parts = String(str).split('-').map(n => parseInt(n, 10))
if (parts.length !== 3 || parts.some(n => isNaN(n))) return {}
return {
4: parts[0],
5: parts[1],
6: parts[2],
8: parts[2],
9: parts[1],
10: parts[0]
}
}
function percentile (sorted, p) {
if (sorted.length === 0) return 0
const idx = Math.ceil((p / 100) * sorted.length) - 1
return sorted[Math.min(Math.max(idx, 0), sorted.length - 1)]
}
function summary (arr) {
const sorted = [...arr].sort((a, b) => a - b)
const mean = arr.reduce((m, v) => m + v, 0) / arr.length
const variance = arr.length > 1
? arr.reduce((m, v) => m + Math.pow(v - mean, 2), 0) / (arr.length - 1)
: 0
const stdDev = Math.sqrt(variance)
const stdErr = stdDev / Math.sqrt(arr.length)
const ci95Low = mean - (1.96 * stdErr)
const ci95High = mean + (1.96 * stdErr)
return {
min: sorted[0],
mean,
stdDev,
ci95Low,
ci95High,
max: sorted[sorted.length - 1],
p1: percentile(sorted, 1),
p5: percentile(sorted, 5),
p10: percentile(sorted, 10),
p25: percentile(sorted, 25),
p50: percentile(sorted, 50),
p75: percentile(sorted, 75),
p90: percentile(sorted, 90),
p95: percentile(sorted, 95),
p99: percentile(sorted, 99)
}
}
function summaryTable (arr) {
const obj = summary(arr)
const order = [
'min',
'p1',
'p5',
'p10',
'p25',
'p50',
'mean',
'p75',
'p90',
'p95',
'p99',
'max'
]
const table = order
.map(k => ({ stat: k, value: obj[k] }))
.sort((a, b) => a.value - b.value || order.indexOf(a.stat) - order.indexOf(b.stat))
return {
table,
stdDev: obj.stdDev,
ci95Low: obj.ci95Low,
ci95High: obj.ci95High
}
}
function simulateTrial ({ handsPerTrial, startingBankroll, rules, bettingStrategy }) {
let balance = startingBankroll
let rolls = 0
for (let i = 0; i < handsPerTrial; i++) {
const { history, balance: result } = playHand({
rules,
bettingStrategy
})
balance += result
rolls += history.length
}
return { balance, rolls }
}
function monteCarlo ({ trials, handsPerTrial, startingBankroll, rules, bettingStrategy }) {
const results = []
for (let i = 0; i < trials; i++) {
results.push(simulateTrial({ handsPerTrial, startingBankroll, rules, bettingStrategy }))
}
return results
}
function printResults (results) {
if (results.length <= 100) {
console.log('\nTrial Results')
console.table(results.map((r, i) => ({ trial: i + 1, balance: r.balance, rolls: r.rolls })))
} else {
console.log(`\nTrial Results omitted (too many trials: ${results.length})`)
}
console.log('\nFinal Balance Summary')
const balanceSummary = summaryTable(results.map(r => r.balance))
console.table(balanceSummary.table)
console.log(`stdDev: ${balanceSummary.stdDev.toFixed(2)}`)
console.log(`95% CI: [${balanceSummary.ci95Low.toFixed(2)}, ${balanceSummary.ci95High.toFixed(2)}]`)
console.log('\nRoll Count Summary')
const rollSummary = summaryTable(results.map(r => r.rolls))
console.table(rollSummary.table)
console.log(`stdDev: ${rollSummary.stdDev.toFixed(2)}`)
console.log(`95% CI: [${rollSummary.ci95Low.toFixed(2)}, ${rollSummary.ci95High.toFixed(2)}]`)
}
export { monteCarlo, simulateTrial, summary }
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const trials = parseInt(process.argv[2], 10)
const handsPerTrial = parseInt(process.argv[3], 10)
const startingBankroll = parseInt(process.argv[4], 10)
const minBet = parseInt(process.argv[5], 10)
const oddsInput = process.argv[6] || '3-4-5'
// {
// minPassLineOnly,
// minPassLineMaxOdds,
// placeSixEight,
// placeSixEightUnlessPoint,
// minPassLinePlaceSixEight,
// minPassLineMaxOddsPlaceSixEight
// }
const bettingStrategy = process.argv[7] || 'minPassLineMaxOddsPlaceSixEight'
const rules = {
minBet,
maxOddsMultiple: parseOdds(oddsInput)
}
console.log(`Running ${trials} trials with ${handsPerTrial} hand(s) each`)
console.log(`[table rules] minimum bet: $${minBet}, odds ${oddsInput}`)
console.log(`betting strategy: ${bettingStrategy}`)
const results = monteCarlo({ trials, handsPerTrial, startingBankroll, rules, bettingStrategy: bettingStrategies[bettingStrategy] })
printResults(results)
}