getNumericBets

This commit is contained in:
mantikoros 2022-05-06 15:38:54 -04:00
parent dc032c502a
commit 47f18cb307
2 changed files with 32 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import {
NumericContract,
} from './contract'
import { DPM_FEES } from './fees'
import { normpdf } from './normal'
export function getDpmProbability(totalShares: { [outcome: string]: number }) {
// For binary contracts only.
@ -35,12 +36,30 @@ export function getDpmOutcomeProbabilities(totalShares: {
export function getNumericBets(
contract: NumericContract,
bucket: string,
betAmount: number
betAmount: number,
std = 0.05
) {
const { totalShares, bucketCount } = contract
const { bucketCount } = contract
const bucketNumber = parseInt(bucket)
const buckets = _.range(0, bucketNumber)
const bucketNum = +bucket
// const bucketRange =
const mean = bucketNumber / bucketCount
const allDensities = buckets.map((i) => normpdf(i / bucketCount, mean, std))
const densitySum = _.sum(allDensities)
const rawBetAmounts = allDensities
.map((d) => (d / densitySum) * betAmount)
.map((x) => (x >= 1 / bucketCount ? x : 0))
const rawSum = _.sum(rawBetAmounts)
const scaledBetAmounts = rawBetAmounts.map((x) => (x / rawSum) * betAmount)
const bets = scaledBetAmounts
.map((x, i) => (x > 0 ? [i.toString(), x] : undefined))
.filter((x) => x != undefined) as [string, number][]
return bets
}
export function getDpmOutcomeProbabilityAfterBet(

9
common/normal.ts Normal file
View File

@ -0,0 +1,9 @@
export function normpdf(x: number, mean = 0, std = 1) {
if (std === 0) {
return x === mean ? Infinity : 0
}
return Math.exp((-0.5 * Math.pow(x - mean, 2)) / std) / Math.sqrt(TAU * std)
}
const TAU = Math.PI * 2