Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EPIC][x/twap] Geometric TWAP #3013

Closed
p0mvn opened this issue Oct 17, 2022 · 1 comment
Closed

[EPIC][x/twap] Geometric TWAP #3013

p0mvn opened this issue Oct 17, 2022 · 1 comment
Assignees
Labels
C:x/twap Changes to the twap module F: geometric-twap

Comments

@p0mvn
Copy link
Member

p0mvn commented Oct 17, 2022

Background

While the arithmetic mean TWAPs are much more widely used, they should theoretically be less accurate in measuring a geometric Brownian motion process (which is how price movements are usually modeled)

Arithmetic TWAP tends to overweight higher prices relative to lower ones.

Therefore, we should implement Geometric TWAP.

Suggested Design

The geometric mean can be expressed as the exponential of the arithmetic mean of logarithms.

Ultimately, we would like to calculate:

image

Therefore, we should be able to utilize our existing arithmetic accumulator system in the log scale.

As the first step, I propose finalizing the logarithm implementation: #2788

Next, integrate the geometric TWAP into the twap module by utilizing the log-average relationship described above.

Core Progress

Acceptance Criteria

  • Geometric TWAP is implemented using log-average
  • Logarithm implementation is efficient and accurate
  • The log-average rounding is insignificant
  • Integration into the existing TWAP module is sound

Sources

@p0mvn p0mvn added the C:x/twap Changes to the twap module label Oct 17, 2022
@p0mvn p0mvn self-assigned this Oct 17, 2022
@osmo-bot osmo-bot moved this to Needs Review 🔍 in Osmosis Chain Development Oct 17, 2022
@p0mvn p0mvn removed their assignment Oct 17, 2022
@p0mvn p0mvn changed the title [x/twap] Geometric TWAP [EPIC][x/twap] Geometric TWAP Oct 17, 2022
@p0mvn p0mvn self-assigned this Nov 15, 2022
@p0mvn
Copy link
Member Author

p0mvn commented Nov 15, 2022

Some notes on the investigations so far:

The naive approach to computing a geometric weighted average is not desirable. First, it is computationally expensive. Second, it causes overflows when attempting to exponentiate spot price with the time weight denominated in milliseconds. Using seconds instead would make us lose precision.

The proposed approach requires using Power() function that takes a decimal exponent.

We already have a Taylor series approximation for bases <= 2 here.

One approach is to implement Power() for decimal bases > 2 by following this blogpost: https://xn--2-umb.com/22/exp-ln/

Earlier I suggested using Euler's number (that is > 2) as the base and natural log as the exponent. However, there is no particular reason for that.

Therefore, we can follow the Uniswap v3 choice of base 1.0001 that is within the bounds for the existing Power function to compute:
$$GeometricTwap(P) = 1.0001^{ArithmeticTwap(log_{1.0001}{P})}$$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/twap Changes to the twap module F: geometric-twap
Projects
Archived in project
Development

No branches or pull requests

1 participant