calculateNumericDpmShares: use sorted order

This commit is contained in:
mantikoros 2022-05-06 17:19:56 -04:00
parent d1d381c51b
commit 83c89ad940

View File

@ -107,14 +107,6 @@ export function calculateDpmShares(
return Math.sqrt(bet ** 2 + shares ** 2 + c) - shares return Math.sqrt(bet ** 2 + shares ** 2 + c) - shares
} }
const zigZagOrder = (length: number) => {
const mid = Math.floor(length / 2)
return _.range(0, mid)
.flatMap((i) => [i, length - i - 1])
.concat(length % 2 === 0 ? [] : [mid])
}
export function calculateNumericDpmShares( export function calculateNumericDpmShares(
totalShares: { totalShares: {
[outcome: string]: number [outcome: string]: number
@ -125,7 +117,12 @@ export function calculateNumericDpmShares(
totalShares = _.cloneDeep(totalShares) totalShares = _.cloneDeep(totalShares)
for (let i of zigZagOrder(bets.length)) { const order = _.sortBy(
bets.map(([, amount], i) => [amount, i]),
([amount]) => amount
).map(([, i]) => i)
for (let i of order) {
const [bucket, bet] = bets[i] const [bucket, bet] = bets[i]
shares[i] = calculateDpmShares(totalShares, bet, bucket) shares[i] = calculateDpmShares(totalShares, bet, bucket)
totalShares = addObjects(totalShares, { [bucket]: shares[i] }) totalShares = addObjects(totalShares, { [bucket]: shares[i] })