pymarket.mechanisms.p2p_random module

class pymarket.mechanisms.p2p_random.P2PTrading(bids, *args, **kwargs)[source]

Bases: pymarket.mechanisms.mechanism.Mechanism

Interface for P2PTrading.

Parameters:bids (pd.DataFrame) – Collections of bids to use
pymarket.mechanisms.p2p_random.p2p_random(bids, p_coef=0.5, r=None)[source]

Computes all the trades using a P2P random trading process inspired in [1].

Parameters:
  • bids (pd.DataFrame) – Collection of bids that will trade. Precondition: a user participates only in one side of the market, i.e, it cannot sell and buy in the same run.
  • p_coef (float) – coefficient to calculate the trading price as a convex combination of the price of the seller and the price of the buyer. If 1, the seller gets all the profit and if 0, the buyer gets all the profit.
  • r (np.random.RandomState) – Random state to generate stochastic values. If None, then the outcome of the market will be different on each run.
Returns:

  • trans (TransactionManger) – Collection of all the transactions that ocurred in the market
  • extra (dict) – Extra information provided by the mechanisms. Keys:
    • trading_list: list of list of tuples of all the pairs that traded in each round.

Notes

[1] Blouin, Max R., and Roberto Serrano. “A decentralized market with common values uncertainty: Non-steady states.” The Review of Economic Studies 68.2 (2001): 323-346.

Examples

>>> bm = pm.BidManager()
>>> bm.add_bid(1, 3, 0)
0
>>> bm.add_bid(1, 0.5, 1)
1
>>> bm.add_bid(1, 1, 2, False)
2
>>> bm.add_bid(1, 2, 3, False)
3
>>> r = np.random.RandomState(420)
>>> trans, extra = p2p_random(bm.get_df(), r=r)
>>> extra
{'trading_list': [[(0, 3), (1, 2)]]}
>>> trans.get_df()
   bid  quantity  price  source  active
0    0         1    2.5       3   False
1    3         1    2.5       0   False
2    1         0    0.0       2    True
3    2         0    0.0       1    True