From 47f18cb30776fb3ae166b473dcda8bb32af3b01d Mon Sep 17 00:00:00 2001 From: mantikoros Date: Fri, 6 May 2022 15:38:54 -0400 Subject: [PATCH] getNumericBets --- common/calculate-dpm.ts | 27 +++++++++++++++++++++++---- common/normal.ts | 9 +++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 common/normal.ts diff --git a/common/calculate-dpm.ts b/common/calculate-dpm.ts index 39f97585..eb3383b9 100644 --- a/common/calculate-dpm.ts +++ b/common/calculate-dpm.ts @@ -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( diff --git a/common/normal.ts b/common/normal.ts new file mode 100644 index 00000000..3d21af4b --- /dev/null +++ b/common/normal.ts @@ -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