manifold/common/calculate.ts

167 lines
5.0 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { Contract } from './contract'
import { FEES } from './fees'
2022-01-10 23:52:03 +00:00
export const blah = () => 999
export const getProbability = (pool: { YES: number; NO: number }) => {
2022-01-10 23:52:03 +00:00
const [yesPool, noPool] = [pool.YES, pool.NO]
const numerator = Math.pow(yesPool, 2)
const denominator = Math.pow(yesPool, 2) + Math.pow(noPool, 2)
return numerator / denominator
}
export function getProbabilityAfterBet(
pool: { YES: number; NO: number },
2022-01-10 23:52:03 +00:00
outcome: 'YES' | 'NO',
bet: number
) {
const [YES, NO] = [
2022-01-10 23:52:03 +00:00
pool.YES + (outcome === 'YES' ? bet : 0),
pool.NO + (outcome === 'NO' ? bet : 0),
]
return getProbability({ YES, NO })
}
export function calculateShares(
pool: { YES: number; NO: number },
bet: number,
2022-01-10 23:52:03 +00:00
betChoice: 'YES' | 'NO'
) {
2022-01-10 23:52:03 +00:00
const [yesPool, noPool] = [pool.YES, pool.NO]
2022-01-10 23:52:03 +00:00
return betChoice === 'YES'
? bet + (bet * noPool ** 2) / (yesPool ** 2 + bet * yesPool)
2022-01-10 23:52:03 +00:00
: bet + (bet * yesPool ** 2) / (noPool ** 2 + bet * noPool)
}
export function calculatePayout(
contract: Contract,
bet: Bet,
2022-01-10 23:52:03 +00:00
outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT'
) {
2022-01-10 23:52:03 +00:00
const { amount, outcome: betOutcome, shares } = bet
2022-01-10 23:52:03 +00:00
if (outcome === 'CANCEL') return amount
if (outcome === 'MKT') return calculateMktPayout(contract, bet)
2022-01-10 23:52:03 +00:00
if (betOutcome !== outcome) return 0
2022-01-10 23:52:03 +00:00
const { totalShares, totalBets } = contract
2022-01-10 23:52:03 +00:00
if (totalShares[outcome] === 0) return 0
2022-01-10 23:52:03 +00:00
const startPool = contract.startPool.YES + contract.startPool.NO
const truePool = contract.pool.YES + contract.pool.NO - startPool
if (totalBets[outcome] >= truePool)
2022-01-10 23:52:03 +00:00
return (amount / totalBets[outcome]) * truePool
2022-01-10 23:52:03 +00:00
const total = totalShares[outcome] - totalBets[outcome]
const winningsPool = truePool - totalBets[outcome]
2022-01-10 23:52:03 +00:00
return (1 - FEES) * (amount + ((shares - amount) / total) * winningsPool)
}
export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {
2022-01-10 23:52:03 +00:00
const { amount, outcome, shares } = bet
const { totalShares, totalBets } = contract
2022-01-10 23:52:03 +00:00
const startPool = contract.startPool.YES + contract.startPool.NO
const truePool = amount + contract.pool.YES + contract.pool.NO - startPool
2022-01-10 23:52:03 +00:00
const totalBetsOutcome = totalBets[outcome] + amount
const totalSharesOutcome = totalShares[outcome] + shares
if (totalBetsOutcome >= truePool)
2022-01-10 23:52:03 +00:00
return (amount / totalBetsOutcome) * truePool
2022-01-10 23:52:03 +00:00
const total = totalSharesOutcome - totalBetsOutcome
const winningsPool = truePool - totalBetsOutcome
2022-01-10 23:52:03 +00:00
return (1 - FEES) * (amount + ((shares - amount) / total) * winningsPool)
}
function calculateMktPayout(contract: Contract, bet: Bet) {
const p =
2022-01-10 23:52:03 +00:00
contract.pool.YES ** 2 / (contract.pool.YES ** 2 + contract.pool.NO ** 2)
const weightedTotal =
2022-01-10 23:52:03 +00:00
p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO
2022-01-10 23:52:03 +00:00
const startPool = contract.startPool.YES + contract.startPool.NO
const truePool = contract.pool.YES + contract.pool.NO - startPool
2022-01-10 23:52:03 +00:00
const betP = bet.outcome === 'YES' ? p : 1 - p
if (weightedTotal >= truePool) {
2022-01-10 23:52:03 +00:00
return ((betP * bet.amount) / weightedTotal) * truePool
}
2022-01-10 23:52:03 +00:00
const winningsPool = truePool - weightedTotal
const weightedShareTotal =
p * (contract.totalShares.YES - contract.totalBets.YES) +
2022-01-10 23:52:03 +00:00
(1 - p) * (contract.totalShares.NO - contract.totalBets.NO)
return (
(1 - FEES) *
(betP * bet.amount +
((betP * (bet.shares - bet.amount)) / weightedShareTotal) * winningsPool)
2022-01-10 23:52:03 +00:00
)
}
export function resolvedPayout(contract: Contract, bet: Bet) {
if (contract.resolution)
2022-01-10 23:52:03 +00:00
return calculatePayout(contract, bet, contract.resolution)
throw new Error('Contract was not resolved')
}
export function currentValue(contract: Contract, bet: Bet) {
2022-01-10 23:52:03 +00:00
const prob = getProbability(contract.pool)
const yesPayout = calculatePayout(contract, bet, 'YES')
const noPayout = calculatePayout(contract, bet, 'NO')
2022-01-10 23:52:03 +00:00
return prob * yesPayout + (1 - prob) * noPayout
}
export function calculateSaleAmount(contract: Contract, bet: Bet) {
2022-01-10 23:52:03 +00:00
const { shares, outcome } = bet
2022-01-10 23:52:03 +00:00
const { YES: yesPool, NO: noPool } = contract.pool
const { YES: yesStart, NO: noStart } = contract.startPool
const { YES: yesShares, NO: noShares } = contract.totalShares
2022-01-10 23:52:03 +00:00
const [y, n, s] = [yesPool, noPool, shares]
const shareValue =
2022-01-10 23:52:03 +00:00
outcome === 'YES'
? // https://www.wolframalpha.com/input/?i=b+%2B+%28b+n%5E2%29%2F%28y+%28-b+%2B+y%29%29+%3D+c+solve+b
(n ** 2 +
s * y +
y ** 2 -
Math.sqrt(
n ** 4 + (s - y) ** 2 * y ** 2 + 2 * n ** 2 * y * (s + y)
)) /
(2 * y)
: (y ** 2 +
s * n +
n ** 2 -
Math.sqrt(
y ** 4 + (s - n) ** 2 * n ** 2 + 2 * y ** 2 * n * (s + n)
)) /
2022-01-10 23:52:03 +00:00
(2 * n)
2022-01-10 23:52:03 +00:00
const startPool = yesStart + noStart
const pool = yesPool + noPool - startPool
2022-01-10 23:52:03 +00:00
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
const f = pool / (probBefore * yesShares + (1 - probBefore) * noShares)
2022-01-10 23:52:03 +00:00
const myPool = outcome === 'YES' ? yesPool - yesStart : noPool - noStart
2022-01-10 23:52:03 +00:00
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
2022-01-10 23:52:03 +00:00
const saleAmount = (1 - FEES) * adjShareValue
return saleAmount
}