xiaohongshu
xiaohongshu
distribution-search
Guidance for finding probability distributions that satisfy specific statistical constraints such as KL divergence targets, entropy requirements, or moment conditions. This skill should be used when tasks involve constructing discrete or continuous probability distributions with specified diverge...
Full skill instructions
This skill provides systematic approaches for finding probability distributions that meet specific statistical constraints. Common tasks include constructing distributions with target KL divergence values (forward or backward), specified entropy, moment constraints, or combinations thereof. The approach emphasizes mathematical analysis before implementation, efficient parameterization, modular code structure, and rigorous verification.
Before writing any code, thoroughly analyze the mathematical constraints:
1. Constraint Feasibility
2. Degrees of Freedom Analysis
3. Analytical Derivations
Start Simple, Plan for Complexity
Two-group distributions: Divide elements into high-probability and low-probability groups
Multi-group distributions: If two groups are insufficient, add more groups
Continuous parameterizations: For smooth optimization landscapes
Computational Efficiency for Large Vocabularies
For large vocabulary sizes (e.g., V = 150,000):
Forward KL = k * p_high * log(p_high * V) + (V - k) * p_low * log(p_low * V)
Choose Appropriate Methods
Implementation Pattern
def objective(params, target_forward_kl, target_backward_kl, vocab_size):
# Extract parameters
k, log_ratio = params
k = int(round(k))
# Compute probabilities
p_high, p_low = compute_probs(k, log_ratio, vocab_size)
# Validate probabilities
if p_high <= 0 or p_low <= 0 or p_high > 1 or p_low > 1:
return [1e10, 1e10] # Infeasible
# Compute KL divergences using closed-form formulas
forward_kl = compute_forward_kl(k, p_high, p_low, vocab_size)
backward_kl = compute_backward_kl(k, p_high, p_low, vocab_size)
return [forward_kl - target_forward_kl, backward_kl - target_backward_kl]
Grid Search for Discrete Parameters
best_solution = None
best_error = float('inf')
for k in range(1, vocab_size):
# Optimize continuous parameters for this k
result = optimize_continuous_params(k, targets, vocab_size)
if result.error < best_error:
best_error = result.error
best_solution = result
Modular Structure to Prevent Inconsistencies
Create separate, reusable functions for core computations:
# Core computation functions - define ONCE, use everywhere
def forward_kl(p, q, mask=None):
"""Compute D_KL(P || Q) = sum_i p_i * log(p_i / q_i)"""
if mask is None:
mask = p > 1e-30
return np.sum(p[mask] * np.log(p[mask] / q[mask]))
def backward_kl(p, q, mask=None):
"""Compute D_KL(Q || P) = sum_i q_i * log(q_i / p_i)"""
if mask is None:
mask = p > 1e-30
return np.sum(q[mask] * np.log(q[mask] / p[mask]))
def entropy(p, mask=None):
"""Compute H(P) = -sum_i p_i * log(p_i)"""
if mask is None:
mask = p > 1e-30
return -np.sum(p[mask] * np.log(p[mask]))
Import in All Scripts
# In optimization script
from kl_utils import forward_kl, backward_kl
# In verification script - use SAME functions
from kl_utils import forward_kl, backward_kl
Verification Checklist
For the final solution, verify:
Distribution Properties:
[ ] All probabilities are positive
[ ] All probabilities are <= 1
[ ] Sum of probabilities equals 1.0 (within floating-point tolerance)
[ ] No NaN or Inf values
Constraint Satisfaction:
[ ] Forward KL divergence within tolerance
[ ] Backward KL divergence within tolerance
[ ] Other constraints (entropy, moments) within tolerance
Numerical Precision:
[ ] Tolerance requirements are met (e.g., |error| < 1e-6)
[ ] Floating-point sum is acceptably close to 1.0
Verification Script Structure
def verify_distribution(p, q, target_forward, target_backward, tol=1e-6):
print(f"Sum of probabilities: {np.sum(p)}")
print(f"Min probability: {np.min(p)}")
print(f"Max probability: {np.max(p)}")
print(f"Any NaN: {np.any(np.isnan(p))}")
print(f"Any Inf: {np.any(np.isinf(p))}")
fwd = forward_kl(p, q)
bwd = backward_kl(p, q)
print(f"\nForward KL: {fwd:.10f} (target: {target_forward}, error: {abs(fwd - target_forward):.2e})")
print(f"Backward KL: {bwd:.10f} (target: {target_backward}, error: {abs(bwd - target_backward):.2e})")
fwd_ok = abs(fwd - target_forward) < tol
bwd_ok = abs(bwd - target_backward) < tol
print(f"\nForward KL within tolerance: {'PASS' if fwd_ok else 'FAIL'}")
print(f"Backward KL within tolerance: {'PASS' if bwd_ok else 'FAIL'}")
return fwd_ok and bwd_ok
Problem: Creating arrays of size V = 150,000 elements causes memory issues and timeouts Solution: Use closed-form formulas for group-based distributions; only create full arrays for final verification
Problem: Different scripts implement KL divergence formulas differently, leading to discrepancies Solution: Define core computation functions once and import them everywhere
Problem: Masking logic differs between forward and backward KL, or mask sum is incorrectly used Solution: Use consistent masking (p > 1e-30) and sum over masked elements, not multiply by mask count
Problem: Simple parameterizations cannot satisfy all constraints simultaneously Solution: Analyze degrees of freedom before implementation; plan for more flexible parameterizations
Problem: File writes are truncated, leaving incomplete code Solution: Verify file content after every write by reading it back or attempting to import/execute
Problem: Attempting optimization without verifying a solution exists Solution: Mathematically analyze constraints to establish feasibility before coding
Problem: Optimization finds a local minimum that doesn't satisfy constraints Solution: Try multiple initializations; use grid search over discrete parameters; verify final solution
Problem: Probability sum not exactly 1.0 due to floating-point arithmetic Solution: Use appropriate tolerances; normalize probabilities after construction; verify precision is acceptable for the task
Forward KL (information projection):
D_KL(P || Q) = sum_i P(i) * log(P(i) / Q(i))
Backward KL (moment projection):
D_KL(Q || P) = sum_i Q(i) * log(Q(i) / P(i))
For P with k elements at probability p_high and (V-k) elements at probability p_low, with Q uniform:
D_KL(P || Q) = k * p_high * log(p_high * V) + (V - k) * p_low * log(p_low * V)
D_KL(Q || P) = (1/V) * [k * log(1 / (V * p_high)) + (V - k) * log(1 / (V * p_low))]
= (1/V) * [-k * log(V * p_high) - (V - k) * log(V * p_low)]
When initial approaches fail:
Avoid completely rewriting from scratch each time; instead, modularly modify specific components.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience