Skip to main content

Queue Entropy Analysis

Queue-based concurrent system for Shannon entropy analysis of trader behavior patterns. Functional tests and synthetic market simulations pass locally. Performance (5M packets/sec) are extrapolated from micro benchmarks and require dedicated validation.
Source Code

Summary

Queue Entropy Analysis, provides a working implementation of “shannon entropy calculation” and a concurrent pipeline with tested functional correctness on synthetic data. The repository includes unit tests, edge case coverage, and market simulations. Performance numbers reported by my own “hardware” output is about (5M packets/sec, sub-millisecond latency), in other words based on a short synthetic micro benchmark and should not be treated as proof of throughput.

Key Achievements

Verified implementations:

  • Shannon Entropy Calculation: Implementation matching theoretical expectations
  • Test Coverage: 23 tests (5 queue edge cases, 6 entropy edge cases, 6 pipeline edge cases, 6 market simulations) all passing on my machine
  • Concurrent Pipeline: Working producer, consumer architecture with backpressure.
  • Code Quality: Fixes applied during validation (latency metric calculation, test assertion typos)

Actual performance characteristics:

  • Micro benchmark: Is based on a 5k event high frequency trading simulation, estimated to handle millions of operations per second on this machine; this is only an estimate, and not a sustained rate.
  • Queue implementation: Uses a mutex-based queue (locks so one thread accesses at a time) and a hybrid atomic queue (reduces locking where possible); meaning it is not a fully lock free implementation.
  • Testing: All tests use or uses a synthetic/simulated data; no current real market data validation as of yet.

Technical Implementation

The mutex ensures only one thread modifies the queue at a time, preventing data races in multithreaded use.

Core Concurrent Queue:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
template <typename T>
class ConcurrentQueue {
    void push(const T& value) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_queue.push(value);
        m_cond_var.notify_one();
    }
    
    bool try_pop(T& result) {
        std::lock_guard<std::mutex> lock(m_mutex);
        if (m_queue.empty()) return false;
        result = m_queue.front();
        m_queue.pop();
        return true;
    }
};

Calculates the Shannon entropy of trader actions, measuring their diversity or unpredictability.

Real-Time Entropy Calculation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
double EntropyCalculator::calculate_entropy(const std::vector<TraderAction>& actions) {
    if (actions.empty()) return 0.0;
    std::map<TraderAction, int> counts;
    for (const auto& action : actions) {
        counts[action]++;
    }
    double entropy = 0.0;
    int total = static_cast<int>(actions.size());
    for (const auto& [action, count] : counts) {
        double p = static_cast<double>(count) / total;
        if (p > 0) {
            entropy -= p * std::log2(p);
        }
    }
    return entropy;
}

Validation Results

Test Coverage:

  • Edge Case Testing: 17/17 tests passed (queue, entropy, pipeline edge cases)
  • Market Simulation: 6/6 synthetic scenarios executed (bull, bear, crash, normal, HFT, & recovery)
  • Mathematical Accuracy: Shannon entropy implementation should be correct, due to calculations matching theoretical expectations
  • Performance Note: Micro-benchmark projects millions of ops/sec from a 5k event simulation; dedicated real time testing required for sustained rates.

Market Behavior Patterns (test data):

  • Market Crashes: Low entropy (0.25-0.40 bits) + High sell dominance observed in test data
  • Normal Trading: High entropy (1.54 bits) + Balanced distribution in test data
  • Bull Markets: High entropy (1.39 bits) + More buys than sells in test data
  • Bear Markets: High entropy (1.27 bits) + More sells than buys in test data

Research Applications

Potential future uses (pending real world validation):

  • Behavioral analysis: Entropy quantifies trading action diversity; currently tested on synthetic data
  • Market pattern recognition: Simulations show low entropy correlates with panic selling, high entropy with diverse behavior
  • Queue performance: The pipeline handles simulated high-frequency workloads.
  • Research foundation: Provides a working codebase for entropy based market analysis experiments.

Technical Specifications

Language: C++17 with standard library features Queue Type: Mutex-based (ConcurrentQueue) or hybrid with atomics (OptimizedQueue). Entropy Calculation: Correct Shannon entropy formula H = -Σ(p_i * log2(p_i)) Memory Model: Uses std::mutex for synchronization; atomics for some metrics Thread Safety: Thread safe via mutexes and condition variables Entropy Range: 0.0 to ~1.585 bits (theoretical max for 3 discrete actions)

Current Status

What works:

  • Shannon entropy calculation is mathematically correct
  • All provided unit and simulation tests pass on this machine
  • Concurrent pipeline executes successfully with synthetic workloads
  • Code compiles cleanly with C++17

What is unvalidated:

  • Real market data integration
  • Sustained production throughput
  • Lock free performance (currently uses mutexes)
  • Real world applicability, of entropy volatility correlation

Next steps:

  • Build a proper benchmark harness, for sustained load testing.
  • Validate the performance, using real market data.
  • Test if the system needs a fully concurrent queue that doesn’t use locks. If it does, create or use one.
  • Profile and optimize under realistic workloads.

Repository Structure

Queue/
├── include/                    # Header files
├── src/                       # Source files
├── tests/                     # Test suites
├── CMakeLists.txt            # CMake build configuration
├── Makefile                  # Build automation
└── README.md                 # Project documentation

Build and Usage

Quick Start:

1
2
3
make all
make test
make perf

Manual Compilation:

1
g++ -std=c++17 -I include -pthread -O3 -o market_entropy_analyzer src/*.cpp