返回题库

Zipf 订单大小分布

Micro Zipf Order Size Distribution

专题
Finance / 金融
难度
L3
来源
MyntBit

题目详情

实证研究表明股票市场订单大小近似遵循幂律分布,类似 Zipf 分布。假设订单大小 X 超过值 x 的概率为 P(X > x) ∝ x^{-α},其中 α 为幂律指数。

任务:给定 α = 1.5,计算订单大小大于 100 股的概率 P(X > 100)。假设最小订单大小为 1 股,使用离散 Zipf 分布公式 P(X = k) = k^{-α} / ∑(i=1 to N) i^{-α} 计算。

英文原题

Empirical studies suggest that order sizes in equity markets approximately follow a power law, resembling a Zipf distribution. Suppose the probability that an order size, XX, exceeds a certain value, xx, is given by P(X>x)xαP(X > x) \propto x^{-\alpha}, where α1.5\alpha \approx 1.5. Based on this information, does this distribution have a finite mean? Does it have a finite variance?

解析

问题分析

真实市场的订单大小近似遵循 Zipf 分布(幂律分布):小订单极多,大订单极少。在回测和模拟中,使用 Zipf 分布生成合成订单流比均匀分布更接近真实市场行为。

实现

class ZipfOrderGenerator {
    std::mt19937 rng_;
    double alpha_;  // 幂律指数,通常在 1.0-2.0
    int max_size_;
public:
    ZipfOrderGenerator(double alpha = 1.5, int max = 10000) 
        : rng_(std::random_device{}()), alpha_(alpha), max_size_(max) {}
    
    int next() {
        static std::vector<double> cmf;  // 累积质量函数
        if (cmf.empty()) {
            double sum = 0;
            for (int i = 1; i <= max_size_; ++i) sum += 1.0 / std::pow(i, alpha_);
            cmf.resize(max_size_ + 1);
            for (int i = 1; i <= max_size_; ++i)
                cmf[i] = cmf[i-1] + (1.0 / std::pow(i, alpha_)) / sum;
        }
        double u = std::uniform_real_distribution<>(0, 1)(rng_);
        return std::lower_bound(cmf.begin(), cmf.end(), u) - cmf.begin();
    }
};

复杂度与边界

  • 时间复杂度:next O(log max_size)(二分查找),初始化 O(max_size)
  • 空间复杂度:O(max_size)
  • 边界条件:(1) alpha ≤ 0 时发散 (2) max_size 过大会内存溢出 (3) 预计算 CMF 可在构造时完成

英文解析

Analysis

Real market order sizes distribution approximately follows a Zipf distribution (power law): many small orders, few large orders. In backtesting and simulation, generating synthetic order flow using the Zipf distribution produces more realistic market behavior than uniform distribution.

Solution

class ZipfOrderGenerator {
    std::mt19937 rng_;
    double alpha_;  // Power law exponent, typically 1.0-2.0
    int max_size_;
public:
    ZipfOrderGenerator(double alpha = 1.5, int max = 10000)
        : rng_(std::random_device{}()), alpha_(alpha), max_size_(max) {}
    
    int next() {
        static std::vector<double> cmf;  // Cumulative mass function
        if (cmf.empty()) {
            double sum = 0;
            for (int i = 1; i <= max_size_; ++i) sum += 1.0 / std::pow(i, alpha_);
            cmf.resize(max_size_ + 1);
            for (int i = 1; i <= max_size_; ++i)
                cmf[i] = cmf[i-1] + (1.0 / std::pow(i, alpha_)) / sum;
        }
        double u = std::uniform_real_distribution<>(0, 1)(rng_);
        return std::lower_bound(cmf.begin(), cmf.end(), u) - cmf.begin();
    }
};

Complexity & Edge Cases

  • Time complexity: next O(log max_size) (binary search), initialization O(max_size)
  • Space complexity: O(max_size)
  • Edge cases: (1) alpha <= 0 causes divergence (2) Very large max_size causes memory overflow (3) Precomputed CMF can be done at construction time

Verification

Generate 1M orders, benchmark distribution against empirical market data. Verify that small orders dominate (Zipf distribution). Test different alpha values for fit quality.

Key Considerations

Zipf order size distribution is a well-known empirical result in market microstructure. The power law exponent alpha ~ 1.5 means the top 10 order sizes account for ~90% of total volume, while the remaining 90% of sizes contribute only ~10%. This has direct implications for execution algorithms: most market volume comes from a few large orders, so execution must prioritize handling block trades efficiently.