fix log scale graph
This commit is contained in:
parent
4b3370e374
commit
12567074cc
|
@ -16,8 +16,8 @@ export const getMappedValue =
|
||||||
const { min, max, isLogScale } = contract
|
const { min, max, isLogScale } = contract
|
||||||
|
|
||||||
if (isLogScale) {
|
if (isLogScale) {
|
||||||
const logValue = p * Math.log10(max - min)
|
const logValue = p * Math.log10(max - min + 1)
|
||||||
return 10 ** logValue + min
|
return 10 ** logValue + min - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return p * (max - min) + min
|
return p * (max - min) + min
|
||||||
|
@ -38,7 +38,7 @@ export const getPseudoProbability = (
|
||||||
isLogScale = false
|
isLogScale = false
|
||||||
) => {
|
) => {
|
||||||
if (isLogScale) {
|
if (isLogScale) {
|
||||||
return Math.log10(value - min) / Math.log10(max - min)
|
return Math.log10(value - min + 1) / Math.log10(max - min + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (value - min) / (max - min)
|
return (value - min) / (max - min)
|
||||||
|
|
|
@ -7,7 +7,6 @@ import { Bet } from 'common/bet'
|
||||||
import { getInitialProbability } from 'common/calculate'
|
import { getInitialProbability } from 'common/calculate'
|
||||||
import { BinaryContract, PseudoNumericContract } from 'common/contract'
|
import { BinaryContract, PseudoNumericContract } from 'common/contract'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
import { getMappedValue } from 'common/pseudo-numeric'
|
|
||||||
import { formatLargeNumber } from 'common/util/format'
|
import { formatLargeNumber } from 'common/util/format'
|
||||||
|
|
||||||
export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
||||||
|
@ -29,7 +28,11 @@ export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
||||||
...bets.map((bet) => bet.createdTime),
|
...bets.map((bet) => bet.createdTime),
|
||||||
].map((time) => new Date(time))
|
].map((time) => new Date(time))
|
||||||
|
|
||||||
const f = getMappedValue(contract)
|
const f: (p: number) => number = isBinary
|
||||||
|
? (p) => p
|
||||||
|
: isLogScale
|
||||||
|
? (p) => p * Math.log10(contract.max - contract.min + 1)
|
||||||
|
: (p) => p * (contract.max - contract.min) + contract.min
|
||||||
|
|
||||||
const probs = [startProb, ...bets.map((bet) => bet.probAfter)].map(f)
|
const probs = [startProb, ...bets.map((bet) => bet.probAfter)].map(f)
|
||||||
|
|
||||||
|
@ -69,10 +72,9 @@ export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
||||||
|
|
||||||
const points: { x: Date; y: number }[] = []
|
const points: { x: Date; y: number }[] = []
|
||||||
const s = isBinary ? 100 : 1
|
const s = isBinary ? 100 : 1
|
||||||
const c = isLogScale && contract.min === 0 ? 1 : 0
|
|
||||||
|
|
||||||
for (let i = 0; i < times.length - 1; i++) {
|
for (let i = 0; i < times.length - 1; i++) {
|
||||||
points[points.length] = { x: times[i], y: s * probs[i] + c }
|
points[points.length] = { x: times[i], y: s * probs[i] }
|
||||||
const numPoints: number = Math.floor(
|
const numPoints: number = Math.floor(
|
||||||
dayjs(times[i + 1]).diff(dayjs(times[i]), 'ms') / timeStep
|
dayjs(times[i + 1]).diff(dayjs(times[i]), 'ms') / timeStep
|
||||||
)
|
)
|
||||||
|
@ -84,7 +86,7 @@ export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
||||||
x: dayjs(times[i])
|
x: dayjs(times[i])
|
||||||
.add(thisTimeStep * n, 'ms')
|
.add(thisTimeStep * n, 'ms')
|
||||||
.toDate(),
|
.toDate(),
|
||||||
y: s * probs[i] + c,
|
y: s * probs[i],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,6 +101,9 @@ export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
||||||
|
|
||||||
const formatter = isBinary
|
const formatter = isBinary
|
||||||
? formatPercent
|
? formatPercent
|
||||||
|
: isLogScale
|
||||||
|
? (x: DatumValue) =>
|
||||||
|
formatLargeNumber(10 ** +x.valueOf() + contract.min - 1)
|
||||||
: (x: DatumValue) => formatLargeNumber(+x.valueOf())
|
: (x: DatumValue) => formatLargeNumber(+x.valueOf())
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -111,11 +116,13 @@ export const ContractProbGraph = memo(function ContractProbGraph(props: {
|
||||||
yScale={
|
yScale={
|
||||||
isBinary
|
isBinary
|
||||||
? { min: 0, max: 100, type: 'linear' }
|
? { min: 0, max: 100, type: 'linear' }
|
||||||
: {
|
: isLogScale
|
||||||
min: contract.min + c,
|
? {
|
||||||
max: contract.max + c,
|
min: 0,
|
||||||
type: contract.isLogScale ? 'log' : 'linear',
|
max: Math.log10(contract.max - contract.min + 1),
|
||||||
|
type: 'linear',
|
||||||
}
|
}
|
||||||
|
: { min: contract.min, max: contract.max, type: 'linear' }
|
||||||
}
|
}
|
||||||
yFormat={formatter}
|
yFormat={formatter}
|
||||||
gridYValues={yTickValues}
|
gridYValues={yTickValues}
|
||||||
|
|
|
@ -206,7 +206,7 @@ export function NewContract(props: {
|
||||||
min,
|
min,
|
||||||
max,
|
max,
|
||||||
initialValue,
|
initialValue,
|
||||||
isLogScale: (min ?? 0) < 0 ? false : isLogScale,
|
isLogScale,
|
||||||
groupId: selectedGroup?.id,
|
groupId: selectedGroup?.id,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -293,15 +293,13 @@ export function NewContract(props: {
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
|
|
||||||
{!(min !== undefined && min < 0) && (
|
<Checkbox
|
||||||
<Checkbox
|
className="my-2 text-sm"
|
||||||
className="my-2 text-sm"
|
label="Log scale"
|
||||||
label="Log scale"
|
checked={isLogScale}
|
||||||
checked={isLogScale}
|
toggle={() => setIsLogScale(!isLogScale)}
|
||||||
toggle={() => setIsLogScale(!isLogScale)}
|
disabled={isSubmitting}
|
||||||
disabled={isSubmitting}
|
/>
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{min !== undefined && max !== undefined && min >= max && (
|
{min !== undefined && max !== undefined && min >= max && (
|
||||||
<div className="mt-2 mb-2 text-sm text-red-500">
|
<div className="mt-2 mb-2 text-sm text-red-500">
|
||||||
|
|
Loading…
Reference in New Issue
Block a user