manifold/common/calculate-cpmm.ts

98 lines
2.3 KiB
TypeScript
Raw Normal View History

import * as _ from 'lodash'
import { Bet } from './bet'
import { Binary, CPMM, FullContract } from './contract'
export function getCpmmProbability(pool: { [outcome: string]: number }) {
// For binary contracts only.
const { YES, NO } = pool
return NO / (YES + NO)
}
2022-03-03 17:06:24 +00:00
export function getCpmmOutcomeProbabilityAfterBet(
contract: FullContract<CPMM, Binary>,
outcome: string,
bet: number
) {
const { newPool } = calculateCpmmPurchase(contract, bet, outcome)
2022-03-03 17:06:24 +00:00
const p = getCpmmProbability(newPool)
return outcome === 'NO' ? 1 - p : p
}
export function calculateCpmmShares(
pool: {
[outcome: string]: number
},
k: number,
bet: number,
betChoice: string
) {
const { YES: y, NO: n } = pool
const numerator = bet ** 2 + bet * (y + n) - k + y * n
const denominator = betChoice === 'YES' ? bet + n : bet + y
const shares = numerator / denominator
return shares
}
export function calculateCpmmPurchase(
contract: FullContract<CPMM, Binary>,
bet: number,
outcome: string
) {
const { pool, k } = contract
const shares = calculateCpmmShares(pool, k, bet, outcome)
const { YES: y, NO: n } = pool
const [newY, newN] =
outcome === 'YES'
? [y - shares + bet, n + bet]
: [y + bet, n - shares + bet]
const newPool = { YES: newY, NO: newN }
return { shares, newPool }
}
export function calculateCpmmShareValue(
contract: FullContract<CPMM, Binary>,
shares: number,
outcome: string
) {
const { pool, k } = contract
const { YES: y, NO: n } = pool
const poolChange = outcome === 'YES' ? shares + y - n : shares + n - y
const shareValue = 0.5 * (shares + y + n - Math.sqrt(4 * k + poolChange ** 2))
return shareValue
}
export function calculateCpmmSale(
contract: FullContract<CPMM, Binary>,
bet: Bet
) {
const { shares, outcome } = bet
const saleValue = calculateCpmmShareValue(contract, shares, outcome)
const { pool } = contract
const { YES: y, NO: n } = pool
const [newY, newN] =
outcome === 'YES'
? [y + shares - saleValue, n - saleValue]
: [y - saleValue, n + shares - saleValue]
const newPool = { YES: newY, NO: newN }
return { saleValue, newPool }
}
2022-03-03 05:32:33 +00:00
export function getCpmmProbabilityAfterSale(
contract: FullContract<CPMM, Binary>,
bet: Bet
) {
const { newPool } = calculateCpmmSale(contract, bet)
return getCpmmProbability(newPool)
}