RustKernels
GPU-accelerated kernel library for financial services and analytics
Overview
RustKernels provides 106 GPU-accelerated algorithms across 14 domain-specific crates, designed for financial services, compliance, and enterprise analytics. Ported from the DotCompute C# implementation to Rust, using the RingKernel framework.
This is a specialized compute library for financial and enterprise workloads, not a general-purpose GPU compute framework.
Key Features
| Feature | Description |
|---|---|
| 14 Domain Categories | Graph analytics, ML, compliance, risk, treasury, and more |
| 106 Kernels | Comprehensive coverage of financial algorithms |
| Dual Execution Modes | Batch (CPU-orchestrated) and Ring (GPU-persistent) |
| Enterprise Ready | Apache-2.0 license, domain-based feature gating |
| K2K Messaging | Cross-kernel coordination patterns |
| Fixed-Point Arithmetic | Exact financial calculations |
Execution Modes
Kernels operate in one of two modes:
| Mode | Latency | Overhead | State Location | Best For |
|---|---|---|---|---|
| Batch | 10-50μs | Higher | CPU memory | Heavy periodic computation |
| Ring | 100-500ns | Minimal | GPU memory | High-frequency streaming |
Most kernels support both modes. Choose based on your latency requirements.
Domains at a Glance
| Domain | Crate | Kernels | Description |
|---|---|---|---|
| Graph Analytics | rustkernel-graph | 28 | PageRank, community detection, GNN inference, graph attention |
| Statistical ML | rustkernel-ml | 17 | Clustering, NLP embeddings, federated learning, healthcare analytics |
| Compliance | rustkernel-compliance | 11 | AML patterns, KYC, sanctions screening |
| Temporal Analysis | rustkernel-temporal | 7 | Forecasting, anomaly detection, decomposition |
| Risk Analytics | rustkernel-risk | 5 | Credit scoring, VaR, stress testing, correlation |
| Banking | rustkernel-banking | 1 | Fraud pattern matching |
| Behavioral Analytics | rustkernel-behavioral | 6 | Profiling, forensics, event correlation |
| Order Matching | rustkernel-orderbook | 1 | Order book matching engine |
| Process Intelligence | rustkernel-procint | 7 | DFG, conformance, digital twin simulation |
| Clearing | rustkernel-clearing | 5 | Netting, settlement, DVP matching |
| Treasury | rustkernel-treasury | 5 | Cash flow, FX hedging, liquidity |
| Accounting | rustkernel-accounting | 9 | Network generation, reconciliation |
| Payments | rustkernel-payments | 2 | Payment processing, flow analysis |
| Audit | rustkernel-audit | 2 | Feature extraction, hypergraph construction |
Quick Start
Add to your Cargo.toml:
[dependencies]
rustkernel = "0.1.0"
Basic usage:
use rustkernel::prelude::*;
use rustkernel::graph::centrality::PageRank;
// Create a kernel instance
let kernel = PageRank::new();
// Access kernel metadata
let metadata = kernel.metadata();
println!("Kernel: {}", metadata.id);
println!("Domain: {:?}", metadata.domain);
// Execute (batch mode)
let result = kernel.execute(input).await?;
Feature Flags
Control which domains are compiled:
# Only what you need
rustkernel = { version = "0.1.0", features = ["graph", "risk"] }
# Everything
rustkernel = { version = "0.1.0", features = ["full"] }
Default features: graph, ml, compliance, temporal, risk.
Requirements
- Rust 1.85 or later
- RustCompute (RingKernel framework)
- CUDA toolkit (optional, falls back to CPU execution)
Project Structure
crates/
├── rustkernel/ # Facade crate, re-exports all domains
├── rustkernel-core/ # Core traits, registry, licensing
├── rustkernel-derive/ # Procedural macros
├── rustkernel-cli/ # Command-line interface
└── rustkernel-{domain}/ # 14 domain-specific crates
Building
# Build entire workspace
cargo build --workspace
# Run all tests
cargo test --workspace
# Test single domain
cargo test --package rustkernel-graph
# Generate API documentation
cargo doc --workspace --no-deps --open
License
Licensed under Apache-2.0. See LICENSE for details.
Installation
This guide covers installing RustKernels and its dependencies.
Prerequisites
Rust Toolchain
RustKernels requires Rust 1.85 or later:
# Install Rust via rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Update to latest stable
rustup update stable
# Verify version
rustc --version # Should be 1.85.0 or higher
RustCompute Framework
RustKernels depends on the RustCompute (RingKernel) framework for GPU execution:
# Clone RustCompute alongside RustKernels
cd /path/to/your/projects
git clone https://github.com/mivertowski/RustCompute.git
# Directory structure should be:
# projects/
# ├── RustCompute/
# │ └── RustCompute/
# └── RustKernels/
# └── RustKernels/
CUDA Toolkit (Optional)
For GPU acceleration, install the CUDA toolkit:
- Linux: Install via your package manager or from NVIDIA’s website
- Windows: Download installer from NVIDIA
- macOS: Not supported for CUDA (CPU fallback only)
# Verify CUDA installation
nvcc --version
nvidia-smi
If CUDA is not available, RustKernels falls back to CPU execution automatically.
Adding RustKernels to Your Project
Basic Installation
Add to your Cargo.toml:
[dependencies]
rustkernel = "0.1.0"
This includes the default feature set: graph, ml, compliance, temporal, risk.
Selective Installation
Only include the domains you need to reduce compile time and binary size:
[dependencies]
rustkernel = { version = "0.1.0", default-features = false, features = ["graph", "accounting"] }
Full Installation
Include all 14 domains:
[dependencies]
rustkernel = { version = "0.1.0", features = ["full"] }
Available Features
| Feature | Domain | Description |
|---|---|---|
graph | Graph Analytics | Centrality, community detection, similarity |
ml | Statistical ML | Clustering, anomaly detection, regression |
compliance | Compliance | AML, KYC, sanctions screening |
temporal | Temporal Analysis | Forecasting, anomaly detection |
risk | Risk Analytics | Credit scoring, VaR, stress testing |
banking | Banking | Fraud pattern detection |
behavioral | Behavioral | Profiling, forensics |
orderbook | Order Matching | Order book engine |
procint | Process Intelligence | DFG, conformance checking |
clearing | Clearing | Netting, settlement |
treasury | Treasury | Cash flow, FX hedging |
accounting | Accounting | Network generation, reconciliation |
payments | Payments | Payment processing |
audit | Audit | Feature extraction |
full | All | Enables all domains |
Building from Source
Clone and build the entire workspace:
# Clone the repository
git clone https://github.com/mivertowski/RustKernels.git
cd RustKernels
# Build all crates
cargo build --workspace
# Build in release mode
cargo build --workspace --release
# Run tests
cargo test --workspace
Verifying Installation
Create a simple test file:
// src/main.rs
use rustkernel::prelude::*;
fn main() {
println!("RustKernels installed successfully!");
// List available domains
let domains = [
"Graph Analytics",
"Statistical ML",
"Compliance",
"Temporal Analysis",
"Risk Analytics",
];
for domain in domains {
println!(" - {}", domain);
}
}
Run with:
cargo run
Troubleshooting
RustCompute Not Found
If you see path errors related to RustCompute:
- Ensure RustCompute is cloned at the expected location
- Check that the directory structure matches what’s expected in
Cargo.toml - Verify the RustCompute workspace builds independently
CUDA Not Detected
If GPU execution isn’t working:
- Verify CUDA installation with
nvcc --version - Check GPU availability with
nvidia-smi - Ensure CUDA libraries are in your PATH
- RustKernels will fall back to CPU if CUDA isn’t available
Compilation Errors
For Rust version issues:
# Ensure you're on the correct toolchain
rustup override set stable
rustup update
Next Steps
- Quick Start - Run your first kernel
- Execution Modes - Understand Batch vs Ring modes
- Kernel Catalogue - Browse available kernels
Quick Start
Get up and running with RustKernels in 5 minutes.
Your First Kernel
Let’s run a PageRank calculation on a simple graph.
Step 1: Create a New Project
cargo new my-analytics
cd my-analytics
Step 2: Add Dependencies
Edit Cargo.toml:
[package]
name = "my-analytics"
version = "0.1.0"
edition = "2024"
[dependencies]
rustkernel = { version = "0.1.0", features = ["graph"] }
tokio = { version = "1.0", features = ["full"] }
Step 3: Write Your Code
Edit src/main.rs:
use rustkernel::prelude::*;
use rustkernel::graph::centrality::{PageRank, PageRankInput};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a PageRank kernel
let kernel = PageRank::new();
// Print kernel metadata
let metadata = kernel.metadata();
println!("Kernel: {}", metadata.id);
println!("Domain: {:?}", metadata.domain);
println!("Mode: {:?}", metadata.mode);
// Prepare input: a simple 4-node graph
// Node 0 -> Node 1, Node 2
// Node 1 -> Node 2
// Node 2 -> Node 0, Node 3
// Node 3 -> Node 0
let input = PageRankInput {
num_nodes: 4,
edges: vec![
(0, 1), (0, 2),
(1, 2),
(2, 0), (2, 3),
(3, 0),
],
damping_factor: 0.85,
max_iterations: 100,
tolerance: 1e-6,
};
// Execute the kernel
let result = kernel.execute(input).await?;
// Print results
println!("\nPageRank Scores:");
for (node, score) in result.scores.iter().enumerate() {
println!(" Node {}: {:.4}", node, score);
}
println!("\nConverged in {} iterations", result.iterations);
Ok(())
}
Step 4: Run
cargo run
Expected output:
Kernel: graph/pagerank
Domain: GraphAnalytics
Mode: Batch
PageRank Scores:
Node 0: 0.3682
Node 1: 0.1418
Node 2: 0.2879
Node 3: 0.2021
Converged in 23 iterations
Using Multiple Kernels
Combine kernels from different domains:
use rustkernel::prelude::*;
use rustkernel::graph::centrality::PageRank;
use rustkernel::graph::community::LouvainCommunity;
use rustkernel::graph::metrics::GraphDensity;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Analyze the same graph with multiple kernels
let edges = vec![
(0, 1), (0, 2), (1, 2),
(2, 3), (3, 4), (4, 2),
];
// Centrality analysis
let pagerank = PageRank::new();
let pr_result = pagerank.execute(PageRankInput {
num_nodes: 5,
edges: edges.clone(),
damping_factor: 0.85,
max_iterations: 100,
tolerance: 1e-6,
}).await?;
// Community detection
let louvain = LouvainCommunity::new();
let community_result = louvain.execute(LouvainInput {
num_nodes: 5,
edges: edges.clone(),
resolution: 1.0,
}).await?;
// Graph metrics
let density = GraphDensity::new();
let density_result = density.execute(DensityInput {
num_nodes: 5,
num_edges: edges.len(),
}).await?;
println!("Analysis complete:");
println!(" Communities found: {}", community_result.num_communities);
println!(" Graph density: {:.4}", density_result.density);
println!(" Most central node: {}", pr_result.top_node());
Ok(())
}
Kernel Configuration
Most kernels accept configuration options:
use rustkernel::ml::clustering::{KMeans, KMeansConfig};
let config = KMeansConfig {
num_clusters: 5,
max_iterations: 300,
tolerance: 1e-4,
initialization: KMeansInit::KMeansPlusPlus,
..Default::default()
};
let kernel = KMeans::with_config(config);
Batch vs Ring Mode
Batch Mode (Default)
CPU-orchestrated execution. Best for periodic computations:
// Batch kernels implement BatchKernel trait
let kernel = PageRank::new();
let result = kernel.execute(input).await?;
Ring Mode
GPU-persistent actors for streaming workloads:
// Ring kernels implement RingKernelHandler trait
use rustkernel::graph::centrality::PageRankRing;
// Ring kernels maintain persistent GPU state
let ring = PageRankRing::new();
// Send streaming updates
ring.add_edge(0, 1).await?;
ring.add_edge(1, 2).await?;
// Query current state
let scores = ring.query_scores().await?;
See Execution Modes for detailed comparison.
Error Handling
RustKernels uses standard Rust error handling:
use rustkernel::prelude::*;
use rustkernel::error::KernelError;
match kernel.execute(input).await {
Ok(result) => println!("Success: {:?}", result),
Err(KernelError::InvalidInput(msg)) => {
eprintln!("Invalid input: {}", msg);
}
Err(KernelError::ExecutionFailed(msg)) => {
eprintln!("Execution failed: {}", msg);
}
Err(e) => eprintln!("Error: {}", e),
}
Next Steps
- Architecture Overview - Understand the system design
- Kernel Catalogue - Explore all 82 kernels
- Accounting Network Generation - Deep-dive article
Architecture Overview
RustKernels is designed as a modular, high-performance GPU kernel library for financial and enterprise workloads. This document explains the system architecture and key design decisions.
System Design
┌─────────────────────────────────────────────────────────────────┐
│ rustkernel (facade) │
│ Re-exports all domain crates │
└─────────────────────────────────────────────────────────────────┘
│
┌───────────────────────┼───────────────────────┐
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ rustkernel-core │ │rustkernel-derive│ │ rustkernel-cli │
│ │ │ │ │ │
│ - Traits │ │ - #[gpu_kernel] │ │ - CLI tool │
│ - Registry │ │ - #[derive(...)]│ │ - Management │
│ - K2K messaging │ │ │ │ │
│ - Licensing │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ 14 Domain Crates │
│ │
│ graph │ ml │ compliance │ temporal │ risk │ banking │ ... │
│ │
│ Each domain implements domain-specific kernels using the core │
│ traits and infrastructure │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ RustCompute (RingKernel) │
│ GPU execution framework │
└─────────────────────────────────────────────────────────────────┘
Workspace Structure
The workspace contains 18 crates organized by concern:
Infrastructure Crates
| Crate | Purpose |
|---|---|
rustkernel | Facade crate, re-exports all domains |
rustkernel-core | Core traits, registry, licensing, K2K coordination |
rustkernel-derive | Procedural macros for kernel definition |
rustkernel-cli | Command-line interface for kernel management |
Domain Crates
14 domain-specific crates, each containing kernels for a particular business area:
crates/
├── rustkernel-graph/ # Graph analytics (21 kernels)
├── rustkernel-ml/ # Statistical ML (8 kernels)
├── rustkernel-compliance/ # AML/KYC (9 kernels)
├── rustkernel-temporal/ # Time series (7 kernels)
├── rustkernel-risk/ # Risk analytics (4 kernels)
├── rustkernel-banking/ # Banking (1 kernel)
├── rustkernel-behavioral/ # Behavioral (6 kernels)
├── rustkernel-orderbook/ # Order matching (1 kernel)
├── rustkernel-procint/ # Process intelligence (4 kernels)
├── rustkernel-clearing/ # Clearing/settlement (5 kernels)
├── rustkernel-treasury/ # Treasury (5 kernels)
├── rustkernel-accounting/ # Accounting (7 kernels)
├── rustkernel-payments/ # Payments (2 kernels)
└── rustkernel-audit/ # Audit (2 kernels)
Core Traits
All kernels are built on a set of core traits defined in rustkernel-core:
GpuKernel
The base trait for all kernels:
pub trait GpuKernel: Send + Sync + Debug {
/// Returns kernel metadata (ID, domain, mode, etc.)
fn metadata(&self) -> &KernelMetadata;
/// Validates kernel configuration
fn validate(&self) -> Result<()>;
}
BatchKernel
For CPU-orchestrated batch execution:
pub trait BatchKernel<I, O>: GpuKernel {
/// Execute the kernel with given input
async fn execute(&self, input: I) -> Result<O>;
}
RingKernelHandler
For GPU-persistent actor execution:
pub trait RingKernelHandler<M, R>: GpuKernel
where
M: RingMessage,
R: RingMessage,
{
/// Handle a message and produce a response
async fn handle(&self, ctx: &mut RingContext, msg: M) -> Result<R>;
}
IterativeKernel
For multi-pass algorithms (PageRank, K-Means, etc.):
pub trait IterativeKernel<S, I, O>: GpuKernel {
/// Create initial state from input
fn initial_state(&self, input: &I) -> S;
/// Perform one iteration
async fn iterate(&self, state: &mut S, input: &I) -> Result<IterationResult<O>>;
/// Check if algorithm has converged
fn converged(&self, state: &S, threshold: f64) -> bool;
}
Kernel Metadata
Every kernel has associated metadata:
pub struct KernelMetadata {
/// Unique identifier (e.g., "graph/pagerank")
pub id: String,
/// Execution mode
pub mode: KernelMode,
/// Business domain
pub domain: Domain,
/// Human-readable description
pub description: String,
/// Expected throughput (ops/sec)
pub expected_throughput: u64,
/// Target latency in microseconds
pub target_latency_us: f64,
/// Whether GPU-native execution is required
pub requires_gpu_native: bool,
/// Kernel version
pub version: u32,
}
K2K (Kernel-to-Kernel) Messaging
RustKernels supports cross-kernel coordination through K2K messaging patterns:
Available Patterns
| Pattern | Use Case |
|---|---|
IterativeState | Track convergence across iterations |
ScatterGatherState | Parallel worker coordination |
FanOutTracker | Broadcast patterns |
PipelineTracker | Multi-stage processing |
Example: Iterative Coordination
use rustkernel_core::k2k::IterativeState;
let mut state = IterativeState::new(max_iterations);
while !state.converged() {
// Execute iteration across kernels
let results = execute_iteration(&mut state).await?;
// Update convergence tracking
state.update(results.delta);
}
Domain Crate Structure
Each domain crate follows a consistent structure:
rustkernel-{domain}/
├── Cargo.toml
└── src/
├── lib.rs # Module exports, register_all()
├── messages.rs # Batch kernel input/output types
├── ring_messages.rs # Ring message types
├── types.rs # Common domain types
└── {feature}.rs # Kernel implementations
Example: Graph Analytics Crate
rustkernel-graph/
└── src/
├── lib.rs
├── messages.rs
├── ring_messages.rs
├── types.rs
├── centrality.rs # PageRank, Betweenness, etc.
├── community.rs # Louvain, Label Propagation
├── similarity.rs # Jaccard, Cosine, Adamic-Adar
├── metrics.rs # Density, Clustering Coefficient
└── motif.rs # Triangle counting, k-cliques
Ring Message Type IDs
Each domain has a reserved range for Ring message type IDs to avoid collisions:
| Domain | Range |
|---|---|
| Graph | 200-299 |
| Compliance | 300-399 |
| Temporal | 400-499 |
| Risk | 600-699 |
| ML | 700-799 |
Licensing System
RustKernels includes an enterprise licensing system:
- DevelopmentLicense: All features enabled (default for local development)
- ProductionLicense: Domain-based feature gating
- Validation occurs at kernel registration and activation time
use rustkernel_core::license::{LicenseValidator, DevelopmentLicense};
let validator = DevelopmentLicense::new();
assert!(validator.is_domain_licensed(Domain::GraphAnalytics));
Fixed-Point Arithmetic
For GPU-compatible and exact financial calculations, Ring messages use fixed-point arithmetic:
// 18 decimal places (accounting kernels)
const SCALE: i128 = 1_000_000_000_000_000_000;
pub struct FixedPoint128 {
pub value: i128,
}
impl FixedPoint128 {
pub fn from_f64(v: f64) -> Self {
Self { value: (v * SCALE as f64) as i128 }
}
pub fn to_f64(&self) -> f64 {
self.value as f64 / SCALE as f64
}
}
Next Steps
- Execution Modes - Deep dive into Batch vs Ring
- Kernel Catalogue - Browse available kernels
- Quick Start - Run your first kernel
Execution Modes
RustKernels supports two execution modes with different performance characteristics. Understanding these modes is essential for choosing the right approach for your workload.
Overview
| Aspect | Batch Mode | Ring Mode |
|---|---|---|
| Latency | 10-50μs | 100-500ns |
| Launch Overhead | Higher | Minimal |
| State Location | CPU memory | GPU memory |
| Programming Model | Request/response | Actor messages |
| Best For | Heavy periodic computation | High-frequency streaming |
Batch Mode
Batch mode provides CPU-orchestrated kernel execution. The kernel is launched on-demand, executes on the GPU, and returns results to the CPU.
Characteristics
- Launch overhead: 10-50μs per invocation
- State management: State lives in CPU memory between calls
- Execution model: Synchronous request/response
- Data transfer: Input copied to GPU, output copied back
When to Use Batch Mode
- Heavy computational tasks (matrix operations, large graph processing)
- Periodic batch jobs (nightly risk calculations, weekly reports)
- Tasks where launch overhead is negligible compared to computation time
- When you need to process a complete dataset at once
Implementation
Batch kernels implement the BatchKernel trait:
use rustkernel_core::traits::{GpuKernel, BatchKernel};
use rustkernel_core::kernel::KernelMetadata;
pub struct MyBatchKernel {
metadata: KernelMetadata,
}
impl GpuKernel for MyBatchKernel {
fn metadata(&self) -> &KernelMetadata {
&self.metadata
}
}
impl BatchKernel<MyInput, MyOutput> for MyBatchKernel {
async fn execute(&self, input: MyInput) -> Result<MyOutput> {
// GPU computation here
Ok(output)
}
}
Usage Example
use rustkernel::graph::centrality::PageRank;
let kernel = PageRank::new();
// Prepare large graph input
let input = PageRankInput {
num_nodes: 1_000_000,
edges: load_edges_from_file()?,
damping_factor: 0.85,
max_iterations: 100,
tolerance: 1e-6,
};
// Execute - may take seconds for large graphs
let result = kernel.execute(input).await?;
println!("Top node: {} with score {:.4}",
result.top_node(),
result.scores[result.top_node()]
);
Ring Mode
Ring mode provides GPU-persistent actors. The kernel maintains state on the GPU and processes messages with minimal latency.
Characteristics
- Message latency: 100-500ns per message
- State persistence: State remains on GPU between messages
- Execution model: Asynchronous actor messages
- Data transfer: Only message payloads transferred
When to Use Ring Mode
- High-frequency operations (order matching, real-time scoring)
- Streaming workloads (continuous data feeds)
- When sub-millisecond latency is critical
- Incremental updates to persistent state
Implementation
Ring kernels implement the RingKernelHandler trait:
use rustkernel_core::traits::{GpuKernel, RingKernelHandler};
use rustkernel_core::ring::{RingContext, RingMessage};
pub struct MyRingKernel {
metadata: KernelMetadata,
}
impl GpuKernel for MyRingKernel {
fn metadata(&self) -> &KernelMetadata {
&self.metadata
}
}
impl RingKernelHandler<MyRequest, MyResponse> for MyRingKernel {
async fn handle(
&self,
ctx: &mut RingContext,
msg: MyRequest,
) -> Result<MyResponse> {
// Process message, update GPU state
Ok(response)
}
}
Ring Message Definition
Ring messages use fixed-point arithmetic and rkyv serialization:
use ringkernel_derive::RingMessage;
use rkyv::{Archive, Serialize, Deserialize};
#[derive(Debug, Clone, Archive, Serialize, Deserialize, RingMessage)]
#[archive(check_bytes)]
#[message(type_id = 200)] // Unique within domain range
pub struct ScoreQueryRequest {
#[message(id)]
pub id: MessageId,
pub node_id: u32,
}
#[derive(Debug, Clone, Archive, Serialize, Deserialize, RingMessage)]
#[archive(check_bytes)]
#[message(type_id = 201)]
pub struct ScoreQueryResponse {
#[message(id)]
pub id: MessageId,
pub score_fp: i64, // Fixed-point score
}
Usage Example
use rustkernel::graph::centrality::PageRankRing;
// Create ring kernel (initializes GPU state)
let ring = PageRankRing::new();
// Stream of edge updates
for edge in incoming_edges {
// Low-latency edge addition
ring.add_edge(edge.from, edge.to).await?;
}
// Query current scores (sub-millisecond)
let scores = ring.query_scores().await?;
// Trigger re-computation if needed
ring.recalculate().await?;
Choosing Between Modes
Decision Matrix
| Scenario | Recommended Mode | Reason |
|---|---|---|
| Nightly risk report | Batch | Large computation, latency not critical |
| Real-time fraud scoring | Ring | Sub-ms latency required |
| Graph analysis on static data | Batch | One-time computation |
| Order book matching | Ring | Continuous high-frequency updates |
| ML model inference (bulk) | Batch | Process entire batch at once |
| ML model inference (streaming) | Ring | Incremental predictions |
Hybrid Approach
Many applications combine both modes:
// Batch: Initial heavy computation
let graph_kernel = GraphBuilder::new();
let initial_graph = graph_kernel.build(edges).await?;
// Ring: Real-time updates
let ring_kernel = GraphRing::from(initial_graph);
loop {
// Process streaming updates with low latency
let update = receive_update().await;
ring_kernel.handle(update).await?;
// Periodically sync state back for batch analysis
if should_sync() {
let snapshot = ring_kernel.snapshot().await?;
batch_analysis(snapshot).await?;
}
}
Performance Considerations
Batch Mode Optimization
- Batch your inputs: Process multiple items in one call
- Minimize data transfer: Only send required fields
- Use async: Don’t block on kernel completion
// Good: Process many items at once
let results = kernel.execute_batch(items).await?;
// Avoid: Processing one at a time
for item in items {
let result = kernel.execute(item).await?; // High overhead
}
Ring Mode Optimization
- Keep messages small: Only transfer deltas
- Batch when possible: Group related messages
- Use K2K for coordination: Avoid CPU round-trips
// Good: Small incremental update
ring.update_score(node_id, delta).await?;
// Avoid: Transferring full state
ring.set_all_scores(full_score_vector).await?; // Large transfer
Iterative Kernels
Some algorithms naturally span multiple iterations (PageRank, K-Means). These implement IterativeKernel:
pub trait IterativeKernel<S, I, O>: GpuKernel {
fn initial_state(&self, input: &I) -> S;
async fn iterate(&self, state: &mut S, input: &I) -> Result<IterationResult<O>>;
fn converged(&self, state: &S, threshold: f64) -> bool;
}
Usage:
let kernel = PageRank::new();
// Create initial state
let mut state = kernel.initial_state(&input);
// Iterate until convergence
while !kernel.converged(&state, tolerance) {
let result = kernel.iterate(&mut state, &input).await?;
println!("Iteration {}: delta={:.6}", result.iteration, result.delta);
}
Next Steps
- Architecture Overview - System design and components
- Kernel Catalogue - Available kernels by domain
- K2K Messaging - Cross-kernel coordination
Kernel Catalogue
RustKernels provides 106 GPU-accelerated kernels across 14 domain-specific crates. This catalogue organizes kernels by business domain.
Quick Reference
| Domain | Crate | Kernels | Primary Use Cases |
|---|---|---|---|
| Graph Analytics | rustkernel-graph | 28 | Centrality, GNN inference, community detection |
| Statistical ML | rustkernel-ml | 17 | Clustering, NLP, federated learning, healthcare |
| Compliance | rustkernel-compliance | 11 | AML, KYC, sanctions screening |
| Temporal Analysis | rustkernel-temporal | 7 | Forecasting, seasonality, anomalies |
| Risk Analytics | rustkernel-risk | 5 | Credit, market, portfolio risk |
| Banking | rustkernel-banking | 1 | Fraud pattern matching |
| Behavioral Analytics | rustkernel-behavioral | 6 | Profiling, forensics, correlation |
| Order Matching | rustkernel-orderbook | 1 | Order book matching engine |
| Process Intelligence | rustkernel-procint | 7 | DFG, conformance, digital twin simulation |
| Clearing | rustkernel-clearing | 5 | Netting, settlement, DVP |
| Treasury | rustkernel-treasury | 5 | Cash flow, FX, liquidity |
| Accounting | rustkernel-accounting | 9 | Network generation, reconciliation |
| Payments | rustkernel-payments | 2 | Payment processing, flow analysis |
| Audit | rustkernel-audit | 2 | Feature extraction, hypergraph |
Kernels by Execution Mode
Batch-Only Kernels (35)
Heavy computation kernels that only support batch mode:
- Graph: BetweennessCentrality, FullGraphMetrics, GNNInference, GraphAttention
- ML: DBSCAN, HierarchicalClustering, IsolationForest, SecureAggregation, DrugInteractionPrediction
- Compliance: EntityResolution, TransactionMonitoring
- Process: NextActivityPrediction, EventLogImputation, DigitalTwin
- And more…
Ring-Only Kernels (0)
Currently all Ring-capable kernels also support Batch mode.
Dual-Mode Kernels (71)
Kernels supporting both Batch and Ring execution:
- All centrality measures (PageRank, Degree, Closeness, etc.)
- All clustering algorithms (KMeans, Louvain, etc.)
- All risk calculations (VaR, Credit Scoring, etc.)
- Streaming ML (StreamingIsolationForest, AdaptiveThreshold)
- And more…
Using the Catalogue
Each domain page includes:
- Domain Overview - Purpose and key use cases
- Kernel List - All kernels with brief descriptions
- Kernel Details - For each kernel:
- Kernel ID and modes
- Input/output types
- Usage examples
- Performance characteristics
Feature Flags
Enable specific domains via Cargo features:
# Default domains
rustkernel = "0.1.0" # graph, ml, compliance, temporal, risk
# Selective
rustkernel = { version = "0.1.0", features = ["accounting", "treasury"] }
# All domains
rustkernel = { version = "0.1.0", features = ["full"] }
Kernel ID Convention
Kernel IDs follow the pattern {domain}/{kernel-name}:
graph/pagerank
ml/kmeans
compliance/aml-pattern-detection
risk/monte-carlo-var
accounting/network-generation
This enables hierarchical organization and clear domain ownership.
Graph Analytics
Crate: rustkernel-graph
Kernels: 28
Feature: graph (included in default features)
Graph analytics kernels for network analysis, social network analysis, knowledge graph operations, and AML/fraud detection.
Kernel Overview
Centrality Measures (6)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| PageRank | graph/pagerank | Batch, Ring | Power iteration with teleportation |
| DegreeCentrality | graph/degree-centrality | Batch, Ring | In/out/total degree counting |
| BetweennessCentrality | graph/betweenness-centrality | Batch | Brandes algorithm |
| ClosenessCentrality | graph/closeness-centrality | Batch, Ring | BFS-based distance calculation |
| EigenvectorCentrality | graph/eigenvector-centrality | Batch, Ring | Power iteration method |
| KatzCentrality | graph/katz-centrality | Batch, Ring | Attenuated path counting |
Community Detection (3)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| ModularityScore | graph/modularity-score | Batch | Community quality metric |
| LouvainCommunity | graph/louvain-community | Batch, Ring | Modularity optimization |
| LabelPropagation | graph/label-propagation | Batch, Ring | Fast community detection |
Similarity Measures (5)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| JaccardSimilarity | graph/jaccard-similarity | Batch, Ring | Neighbor set overlap |
| CosineSimilarity | graph/cosine-similarity | Batch, Ring | Vector-based similarity |
| AdamicAdarIndex | graph/adamic-adar-index | Batch | Weighted common neighbors |
| CommonNeighbors | graph/common-neighbors | Batch, Ring | Shared neighbor counting |
| ValueSimilarity | graph/value-similarity | Batch | Distribution comparison (JSD/Wasserstein) |
Graph Metrics (5)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| GraphDensity | graph/graph-density | Batch, Ring | Edge density calculation |
| AveragePathLength | graph/average-path-length | Batch | BFS-based distance sampling |
| ClusteringCoefficient | graph/clustering-coefficient | Batch, Ring | Local/global clustering |
| ConnectedComponents | graph/connected-components | Batch, Ring | Union-Find algorithm |
| FullGraphMetrics | graph/full-graph-metrics | Batch | Combined metrics computation |
Motif Detection (3)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| TriangleCounting | graph/triangle-counting | Batch, Ring | Triangle enumeration |
| MotifDetection | graph/motif-detection | Batch | Subgraph pattern matching |
| KCliqueDetection | graph/k-clique-detection | Batch | Complete subgraph finding |
Topology Analysis (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| DegreeRatio | graph/degree-ratio | Ring | In/out ratio for source/sink classification |
| StarTopologyScore | graph/star-topology | Batch | Hub-and-spoke detection (smurfing) |
Cycle Detection (1)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| ShortCycleParticipation | graph/cycle-participation | Batch | 2-4 hop cycle detection (AML) |
Path Analysis (1)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| ShortestPath | graph/shortest-path | Batch | BFS/Delta-Stepping SSSP/APSP |
Graph Neural Networks (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| GNNInference | graph/gnn-inference | Batch | Message-passing neural network inference |
| GraphAttention | graph/graph-attention | Batch | Multi-head graph attention networks |
Kernel Details
PageRank
The PageRank algorithm computes the importance of nodes based on link structure.
ID: graph/pagerank
Modes: Batch, Ring
Throughput: ~100,000 nodes/sec
Latency: 50μs (Batch), 500ns (Ring)
Input
pub struct PageRankInput {
/// Number of nodes in the graph
pub num_nodes: u32,
/// Edges as (from, to) pairs
pub edges: Vec<(u32, u32)>,
/// Damping factor (typically 0.85)
pub damping_factor: f64,
/// Maximum iterations
pub max_iterations: u32,
/// Convergence tolerance
pub tolerance: f64,
}
Output
pub struct PageRankOutput {
/// PageRank scores indexed by node ID
pub scores: Vec<f64>,
/// Number of iterations performed
pub iterations: u32,
/// Final delta (convergence measure)
pub delta: f64,
}
Example
use rustkernel::graph::centrality::{PageRank, PageRankInput};
let kernel = PageRank::new();
let input = PageRankInput {
num_nodes: 4,
edges: vec![(0, 1), (0, 2), (1, 2), (2, 0), (2, 3), (3, 0)],
damping_factor: 0.85,
max_iterations: 100,
tolerance: 1e-6,
};
let result = kernel.execute(input).await?;
println!("Top node: {}", result.top_node());
LouvainCommunity
Detects communities using the Louvain method for modularity optimization.
ID: graph/louvain-community
Modes: Batch, Ring
Throughput: ~50,000 nodes/sec
Input
pub struct LouvainInput {
pub num_nodes: u32,
pub edges: Vec<(u32, u32)>,
/// Resolution parameter (1.0 = standard modularity)
pub resolution: f64,
}
Output
pub struct LouvainOutput {
/// Community assignment per node
pub communities: Vec<u32>,
/// Number of communities found
pub num_communities: u32,
/// Final modularity score
pub modularity: f64,
}
TriangleCounting
Counts triangles in the graph, useful for clustering coefficient and network density analysis.
ID: graph/triangle-counting
Modes: Batch, Ring
Example
use rustkernel::graph::motif::{TriangleCounting, TriangleInput};
let kernel = TriangleCounting::new();
let result = kernel.execute(TriangleInput {
num_nodes: 100,
edges: edges,
}).await?;
println!("Triangles: {}", result.triangle_count);
println!("Clustering coefficient: {:.4}", result.global_clustering);
ShortCycleParticipation
Detects participation in short cycles (2-4 hops) which are key indicators for AML.
ID: graph/cycle-participation
Modes: Batch
Throughput: ~25,000 nodes/sec
Short cycles are critical AML indicators:
- 2-cycles (reciprocal): Immediate return transactions
- 3-cycles (triangles): Layering patterns - HIGH AML risk
- 4-cycles (squares): Organized laundering - CRITICAL AML risk
Example
use rustkernel::graph::cycles::{ShortCycleParticipation, CycleRiskLevel};
let kernel = ShortCycleParticipation::new();
let results = kernel.compute_all(&graph);
// Find high-risk nodes
for result in &results {
if matches!(result.risk_level, CycleRiskLevel::High | CycleRiskLevel::Critical) {
println!("HIGH RISK: Node {} participates in {} 4-cycles",
result.node_index, result.cycle_count_4hop);
}
}
// Count triangles in the graph
let triangles = ShortCycleParticipation::count_triangles(&graph);
DegreeRatio
Calculates in-degree/out-degree ratio for node classification.
ID: graph/degree-ratio
Modes: Ring
Latency: ~300ns per query
Classifies nodes as:
- Source: Mostly outgoing edges (payment originators)
- Sink: Mostly incoming edges (collection accounts)
- Balanced: Equal in/out (intermediary accounts)
Example
use rustkernel::graph::topology::{DegreeRatio, NodeClassification};
let results = DegreeRatio::compute_batch(&graph);
let roles = DegreeRatio::classify_nodes(&graph);
println!("Sources: {:?}", roles.sources);
println!("Sinks: {:?}", roles.sinks);
StarTopologyScore
Detects hub-and-spoke patterns for smurfing and money mule detection.
ID: graph/star-topology
Modes: Batch
Throughput: ~20,000 nodes/sec
Star types:
- In-Star: Collection pattern (many payers to one receiver)
- Out-Star: Distribution pattern (smurfing indicator)
- Mixed: Money mule hub
Example
use rustkernel::graph::topology::{StarTopologyScore, StarType};
let kernel = StarTopologyScore::with_min_degree(10);
let hubs = kernel.top_k_hubs(&graph, 10);
// Find potential smurfing accounts (out-stars)
let out_stars = kernel.find_out_stars(&graph, 0.8);
for hub in out_stars {
println!("POTENTIAL SMURFING: Node {} with score {:.2}",
hub.node_index, hub.star_score);
}
ShortestPath
Computes shortest paths using BFS or Delta-Stepping algorithm.
ID: graph/shortest-path
Modes: Batch
Throughput: ~50,000 nodes/sec
Supports:
- Single-source shortest path (SSSP)
- All-pairs shortest path (APSP)
- K-shortest paths (Yen’s algorithm)
Example
use rustkernel::graph::paths::ShortestPath;
// Single-source shortest path
let sssp = ShortestPath::compute_sssp_bfs(&graph, source);
println!("Distance to target: {}", sssp[target].distance);
// Reconstruct path
if let Some(path) = ShortestPath::compute_path(&graph, source, target) {
println!("Path: {:?}", path.node_path);
}
// Graph diameter
let diameter = ShortestPath::compute_diameter(&graph);
ValueSimilarity
Compares node value distributions using statistical distance metrics.
ID: graph/value-similarity
Modes: Batch
Throughput: ~25,000 pairs/sec
Metrics:
- Jensen-Shannon Divergence (JSD): Symmetric KL divergence
- Wasserstein Distance: Earth Mover’s Distance
Example
use rustkernel::graph::similarity::{ValueSimilarity, ValueDistribution};
// Create distributions from transaction amounts
let dist = ValueDistribution::from_values(&node_amounts, 50);
// Find similar nodes using JSD
let pairs = ValueSimilarity::compute_all_pairs_jsd(&dist, 0.9, 100);
for pair in &pairs {
println!("Similar: {} and {} (similarity: {:.3})",
pair.node_a, pair.node_b, pair.similarity);
}
GNNInference
Graph Neural Network inference using message passing.
ID: graph/gnn-inference
Modes: Batch
Throughput: ~10,000 nodes/sec
Supports configurable aggregation functions (mean, sum, max) and multiple message passing iterations.
Example
use rustkernel::graph::gnn::{GNNInference, GNNConfig, AggregationType};
let kernel = GNNInference::new();
// Configure the GNN
let config = GNNConfig {
hidden_dim: 64,
output_dim: 32,
num_layers: 2,
aggregation: AggregationType::Mean,
activation: ActivationType::ReLU,
};
// Run inference
let node_embeddings = kernel.infer(&graph, &node_features, &config)?;
println!("Node 0 embedding: {:?}", node_embeddings[0]);
GraphAttention
Multi-head graph attention network for node classification and link prediction.
ID: graph/graph-attention
Modes: Batch
Throughput: ~8,000 nodes/sec
Uses self-attention to learn importance weights for neighboring nodes.
Example
use rustkernel::graph::gnn::{GraphAttention, AttentionConfig};
let kernel = GraphAttention::new();
let config = AttentionConfig {
num_heads: 4,
hidden_dim: 64,
output_dim: 32,
dropout: 0.1,
concat_heads: true,
};
let output = kernel.forward(&graph, &node_features, &config)?;
println!("Attention weights: {:?}", output.attention_weights);
Ring Mode Usage
For high-frequency graph updates, use Ring mode:
use rustkernel::graph::centrality::PageRankRing;
let ring = PageRankRing::new();
// Add edges with low latency
ring.add_edge(0, 1).await?;
ring.add_edge(1, 2).await?;
// Query current scores
let score = ring.query_score(0).await?;
// Trigger recalculation
ring.recalculate().await?;
Performance Tips
- Use CSR format: For large static graphs, convert to CSR before processing
- Batch edge updates: When using Ring mode, batch multiple edges when possible
- Choose appropriate algorithms: BetweennessCentrality is O(V*E), consider sampling for large graphs
- Leverage GPU: Ensure CUDA is available for maximum throughput
Statistical ML
Crate: rustkernel-ml
Kernels: 17
Feature: ml (included in default features)
Machine learning kernels for clustering, anomaly detection, NLP, federated learning, and healthcare analytics.
Kernel Overview
Clustering (3)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| KMeans | ml/kmeans | Batch, Ring | K-means++ clustering |
| DBSCAN | ml/dbscan | Batch | Density-based clustering |
| HierarchicalClustering | ml/hierarchical-clustering | Batch | Agglomerative clustering |
Anomaly Detection (3)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| IsolationForest | ml/isolation-forest | Batch | Tree-based anomaly detection |
| LocalOutlierFactor | ml/local-outlier-factor | Batch, Ring | Density-based outlier detection |
| EnsembleVoting | ml/ensemble-voting | Batch, Ring | Combine multiple detectors |
Regression (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| LinearRegression | ml/linear-regression | Batch, Ring | Ordinary least squares |
| RidgeRegression | ml/ridge-regression | Batch, Ring | L2-regularized regression |
NLP / Embeddings (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| EmbeddingGeneration | ml/embedding-generation | Batch | Generate text embeddings from documents |
| SemanticSimilarity | ml/semantic-similarity | Batch | Compute similarity between document embeddings |
Federated Learning (1)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| SecureAggregation | ml/secure-aggregation | Batch | Privacy-preserving distributed model training |
Healthcare Analytics (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| DrugInteractionPrediction | ml/drug-interaction | Batch | Predict multi-drug interaction risks |
| ClinicalPathwayConformance | ml/clinical-pathway | Batch | Check treatment guideline compliance |
Streaming ML (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| StreamingIsolationForest | ml/streaming-iforest | Batch, Ring | Online anomaly detection |
| AdaptiveThreshold | ml/adaptive-threshold | Batch, Ring | Self-adjusting anomaly thresholds |
Explainability (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| SHAPValues | ml/shap-values | Batch | GPU-accelerated SHAP explanations |
| FeatureImportance | ml/feature-importance | Batch | Real-time feature attribution |
Kernel Details
KMeans
Partitions data into K clusters using the K-means++ initialization.
ID: ml/kmeans
Modes: Batch, Ring
Throughput: ~500,000 points/sec
Input
pub struct KMeansInput {
/// Data points as flattened array
pub points: Vec<f64>,
/// Number of dimensions per point
pub dimensions: u32,
/// Number of clusters
pub k: u32,
/// Maximum iterations
pub max_iterations: u32,
/// Convergence tolerance
pub tolerance: f64,
}
Output
pub struct KMeansOutput {
/// Cluster assignment per point
pub assignments: Vec<u32>,
/// Centroids (k * dimensions)
pub centroids: Vec<f64>,
/// Iterations performed
pub iterations: u32,
/// Inertia (sum of squared distances)
pub inertia: f64,
}
Example
use rustkernel::ml::clustering::{KMeans, KMeansInput};
let kernel = KMeans::new();
let input = KMeansInput {
points: vec![
1.0, 2.0, // Point 0
1.5, 1.8, // Point 1
5.0, 8.0, // Point 2
6.0, 9.0, // Point 3
],
dimensions: 2,
k: 2,
max_iterations: 100,
tolerance: 1e-4,
};
let result = kernel.execute(input).await?;
println!("Clusters: {:?}", result.assignments);
IsolationForest
Detects anomalies by isolating observations using random forests.
ID: ml/isolation-forest
Modes: Batch
Input
pub struct IsolationForestInput {
pub points: Vec<f64>,
pub dimensions: u32,
/// Number of trees
pub n_estimators: u32,
/// Subsample size
pub max_samples: u32,
/// Contamination ratio (expected anomaly rate)
pub contamination: f64,
}
Output
pub struct IsolationForestOutput {
/// Anomaly scores (higher = more anomalous)
pub scores: Vec<f64>,
/// Binary labels (-1 = anomaly, 1 = normal)
pub labels: Vec<i32>,
}
LocalOutlierFactor
Measures local density deviation to identify outliers.
ID: ml/local-outlier-factor
Modes: Batch, Ring
Example
use rustkernel::ml::anomaly::{LocalOutlierFactor, LOFInput};
let kernel = LocalOutlierFactor::new();
let result = kernel.execute(LOFInput {
points: data_points,
dimensions: 3,
k_neighbors: 20,
}).await?;
// Scores > 1.0 indicate outliers
let outliers: Vec<usize> = result.scores
.iter()
.enumerate()
.filter(|(_, &s)| s > 1.5)
.map(|(i, _)| i)
.collect();
LinearRegression
Fits a linear model using ordinary least squares.
ID: ml/linear-regression
Modes: Batch, Ring
Input
pub struct LinearRegressionInput {
/// Feature matrix (n_samples * n_features)
pub features: Vec<f64>,
/// Target values (n_samples)
pub targets: Vec<f64>,
/// Number of features
pub n_features: u32,
/// Whether to fit intercept
pub fit_intercept: bool,
}
Output
pub struct LinearRegressionOutput {
/// Coefficients (n_features, or n_features + 1 with intercept)
pub coefficients: Vec<f64>,
/// Intercept (if fit_intercept = true)
pub intercept: f64,
/// R-squared score
pub r_squared: f64,
}
EmbeddingGeneration
Generates text embeddings from documents using TF-IDF and n-gram features.
ID: ml/embedding-generation
Modes: Batch
Example
use rustkernel::ml::nlp::{EmbeddingGeneration, EmbeddingConfig};
let kernel = EmbeddingGeneration::new();
let config = EmbeddingConfig {
embedding_dim: 128,
ngram_range: (1, 2),
max_features: 10000,
use_idf: true,
};
let documents = vec!["financial transaction", "bank transfer"];
let embeddings = kernel.generate(&documents, &config)?;
SemanticSimilarity
Computes cosine similarity between document embeddings.
ID: ml/semantic-similarity
Modes: Batch
Example
use rustkernel::ml::nlp::{SemanticSimilarity, SimilarityConfig};
let kernel = SemanticSimilarity::new();
let similar = kernel.find_similar(
&embeddings,
query_index,
&SimilarityConfig { top_k: 10, threshold: 0.5, include_self: false }
)?;
SecureAggregation
Privacy-preserving federated learning with differential privacy.
ID: ml/secure-aggregation
Modes: Batch
Aggregates model updates from multiple clients while preserving privacy through noise injection and gradient clipping.
Example
use rustkernel::ml::federated::{SecureAggregation, AggregationConfig};
let kernel = SecureAggregation::new();
let config = AggregationConfig {
num_clients: 10,
clip_threshold: 1.0,
noise_multiplier: 0.1,
secure_mode: true,
};
let global_update = kernel.aggregate(&client_updates, &config)?;
DrugInteractionPrediction
Predicts multi-drug interaction risks using hypergraph neural networks.
ID: ml/drug-interaction
Modes: Batch
Example
use rustkernel::ml::healthcare::{DrugInteractionPrediction, DrugProfile};
let kernel = DrugInteractionPrediction::new();
let drugs = vec![
DrugProfile { id: "D001", features: moa_features.clone() },
DrugProfile { id: "D002", features: target_features.clone() },
];
let result = kernel.predict(&drugs)?;
println!("Interaction risk: {:.2}", result.risk_score);
ClinicalPathwayConformance
Checks treatment event sequences against clinical guidelines.
ID: ml/clinical-pathway
Modes: Batch
Example
use rustkernel::ml::healthcare::{ClinicalPathwayConformance, ClinicalPathway};
let kernel = ClinicalPathwayConformance::new();
let pathway = ClinicalPathway {
name: "Sepsis Protocol".to_string(),
required_steps: vec!["blood_culture", "antibiotics", "fluids"],
max_time_hours: 3.0,
};
let result = kernel.check_conformance(&events, &pathway)?;
println!("Conformance: {:.1}%", result.conformance_score * 100.0);
Ring Mode for Streaming ML
Ring mode enables online learning scenarios:
use rustkernel::ml::clustering::KMeansRing;
let ring = KMeansRing::new(k: 5, dimensions: 3);
// Stream data points
for point in incoming_stream {
// Assign to nearest cluster (sub-millisecond)
let cluster = ring.assign_point(point).await?;
// Periodically update centroids
if should_update_centroids() {
ring.update_centroids().await?;
}
}
Performance Considerations
- Dimensionality: High dimensions slow down distance calculations
- Memory: KMeans stores all points; for very large datasets, consider mini-batch
- Initialization: K-means++ is more expensive but gives better results
- GPU utilization: Ensure batch sizes are large enough to saturate GPU
Compliance
Crate: rustkernel-compliance
Kernels: 11
Feature: compliance (included in default features)
Anti-money laundering (AML), Know Your Customer (KYC), and regulatory compliance kernels.
Kernel Overview
AML Pattern Detection (6)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| CircularFlowRatio | compliance/circular-flow | Batch, Ring | Detect circular fund flows |
| ReciprocityFlowRatio | compliance/reciprocity-flow | Batch, Ring | Identify reciprocal transactions |
| RapidMovement | compliance/rapid-movement | Batch, Ring | Flag rapid fund movements |
| AMLPatternDetection | compliance/aml-pattern | Batch, Ring | Combined AML scoring |
| FlowReversalPattern | compliance/flow-reversal | Batch | Transaction reversal detection (wash trading) |
| FlowSplitRatio | compliance/flow-split | Batch | Structuring/smurfing detection |
KYC/Screening (4)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| KYCScoring | compliance/kyc-scoring | Batch, Ring | Customer risk scoring |
| EntityResolution | compliance/entity-resolution | Batch | Match entities across records |
| SanctionsScreening | compliance/sanctions-screening | Batch, Ring | Check against sanctions lists |
| PEPScreening | compliance/pep-screening | Batch, Ring | Politically Exposed Person screening |
Monitoring (1)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| TransactionMonitoring | compliance/transaction-monitoring | Batch | Real-time transaction analysis |
Kernel Details
AMLPatternDetection
Comprehensive AML scoring combining multiple detection methods.
ID: compliance/aml-pattern-detection
Modes: Batch, Ring
Input
pub struct AMLPatternInput {
/// Transaction graph edges (from_account, to_account, amount)
pub transactions: Vec<(String, String, f64)>,
/// Time window in seconds
pub time_window: u64,
/// Minimum amount threshold
pub min_amount: f64,
/// Detection thresholds
pub thresholds: AMLThresholds,
}
pub struct AMLThresholds {
pub circular_flow_threshold: f64,
pub reciprocity_threshold: f64,
pub rapid_movement_threshold: f64,
pub structuring_threshold: f64,
}
Output
pub struct AMLPatternOutput {
/// Overall risk scores per account
pub risk_scores: HashMap<String, f64>,
/// Detected patterns
pub patterns: Vec<DetectedPattern>,
/// High-risk accounts
pub flagged_accounts: Vec<String>,
}
pub struct DetectedPattern {
pub pattern_type: PatternType,
pub accounts: Vec<String>,
pub confidence: f64,
pub description: String,
}
Example
use rustkernel::compliance::aml::{AMLPatternDetection, AMLPatternInput};
let kernel = AMLPatternDetection::new();
let input = AMLPatternInput {
transactions: vec![
("A".into(), "B".into(), 9500.0),
("B".into(), "C".into(), 9400.0),
("C".into(), "A".into(), 9300.0), // Circular flow
],
time_window: 86400, // 24 hours
min_amount: 1000.0,
thresholds: AMLThresholds::default(),
};
let result = kernel.execute(input).await?;
for pattern in result.patterns {
println!("Detected: {:?} with confidence {:.2}",
pattern.pattern_type,
pattern.confidence
);
}
SanctionsScreening
Screens entities against sanctions and watchlists.
ID: compliance/sanctions-screening
Modes: Batch, Ring
Input
pub struct SanctionsScreeningInput {
/// Entities to screen
pub entities: Vec<EntityInfo>,
/// Sanctions list identifier
pub list_ids: Vec<String>,
/// Fuzzy matching threshold (0.0-1.0)
pub match_threshold: f64,
}
pub struct EntityInfo {
pub name: String,
pub aliases: Vec<String>,
pub country: Option<String>,
pub date_of_birth: Option<String>,
}
Output
pub struct SanctionsScreeningOutput {
/// Matches found
pub matches: Vec<SanctionsMatch>,
/// Number of entities screened
pub entities_screened: u32,
/// Processing time
pub processing_time_ms: u64,
}
pub struct SanctionsMatch {
pub entity_index: u32,
pub list_id: String,
pub matched_entry: String,
pub match_score: f64,
pub match_type: MatchType,
}
KYCScoring
Computes customer risk scores for KYC compliance.
ID: compliance/kyc-scoring
Modes: Batch, Ring
Example
use rustkernel::compliance::kyc::{KYCScoring, KYCInput};
let kernel = KYCScoring::new();
let result = kernel.execute(KYCInput {
customer_id: "CUST001".into(),
transaction_volume: 150000.0,
transaction_count: 45,
countries: vec!["US".into(), "UK".into()],
account_age_days: 365,
verification_level: VerificationLevel::Enhanced,
}).await?;
println!("Risk score: {:.2}", result.risk_score);
println!("Risk level: {:?}", result.risk_level);
CircularFlowRatio
Detects circular transaction patterns indicative of money laundering.
ID: compliance/circular-flow-ratio
Modes: Batch, Ring
The kernel analyzes transaction graphs to find cycles where money flows back to its origin:
A → B → C → A (circular flow detected)
Output
pub struct CircularFlowOutput {
/// Detected circular flows
pub circular_flows: Vec<CircularFlow>,
/// Overall circular flow ratio
pub overall_ratio: f64,
}
pub struct CircularFlow {
/// Accounts in the circular path
pub path: Vec<String>,
/// Total amount circulated
pub amount: f64,
/// Time span of the cycle
pub time_span_seconds: u64,
}
FlowReversalPattern
Detects transaction reversals (A→B followed by B→A) that may indicate wash trading, round-tripping, or layering.
ID: compliance/flow-reversal
Modes: Batch
Throughput: ~80,000 transactions/sec
Configuration
pub struct FlowReversalConfig {
/// Maximum time window to consider reversals (seconds)
pub max_window_seconds: u64, // default: 86400 (24 hours)
/// Time threshold for suspicious reversals (seconds)
pub suspicious_window_seconds: u64, // default: 3600 (1 hour)
/// Time threshold for critical reversals (seconds)
pub critical_window_seconds: u64, // default: 300 (5 minutes)
/// Minimum amount match ratio (0-1)
pub min_amount_match_ratio: f64, // default: 0.9
}
Output
pub struct FlowReversalResult {
/// Detected reversal pairs
pub reversals: Vec<FlowReversalPair>,
/// Total reversal volume
pub reversal_volume: f64,
/// Reversal ratio (reversal volume / total volume)
pub reversal_ratio: f64,
/// Entities with multiple reversals
pub repeat_offenders: Vec<(u64, u32)>,
/// Overall risk score (0-100)
pub risk_score: f64,
}
pub struct FlowReversalPair {
pub original_tx_id: u64,
pub reversal_tx_id: u64,
pub entity_a: u64,
pub entity_b: u64,
pub original_amount: f64,
pub reversal_amount: f64,
pub time_delta: u64,
pub amount_match_ratio: f64,
pub risk_level: ReversalRiskLevel, // Normal, Suspicious, High, Critical
}
Example
use rustkernel::compliance::aml::{FlowReversalPattern, FlowReversalConfig};
let result = FlowReversalPattern::compute(&transactions, &FlowReversalConfig {
max_window_seconds: 86400,
suspicious_window_seconds: 3600,
critical_window_seconds: 300,
min_amount_match_ratio: 0.9,
});
// Find critical reversals
for reversal in &result.reversals {
if matches!(reversal.risk_level, ReversalRiskLevel::Critical) {
println!("CRITICAL: {} -> {} reversed in {}s",
reversal.entity_a, reversal.entity_b, reversal.time_delta);
}
}
FlowSplitRatio
Detects structuring (smurfing) patterns where transactions are split to avoid reporting thresholds.
ID: compliance/flow-split
Modes: Batch
Throughput: ~60,000 transactions/sec
Configuration
pub struct FlowSplitConfig {
/// Reporting threshold to detect structuring around (e.g., $10,000)
pub reporting_threshold: f64, // default: 10_000.0 (BSA threshold)
/// Time window to look for split transactions (seconds)
pub window_seconds: u64, // default: 86400 (24 hours)
/// Minimum number of transactions to constitute a split
pub min_split_count: usize, // default: 3
}
Output
pub struct FlowSplitResult {
/// Detected split patterns
pub splits: Vec<FlowSplitPattern>,
/// Entities with structuring patterns
pub structuring_entities: Vec<u64>,
/// Total amount in split patterns
pub split_volume: f64,
/// Split ratio (split volume / total volume)
pub split_ratio: f64,
/// Overall risk score (0-100)
pub risk_score: f64,
}
pub struct FlowSplitPattern {
pub source_entity: u64,
pub dest_entities: Vec<u64>,
pub transaction_ids: Vec<u64>,
pub amounts: Vec<f64>,
pub total_amount: f64,
pub time_span: u64,
pub estimated_threshold: f64,
pub risk_level: SplitRiskLevel, // Normal, Elevated, High, Critical
}
Example
use rustkernel::compliance::aml::{FlowSplitRatio, FlowSplitConfig};
let result = FlowSplitRatio::compute(&transactions, &FlowSplitConfig {
reporting_threshold: 10_000.0, // BSA threshold
window_seconds: 86400,
min_split_count: 3,
});
// Find structuring entities
for entity in &result.structuring_entities {
println!("STRUCTURING ALERT: Entity {} flagged", entity);
}
// Analyze high-risk splits
for split in result.splits.iter().filter(|s|
matches!(s.risk_level, SplitRiskLevel::High | SplitRiskLevel::Critical)
) {
println!("Split detected: {} transactions totaling ${:.2}",
split.transaction_ids.len(), split.total_amount);
}
Ring Mode for Real-Time Compliance
Ring mode enables real-time transaction screening:
use rustkernel::compliance::aml::AMLPatternRing;
let ring = AMLPatternRing::new();
// Process streaming transactions
for tx in transaction_stream {
// Sub-millisecond screening
let alert = ring.screen_transaction(tx).await?;
if alert.risk_score > threshold {
notify_compliance_team(alert);
}
}
Integration Patterns
Batch Processing (Daily/Weekly)
// Load all transactions for period
let transactions = load_transactions(start_date, end_date)?;
// Run comprehensive AML analysis
let aml_result = aml_kernel.execute(AMLPatternInput {
transactions,
..Default::default()
}).await?;
// Generate compliance report
generate_sar_report(aml_result)?;
Real-Time Screening
// Screen each transaction before processing
async fn process_transaction(tx: Transaction) -> Result<()> {
let screening = sanctions_ring.screen(&tx.counterparty).await?;
if screening.is_match() {
return Err(TransactionBlocked::Sanctions);
}
// Continue with transaction processing
process_payment(tx).await
}
Regulatory Alignment
These kernels support requirements from:
- FATF: Financial Action Task Force recommendations
- BSA/AML: Bank Secrecy Act
- EU AMLD: EU Anti-Money Laundering Directives
- OFAC: Office of Foreign Assets Control sanctions
Temporal Analysis
Crate: rustkernel-temporal
Kernels: 7
Feature: temporal (included in default features)
Time series analysis kernels for forecasting, anomaly detection, and pattern recognition.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| ARIMAForecast | temporal/arima-forecast | Batch, Ring | Auto-regressive forecasting |
| ProphetDecomposition | temporal/prophet-decomposition | Batch | Facebook Prophet-style decomposition |
| ChangePointDetection | temporal/change-point-detection | Batch, Ring | Structural break detection |
| TimeSeriesAnomalyDetection | temporal/anomaly-detection | Batch, Ring | Anomaly scoring |
| SeasonalDecomposition | temporal/seasonal-decomposition | Batch | STL decomposition |
| TrendExtraction | temporal/trend-extraction | Batch, Ring | Trend component isolation |
| VolatilityAnalysis | temporal/volatility-analysis | Batch, Ring | GARCH/EWMA volatility |
Kernel Details
ARIMAForecast
Auto-Regressive Integrated Moving Average forecasting.
ID: temporal/arima-forecast
Modes: Batch, Ring
Input
pub struct ARIMAInput {
/// Time series values
pub values: Vec<f64>,
/// AR order (p)
pub p: u32,
/// Differencing order (d)
pub d: u32,
/// MA order (q)
pub q: u32,
/// Forecast horizon
pub forecast_periods: u32,
}
Output
pub struct ARIMAOutput {
/// Forecasted values
pub forecast: Vec<f64>,
/// Confidence intervals (lower, upper)
pub confidence_intervals: Vec<(f64, f64)>,
/// Fitted values
pub fitted: Vec<f64>,
/// Model coefficients
pub coefficients: ARIMACoefficients,
}
Example
use rustkernel::temporal::forecasting::{ARIMAForecast, ARIMAInput};
let kernel = ARIMAForecast::new();
let input = ARIMAInput {
values: historical_prices,
p: 2, // AR(2)
d: 1, // First differencing
q: 1, // MA(1)
forecast_periods: 30,
};
let result = kernel.execute(input).await?;
println!("30-day forecast: {:?}", result.forecast);
ChangePointDetection
Detects structural breaks in time series data.
ID: temporal/change-point-detection
Modes: Batch, Ring
Input
pub struct ChangePointInput {
pub values: Vec<f64>,
/// Detection method
pub method: ChangePointMethod,
/// Minimum segment length
pub min_segment_length: u32,
/// Penalty factor for number of change points
pub penalty: f64,
}
pub enum ChangePointMethod {
PELT, // Pruned Exact Linear Time
BinSeg, // Binary Segmentation
Window, // Sliding window
}
Output
pub struct ChangePointOutput {
/// Indices of detected change points
pub change_points: Vec<u32>,
/// Segment statistics
pub segments: Vec<SegmentStats>,
/// Overall detection confidence
pub confidence: f64,
}
VolatilityAnalysis
Estimates and forecasts volatility using GARCH/EWMA models.
ID: temporal/volatility-analysis
Modes: Batch, Ring
Example
use rustkernel::temporal::volatility::{VolatilityAnalysis, VolatilityInput};
let kernel = VolatilityAnalysis::new();
let result = kernel.execute(VolatilityInput {
returns: daily_returns,
model: VolatilityModel::GARCH { p: 1, q: 1 },
forecast_periods: 10,
}).await?;
println!("Current volatility: {:.4}", result.current_volatility);
println!("VaR (95%): {:.4}", result.var_95);
SeasonalDecomposition
Decomposes time series into trend, seasonal, and residual components.
ID: temporal/seasonal-decomposition
Modes: Batch
Input
pub struct SeasonalDecompInput {
pub values: Vec<f64>,
/// Seasonal period (e.g., 12 for monthly, 7 for daily)
pub period: u32,
/// Decomposition model
pub model: DecompModel,
}
pub enum DecompModel {
Additive, // y = trend + seasonal + residual
Multiplicative, // y = trend * seasonal * residual
}
Output
pub struct SeasonalDecompOutput {
pub trend: Vec<f64>,
pub seasonal: Vec<f64>,
pub residual: Vec<f64>,
/// Seasonal strength measure
pub seasonal_strength: f64,
}
TimeSeriesAnomalyDetection
Identifies anomalies in time series using multiple detection methods.
ID: temporal/anomaly-detection
Modes: Batch, Ring
Example
use rustkernel::temporal::detection::{TimeSeriesAnomalyDetection, AnomalyInput};
let kernel = TimeSeriesAnomalyDetection::new();
let result = kernel.execute(AnomalyInput {
values: sensor_readings,
method: AnomalyMethod::Twitter, // Twitter's anomaly detection
sensitivity: 0.05,
}).await?;
for anomaly in result.anomalies {
println!("Anomaly at index {}: value={:.2}, score={:.2}",
anomaly.index,
anomaly.value,
anomaly.score
);
}
Ring Mode for Streaming
Ring mode enables real-time time series processing:
use rustkernel::temporal::detection::AnomalyDetectionRing;
let ring = AnomalyDetectionRing::new();
// Process streaming data points
for (timestamp, value) in data_stream {
let result = ring.process_point(timestamp, value).await?;
if result.is_anomaly {
alert_system.notify(timestamp, result.anomaly_score);
}
}
Use Cases
Financial Time Series
- Stock price forecasting
- Volatility estimation for options pricing
- Regime change detection in markets
Operational Monitoring
- Server metric anomaly detection
- IoT sensor analysis
- Capacity planning forecasts
Business Analytics
- Sales seasonality analysis
- Demand forecasting
- Trend identification for KPIs
Risk Analytics
Crate: rustkernel-risk
Kernels: 4
Feature: risk (included in default features)
Financial risk calculation kernels for credit risk, market risk, and portfolio analysis.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| CreditRiskScoring | risk/credit-risk-scoring | Batch, Ring | PD/LGD/EAD calculations |
| MonteCarloVaR | risk/monte-carlo-var | Batch, Ring | Value-at-Risk simulation |
| PortfolioRiskAggregation | risk/portfolio-risk-aggregation | Batch, Ring | Portfolio-level risk metrics |
| StressTesting | risk/stress-testing | Batch | Scenario-based stress analysis |
Kernel Details
CreditRiskScoring
Calculates Probability of Default (PD), Loss Given Default (LGD), and Exposure at Default (EAD).
ID: risk/credit-risk-scoring
Modes: Batch, Ring
Input
pub struct CreditRiskInput {
/// Borrower information
pub borrowers: Vec<BorrowerInfo>,
/// Scoring model to use
pub model: CreditModel,
/// Time horizon in months
pub horizon_months: u32,
}
pub struct BorrowerInfo {
pub id: String,
pub credit_score: u32,
pub debt_to_income: f64,
pub loan_amount: f64,
pub collateral_value: Option<f64>,
pub industry_code: String,
pub years_in_business: u32,
}
pub enum CreditModel {
Scorecard, // Traditional scorecard
IRB, // Basel IRB approach
Merton, // Structural model
}
Output
pub struct CreditRiskOutput {
/// Risk metrics per borrower
pub risk_metrics: Vec<BorrowerRisk>,
/// Portfolio-level metrics
pub portfolio_metrics: PortfolioCreditMetrics,
}
pub struct BorrowerRisk {
pub borrower_id: String,
pub pd: f64, // Probability of Default
pub lgd: f64, // Loss Given Default
pub ead: f64, // Exposure at Default
pub expected_loss: f64,
pub risk_weight: f64, // For RWA calculation
}
Example
use rustkernel::risk::credit::{CreditRiskScoring, CreditRiskInput};
let kernel = CreditRiskScoring::new();
let result = kernel.execute(CreditRiskInput {
borrowers: vec![
BorrowerInfo {
id: "B001".into(),
credit_score: 720,
debt_to_income: 0.35,
loan_amount: 250_000.0,
collateral_value: Some(300_000.0),
industry_code: "REAL_ESTATE".into(),
years_in_business: 5,
},
],
model: CreditModel::IRB,
horizon_months: 12,
}).await?;
let risk = &result.risk_metrics[0];
println!("PD: {:.2}%, LGD: {:.2}%, EL: ${:.2}",
risk.pd * 100.0,
risk.lgd * 100.0,
risk.expected_loss
);
MonteCarloVaR
Calculates Value-at-Risk using Monte Carlo simulation.
ID: risk/monte-carlo-var
Modes: Batch, Ring
Throughput: ~1M simulations/sec
Input
pub struct VaRInput {
/// Portfolio positions
pub positions: Vec<Position>,
/// Number of simulations
pub n_simulations: u32,
/// Time horizon in days
pub horizon_days: u32,
/// Confidence levels
pub confidence_levels: Vec<f64>,
/// Correlation matrix (flattened)
pub correlations: Vec<f64>,
/// Volatilities per asset
pub volatilities: Vec<f64>,
}
pub struct Position {
pub asset_id: String,
pub quantity: f64,
pub current_price: f64,
}
Output
pub struct VaROutput {
/// VaR at each confidence level
pub var_values: HashMap<String, f64>,
/// Expected Shortfall (CVaR)
pub expected_shortfall: HashMap<String, f64>,
/// Simulated P&L distribution
pub pnl_distribution: Vec<f64>,
/// Component VaR by position
pub component_var: Vec<(String, f64)>,
}
Example
use rustkernel::risk::market::{MonteCarloVaR, VaRInput};
let kernel = MonteCarloVaR::new();
let result = kernel.execute(VaRInput {
positions: portfolio_positions,
n_simulations: 100_000,
horizon_days: 10,
confidence_levels: vec![0.95, 0.99],
correlations: correlation_matrix,
volatilities: asset_volatilities,
}).await?;
println!("10-day VaR (99%): ${:.2}", result.var_values["0.99"]);
println!("Expected Shortfall (99%): ${:.2}", result.expected_shortfall["0.99"]);
PortfolioRiskAggregation
Aggregates risk across multiple portfolios with diversification effects.
ID: risk/portfolio-risk-aggregation
Modes: Batch, Ring
Example
use rustkernel::risk::portfolio::{PortfolioRiskAggregation, AggregationInput};
let kernel = PortfolioRiskAggregation::new();
let result = kernel.execute(AggregationInput {
portfolio_vars: vec![
("Equities".into(), 1_000_000.0),
("Fixed Income".into(), 500_000.0),
("Commodities".into(), 250_000.0),
],
correlations: correlation_matrix,
method: AggregationMethod::VarianceCovariance,
}).await?;
println!("Undiversified VaR: ${:.2}", result.undiversified_var);
println!("Diversified VaR: ${:.2}", result.diversified_var);
println!("Diversification benefit: ${:.2}", result.diversification_benefit);
StressTesting
Evaluates portfolio impact under stress scenarios.
ID: risk/stress-testing
Modes: Batch
Input
pub struct StressTestInput {
pub portfolio: Portfolio,
pub scenarios: Vec<StressScenario>,
}
pub struct StressScenario {
pub name: String,
/// Shocks to risk factors
pub shocks: HashMap<String, f64>,
pub description: String,
}
Output
pub struct StressTestOutput {
pub results: Vec<ScenarioResult>,
pub worst_case: ScenarioResult,
}
pub struct ScenarioResult {
pub scenario_name: String,
pub pnl_impact: f64,
pub pnl_impact_pct: f64,
pub positions_affected: Vec<PositionImpact>,
}
Ring Mode for Real-Time Risk
Ring mode enables streaming risk calculations:
use rustkernel::risk::market::MonteCarloVaRRing;
let ring = MonteCarloVaRRing::new();
// Real-time position updates
ring.update_position("AAPL", 100, 185.50).await?;
ring.update_position("GOOG", 50, 142.30).await?;
// Query current VaR (sub-millisecond)
let current_var = ring.query_var(0.99).await?;
println!("Current VaR: ${:.2}", current_var);
// Recalculate on market data updates
ring.recalculate().await?;
Regulatory Applications
These kernels support:
- Basel III/IV: RWA calculation, capital adequacy
- FRTB: Fundamental Review of the Trading Book
- CCAR/DFAST: Fed stress testing requirements
- Solvency II: Insurance capital requirements
Banking
Crate: rustkernel-banking
Kernels: 1
Feature: banking
Specialized banking operations kernel for fraud detection.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| FraudPatternMatch | banking/fraud-pattern-match | Batch, Ring | Real-time fraud detection |
Kernel Details
FraudPatternMatch
GPU-accelerated fraud pattern detection using behavioral analysis and rule matching.
ID: banking/fraud-pattern-match
Modes: Batch, Ring
Throughput: ~500,000 transactions/sec
Input
pub struct FraudPatternInput {
/// Transactions to analyze
pub transactions: Vec<Transaction>,
/// Pattern rules to apply
pub rules: Vec<FraudRule>,
/// Historical behavior profiles
pub profiles: HashMap<String, BehaviorProfile>,
/// Detection threshold
pub threshold: f64,
}
pub struct Transaction {
pub id: String,
pub account_id: String,
pub amount: f64,
pub currency: String,
pub merchant_category: String,
pub location: Option<GeoLocation>,
pub timestamp: u64,
pub channel: TransactionChannel,
}
pub struct BehaviorProfile {
pub avg_transaction_amount: f64,
pub typical_merchants: Vec<String>,
pub typical_locations: Vec<GeoLocation>,
pub typical_hours: Vec<u8>,
}
pub enum TransactionChannel {
CardPresent,
CardNotPresent,
ATM,
Wire,
ACH,
}
Output
pub struct FraudPatternOutput {
/// Fraud scores per transaction
pub scores: Vec<TransactionScore>,
/// Triggered rules
pub triggered_rules: Vec<TriggeredRule>,
/// Recommended actions
pub actions: Vec<RecommendedAction>,
}
pub struct TransactionScore {
pub transaction_id: String,
pub fraud_score: f64,
pub risk_factors: Vec<RiskFactor>,
pub recommendation: Recommendation,
}
pub enum Recommendation {
Approve,
Review,
Decline,
Challenge, // 3D Secure, OTP, etc.
}
Example
use rustkernel::banking::fraud::{FraudPatternMatch, FraudPatternInput};
let kernel = FraudPatternMatch::new();
let result = kernel.execute(FraudPatternInput {
transactions: vec![
Transaction {
id: "TX001".into(),
account_id: "ACC123".into(),
amount: 5000.0,
currency: "USD".into(),
merchant_category: "ELECTRONICS".into(),
location: Some(GeoLocation { lat: 40.7128, lon: -74.0060 }),
timestamp: 1699000000,
channel: TransactionChannel::CardNotPresent,
},
],
rules: default_rules(),
profiles: customer_profiles,
threshold: 0.7,
}).await?;
for score in result.scores {
if score.fraud_score > 0.7 {
println!("High risk: {} - score {:.2}",
score.transaction_id,
score.fraud_score
);
for factor in score.risk_factors {
println!(" - {:?}", factor);
}
}
}
Detection Methods
Behavioral Analysis
Compares transactions against established customer behavior:
- Amount deviation: Transaction size vs historical average
- Location deviation: Geographic distance from typical locations
- Time deviation: Transaction time vs typical activity hours
- Merchant deviation: New merchant category or type
Rule-Based Detection
Configurable rules for known fraud patterns:
pub struct FraudRule {
pub id: String,
pub name: String,
pub conditions: Vec<RuleCondition>,
pub score_impact: f64,
pub enabled: bool,
}
pub enum RuleCondition {
AmountGreaterThan(f64),
VelocityExceeds { count: u32, window_seconds: u64 },
LocationMismatch { max_distance_km: f64 },
NewMerchant,
HighRiskCountry(Vec<String>),
CardNotPresentHighValue,
}
Velocity Checks
Detects rapid transaction patterns:
- Multiple transactions in short time
- Multiple cards on same device
- Multiple devices for same card
Ring Mode for Real-Time Scoring
Ring mode enables sub-millisecond fraud scoring:
use rustkernel::banking::fraud::FraudPatternRing;
let ring = FraudPatternRing::new();
// Pre-load customer profiles
ring.load_profiles(profiles).await?;
// Score transactions in real-time
async fn score_transaction(tx: Transaction) -> FraudDecision {
let score = ring.score(tx).await?;
match score.fraud_score {
s if s < 0.3 => FraudDecision::Approve,
s if s < 0.7 => FraudDecision::Review,
_ => FraudDecision::Decline,
}
}
Integration Patterns
Authorization Flow
// In payment authorization path
async fn authorize(tx: Transaction) -> AuthResult {
// 1. Real-time fraud scoring (< 100ms)
let fraud_result = fraud_ring.score(tx.clone()).await?;
if fraud_result.recommendation == Recommendation::Decline {
return AuthResult::Declined("Fraud risk");
}
if fraud_result.recommendation == Recommendation::Challenge {
return AuthResult::Challenge3DS;
}
// 2. Continue with authorization
process_authorization(tx).await
}
Batch Analysis
// Daily fraud pattern review
async fn daily_fraud_review() {
let transactions = load_day_transactions().await?;
let result = fraud_kernel.execute(FraudPatternInput {
transactions,
rules: all_rules(),
profiles: all_profiles(),
threshold: 0.5, // Lower threshold for review
}).await?;
// Generate suspicious activity report
generate_sar_report(result.scores.filter(|s| s.fraud_score > 0.5));
}
Performance Considerations
- Profile caching: Keep frequently accessed profiles in GPU memory
- Rule optimization: Order rules by selectivity (most filtering first)
- Batch when possible: Process multiple transactions per GPU call
- Async patterns: Don’t block authorization on slow operations
Behavioral Analytics
Crate: rustkernel-behavioral
Kernels: 6
Feature: behavioral
Behavioral profiling, forensic analysis, and event correlation kernels.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| BehavioralProfiling | behavioral/profiling | Batch, Ring | Build user behavior profiles |
| AnomalyProfiling | behavioral/anomaly-profiling | Batch, Ring | Detect profile deviations |
| FraudSignatureDetection | behavioral/fraud-signature | Batch, Ring | Match known fraud patterns |
| CausalGraphConstruction | behavioral/causal-graph | Batch | Build causal relationship graphs |
| ForensicQueryExecution | behavioral/forensic-query | Batch, Ring | Complex forensic queries |
| EventCorrelationKernel | behavioral/event-correlation | Batch, Ring | Correlate events across sources |
Kernel Details
BehavioralProfiling
Constructs behavioral profiles from historical activity data.
ID: behavioral/profiling
Modes: Batch, Ring
Input
pub struct ProfilingInput {
pub entity_id: String,
pub events: Vec<BehaviorEvent>,
pub profile_type: ProfileType,
pub time_window_days: u32,
}
pub struct BehaviorEvent {
pub timestamp: u64,
pub event_type: String,
pub attributes: HashMap<String, String>,
pub numeric_values: HashMap<String, f64>,
}
pub enum ProfileType {
User,
Account,
Device,
Session,
}
Output
pub struct ProfilingOutput {
pub profile: BehaviorProfile,
pub confidence: f64,
pub data_quality: DataQuality,
}
pub struct BehaviorProfile {
pub entity_id: String,
pub typical_patterns: Vec<Pattern>,
pub statistics: ProfileStatistics,
pub risk_indicators: Vec<RiskIndicator>,
}
EventCorrelationKernel
Correlates events across multiple data sources to identify related activities.
ID: behavioral/event-correlation
Modes: Batch, Ring
Example
use rustkernel::behavioral::correlation::{EventCorrelationKernel, CorrelationInput};
let kernel = EventCorrelationKernel::new();
let result = kernel.execute(CorrelationInput {
events: vec![
Event { source: "auth", type_: "login_failure", entity: "user123", ts: 1000 },
Event { source: "auth", type_: "login_success", entity: "user123", ts: 1005 },
Event { source: "api", type_: "data_export", entity: "user123", ts: 1010 },
],
correlation_window_seconds: 60,
correlation_rules: default_rules(),
}).await?;
for chain in result.correlated_chains {
println!("Attack chain detected:");
for event in chain.events {
println!(" {} -> {}", event.source, event.type_);
}
}
CausalGraphConstruction
Builds causal relationship graphs from event sequences.
ID: behavioral/causal-graph
Modes: Batch
Output
pub struct CausalGraphOutput {
pub nodes: Vec<CausalNode>,
pub edges: Vec<CausalEdge>,
pub root_causes: Vec<String>,
pub impact_paths: Vec<ImpactPath>,
}
pub struct CausalEdge {
pub from: String,
pub to: String,
pub strength: f64,
pub lag_seconds: u64,
}
Use Cases
Security Operations
- Detect account takeover attempts
- Identify insider threats
- Correlate security events across systems
Fraud Investigation
- Build fraud case timelines
- Identify related accounts
- Trace fund flows across entities
User Analytics
- Understand user journeys
- Predict churn risk
- Personalize experiences
Order Matching
Crate: rustkernel-orderbook
Kernels: 1
Feature: orderbook
High-performance order book matching engine for trading systems.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| OrderMatchingEngine | orderbook/matching-engine | Batch, Ring | GPU-accelerated order matching |
Kernel Details
OrderMatchingEngine
Ultra-low latency order matching with price-time priority.
ID: orderbook/matching-engine
Modes: Batch, Ring
Latency: <1μs per order (Ring mode)
Input
pub struct OrderInput {
pub orders: Vec<Order>,
pub symbol: String,
}
pub struct Order {
pub id: String,
pub side: Side,
pub price: f64,
pub quantity: u64,
pub order_type: OrderType,
pub time_in_force: TimeInForce,
pub timestamp: u64,
}
pub enum Side {
Buy,
Sell,
}
pub enum OrderType {
Limit,
Market,
StopLimit { trigger_price: f64 },
IcebergLimit { display_qty: u64 },
}
pub enum TimeInForce {
GTC, // Good Till Cancelled
IOC, // Immediate Or Cancel
FOK, // Fill Or Kill
GTD { expiry: u64 }, // Good Till Date
}
Output
pub struct OrderOutput {
pub executions: Vec<Execution>,
pub book_state: BookState,
pub statistics: MatchingStatistics,
}
pub struct Execution {
pub execution_id: String,
pub buy_order_id: String,
pub sell_order_id: String,
pub price: f64,
pub quantity: u64,
pub timestamp: u64,
}
pub struct BookState {
pub bids: Vec<PriceLevel>,
pub asks: Vec<PriceLevel>,
pub last_trade_price: f64,
pub last_trade_quantity: u64,
}
Example
use rustkernel::orderbook::{OrderMatchingEngine, OrderInput, Order, Side, OrderType};
let kernel = OrderMatchingEngine::new();
let result = kernel.execute(OrderInput {
orders: vec![
Order {
id: "O1".into(),
side: Side::Buy,
price: 100.50,
quantity: 1000,
order_type: OrderType::Limit,
time_in_force: TimeInForce::GTC,
timestamp: 1699000000,
},
Order {
id: "O2".into(),
side: Side::Sell,
price: 100.50,
quantity: 500,
order_type: OrderType::Limit,
time_in_force: TimeInForce::GTC,
timestamp: 1699000001,
},
],
symbol: "AAPL".into(),
}).await?;
for exec in result.executions {
println!("Execution: {} shares @ ${:.2}",
exec.quantity,
exec.price
);
}
Ring Mode for Live Trading
Ring mode maintains order book state on GPU for sub-microsecond matching:
use rustkernel::orderbook::OrderMatchingRing;
let ring = OrderMatchingRing::new("AAPL");
// Process incoming orders
async fn process_order(order: Order) -> Vec<Execution> {
match order.order_type {
OrderType::Limit => ring.add_limit_order(order).await?,
OrderType::Market => ring.add_market_order(order).await?,
_ => unimplemented!(),
}
}
// Cancel order
ring.cancel_order("O123").await?;
// Query book state
let book = ring.get_book_snapshot().await?;
println!("Best bid: ${:.2}", book.bids[0].price);
println!("Best ask: ${:.2}", book.asks[0].price);
Matching Rules
Price-Time Priority
Orders are matched following price-time priority:
- Best price first (highest bid, lowest ask)
- Earlier orders at same price matched first
Order Types
| Type | Behavior |
|---|---|
| Limit | Rests on book until filled or cancelled |
| Market | Executes immediately at best available |
| Stop-Limit | Converts to limit when trigger price hit |
| Iceberg | Only displays partial quantity |
Time in Force
| TIF | Behavior |
|---|---|
| GTC | Remains until filled or cancelled |
| IOC | Fill available, cancel remainder |
| FOK | Fill entire quantity or reject |
| GTD | Expires at specified time |
Performance Characteristics
- Throughput: >1M orders/sec (batch)
- Latency: <1μs per order (ring)
- Book depth: Unlimited price levels
- Symbols: One ring per symbol
Integration Notes
For production trading systems:
- Sequencer: Orders must be sequenced before matching
- Persistence: Log all orders and executions
- Risk checks: Pre-trade risk should precede matching
- Market data: Publish book updates after each match
Process Intelligence
Crate: rustkernel-procint
Kernels: 7
Feature: procint
Process mining and analysis kernels for business process optimization.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| DFGConstruction | procint/dfg-construction | Batch, Ring | Build Directly-Follows Graphs |
| PartialOrderAnalysis | procint/partial-order-analysis | Batch | Analyze process concurrency |
| ConformanceChecking | procint/conformance-checking | Batch, Ring | Check process compliance |
| OCPMPatternMatching | procint/ocpm-pattern-matching | Batch | Object-Centric Process Mining |
| NextActivityPrediction | procint/next-activity-prediction | Batch | Predict next activity in process |
| EventLogImputation | procint/event-log-imputation | Batch | Handle missing events in logs |
| DigitalTwin | procint/digital-twin | Batch | Process simulation for what-if analysis |
Kernel Details
DFGConstruction
Constructs Directly-Follows Graphs from event logs.
ID: procint/dfg-construction
Modes: Batch, Ring
Input
pub struct DFGInput {
/// Event log entries
pub events: Vec<ProcessEvent>,
/// Minimum edge frequency threshold
pub min_frequency: u32,
}
pub struct ProcessEvent {
pub case_id: String,
pub activity: String,
pub timestamp: u64,
pub resource: Option<String>,
pub attributes: HashMap<String, String>,
}
Output
pub struct DFGOutput {
/// Activities (nodes)
pub activities: Vec<Activity>,
/// Edges with frequencies
pub edges: Vec<DFGEdge>,
/// Start activities
pub start_activities: Vec<String>,
/// End activities
pub end_activities: Vec<String>,
}
pub struct DFGEdge {
pub from: String,
pub to: String,
pub frequency: u32,
pub avg_duration_seconds: f64,
}
Example
use rustkernel::procint::dfg::{DFGConstruction, DFGInput};
let kernel = DFGConstruction::new();
let result = kernel.execute(DFGInput {
events: vec![
ProcessEvent { case_id: "C1".into(), activity: "Submit".into(), timestamp: 1000, .. },
ProcessEvent { case_id: "C1".into(), activity: "Review".into(), timestamp: 2000, .. },
ProcessEvent { case_id: "C1".into(), activity: "Approve".into(), timestamp: 3000, .. },
],
min_frequency: 1,
}).await?;
for edge in result.edges {
println!("{} -> {} (freq: {}, avg: {:.1}s)",
edge.from,
edge.to,
edge.frequency,
edge.avg_duration_seconds
);
}
ConformanceChecking
Checks if process executions conform to a reference model.
ID: procint/conformance-checking
Modes: Batch, Ring
Input
pub struct ConformanceInput {
pub events: Vec<ProcessEvent>,
pub reference_model: ProcessModel,
pub tolerance: ConformanceTolerance,
}
pub struct ProcessModel {
pub activities: Vec<String>,
pub transitions: Vec<(String, String)>,
pub start: String,
pub end: String,
}
Output
pub struct ConformanceOutput {
/// Fitness score (0.0 - 1.0)
pub fitness: f64,
/// Precision score
pub precision: f64,
/// Deviations found
pub deviations: Vec<Deviation>,
/// Per-case conformance
pub case_conformance: HashMap<String, f64>,
}
pub struct Deviation {
pub case_id: String,
pub deviation_type: DeviationType,
pub activity: String,
pub description: String,
}
OCPMPatternMatching
Object-Centric Process Mining for complex, multi-object processes.
ID: procint/ocpm-pattern-matching
Modes: Batch
Example
use rustkernel::procint::ocpm::{OCPMPatternMatching, OCPMInput};
let kernel = OCPMPatternMatching::new();
let result = kernel.execute(OCPMInput {
events: order_events,
object_types: vec!["Order".into(), "Item".into(), "Delivery".into()],
patterns: vec![
Pattern::BottleneckDetection,
Pattern::ObjectLifecycle,
Pattern::InteractionAnalysis,
],
}).await?;
for bottleneck in result.bottlenecks {
println!("Bottleneck: {} (avg wait: {:.1}h)",
bottleneck.activity,
bottleneck.avg_wait_hours
);
}
NextActivityPrediction
Predicts the next activity in a process using sequence models.
ID: procint/next-activity-prediction
Modes: Batch
Example
use rustkernel::procint::prediction::{NextActivityPrediction, PredictionConfig};
let kernel = NextActivityPrediction::new();
let config = PredictionConfig {
sequence_length: 10,
top_k: 3,
};
let predictions = kernel.predict(&event_sequence, &config)?;
for (activity, prob) in predictions {
println!("{}: {:.1}%", activity, prob * 100.0);
}
EventLogImputation
Handles missing events, incorrect timestamps, and duplicates in event logs.
ID: procint/event-log-imputation
Modes: Batch
Example
use rustkernel::procint::imputation::{EventLogImputation, ImputationConfig};
let kernel = EventLogImputation::new();
let config = ImputationConfig {
detect_missing: true,
fix_timestamps: true,
remove_duplicates: true,
};
let cleaned_log = kernel.impute(&raw_events, &config)?;
println!("Fixed {} issues", cleaned_log.issues_fixed);
DigitalTwin
Process simulation for what-if analysis and optimization using Monte Carlo methods.
ID: procint/digital-twin
Modes: Batch
Example
use rustkernel::procint::simulation::{DigitalTwin, ProcessModel, SimulationConfig};
let kernel = DigitalTwin::new();
let config = SimulationConfig {
num_simulations: 1000,
time_horizon_hours: 24.0,
seed: Some(42),
};
let result = kernel.simulate(&process_model, &config)?;
println!("Avg completion time: {:.2}h", result.avg_completion_time_hours);
println!("Bottleneck: {}", result.bottlenecks[0].activity);
Use Cases
Process Discovery
- Automatically discover process models from logs
- Identify common paths and variants
- Measure process performance
Compliance Monitoring
- Ensure processes follow defined procedures
- Detect deviations in real-time
- Generate audit trails
Process Optimization
- Identify bottlenecks using DigitalTwin simulation
- Analyze resource utilization
- Run what-if scenarios for capacity planning
Predictive Analytics
- Predict next activities for proactive intervention
- Clean and impute event logs for better analysis
- Estimate remaining process time
Clearing
Crate: rustkernel-clearing
Kernels: 5
Feature: clearing
Post-trade clearing, settlement, and netting kernels for financial market infrastructure.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| ClearingValidation | clearing/validation | Batch, Ring | Validate clearing eligibility |
| DVPMatching | clearing/dvp-matching | Batch, Ring | Delivery vs Payment matching |
| NettingCalculation | clearing/netting-calculation | Batch, Ring | Multilateral netting |
| SettlementExecution | clearing/settlement-execution | Batch | Execute settlement instructions |
| ZeroBalanceFrequency | clearing/zero-balance-frequency | Batch, Ring | Optimize netting efficiency |
Kernel Details
NettingCalculation
Calculates multilateral netting positions to minimize settlement volume.
ID: clearing/netting-calculation
Modes: Batch, Ring
Input
pub struct NettingInput {
/// Trades to net
pub trades: Vec<Trade>,
/// Netting set definition
pub netting_set: NettingSet,
/// Currency for settlement
pub settlement_currency: String,
}
pub struct Trade {
pub id: String,
pub buyer: String,
pub seller: String,
pub instrument: String,
pub quantity: i64,
pub price: f64,
pub trade_date: u64,
pub settlement_date: u64,
}
pub struct NettingSet {
pub participants: Vec<String>,
pub netting_type: NettingType,
}
pub enum NettingType {
Bilateral,
Multilateral,
CCP, // Central Counterparty
}
Output
pub struct NettingOutput {
/// Net positions per participant
pub positions: Vec<NetPosition>,
/// Settlement instructions
pub instructions: Vec<SettlementInstruction>,
/// Netting statistics
pub statistics: NettingStatistics,
}
pub struct NetPosition {
pub participant: String,
pub instrument: String,
pub net_quantity: i64,
pub net_value: f64,
}
pub struct NettingStatistics {
pub gross_value: f64,
pub net_value: f64,
pub netting_efficiency: f64, // (1 - net/gross) * 100
pub trades_netted: u32,
}
Example
use rustkernel::clearing::netting::{NettingCalculation, NettingInput};
let kernel = NettingCalculation::new();
let result = kernel.execute(NettingInput {
trades: vec![
Trade { buyer: "A".into(), seller: "B".into(), quantity: 100, price: 50.0, .. },
Trade { buyer: "B".into(), seller: "A".into(), quantity: 80, price: 51.0, .. },
Trade { buyer: "A".into(), seller: "C".into(), quantity: 50, price: 49.0, .. },
],
netting_set: NettingSet {
participants: vec!["A".into(), "B".into(), "C".into()],
netting_type: NettingType::Multilateral,
},
settlement_currency: "USD".into(),
}).await?;
println!("Netting efficiency: {:.1}%", result.statistics.netting_efficiency);
for pos in result.positions {
println!("{}: {} units, ${:.2}", pos.participant, pos.net_quantity, pos.net_value);
}
DVPMatching
Matches Delivery versus Payment instructions to ensure atomic settlement.
ID: clearing/dvp-matching
Modes: Batch, Ring
Output
pub struct DVPMatchOutput {
pub matched_pairs: Vec<MatchedPair>,
pub unmatched_deliveries: Vec<String>,
pub unmatched_payments: Vec<String>,
pub match_rate: f64,
}
SettlementExecution
Executes settlement instructions with fail handling.
ID: clearing/settlement-execution
Modes: Batch
Example
use rustkernel::clearing::settlement::{SettlementExecution, SettlementInput};
let kernel = SettlementExecution::new();
let result = kernel.execute(SettlementInput {
instructions: settlement_instructions,
available_securities: securities_inventory,
available_cash: cash_positions,
fail_tolerance: FailTolerance::PartialAllowed,
}).await?;
println!("Settled: {}/{}", result.settled_count, result.total_count);
for fail in result.fails {
println!("Failed: {} - {}", fail.instruction_id, fail.reason);
}
Use Cases
- CCP clearing: Central counterparty netting and novation
- Securities settlement: DVP settlement for equities, bonds
- FX settlement: CLS-style payment-versus-payment
- Derivatives clearing: Margin calculation and variation margin
Treasury
Crate: rustkernel-treasury
Kernels: 5
Feature: treasury
Treasury management kernels for cash flow, FX hedging, and liquidity optimization.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| CashFlowForecasting | treasury/cash-flow-forecasting | Batch, Ring | Predict future cash flows |
| CollateralOptimization | treasury/collateral-optimization | Batch | Optimize collateral allocation |
| FXHedging | treasury/fx-hedging | Batch, Ring | FX exposure and hedging |
| InterestRateRisk | treasury/interest-rate-risk | Batch, Ring | Duration, convexity, DV01 |
| LiquidityOptimization | treasury/liquidity-optimization | Batch | LCR/NSFR optimization |
Kernel Details
CashFlowForecasting
Forecasts cash positions across accounts and time horizons.
ID: treasury/cash-flow-forecasting
Modes: Batch, Ring
Input
pub struct CashFlowInput {
/// Current positions
pub positions: Vec<CashPosition>,
/// Expected inflows
pub inflows: Vec<CashFlow>,
/// Expected outflows
pub outflows: Vec<CashFlow>,
/// Forecast horizon in days
pub horizon_days: u32,
}
pub struct CashPosition {
pub account_id: String,
pub currency: String,
pub balance: f64,
}
pub struct CashFlow {
pub account_id: String,
pub amount: f64,
pub currency: String,
pub expected_date: u64,
pub probability: f64,
pub category: FlowCategory,
}
Output
pub struct CashFlowOutput {
/// Daily forecast per account/currency
pub daily_forecast: Vec<DailyPosition>,
/// Minimum/maximum projections
pub min_projection: Vec<f64>,
pub max_projection: Vec<f64>,
/// Shortfall alerts
pub shortfall_alerts: Vec<ShortfallAlert>,
}
FXHedging
Analyzes FX exposures and recommends hedging strategies.
ID: treasury/fx-hedging
Modes: Batch, Ring
Example
use rustkernel::treasury::fx::{FXHedging, FXHedgingInput};
let kernel = FXHedging::new();
let result = kernel.execute(FXHedgingInput {
exposures: vec![
FXExposure {
currency_pair: "EUR/USD".into(),
amount: 1_000_000.0,
direction: ExposureDirection::Long,
maturity_days: 90,
},
],
hedging_instruments: available_instruments,
risk_tolerance: RiskTolerance::Moderate,
hedge_ratio_target: 0.80,
}).await?;
for recommendation in result.recommendations {
println!("Hedge {} with {} {} forward",
recommendation.exposure,
recommendation.amount,
recommendation.instrument
);
}
InterestRateRisk
Calculates interest rate risk metrics for fixed income portfolios.
ID: treasury/interest-rate-risk
Modes: Batch, Ring
Output
pub struct InterestRateRiskOutput {
/// Modified duration
pub duration: f64,
/// Convexity
pub convexity: f64,
/// Dollar value of a basis point
pub dv01: f64,
/// Key rate durations
pub key_rate_durations: HashMap<String, f64>,
/// Scenario analysis
pub scenario_pnl: HashMap<String, f64>,
}
LiquidityOptimization
Optimizes liquidity positions for regulatory compliance (LCR, NSFR).
ID: treasury/liquidity-optimization
Modes: Batch
Example
use rustkernel::treasury::liquidity::{LiquidityOptimization, LiquidityInput};
let kernel = LiquidityOptimization::new();
let result = kernel.execute(LiquidityInput {
assets: liquid_assets,
liabilities: funding_sources,
target_lcr: 1.10, // 110% target
target_nsfr: 1.05, // 105% target
constraints: optimization_constraints,
}).await?;
println!("Current LCR: {:.1}%", result.current_lcr * 100.0);
println!("Optimized LCR: {:.1}%", result.optimized_lcr * 100.0);
for action in result.recommended_actions {
println!("Action: {}", action.description);
}
Use Cases
- Cash management: Forecast positions, optimize sweeps
- FX treasury: Manage currency exposures, hedge programs
- ALM: Asset-liability management, gap analysis
- Regulatory: LCR/NSFR compliance, stress testing
Accounting
Crate: rustkernel-accounting
Kernels: 9
Feature: accounting
Accounting network generation, reconciliation, and analysis kernels for financial close and audit.
Kernel Overview
Core Kernels (7)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| ChartOfAccountsMapping | accounting/coa-mapping | Batch | Map between chart of accounts |
| JournalTransformation | accounting/journal-transformation | Batch, Ring | Transform journal entries |
| GLReconciliation | accounting/gl-reconciliation | Batch, Ring | General ledger reconciliation |
| NetworkAnalysis | accounting/network-analysis | Batch, Ring | Intercompany network analysis |
| TemporalCorrelation | accounting/temporal-correlation | Batch | Account correlation over time |
| NetworkGeneration | accounting/network-generation | Batch | Generate accounting networks |
| NetworkGenerationRing | accounting/network-generation-ring | Ring | Streaming network generation |
Detection Kernels (2)
| Kernel | ID | Modes | Description |
|---|---|---|---|
| SuspenseAccountDetection | accounting/suspense-detection | Batch | Centrality-based suspense account detection |
| GaapViolationDetection | accounting/gaap-violation | Batch | GAAP prohibited flow pattern detection |
Kernel Details
NetworkGeneration
Transforms double-entry journal entries into directed accounting flow networks.
ID: accounting/network-generation
Modes: Batch
Feature Article: Accounting Network Generation
Input
pub struct NetworkGenerationInput {
/// Journal entries to process
pub entries: Vec<JournalEntry>,
/// Configuration options
pub config: Option<NetworkGenerationConfig>,
}
pub struct JournalEntry {
pub id: u64,
pub date: u64,
pub posting_date: u64,
pub document_number: String,
pub lines: Vec<JournalLine>,
pub status: JournalStatus,
pub source_system: String,
pub description: String,
}
pub struct JournalLine {
pub line_number: u32,
pub account_code: String,
pub debit: f64,
pub credit: f64,
pub currency: String,
pub entity_id: String,
pub cost_center: Option<String>,
pub description: String,
}
Output
pub struct NetworkGenerationOutput {
/// Generated accounting flows
pub flows: Vec<AccountingFlow>,
/// Network statistics
pub stats: NetworkGenerationStats,
}
pub struct AccountingFlow {
pub flow_id: String,
pub entry_id: u64,
pub from_account: String,
pub to_account: String,
pub amount: f64,
pub method: SolvingMethod,
pub confidence: f64,
pub from_entity: String,
pub to_entity: String,
/// Account classification
pub from_account_class: Option<AccountClass>,
pub to_account_class: Option<AccountClass>,
/// Detected transaction pattern
pub pattern: Option<TransactionPattern>,
/// VAT/tax indicators
pub is_tax_flow: bool,
pub vat_rate: Option<f64>,
}
Solving Methods
The kernel uses five methods with decreasing confidence:
| Method | Confidence | Description |
|---|---|---|
| Method A | 1.00 | Trivial 1-to-1 mapping |
| Method B | 0.95 | n-to-n bijective matching |
| Method C | 0.85 | n-to-m partition matching |
| Method D | 0.70 | Account aggregation |
| Method E | 0.50 | Entity decomposition |
Example
use rustkernel::accounting::network_generation::{NetworkGeneration, NetworkGenerationInput};
let kernel = NetworkGeneration::new();
let result = kernel.execute(NetworkGenerationInput {
entries: journal_entries,
config: Some(NetworkGenerationConfig {
enable_pattern_matching: true,
enable_vat_detection: true,
..Default::default()
}),
}).await?;
println!("Generated {} flows", result.flows.len());
println!("Weighted confidence: {:.2}", result.stats.weighted_confidence);
// Analyze flows by pattern
let sales = result.flows.iter()
.filter(|f| matches!(f.pattern, Some(TransactionPattern::SaleWithVat)))
.count();
println!("Sales transactions: {}", sales);
GLReconciliation
Reconciles general ledger balances across systems.
ID: accounting/gl-reconciliation
Modes: Batch, Ring
Output
pub struct ReconciliationOutput {
pub matched_pairs: Vec<MatchedPair>,
pub unmatched: Vec<String>,
pub exceptions: Vec<ReconciliationException>,
pub stats: ReconciliationStats,
}
pub struct ReconciliationStats {
pub total_items: usize,
pub matched_count: usize,
pub match_rate: f64,
pub total_variance: f64,
}
NetworkAnalysis
Analyzes intercompany transaction networks for consolidation.
ID: accounting/network-analysis
Modes: Batch, Ring
Example
use rustkernel::accounting::network::{NetworkAnalysis, NetworkAnalysisInput};
let kernel = NetworkAnalysis::new();
let result = kernel.execute(NetworkAnalysisInput {
transactions: intercompany_transactions,
entities: group_entities,
analysis_type: AnalysisType::Elimination,
}).await?;
println!("Elimination entries needed: {}", result.elimination_entries.len());
for entry in result.elimination_entries {
println!("Eliminate: {} -> {} (${:.2})",
entry.from_entity,
entry.to_entity,
entry.amount
);
}
TemporalCorrelation
Analyzes correlations between accounts over time to detect anomalies.
ID: accounting/temporal-correlation
Modes: Batch
Output
pub struct CorrelationOutput {
pub correlations: Vec<AccountCorrelation>,
pub anomalies: Vec<CorrelationAnomaly>,
pub stats: CorrelationStats,
}
pub struct AccountCorrelation {
pub account_a: String,
pub account_b: String,
pub coefficient: f64,
pub p_value: f64,
}
SuspenseAccountDetection
Identifies suspense accounts using centrality-based analysis on the account transaction graph.
ID: accounting/suspense-detection
Modes: Batch
Throughput: ~20,000 accounts/sec
Suspense accounts are detected based on:
- High centrality: Accounts that connect many other accounts
- High turnover: Large volume relative to balance
- Short holding period: Funds don’t stay long
- Balanced flows: Equal in/out suggests clearing function
- Zero end balance: Period-end balance near zero
- Naming patterns: Contains “suspense”, “clearing”, “holding”
Configuration
pub struct SuspenseDetectionConfig {
/// Minimum betweenness centrality to flag
pub centrality_threshold: f64, // default: 0.1
/// Minimum turnover ratio (turnover/balance)
pub turnover_ratio_threshold: f64, // default: 10.0
/// Maximum average holding period (days)
pub holding_period_threshold: f64, // default: 7.0
/// Minimum balance ratio to consider balanced (0-1)
pub balance_ratio_threshold: f64, // default: 0.9
/// Minimum counterparty count to flag
pub counterparty_threshold: usize, // default: 5
/// Maximum balance to consider "zero"
pub zero_balance_threshold: f64, // default: 100.0
}
Output
pub struct SuspenseAccountResult {
/// Detected suspense account candidates
pub candidates: Vec<SuspenseAccountCandidate>,
/// High-risk accounts
pub high_risk_accounts: Vec<String>,
/// Total accounts analyzed
pub accounts_analyzed: usize,
/// Overall risk score
pub risk_score: f64,
}
pub struct SuspenseAccountCandidate {
pub account_code: String,
pub account_name: String,
pub suspense_score: f64, // 0-100
pub centrality_score: f64,
pub turnover_volume: f64,
pub avg_holding_period: f64,
pub counterparty_count: usize,
pub balance_ratio: f64,
pub risk_level: SuspenseRiskLevel, // Low, Medium, High, Critical
pub indicators: Vec<SuspenseIndicator>,
}
Example
use rustkernel::accounting::detection::{SuspenseAccountDetection, SuspenseDetectionConfig};
let result = SuspenseAccountDetection::detect(&journal_entries, &SuspenseDetectionConfig {
centrality_threshold: 0.1,
holding_period_threshold: 7.0,
..Default::default()
});
// Review high-risk suspense accounts
for account in &result.high_risk_accounts {
println!("HIGH RISK: Account {} flagged as suspense", account);
}
// Analyze candidates
for candidate in &result.candidates {
println!("{}: score={:.1}, centrality={:.3}, indicators={:?}",
candidate.account_code,
candidate.suspense_score,
candidate.centrality_score,
candidate.indicators);
}
GaapViolationDetection
Detects prohibited transaction patterns that violate GAAP principles.
ID: accounting/gaap-violation
Modes: Batch
Throughput: ~15,000 entries/sec
Detected violation types:
- DirectRevenueExpense: Direct transfer from revenue to expense without capital account
- RevenueInflation: Circular flows that may inflate revenue
- ImproperAssetExpense: Asset expensed without proper depreciation
- SuspenseAccountMisuse: Large amounts in suspense accounts
- ImproperElimination: Incorrect intercompany eliminations
- ProhibitedRelatedParty: Prohibited related-party transactions
Configuration
pub struct GaapDetectionConfig {
/// Threshold for suspense account amounts
pub suspense_amount_threshold: f64, // default: 10_000.0
/// Minimum amount for asset-to-expense flag
pub asset_expense_threshold: f64, // default: 5_000.0
/// Minimum circular flow amount
pub circular_flow_threshold: f64, // default: 1_000.0
}
Output
pub struct GaapViolationResult {
/// Detected violations
pub violations: Vec<GaapViolation>,
/// Total entries analyzed
pub entries_analyzed: usize,
/// Total amount at risk
pub amount_at_risk: f64,
/// Overall compliance score (0-100, higher is better)
pub compliance_score: f64,
/// Violation counts by type
pub violation_counts: HashMap<String, usize>,
}
pub struct GaapViolation {
pub id: String,
pub violation_type: GaapViolationType,
pub accounts: Vec<String>,
pub entry_ids: Vec<u64>,
pub amount: f64,
pub description: String,
pub severity: GaapViolationSeverity, // Minor, Moderate, Major, Critical
pub remediation: String,
}
Example
use rustkernel::accounting::detection::{GaapViolationDetection, GaapDetectionConfig};
use std::collections::HashMap;
// Map account codes to types
let mut account_types = HashMap::new();
account_types.insert("SALES_REVENUE".to_string(), AccountType::Revenue);
account_types.insert("SALARIES_EXPENSE".to_string(), AccountType::Expense);
account_types.insert("EQUIPMENT_ASSET".to_string(), AccountType::Asset);
let result = GaapViolationDetection::detect(
&journal_entries,
&account_types,
&GaapDetectionConfig::default()
);
println!("Compliance score: {:.1}%", result.compliance_score);
println!("Amount at risk: ${:.2}", result.amount_at_risk);
// Review violations by severity
for violation in result.violations.iter()
.filter(|v| matches!(v.severity, GaapViolationSeverity::Major | GaapViolationSeverity::Critical))
{
println!("{}: {} - {}",
violation.id,
violation.description,
violation.remediation);
}
Enhanced Features
Account Classification
Automatic classification of accounts:
- Asset (1xxx): Cash, receivables, inventory
- Liability (2xxx): Payables, debt, accruals
- Equity (3xxx): Capital, retained earnings
- Revenue (4xxx): Sales, service income
- COGS (5xxx): Cost of goods sold
- Expense (6xxx-7xxx): Operating expenses
- Tax: VAT, GST, withholding tax
VAT Detection
Automatic detection of VAT patterns:
- EU standard rates (19-25%)
- Reduced rates (5-10%)
- GST/HST (Canada, Australia)
Transaction Patterns
Recognition of common patterns:
- SimpleSale, SaleWithVat
- SimplePurchase, PurchaseWithVat
- Payment, Receipt
- Payroll, Depreciation
- Intercompany, CostAllocation
Use Cases
- Financial close: Automate journal analysis
- Audit: Trace value flows, detect anomalies
- Consolidation: Identify elimination entries
- Compliance: VAT reporting, intercompany analysis
Payments
Crate: rustkernel-payments
Kernels: 2
Feature: payments
Payment processing and flow analysis kernels for payment systems.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| PaymentProcessing | payments/processing | Batch, Ring | Process payment instructions |
| FlowAnalysis | payments/flow-analysis | Batch, Ring | Analyze payment flows |
Kernel Details
PaymentProcessing
Processes and validates payment instructions.
ID: payments/processing
Modes: Batch, Ring
Input
pub struct PaymentProcessingInput {
pub payments: Vec<Payment>,
pub validation_rules: Vec<ValidationRule>,
pub processing_window: ProcessingWindow,
}
pub struct Payment {
pub id: String,
pub sender: PartyInfo,
pub receiver: PartyInfo,
pub amount: f64,
pub currency: String,
pub payment_type: PaymentType,
pub value_date: u64,
pub reference: String,
pub remittance_info: Option<String>,
}
pub enum PaymentType {
Wire,
ACH,
SEPA,
SWIFT,
RTP, // Real-Time Payments
FedNow,
}
Output
pub struct PaymentProcessingOutput {
pub processed: Vec<ProcessedPayment>,
pub rejected: Vec<RejectedPayment>,
pub pending: Vec<String>,
pub statistics: ProcessingStatistics,
}
pub struct ProcessedPayment {
pub id: String,
pub status: PaymentStatus,
pub processing_time_us: u64,
pub fees: f64,
}
pub struct RejectedPayment {
pub id: String,
pub reason: RejectionReason,
pub failed_rules: Vec<String>,
}
Example
use rustkernel::payments::processing::{PaymentProcessing, PaymentProcessingInput};
let kernel = PaymentProcessing::new();
let result = kernel.execute(PaymentProcessingInput {
payments: incoming_payments,
validation_rules: default_rules(),
processing_window: ProcessingWindow::Same_Day,
}).await?;
println!("Processed: {}", result.processed.len());
println!("Rejected: {}", result.rejected.len());
for rejection in result.rejected {
println!("Rejected {}: {:?}", rejection.id, rejection.reason);
}
FlowAnalysis
Analyzes payment flows for patterns, anomalies, and liquidity insights.
ID: payments/flow-analysis
Modes: Batch, Ring
Input
pub struct FlowAnalysisInput {
pub payments: Vec<Payment>,
pub analysis_type: FlowAnalysisType,
pub time_window: TimeWindow,
}
pub enum FlowAnalysisType {
VolumeAnalysis,
NetworkAnalysis,
AnomalyDetection,
LiquidityForecasting,
}
Output
pub struct FlowAnalysisOutput {
/// Flow statistics by counterparty
pub counterparty_flows: Vec<CounterpartyFlow>,
/// Detected anomalies
pub anomalies: Vec<FlowAnomaly>,
/// Network metrics
pub network_metrics: NetworkMetrics,
/// Liquidity forecast
pub liquidity_forecast: Option<LiquidityForecast>,
}
pub struct CounterpartyFlow {
pub counterparty: String,
pub inflow_volume: f64,
pub outflow_volume: f64,
pub net_flow: f64,
pub transaction_count: u32,
}
Example
use rustkernel::payments::flow::{FlowAnalysis, FlowAnalysisInput};
let kernel = FlowAnalysis::new();
let result = kernel.execute(FlowAnalysisInput {
payments: daily_payments,
analysis_type: FlowAnalysisType::NetworkAnalysis,
time_window: TimeWindow::Days(30),
}).await?;
// Top counterparties by volume
for flow in result.counterparty_flows.iter().take(10) {
println!("{}: in=${:.0}, out=${:.0}, net=${:.0}",
flow.counterparty,
flow.inflow_volume,
flow.outflow_volume,
flow.net_flow
);
}
Ring Mode for Real-Time Processing
use rustkernel::payments::processing::PaymentProcessingRing;
let ring = PaymentProcessingRing::new();
// Process payments as they arrive
for payment in payment_stream {
let result = ring.process(payment).await?;
match result.status {
PaymentStatus::Processed => send_confirmation(result),
PaymentStatus::Rejected(reason) => notify_sender(reason),
PaymentStatus::Pending => queue_for_review(result),
}
}
Use Cases
- Payment hubs: Central payment processing
- Real-time payments: Instant payment validation
- Correspondent banking: SWIFT message processing
- Treasury: Cash position forecasting
Audit
Crate: rustkernel-audit
Kernels: 2
Feature: audit
Financial audit and forensic analysis kernels.
Kernel Overview
| Kernel | ID | Modes | Description |
|---|---|---|---|
| FeatureExtraction | audit/feature-extraction | Batch, Ring | Extract audit-relevant features |
| HypergraphConstruction | audit/hypergraph-construction | Batch | Build multi-entity relationship graphs |
Kernel Details
FeatureExtraction
Extracts features from financial data for audit analysis and anomaly detection.
ID: audit/feature-extraction
Modes: Batch, Ring
Input
pub struct FeatureExtractionInput {
pub transactions: Vec<AuditTransaction>,
pub feature_config: FeatureConfig,
pub entity_context: Option<EntityContext>,
}
pub struct AuditTransaction {
pub id: String,
pub timestamp: u64,
pub amount: f64,
pub account_from: String,
pub account_to: String,
pub entity_id: String,
pub user_id: String,
pub transaction_type: String,
pub attributes: HashMap<String, String>,
}
pub struct FeatureConfig {
pub temporal_features: bool,
pub behavioral_features: bool,
pub network_features: bool,
pub benford_analysis: bool,
}
Output
pub struct FeatureExtractionOutput {
/// Extracted features per transaction
pub features: Vec<TransactionFeatures>,
/// Aggregate features
pub aggregate_features: AggregateFeatures,
/// Benford's Law analysis
pub benford_results: Option<BenfordResults>,
}
pub struct TransactionFeatures {
pub transaction_id: String,
/// Temporal features
pub hour_of_day: u8,
pub day_of_week: u8,
pub is_weekend: bool,
pub is_month_end: bool,
/// Amount features
pub amount_log: f64,
pub round_amount_flag: bool,
pub just_below_threshold: bool,
/// Behavioral features
pub velocity_1h: u32,
pub velocity_24h: u32,
pub deviation_from_mean: f64,
}
pub struct BenfordResults {
pub first_digit_distribution: Vec<f64>,
pub expected_distribution: Vec<f64>,
pub chi_square_statistic: f64,
pub p_value: f64,
pub conformity_score: f64,
}
Example
use rustkernel::audit::feature_extraction::{FeatureExtraction, FeatureExtractionInput};
let kernel = FeatureExtraction::new();
let result = kernel.execute(FeatureExtractionInput {
transactions: journal_entries,
feature_config: FeatureConfig {
temporal_features: true,
behavioral_features: true,
network_features: true,
benford_analysis: true,
},
entity_context: None,
}).await?;
// Check Benford's Law conformity
if let Some(benford) = result.benford_results {
if benford.conformity_score < 0.8 {
println!("Warning: Data may not conform to Benford's Law");
println!("Conformity: {:.1}%", benford.conformity_score * 100.0);
}
}
// Find suspicious transactions
for feat in result.features {
if feat.just_below_threshold && feat.is_weekend {
println!("Suspicious: {} - just below threshold on weekend",
feat.transaction_id
);
}
}
HypergraphConstruction
Builds hypergraphs representing complex multi-entity relationships.
ID: audit/hypergraph-construction
Modes: Batch
A hypergraph allows edges to connect more than two nodes, capturing complex relationships like:
- A transaction involving multiple parties
- A document signed by multiple entities
- An event affecting multiple accounts
Input
pub struct HypergraphInput {
pub events: Vec<AuditEvent>,
pub entity_types: Vec<EntityType>,
pub relationship_rules: Vec<RelationshipRule>,
}
pub struct AuditEvent {
pub id: String,
pub event_type: String,
pub entities: Vec<EntityReference>,
pub timestamp: u64,
pub attributes: HashMap<String, String>,
}
pub struct EntityReference {
pub entity_id: String,
pub entity_type: EntityType,
pub role: String, // e.g., "sender", "approver", "beneficiary"
}
Output
pub struct HypergraphOutput {
pub nodes: Vec<HypergraphNode>,
pub hyperedges: Vec<Hyperedge>,
pub metrics: HypergraphMetrics,
}
pub struct Hyperedge {
pub id: String,
pub nodes: Vec<String>,
pub edge_type: String,
pub weight: f64,
pub attributes: HashMap<String, String>,
}
pub struct HypergraphMetrics {
pub node_count: usize,
pub hyperedge_count: usize,
pub avg_hyperedge_size: f64,
pub max_hyperedge_size: usize,
pub connected_components: usize,
}
Example
use rustkernel::audit::hypergraph::{HypergraphConstruction, HypergraphInput};
let kernel = HypergraphConstruction::new();
let result = kernel.execute(HypergraphInput {
events: audit_events,
entity_types: vec![
EntityType::User,
EntityType::Account,
EntityType::Document,
],
relationship_rules: default_rules(),
}).await?;
// Find densely connected entity clusters
for component in result.connected_components() {
if component.density > 0.8 {
println!("Highly connected cluster: {:?}", component.entities);
}
}
Use Cases
Internal Audit
- Journal entry testing
- Segregation of duties analysis
- Unusual transaction detection
External Audit
- Substantive testing sample selection
- Benford’s Law analysis
- Related party transaction identification
Fraud Investigation
- Network analysis of parties
- Pattern detection across time
- Relationship mapping
Regulatory Compliance
- SOX testing automation
- Audit trail analysis
- Control effectiveness testing
Articles
Technical deep-dives into RustKernels algorithms, implementation details, and use cases.
Featured Articles
Accounting Network Generation: Transforming Journal Entries to Directed Graphs
A comprehensive look at the GPU-accelerated transformation of double-entry bookkeeping journal entries into directed accounting networks. Covers the five solving methods, confidence calculation, VAT detection, and pattern recognition.
Topics covered:
- The accounting network problem
- Five solving methods (A-E) with confidence hierarchy
- Fixed-point arithmetic for financial precision
- Account classification and VAT detection
- Transaction pattern recognition
- Performance characteristics
Upcoming Articles
- Graph Analytics at Scale: PageRank and community detection on billion-edge graphs
- Real-Time Risk: Streaming VaR calculation with Ring kernels
- Process Mining with DFG: Automatically discovering business processes
- Compliance Patterns: Detecting AML patterns in transaction networks
Contributing Articles
Want to contribute an article? Articles should:
- Focus on a specific kernel or algorithm
- Explain the underlying theory
- Show practical implementation details
- Include code examples
- Discuss performance characteristics
Submit proposals via GitHub issues.
Accounting Network Generation: Transforming Journal Entries to Directed Graphs
Published: January 2026 Domain: Accounting Kernels: NetworkGeneration, NetworkGenerationRing
Abstract
This article describes the GPU-accelerated transformation of double-entry bookkeeping journal entries into directed accounting networks. We implement five solving methods with decreasing confidence levels, enabling sophisticated analysis of value flows between accounts. The implementation includes automatic account classification, VAT/tax detection, transaction pattern recognition, and confidence boosting based on domain knowledge.
Introduction
Traditional accounting views journal entries as balanced debit/credit pairs following the fundamental equation:
Assets = Liabilities + Equity
Every transaction creates at least one debit and one credit, always balancing. However, understanding the flow of value between accounts requires transforming these entries into a directed graph representation where:
- Nodes represent accounts
- Edges represent flows (directed from debit accounts to credit accounts)
- Edge weights represent amounts transferred
This transformation enables powerful analytics:
- Flow tracing: Follow value through the organization
- Anomaly detection: Identify unusual patterns
- Intercompany analysis: Detect circular flows requiring elimination
- Audit trails: Trace specific amounts to source transactions
The challenge lies in determining which debit amount flows to which credit account when an entry has multiple lines.
The Solving Problem
Consider a simple journal entry:
Dr. Cash $1,000
Cr. Revenue $1,000
The flow is trivial: Cash receives $1,000 from Revenue.
But what about:
Dr. Cash $1,000
Dr. Receivables $500
Cr. Revenue $1,200
Cr. Deferred Revenue $300
Which debit flows to which credit? There are multiple valid interpretations. Our algorithm provides a principled approach to solve this ambiguity while quantifying confidence.
The Five Solving Methods
We implement five methods in order of decreasing confidence. The algorithm tries each method in sequence until one succeeds.
Method A: Trivial 1-to-1 (Confidence: 1.0)
When applied: Exactly 1 debit line and 1 credit line.
This is deterministic. The single debit flows entirely to the single credit.
Entry:
Dr. Cash $1,000
Cr. Revenue $1,000
Result:
Flow: Cash -> Revenue ($1,000) [confidence: 1.0]
Approximately 60% of entries in typical general ledgers are 2-line entries solved by Method A.
Method B: n-to-n Bijective Matching (Confidence: 0.95)
When applied: Equal count of debits and credits (n debits, n credits), where n <= 10.
Uses a two-phase greedy matching algorithm:
- Phase 1: Match exact amounts (within tolerance)
- Phase 2: Match remaining by order
Entry:
Dr. Cash $500
Dr. Receivables $300
Cr. Sales $500
Cr. Service $300
Phase 1 matches:
Cash ($500) -> Sales ($500) [exact match]
Receivables ($300) -> Service ($300) [exact match]
Result: Two flows with confidence 0.95
Method C: n-to-m Partition Matching (Confidence: 0.85)
When applied: Unequal counts of debits and credits, total lines <= 20.
Uses subset-sum matching to find which credits combine to match each debit amount.
For small n (<=12): Exhaustive search using bit manipulation (2^n combinations) For larger n: Greedy approximation (sorted descending)
Entry:
Dr. Cash $800
Cr. Revenue $500
Cr. Service $300
Algorithm finds: {Revenue, Service} sums to $800
Result:
Flow: Cash -> Revenue ($500) [confidence: 0.85]
Flow: Cash -> Service ($300) [confidence: 0.85]
If no exact partition exists, falls back to proportional allocation with reduced confidence (0.765).
Method D: Aggregation (Confidence: 0.70)
When applied: Many lines (>20 total), aggregation enabled.
Aggregates to account level and allocates proportionally:
- Sum all debits by account code
- Sum all credits by account code
- For each (debit_account, credit_account) pair:
allocation = debit_amount * (credit_amount / total_credits)
Use case: Large allocation entries with many cost centers.
Method E: Entity Decomposition (Confidence: 0.50)
When applied: Multi-entity entries, decomposition enabled.
Decomposes by entity_id and attempts within-entity matching first:
- Group debits by entity
- Group credits by entity
- Try matching within each entity
- Cross-entity flows get additional 0.8x confidence multiplier
Use case: Intercompany transactions, consolidation entries.
Unsolvable Entries
If all methods fail or entry is unbalanced:
- Route all flows through suspense account
- Confidence: 0.0
- Provides audit trail for investigation
Fixed-Point Arithmetic
Financial calculations require exact precision. Floating-point arithmetic introduces rounding errors that compound over millions of transactions. We use 128-bit fixed-point arithmetic with 18 decimal places:
pub struct FixedPoint128 {
pub value: i128, // Scaled by 10^18
}
const SCALE: i128 = 1_000_000_000_000_000_000; // 10^18
impl FixedPoint128 {
pub fn from_f64(v: f64) -> Self {
Self {
value: (v * SCALE as f64) as i128
}
}
pub fn to_f64(&self) -> f64 {
self.value as f64 / SCALE as f64
}
}
This provides:
- Range: Up to ~170 trillion (sufficient for any practical amount)
- Precision: Exactly 18 decimal places
- Determinism: Same result across platforms
All amount comparisons use tolerance-aware equality:
impl FixedPoint128 {
pub fn approx_eq(&self, other: &Self, tolerance: i128) -> bool {
(self.value - other.value).abs() <= tolerance
}
}
Enhanced Features
Account Classification
Accounts are automatically classified into standard categories:
pub enum AccountClass {
Asset, // 1xxx accounts
Liability, // 2xxx accounts
Equity, // 3xxx accounts
Revenue, // 4xxx accounts
COGS, // 5xxx accounts
Expense, // 6xxx-7xxx accounts
OtherIncomeExpense, // 8xxx accounts
Tax, // VAT, GST, etc.
Intercompany, // Related party accounts
Suspense, // Clearing/suspense
Unknown, // Unclassified
}
Classification uses two strategies:
- Numeric prefix:
1xxx= Asset,2xxx= Liability, etc. - Keyword matching: “CASH”, “RECEIVABLE” -> Asset; “PAYABLE” -> Liability
impl AccountClass {
pub fn from_account_code(code: &str) -> Self {
// Try numeric classification first
if let Some(first_digit) = code.chars().find(|c| c.is_ascii_digit()) {
match first_digit {
'1' => return AccountClass::Asset,
'2' => return AccountClass::Liability,
'3' => return AccountClass::Equity,
'4' => return AccountClass::Revenue,
'5' => return AccountClass::COGS,
'6' | '7' => return AccountClass::Expense,
'8' => return AccountClass::OtherIncomeExpense,
_ => {}
}
}
// Fall back to keyword matching
let upper = code.to_uppercase();
if upper.contains("VAT") || upper.contains("TAX") {
AccountClass::Tax
} else if upper.contains("CASH") || upper.contains("BANK") {
AccountClass::Asset
}
// ... additional patterns
}
}
VAT/Tax Detection
The system automatically detects VAT patterns by analyzing amount relationships:
pub struct VatDetector {
known_rates: Vec<VatRate>,
tolerance: f64,
}
pub struct VatRate {
pub name: String,
pub rate: f64, // e.g., 0.20 for 20%
pub jurisdiction: String,
}
Built-in rates include:
- EU: 19% (Germany), 20% (UK, France), 21% (Netherlands), 23% (Ireland), 25% (Sweden)
- Reduced: 5%, 7%, 10%
- GST/HST: 5% (Canada GST), 13%/15% (HST)
Detection algorithm:
pub fn detect_vat_split(&self, amounts: &[i128]) -> Option<VatPattern> {
// Sort amounts descending
let mut sorted = amounts.to_vec();
sorted.sort_by(|a, b| b.cmp(a));
// Try each pair: largest could be gross, second could be net
for (gross, net) in pairs(&sorted) {
let tax = gross - net;
let implied_rate = tax as f64 / net as f64;
// Check against known rates
for rate in &self.known_rates {
if (implied_rate - rate.rate).abs() < self.tolerance {
return Some(VatPattern {
gross_amount: gross,
net_amount: net,
tax_amount: tax,
rate: rate.clone(),
// ... additional fields
});
}
}
}
None
}
Transaction Pattern Recognition
14 common transaction patterns are automatically detected:
pub enum TransactionPattern {
SimpleSale, // Dr. Asset, Cr. Revenue
SaleWithVat, // Dr. Asset, Cr. Revenue + Tax
SimplePurchase, // Dr. Expense/Asset, Cr. Asset/Liability
PurchaseWithVat, // Dr. Expense + Tax, Cr. Liability
Payment, // Dr. Liability, Cr. Asset
Receipt, // Dr. Asset, Cr. Asset
Payroll, // Dr. Expense, Cr. Multiple Liabilities
Depreciation, // Dr. Expense, Cr. Contra-Asset
Accrual, // Dr. Expense, Cr. Liability
AccrualReversal, // Reverse of accrual
Transfer, // Dr. Asset, Cr. Asset (multiple)
Intercompany, // Cross-entity debits/credits
CostAllocation, // Multiple cost centers
Adjustment, // Miscellaneous adjustments
Unknown,
}
Pattern detection examines:
- Account classes on debit side
- Account classes on credit side
- Presence of tax accounts
- Entity relationships
Confidence Boosting
Recognized patterns boost confidence scores:
| Pattern | Boost |
|---|---|
| SaleWithVat | +0.15 |
| PurchaseWithVat | +0.15 |
| SimpleSale | +0.10 |
| SimplePurchase | +0.10 |
| Payroll | +0.12 |
| Depreciation | +0.15 |
| Intercompany | +0.05 |
Final confidence is capped at 1.0:
let base_confidence = method.confidence();
let pattern_boost = pattern.confidence_boost();
let final_confidence = (base_confidence + pattern_boost).min(1.0);
Data Structures
AccountingFlow
The primary output structure:
pub struct AccountingFlow {
pub flow_id: String,
pub entry_id: u64,
pub from_account: String,
pub to_account: String,
pub amount: FixedPoint128,
pub timestamp: u64,
pub method: SolvingMethod,
pub confidence: f64,
pub from_entity: String,
pub to_entity: String,
pub currency: String,
pub source_lines: Vec<u32>,
// Enhanced fields
pub from_account_class: Option<AccountClass>,
pub to_account_class: Option<AccountClass>,
pub pattern: Option<TransactionPattern>,
pub is_tax_flow: bool,
pub vat_rate: Option<f64>,
pub is_intercompany: bool,
pub confidence_factors: Vec<String>,
}
AccountingNetwork
The complete network representation:
pub struct AccountingNetwork {
pub flows: Vec<AccountingFlow>,
pub accounts: HashSet<String>,
pub account_index: HashMap<String, usize>,
pub adjacency: HashMap<String, Vec<(String, usize)>>,
pub stats: NetworkGenerationStats,
}
impl AccountingNetwork {
pub fn outgoing_flows(&self, account: &str) -> Vec<&AccountingFlow> {
// Returns all flows from this account
}
pub fn incoming_flows(&self, account: &str) -> Vec<&AccountingFlow> {
// Returns all flows to this account
}
pub fn total_volume(&self) -> f64 {
self.flows.iter().map(|f| f.amount.to_f64()).sum()
}
pub fn weighted_confidence(&self) -> f64 {
let total_amount: f64 = self.flows.iter()
.map(|f| f.amount.to_f64())
.sum();
if total_amount == 0.0 {
return self.flows.iter()
.map(|f| f.confidence)
.sum::<f64>() / self.flows.len() as f64;
}
self.flows.iter()
.map(|f| f.confidence * f.amount.to_f64())
.sum::<f64>() / total_amount
}
}
Usage Example
use rustkernel::accounting::network_generation::{
NetworkGeneration,
NetworkGenerationInput,
NetworkGenerationConfig,
};
// Configure the kernel
let config = NetworkGenerationConfig {
amount_tolerance: 0.01,
max_lines_method_b: 10,
max_lines_method_c: 20,
enable_aggregation: true,
enable_decomposition: true,
suspense_account: "SUSPENSE".to_string(),
strict_balance: false,
// Enhanced features
enable_pattern_matching: true,
enable_vat_detection: true,
apply_confidence_boost: true,
annotate_account_classes: true,
custom_vat_rates: vec![],
};
let kernel = NetworkGeneration::with_config(config);
// Process journal entries
let result = kernel.execute(NetworkGenerationInput {
entries: journal_entries,
config: None, // Use kernel's config
}).await?;
// Analyze results
println!("Generated {} flows", result.flows.len());
println!("Total volume: ${:.2}", result.stats.total_volume);
println!("Weighted confidence: {:.2}%", result.stats.weighted_confidence * 100.0);
// Method distribution
println!("\nMethod distribution:");
println!(" Method A (1:1): {}", result.stats.method_a_count);
println!(" Method B (n:n): {}", result.stats.method_b_count);
println!(" Method C (n:m): {}", result.stats.method_c_count);
println!(" Method D (agg): {}", result.stats.method_d_count);
println!(" Method E (dec): {}", result.stats.method_e_count);
// Pattern analysis
println!("\nTransaction patterns:");
println!(" Sales: {}", result.stats.sales_pattern_count);
println!(" Purchases: {}", result.stats.purchase_pattern_count);
println!(" Payments: {}", result.stats.payment_pattern_count);
println!(" Payroll: {}", result.stats.payroll_pattern_count);
println!(" Intercompany: {}", result.stats.intercompany_count);
// VAT analysis
println!("\nVAT detection:");
println!(" VAT entries: {}", result.stats.vat_entries_count);
println!(" Total VAT: ${:.2}", result.stats.total_vat_amount);
Performance Characteristics
| Metric | Value |
|---|---|
| Throughput | ~500,000 entries/sec |
| Memory | ~200 bytes per flow |
| GPU acceleration | Method C subset search |
Typical Distribution
For a standard GL with 100,000 entries:
| Method | Percentage | Avg Confidence |
|---|---|---|
| A | 60% | 1.00 |
| B | 25% | 0.95 |
| C | 10% | 0.85 |
| D | 4% | 0.70 |
| E | 1% | 0.50 |
Overall weighted confidence: ~0.94
Ring Mode for Streaming
For real-time processing, use Ring mode:
use rustkernel::accounting::network_generation::NetworkGenerationRing;
let ring = NetworkGenerationRing::new();
// Add entries as they arrive
ring.add_entry(entry).await?;
// Query flows within time window
let flows = ring.query_flows(start_time, end_time).await?;
// Get current statistics
let stats = ring.get_statistics().await?;
Conclusion
The Accounting Network Generation kernel provides a robust, GPU-accelerated method for transforming traditional journal entries into graph structures. The five-method hierarchy balances precision with practicality, while enhanced features like VAT detection and pattern recognition add domain intelligence to the analysis.
This enables sophisticated applications:
- Audit analytics: Trace flows, detect anomalies
- Consolidation: Identify intercompany eliminations
- Compliance: Analyze tax flows, verify postings
- Forensics: Follow money through the organization
The implementation demonstrates how GPU acceleration can be applied to accounting workloads, achieving throughputs suitable for even the largest enterprise general ledgers.
References
- Hardware Accelerated Method for Accounting Network Generation (Internal paper)
- Double-Entry Bookkeeping and the Fundamental Accounting Equation
- Graph-Based Financial Analysis Techniques
- Fixed-Point Arithmetic for Financial Applications
Changelog
All notable changes to RustKernels are documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[Unreleased]
Added
- Comprehensive documentation site with mdBook
- Kernel catalogue with all 82 kernels documented
- Technical article: Accounting Network Generation
[0.1.0] - 2026-01-12
Added
Infrastructure
rustkernelfacade crate with domain re-exportsrustkernel-corewith core traits, registry, K2K messagingrustkernel-derivewith#[gpu_kernel]and#[derive(KernelMessage)]macrosrustkernel-clicommand-line interface
Graph Analytics (21 kernels)
- PageRank, DegreeCentrality, BetweennessCentrality
- ClosenessCentrality, EigenvectorCentrality, KatzCentrality
- ModularityScore, LouvainCommunity, LabelPropagation
- JaccardSimilarity, CosineSimilarity, AdamicAdarIndex, CommonNeighbors
- GraphDensity, AveragePathLength, ClusteringCoefficient
- ConnectedComponents, FullGraphMetrics
- TriangleCounting, MotifDetection, KCliqueDetection
Statistical ML (8 kernels)
- KMeans, DBSCAN, HierarchicalClustering
- IsolationForest, LocalOutlierFactor, EnsembleVoting
- LinearRegression, RidgeRegression
Compliance (9 kernels)
- CircularFlowRatio, ReciprocityFlowRatio, RapidMovement
- AMLPatternDetection, KYCScoring, EntityResolution
- SanctionsScreening, PEPScreening, TransactionMonitoring
Temporal Analysis (7 kernels)
- ARIMAForecast, ProphetDecomposition, ChangePointDetection
- TimeSeriesAnomalyDetection, SeasonalDecomposition
- TrendExtraction, VolatilityAnalysis
Risk Analytics (4 kernels)
- CreditRiskScoring, MonteCarloVaR
- PortfolioRiskAggregation, StressTesting
Banking (1 kernel)
- FraudPatternMatch
Behavioral Analytics (6 kernels)
- BehavioralProfiling, AnomalyProfiling, FraudSignatureDetection
- CausalGraphConstruction, ForensicQueryExecution, EventCorrelationKernel
Order Matching (1 kernel)
- OrderMatchingEngine
Process Intelligence (4 kernels)
- DFGConstruction, PartialOrderAnalysis
- ConformanceChecking, OCPMPatternMatching
Clearing (5 kernels)
- ClearingValidation, DVPMatching, NettingCalculation
- SettlementExecution, ZeroBalanceFrequency
Treasury (5 kernels)
- CashFlowForecasting, CollateralOptimization
- FXHedging, InterestRateRisk, LiquidityOptimization
Accounting (7 kernels)
- ChartOfAccountsMapping, JournalTransformation
- GLReconciliation, NetworkAnalysis, TemporalCorrelation
- NetworkGeneration with enhanced features:
- Account classification (11 classes)
- VAT/tax detection (EU, GST/HST rates)
- Transaction pattern recognition (14 patterns)
- Confidence boosting
- NetworkGenerationRing (streaming mode)
Payments (2 kernels)
- PaymentProcessing, FlowAnalysis
Audit (2 kernels)
- FeatureExtraction, HypergraphConstruction
Infrastructure Features
- Batch and Ring execution modes
- K2K (kernel-to-kernel) messaging patterns
- Fixed-point arithmetic for financial precision
- Enterprise licensing system
- Feature flags for selective compilation
Version History
| Version | Date | Highlights |
|---|---|---|
| 0.1.0 | 2026-01-12 | Initial release, 82 kernels |
Migration Guides
From DotCompute (C#)
RustKernels is a Rust port of DotCompute. Key differences:
- Async execution: All kernel execution is async
- Ownership: Rust ownership model affects API design
- Error handling: Uses
Result<T, E>instead of exceptions - Ring messages: Use rkyv serialization instead of protobuf
See migration guide (coming soon) for detailed instructions.
Contributing
Thank you for your interest in contributing to RustKernels!
Code of Conduct
This project follows the Rust Code of Conduct. Please be respectful and constructive in all interactions.
Getting Started
Development Setup
-
Clone the repository:
git clone https://github.com/mivertowski/RustKernels.git cd RustKernels -
Ensure RustCompute is available:
# Clone alongside RustKernels cd .. git clone https://github.com/mivertowski/RustCompute.git -
Build and test:
cd RustKernels cargo build --workspace cargo test --workspace
Development Commands
# Format code
cargo fmt --all
# Lint
cargo clippy --all-targets --all-features -- -D warnings
# Run specific domain tests
cargo test --package rustkernel-graph
# Build documentation
cargo doc --workspace --no-deps --open
# Check all features compile
cargo check --all-features
Contributing Code
Types of Contributions
- Bug fixes: Fix issues in existing kernels
- New kernels: Add kernels to existing domains
- Documentation: Improve docs, add examples
- Performance: Optimize existing implementations
- Tests: Increase test coverage
Pull Request Process
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes
- Run tests:
cargo test --workspace - Run lints:
cargo clippy --all-targets -- -D warnings - Format code:
cargo fmt --all - Commit with clear messages
- Open a Pull Request
Commit Messages
Follow conventional commit format:
type(scope): description
[optional body]
[optional footer]
Types: feat, fix, docs, style, refactor, test, chore
Examples:
feat(graph): add triangle counting kernel
fix(accounting): correct VAT detection for reduced rates
docs(readme): update kernel count
test(ml): add edge cases for kmeans
Adding a New Kernel
1. Define the Kernel Struct
use rustkernel_core::{GpuKernel, KernelMetadata, Domain, KernelMode};
#[derive(Debug, Clone)]
pub struct MyNewKernel {
metadata: KernelMetadata,
}
impl MyNewKernel {
pub fn new() -> Self {
Self {
metadata: KernelMetadata {
id: "domain/my-new-kernel".to_string(),
mode: KernelMode::Batch,
domain: Domain::MyDomain,
description: "Description of what this kernel does".to_string(),
expected_throughput: 100_000,
target_latency_us: 50.0,
requires_gpu_native: false,
version: 1,
},
}
}
}
impl GpuKernel for MyNewKernel {
fn metadata(&self) -> &KernelMetadata {
&self.metadata
}
}
2. Implement BatchKernel
use rustkernel_core::BatchKernel;
impl BatchKernel<MyInput, MyOutput> for MyNewKernel {
async fn execute(&self, input: MyInput) -> Result<MyOutput> {
// Implementation
}
}
3. Add Input/Output Types
In messages.rs:
#[derive(Debug, Clone)]
pub struct MyInput {
pub data: Vec<f64>,
pub config: MyConfig,
}
#[derive(Debug, Clone)]
pub struct MyOutput {
pub result: Vec<f64>,
pub statistics: Stats,
}
4. Add Tests
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_my_new_kernel_basic() {
let kernel = MyNewKernel::new();
let input = MyInput { /* ... */ };
let result = kernel.execute(input).await.unwrap();
assert!(/* expected conditions */);
}
#[test]
fn test_my_new_kernel_metadata() {
let kernel = MyNewKernel::new();
assert_eq!(kernel.metadata().id, "domain/my-new-kernel");
}
}
5. Register the Kernel
In the domain’s lib.rs:
pub fn register_all(registry: &mut KernelRegistry) -> Result<()> {
registry.register_batch(MyNewKernel::new())?;
// ... other registrations
Ok(())
}
6. Update Documentation
- Add kernel to domain’s documentation page
- Update kernel count if needed
- Add to changelog
Code Style
Rust Style
- Follow standard Rust idioms
- Use
rustfmtformatting - Address all
clippywarnings - Document public APIs with
///comments
Naming Conventions
- Kernels:
PascalCase(e.g.,PageRank,MonteCarloVaR) - Kernel IDs:
kebab-case(e.g.,graph/page-rank) - Functions/methods:
snake_case - Constants:
SCREAMING_SNAKE_CASE
Error Handling
- Use
Result<T, KernelError>for fallible operations - Provide meaningful error messages
- Don’t panic in library code
Testing Guidelines
Test Categories
- Unit tests: Test individual functions
- Integration tests: Test kernel execution
- Property tests: For numerical algorithms (when applicable)
Test Naming
#[test]
fn test_{function}_{scenario}_{expected_result}() {
// e.g., test_pagerank_empty_graph_returns_empty()
}
Coverage Goals
- New code should have >80% test coverage
- Critical paths should have 100% coverage
Questions?
- Open a GitHub issue for bugs or feature requests
- Use discussions for questions
- Email maintainers for sensitive issues
Thank you for contributing!