Public API
ABMPredictionMarkets.AMMOrder
— TypeAMMOrder <: AbstractOrder
An object representing a bid, ask, or share.
Fields
id::Int
: agent idoption_id
: option idcost::Float64
: total cost of ordern_shares
: the number of shares in the order
Constructors
AMMOrder(id, option, cost, n_shares)
AMMOrder(; id, option, cost, n_shares)
ABMPredictionMarkets.AbstractCDA
— TypeAbstractCDA <: AbstractPredictionMarket
An abstract type for a continuous double auction prediction market.
ABMPredictionMarkets.AbstractCPMM
— TypeAbstractCPMM <: AbstractPredictionMarket
An abstract type for a prediction market using a constant product market maker.
ABMPredictionMarkets.AbstractLSR
— TypeAbstractLSR <: AbstractPredictionMarket
An abstract type for a prediction market using a logarithmic scoring rule.
ABMPredictionMarkets.AbstractOrder
— TypeAbstractOrder
An abstract type representing bids and asks.
ABMPredictionMarkets.AbstractPredictionMarket
— TypeAbstractPredictionMarket
An abstract type for a prediction market.
ABMPredictionMarkets.CDA
— TypeCDA <: AbstractCDA
Holds data and order book for a continuous double auction prediction market simulation.
Fields
order_books::Vector{Vector{Order}}
: outstanding orders (bids and asks). Each sub-vector corresponds to a different marketmarket_prices::Vector{Vector{Float64}}
: the market price in dollars after each interaction. The market price stays the same if a transaction does not occur. Each sub-vector corresponds to a different markettrade_volume::Vector{Vector{Int}}
: each elements represents the number of trades made per stepiteration_ids
::Vector{Vector{Int}}`: iteration number on which market prices are recordedtimes::Vector{Int} = Int[]
: times at which specific events may occur
Constructors
CDA(; order_book)
ABMPredictionMarkets.CPMM
— TypeCPMM <: AbstractCPMM
Holds reserves and data for a constant product market maker prediction market simulation.
Fields
yes_reserves::Vector{Float64}
: reserves for "yes" shares. Each element corresponds to a different marketno_reserves::Vector{Float64}
: reserves for "no" shares. Each element corresponds to a different marketmarket_prices::Vector{Vector{Float64}}
: the market price in dollars after each interaction. The market price stays the same if a transaction does not occur. Each sub-vector corresponds to a different markettrade_volume::Vector{Vector{Int}}
: each elements represents the number of trades made per stepiteration_ids
::Vector{Vector{Int}}`: iteration number on which market prices are recordedtimes::Vector{Int} = Int[]
: times at which specific events may occur
Constructors
CPMM(; yes_reserves, no_reserves, times = Int[])
ABMPredictionMarkets.DiscreteDirichlet
— TypeDiscreteDirichlet{T} <: ContinuousMultivariateDistribution
A discrete version of Dirchlet distribution which sums to 100.
Fields
μ::Vector{T}
: mean probabilities which sum to 1η::T
: a scalar multiple ofμ
inversely related to variance.
ABMPredictionMarkets.LSR
— TypeLSR <: AbstractLSR
An automated market maker using the logarithmic scoring rule.
Fields
elasticity
: elasticity parameter where higher values correspond to less elasticityn_shares::Vector{Vector{Int}}
: each sub-vector corresponds to the number of shares for each option in a given marketmarket_prices::Vector{Vector{Vector{Float64}}}
: the market price in dollars after each interaction. The market price stays the same if a transaction does not occur. Each sub-vector corresponds to a different market at a different iteration.n_decimals::Int
: decimal places for rounding in favor of the automated market makertrade_volume::Vector{Vector{Int}}
: each elements represents the number of trades made per stepiteration_ids
::Vector{Vector{Int}}`: iteration number on which market prices are recordedtimes::Vector{Int} = Int[]
: times at which specific events may occur
Constructors
LSR(; elasticity, n_options, info_times)
References
Berg, H., & Proebsting, T. A. (2009). Hanson’s automated market maker. The Journal of Prediction Markets, 3(1), 45-59.
Hanson, R. (2003). Combinatorial information market design. Information Systems Frontiers, 5, 107-119.
ABMPredictionMarkets.MarketAgent
— TypeMarketAgent <: AbstractAgent
An agent that submits bids and asks in a prediction market.
ABMPredictionMarkets.Order
— TypeOrder <: AbstractOrder
An object representing a bid, ask, or share.
Fields
id::Int
: agent idyes::Bool
: true if represents ayes
shareprice::Int
: the price of a single share, price ∈ [0,100]quantity::Int
: the number of shares at a given pricetype::Symbol
: object type type ∈ [bid,ask,share]
Constructors
Order(id, yes, price, quantity, type)
Order(; id, yes, price, quantity, type)
ABMPredictionMarkets.agent_step!
— Methodagent_step!(agent, ::MarketAgent, ::AbstractPredictionMarket, model)
The agent submits a new order for a bid or ask in each market. The order is checked against potential sellers (buyers), and an exchange is made if one is found. Otherwise, the order is added to the order book.
Arguments
agent
: an agent participating in the prediction market::MarketAgent
: variant of agent possibly of the same type asagent
::AbstractPredictionMarket
: a prediction market typemodel
: an abm object for the prediction market simulation
ABMPredictionMarkets.compute_trade_volume
— Methodcompute_trade_volume(trade_volume, interval_length)
Computes trade volume for a set of time intervals
Arguments
trade_volume
: indicates number of trades per stepinterval_length
: the length of the interval in which trade volume is computed
ABMPredictionMarkets.compute_unpacking_factor
— Methodcompute_unpacking_factor(model)
Computes the ratio of sum of subevents probabilities to the event probability for a 2×2 partition. Event e is indexed by 1 and subevents e₁ and e₂ are indexed by 2 and 3. Probability theory requires p(e) = p(e₁) + p(e₂).
Arguments
model
: an abm object for the prediction market simulation
ABMPredictionMarkets.summarize_by_iteration
— Methodsummarize_by_iteration(values, iteration_ids; fun = x -> x[end])
Summarize by iteration using fun
Arguments
values
: a vector of values after each agent stepiteration_ids
: a vector containing the indices of iterations for each agent step
Keywords
fun = x -> x[end]
: a function applied to market prices of each iteration. By default,
the ending price is used.
Plotting
ABMPredictionMarkets.plot_depth_chart
— Functionplot_depth_chart(order_book::Vector{Order}; kwargs...)
Creates a market depth chart to analyze supply and demand dynamics. The red area on the left represents the reversed cumulative distribution for bids. The blue area on the right represents the cumulative distribution for asks. The mid market price is represented by vertical black line.
Arguments
order_book::Vector{Order}
: a vector of orders (bids and asks) representing the order book (i.e. outstanding orders)
Keywords
config...
: optional keyword arguments to the functionplot
ABMPredictionMarkets.plot_dashboard
— Functionplot_dashboard(
model;
title = make_title(model.market_prices),
size = (1200, 1000),
depth_chart_layout = make_layout(model.market_prices),
outer_layout = [
(label = :a, width = :auto, height = 0.70),
(label = :b, width = :auto, height = 0.30)
],
add_unpacking_factor = false,
n_days = 1,
depth_chart_config = (),
price_chart_config = (),
)
Plots an animated dashboard containing the following:
- A depth chart for each market.
- Historical price for each market.
- Optional unpacking factor based on two market sets.
Arguments
model
: an abm object for the prediction market simulation
Keywords
title
: a 1×n vector of labels for each market. By default, each element is eᵢdepth_chart_layout
: the layout of the depth charts. By default, the smallest possible 2D grid is used.size = (1200, 1000)
: size of dashboard animationouter_layout
: the layout for the dashboardadd_unpacking_factor = false
: includes unpacking factor plot if true. If set to true, default layouts will need to be overwritten.n_days = 1
: the number of days to simulate the prediction marketdepth_chart_config = ()
: optional keyword arguments for the depth chartsprice_chart_config = ()
: optional keyword arguments for price charts
Internal Functions
ABMPredictionMarkets.add_shares!
— Methodadd_shares!(shares, share)
Adds a share to a vector of shares. A new element is add if the shares do not have an entry with the target price. If an entry with the target price exists, the quantity is added to that entry
Arguments
shares
: a vector of current sharesshare
: a share to be added toshares
ABMPredictionMarkets.ask
— Methodask(agent, ::MarketAgent, ::AbstractCDA, model, bidx)
Generates an ask amount according to
v ~ Uniform(p, + 1, p + δ)
,
where p
is the maximum share price.
Arguments
agent
: an agent participating in the prediction market::MarketAgent
: variant of agent possibly of the same type asagent
::AbstractPredictionMarket
: a prediction market typemodel
: an abm object for the prediction market simulationbidx
: the index of the current order book
ABMPredictionMarkets.ask_bid_match!
— Methodask_bid_match!(proposal, model, bidx, i)
For agent i
and agent j
, let b
be the bid amount, a
be the ask amount, and e
be the event. If bₑᵢ = aₑⱼ
, then exchange.
Argument
proposal
: a proposal bid or askmodel
: an abm object for the prediction market simulationbidx
: the index of the current order booki
: the index for the current entry of the order book
ABMPredictionMarkets.ask_match!
— Methodask_match!(proposal, model, bidx, i)
For agent i
and agent j
, let a
be the ask amount, and e
be the event. If aₑᵢ + a¬ₑⱼ = 1
, then remove shares and deduct ask amounts for i
and j
.
Argument
proposal
: a proposal bidmodel
: an abm object for the prediction market simulationi
: the index for the current entry of the order book
ABMPredictionMarkets.bid
— Methodbid(agent, ::MarketAgent, ::AbstractCDA, model, bidx)
Generates an ask amount according to
v ~ Uniform(p - δ, p - 1)
,
where p
is the agent's subject probability of the event, expressed on a scale ranging from 0 100 (cents). The bid price is subtracted from money and added to the bid reserve to ensure the agent has sufficient funds when making bids in multiple markets.
Arguments
agent
: an agent participating in the prediction market::MarketAgent
: variant of agent possibly of the same type asagent
::AbstractPredictionMarket
: a prediction market typemodel
: an abm object for the prediction market simulationbidx
: the index of the current order book
ABMPredictionMarkets.bid_match!
— Methodbid_match!(proposal, model, bidx, i)
For agent i
and agent j
, let b
be the bid amount, and e
be the event. If bₑᵢ + b¬ₑⱼ = 1
, then create new shares for i
and j
.
Argument
proposal
: a proposal bidmodel
: an abm object for the prediction market simulationbidx
: the index of the current order booki
: the index for the current entry of the order book
ABMPredictionMarkets.compute_optimal_purchase
— Methodcompute_optimal_purchase(agent, market::CPMM, belief, midx, yes)
Computes the optimal amount of money to spend on shares based on a logarithmic utility function.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objectcost
: the total cost of the sharesmidx
: market indexyes::Bool
: corresponds to a yes share if true; otherwise, corresponds to a no share
ABMPredictionMarkets.compute_price
— Methodcompute_price(market::AbstractCPMM, midx, yes::Bool)
Returns the current price of the specified share.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objectmidx
: market indexyes::Bool
: corresponds to a yes share if true; otherwise, corresponds to a no share
ABMPredictionMarkets.compute_returns
— Methodcompute_returns(shares, p_true)
Computes the expected return of a vector of shares.
Arguments
shares
: a vector of sharesp_true
: the true probability of the event
ABMPredictionMarkets.cost_to_price
— Methodcost_to_price(cost, price, elasticity)
Finds the total cost of a transaction to move the current price to a new price.
Arguments
cost
: the total dollar amount of an exchange of sharesprice
: the current price of a given shareelasticity
: the elasticity parameter
ABMPredictionMarkets.cost_to_shares
— Methodcost_to_shares(market::AbstractCPMM, cost, midx)
Computes the number of shares required to achieve a given cost.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objectcost
: the total cost of the sharesmidx
: market indexyes::Bool
: corresponds to a yes share if true; otherwise, corresponds to a no share
ABMPredictionMarkets.cost_to_shares
— Methodcost_to_shares(cost, price, elasticity)
Finds the number of shares that can be purchased at a given total cost.
Arguments
cost
: the toal dollar amount of an exchange of sharesprice
: the current price of a given shareelasticity
: the elasticity parameter
ABMPredictionMarkets.create_order
— Methodcreate_order(agent, ::MarketAgent, ::AbstractCDA, model, bidx)
Creates and returns a bid or ask. The function bid
is called if the agent has no shares. The function ask
is called if the agent has no money. If the agent has money and shares, bid
and ask
are called with equal probability.
Arguments
agent::MarketAgent
: an agent participating in the prediction market::MarketAgent
: variant of agent possibly of the same type asagent
::AbstractPredictionMarket
: a prediction market typemodel
: an abm object for the prediction market simulationbidx
: the index of the current order book
ABMPredictionMarkets.get_market_info
— Methodget_market_info(order_book; yes)
Returns the maximum bid and minimum ask in the order book.
Arguments
order_book
: a vector of outstanding orders
Keywords
yes
: returns maximum bid and minimum ask for yes orders if true
ABMPredictionMarkets.get_reserves
— Methodget_reserves(market::AbstractCPMM, midx)
Returns the liquidity reserves for a given market.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objectmidx
: market index
Returns
reserves::Tuple
: yes reserves, no reserves
ABMPredictionMarkets.price_to_cost
— Methodprice_to_cost(market::AbstractCPMM, target_price, midx, yes::Bool)
Computes the total cost required to achieve a target price.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objecttarget_price
: the desired price after transactionmidx
: market indexyes::Bool
: corresponds to a yes share if true; otherwise, corresponds to a no share
ABMPredictionMarkets.price_to_cost
— Methodprice_to_cost(target_price, price, elasticity)
Finds the number of shares needed to change to a new price.
Arguments
target_price
: the new price after purchacing sharesprice
: the current price of a given shareelasticity
: the elasticity parameter
ABMPredictionMarkets.price_to_shares
— Methodprice_to_shares(target_price, price, elasticity)
Finds the number of shares needed to change to a new price.
Arguments
target_price
: the new price after purchacing sharesprice
: the current price of a given shareelasticity
: the elasticity parameter
ABMPredictionMarkets.remove_orders!
— Method remove_orders!(agent::MarketAgent, model, bidx)
Removes orders from the specified order book and transfers funds from the bid reserve to the agent's money fund.
Arguments
agent::MarketAgent
: an agent participating in the prediction marketmodel
: an abm object for the prediction market simulationbidx
: the index of the current order book
ABMPredictionMarkets.sample_ask
— Methodsample_ask(p, δ)
Samples an amount to ask.
v ~ Uniform(p, + 1, p + δ)
,
where p
is typically the maximum share price.
Arguments
p
: is typically the maximum share price. p ∈ [0, 100]δ
: the range of noise added to ask price. δ ≥ 1.
ABMPredictionMarkets.sample_bid
— Methodsample_bid(judgment, δ)
Samples an amount to ask.
v ~ Uniform(judgment, - δ, judgment - 1)
,
where judgment
is the agent's subjective probability.
Arguments
judgment
: s the agent's subjective probability. judgment ∈ [0, 100]δ
: the range of noise added to ask price. δ ≥ 1.
ABMPredictionMarkets.set_elasticity
— Methodset_elasticity(total_money, n_events, upper_price)
Sets the elasticity parameter to a value such that an upper price is achieved if all money in a market allocated to a given event.
Arguments
total_money
: total money in a given market across all participantsn_events
: the number of events that can be purchased in a given marketupper_price
: the maximum price, achieved if all money is placed on a single event
ABMPredictionMarkets.shares_to_cost
— Methodshares_to_cost(market::AbstractCPMM, n_shares, midx)
Computes the total cost associated with a given number of shares of shares.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objectn_shares
: the number of sharesmidx
: market indexyes::Bool
: corresponds to a yes share if true; otherwise, corresponds to a no share
ABMPredictionMarkets.shares_to_cost
— Methodshares_to_cost(price, n_shares, elasticity)
Finds the cost given a number of shares and current price.
Arguments
price
: the current price of a given sharen_shares
: the number of shares purchasedelasticity
: the elasticity parameter
ABMPredictionMarkets.shares_to_price
— Methodshares_to_price(price, n_shares, elasticity)
Finds the new price given a number of shares and current price.
Arguments
price
: the current price of a given sharen_shares
: the number of shares purchasedelasticity
: the elasticity parameter
ABMPredictionMarkets.to_beta
— Methodto_beta(μ, σ)
Returns α and β parameters of beta distribution corresponding to the desired mean and standard deviation.
Arguments
μ
: the desired mean of the beta distributionσ
: the desired standard deviation of the beta distribution
ABMPredictionMarkets.transact!
— Methodtransact!(proposal, ::AbstractCDA, model, bidx)
Attempts to find a possible trade for a submitted proposal (bid or ask). Returns true
if the proposal trade was performed. Otherwise, false
is returned. If the proposed trade is not completed, the proposal is added to the order book.
Arguments
proposal
: a proposal bid or ask::AbstractCDA
: a double continuous auction prediction marketmodel
: an abm object for the prediction market simulationbidx
: the index of the current order book
ABMPredictionMarkets.update_reserves!
— Methodupdate_reserves!(market::AbstractCPMM, n_shares, cost, midx, yes::Bool)
Updates the reserves given a transaction with a specified number of shares and total cost.
Arguments
market::AbstractCPMM
: an abstract constant product market maker objectn_shares
: the number of sharescost
: the total cost of the sharesmidx
: market indexyes::Bool
: corresponds to a yes share if true; otherwise, corresponds to a no share