Documenting the process of building PdfLib.sol and explaining some of it's features
jamco.eth
4 min read·
Paradigm recently put out a paper outlining the basics of what a ‘distribution market’ could look like. The difference between a distribution market and a normal prediction market is that instead of buying outcome tokens from a fixed set, you can bet over continuous outcomes.
That means for a market like ‘How many people will attend the event?’, instead of being forced into options like A: <10,000 B: 10,000 - 20,000 etc, you can say I think 12,345 +/-2,000 people will attend.
To do this we needed to translate the traders bet and confidence into an equation that could generate a distribution of points over some interval - ie we needed a probability density function.
Existing Work
When I looked to see if any probability density function (PDF) libraries existed, there were 3 main stand alone repos in solidity.
Utilities to relate multiple PDFs (like finding the point of maximum difference between 2 PDFs)
A test framework that could link to our other code related to the distribution market.
Adapting a PDF for Solidity
Solidity does not have built in support for floating point math,soanyvaluesthathavedecimalswouldgetcutoff.Theworstformofthisiswhenanumberisadecimallessthan1,whichjustgetsroundedto0,somethingthatisobviouslybadforaPDFwhichhastermslikeσ2π1.
To get around this we scale all our numbers up and work in ‘fixed point math’whereweassumeahugenumberlike1e18actuallyequals1.That’sgreatbecausenowinsteadofroundingnumberslikeπ≈3wecando
π=3.141592653589793238∗1e18=3141592653589793238
So any time we see a number like that, we need to remember the amount we scaled it by, and be careful to either operate on that scale, or convert it back down offchain. Scaling numbers like this is straightforward when we just want to do simple operations like adding or dividing numbers. But the PDF is not so simple, and if we are to work with scaled numbers, we need to see how it behaves for different inputs.
p(x)=σ2π1e−21(σx−μ)2
A Scaled PDF
Lookingattheaboveequationandthinkingbacktoouroriginalexampleofpredictingattendeesatanevent,wecangainsomeunderstandingofhowthePDFshouldscale.Ifweconsideranormaleventtohave100attendees±20,thenwecanworkouttheprobabilitythatxpeoplewillshowup.Onethingtonoticeisthatsinceall those parameters are measured in attendees, if we scale things, then scaling all of the parameters by the same amount makes sense.
Sincetheexponent−21(σx−μ)2hasequalordersofscalinginthenumeratoranddenominator,anyscalingcancelsout.Thecoefficient has a sigma in the denominator, meaning that scaling the inputs will produce a much smaller peak.ThisalignswiththenormalisationconstraintonPDFs,∫−∞∞p(x)dx=1,sincewhenwestretchtherangewithourscaling,theheightmustgetsmallertokeeptheareathesame.
If we scale the inputs of the f(x) by 2, the height of g(x) is 1/2 of f(x)
Since we want to be able to work with negative numbers, we choose the (signed decimal) SD59x18 number type from PRB-Math. This lets us operate with 59 integer numbers and 18 decimals by representing those numbers in the range of an int256.
/// User defined type to ensure we know we are operating with a custom number type, not int256type SD59x18 isint256;
// Upper bound is type(int256).maxint256constant uMAX_SD59x18 =57896044618658097711785492504343953926634992332820282019728_792003956564819967;
// Lower bound is type(int256).minint256constant uMIN_SD59x18 =-57896044618658097711785492504343953926634992332820282019728_792003956564819968;
isMinimumPoint: Checks if a point is the minimum point of a curve given by pdf2 - pdf1
isMaximumPoint: Checks if a point is the maximum point of a curve given by pdf2 - pdf1
pdfDerivativeAtX: The first derivative of a pdf at a given x
pdfSecondDerivativeAtX: The second derivative of a pdf at a given x
These functions have wrappers like the below to receive values in int256, so you don't need to import any Prb-Math types into your code to get values back.
I really thought someone built an on-chain PDF document generator in Solidity and was unreasonably excited for a moment.
Ryan Rasmussen
Commented 3 days ago
This is all I see
James McComish
Commented 3 days ago
😅
Paragraph
Commented 3 days ago
Explore the potential of distribution markets in Paradigm's latest paper, which details innovative ways to allow continuous betting outcomes, like predicting event attendance more precisely. A new probability density function library development proposes enhanced PDF calculations for use in Solidity. @jamco.eth
PdfLib.sol is a library for probability density functions in Solidity. I wrote up some of the process of building it here. And shout out to @prberg for both an amazing foundry template and math library 🙏 https://paragraph.xyz/@jamesmccomish/probability-density-function-library-in-solidity
sorry repo was private, public now https://github.com/jamesmccomish/pdf-lib-sol
well this is cool 3000 $degen
I really thought someone built an on-chain PDF document generator in Solidity and was unreasonably excited for a moment.
This is all I see
😅
Explore the potential of distribution markets in Paradigm's latest paper, which details innovative ways to allow continuous betting outcomes, like predicting event attendance more precisely. A new probability density function library development proposes enhanced PDF calculations for use in Solidity. @jamco.eth