Skip to content

Simulations of the Ethical Riemann Hypothesis (ERH), which states that in a "healthy" moral judgment system, the error in predicting critical misjudgments grows at most like √x, where x is the complexity of the decision.

Notifications You must be signed in to change notification settings

dennislee928/Ethic-Latex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

403 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ethical Riemann Hypothesis (ERH)

License: MIT Python Status

A mathematical framework for analyzing moral judgment errors through an analogy with the Riemann Hypothesis in number theory.


📖 Project Overview

This project introduces the Ethical Riemann Hypothesis (ERH). It posits that in a "healthy" moral judgment system, the cumulative error in predicting critical misjudgments grows at most like $\sqrt{x}$, where $x$ is the complexity of the decision.

Key Concepts

  • Ethical Primes ($P$): Critical misjudgments representing fundamental errors.
  • $\Pi(x)$: The count of ethical primes up to complexity $x$.
  • $E(x) = \Pi(x) - B(x)$: The error term comparing the actual count vs. the baseline expectation.
  • The ERH Condition: $|E(x)| \leq C \cdot x^{1/2 + \epsilon}$ for healthy judgment systems.

Analogy with Number Theory

Number Theory Concept Ethical Judgment Analogy
Prime Numbers Ethical Primes (Critical Misjudgments)
$\pi(x)$ $\Pi(x)$ (Count of ethical primes)
Prime Number Theorem Baseline Expectation$B(x)$
Riemann Hypothesis Ethical Riemann Hypothesis (Bounds on error growth)

🖼️ Demo

Below are visualizations generated by the framework, showcasing the distribution of ethical primes and the behavior of the error term.

Zeta Function Analysis Error Distribution
Zeta Function Visualization Error Distribution Plot
Prime Counting Function$\Pi(x)$ Critical Line Analysis
Prime Counting Function Critical Line Analysis

📂 Project Structure

Architecture: erh_core is the Single Source of Truth for core logic. The backend (erh-security-app) installs the root package in editable mode (-e ../..) to use the latest core. simulation and erh re-export from erh_core when available. V(a) proxies: HuggingFaceEthicalOracle (toxicity→ethical score), GroundTruthProxy.load_from_csv() (empirical data), OracleDrivenJudge (CSV-first, Oracle fallback). Pipeline: run_simulation_batch --mode judge|abm, run_phase_transition_exp.py, run_full_pipeline.sh. Use FULL=1 ./scripts/run_full_pipeline.sh to include generate_all_figures. Verification: python scripts/run_verification_phase4.py [--full].

Ethic-Latex/
├── erh_core/                   # Single Source of Truth (canonical core)
│   ├── core/                   # Core modules (shared by simulation/ and erh/)
│   │   ├── action_space.py     # Action and world generation
│   │   ├── judgement_system.py # Judge implementations
│   │   └── ethical_primes.py   # Prime selection and analysis
│   └── analysis/               # Analysis tools (shared)
│       ├── erh_checks.py       # ERH bound checking
│       ├── statistics.py       # Statistical analysis
│       └── zeta_function.py    # Ethical zeta function
├── simulation/                 # Python simulation framework (research/experiments)
│   ├── models.py               # Pydantic models (Action, Judgment)
│   ├── core/                   # Re-exports from erh_core (backward compatibility)
│   ├── analysis/               # Re-exports from erh_core + simulation-specific
│   │   └── fairness_metrics.py # Simulation-specific fairness analysis
│   ├── quantum/                # Quantum oracle (optional qiskit; NumPy fallback)
│   │   ├── interface.py        # QuantumOracle interface
│   │   ├── simulator.py        # LocalQuantumJudge (local/NumPy)
│   │   └── cloud.py            # CloudQuantumJudge (IBM Quantum Runtime)
│   ├── adversarial.py          # AdversarialAgent for red-team testing
│   ├── visualization/          # Plotting utilities
│   │   └── plots.py            # All visualization functions
│   ├── notebooks/              # Jupyter notebooks for experiments
│   │   ├── 01_basic_simulation.ipynb
│   │   ├── 02_judge_comparison.ipynb
│   │   └── ... (other analysis notebooks)
│   ├── api/                    # FastAPI endpoints
│   ├── real_data/              # Real-world case studies (Adult, Exam Cheating, Sexual Abuse, COMPAS)
│   └── output/                 # Generated figures and data
├── erh/                        # Python SDK package (for distribution)
│   ├── core/                   # Re-exports from erh_core (backward compatibility)
│   ├── analysis/               # Re-exports from erh_core
│   ├── client.py               # SDK client interface
│   └── tools/                  # SDK tools and adapters
├── scripts/                    # Utility scripts
│   ├── fetch_real_data.sh      # Fetch Adult, COMPAS, UCI Student Performance
│   ├── convert_adult_to_csv.py # Adult → data/adult.csv
│   ├── process_student_to_exam_cheating.py  # UCI Student → data/exam_cheating_cases.csv
│   └── generate_synthetic_sexual_abuse.py   # Fallback → data/sexual_abuse_cases.csv
├── docs/                       # Documentation files
├── tests/                      # Test files
├── ethical_riemann_hypothesis.tex  # Main research paper (LaTeX)
└── requirements.txt            # Python dependencies

⚡ Installation

Prerequisites

  • Python: 3.10 or later
  • LaTeX Distribution: (Optional, for compiling the paper)

Python Setup

# Clone the repository
git clone <repository-url>
cd Ethic-Latex

# Recommended: use the install script (creates .venv and installs deps)
bash scripts/install_dependencies.sh
# Then: source .venv/bin/activate  (or .venv\Scripts\activate on Windows)

# Or install manually (use a virtual environment)
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

Optional: for quantum cloud backend, set IBM_QUANTUM_TOKEN in .env (see .env.example). The quantum module works without it (local/NumPy fallback).

LaTeX Setup

To compile the research paper, you need a LaTeX distribution installed.

macOS (Homebrew):

# Minimal (~100MB): BasicTeX
brew install --cask basictex

# Full (~4GB): MacTeX (includes all fonts/packages)
# brew install --cask mactex

After installing BasicTeX, open a new terminal or run eval "$(/usr/libexec/path_helper -s)" so pdflatex is on PATH.

Linux: sudo apt-get install texlive-full latexmk (Ubuntu/Debian)

Windows: Install MiKTeX.

Then compile:

# Using the provided script (checks for pdflatex, prefers latexmk)
bash scripts/compile_latex.sh

# Or manually (single main file)
pdflatex ethical_riemann_hypothesis.tex
bibtex ethical_riemann_hypothesis
pdflatex ethical_riemann_hypothesis.tex

🚀 Quick Start

Running a Basic Simulation

from simulation.core import generate_world, BiasedJudge, evaluate_judgement
from simulation.core import select_ethical_primes, compute_Pi_and_error
from simulation.visualization import plot_Pi_B_E

# 1. Generate moral action space
actions = generate_world(num_actions=1000, complexity_dist='zipf')

# 2. Create and apply a judgment system
judge = BiasedJudge(bias_strength=0.2, noise_scale=0.1)
evaluate_judgement(actions, judge, tau=0.3)

# 3. Extract ethical primes (critical errors)
primes = select_ethical_primes(actions, importance_quantile=0.9)

# 4. Compute and plot error distribution
Pi_x, B_x, E_x, x_vals = compute_Pi_and_error(primes, X_max=100)
plot_Pi_B_E(x_vals, Pi_x, B_x, E_x)

Running Jupyter Notebooks

bash scripts/start_jupyter.sh

Start with simulation/notebooks/01_basic_simulation.ipynb for an introduction.

Batch Simulation Modes

# Judge mode (default): ERH analysis with BiasedJudge, parallel instances
python scripts/run_simulation_batch.py --instances 4 --output-dir results

# ABM mode: Agent-Based Model with multiprocessing
python scripts/run_simulation_batch.py --mode abm --agents 50 --steps 100 --trials 4 --output simulation/output/batch_results.json
  • Judge mode: Uses generate_world, BiasedJudge, select_ethical_primes, etc. Outputs per-instance JSON in --output-dir.
  • ABM mode: Uses ABMSimulator; aggregates results to --output (default: simulation/output/batch_results.json).

Optional: Quantum Judge and Adversarial Testing

from simulation.quantum import LocalQuantumJudge
from simulation.adversarial import AdversarialAgent

# Quantum oracle (local or NumPy fallback if qiskit unavailable)
quantum_judge = LocalQuantumJudge()
# Use in place of BiasedJudge for quantum-backed judgments

# Red-team agent to stress-test ERH bound
agent = AdversarialAgent(n_actions=500)
agent.run(max_steps=100)

Real-World Data (Adult, Exam Cheating, Sexual Abuse, COMPAS)

To run real-data case studies (adult_income_case_study, exam_cheating_case_study, sexual_abuse_case_study, compas_case_study), fetch and prepare datasets:

# 1. Download all datasets (Adult, COMPAS, UCI Student Performance)
bash scripts/fetch_real_data.sh

# 2. Convert and process to case-study CSV schema
python scripts/convert_adult_to_csv.py
python scripts/process_student_to_exam_cheating.py   # UCI Student Performance → exam cheating proxy
python scripts/generate_synthetic_sexual_abuse.py    # Fallback when no public case-level source

Expected outputs:

  • data/adult.csv – Adult Income (from UCI adult.data/test)
  • data/exam_cheating_cases.csv – Exam cheating proxy (from UCI Student Performance)
  • data/sexual_abuse_cases.csv – Sexual abuse reporting (synthetic fallback)
  • data/compas-scores-two-years.csv – COMPAS (from ProPublica GitHub)

Then run:

python -m simulation.real_data.adult_income_case_study
python -m simulation.real_data.exam_cheating_case_study
python -m simulation.real_data.sexual_abuse_case_study
python -m simulation.real_data.compas_case_study   # or run_compas_alpha

CI workflow: The build_thesis_gated.yml pipeline fetches and processes these CSV files on every run before executing case studies.


☁️ Cloud Deployment

  • 🚀 Streamlit Cloud (Recommended - 5 minutes): Push to GitHub, then deploy via the Streamlit website.
  • 📓 Binder (For Notebooks - 2 minutes): Deploy your notebooks for live access.
  • 🐳 Docker (Any Platform):
docker build -t erh-app .
docker run -p 8501:8501 erh-app streamlit run simulation/app.py

See docs/CLOUD_DEPLOYMENT.md for detailed guides.


🔒 ERH-on-Security PoC Design Document

Subject: Analysis of Structural Misjudgment in GitLab DevSecOps Pipelines

This design document outlines a Proof of Concept (PoC) applying the ERH framework to quantify how systematic and fatal security misjudgments accumulate as project complexity increases within a DevSecOps pipeline.

1. Objectives & Scope

Category Description
Core Problem Quantify the rate at which "truly fatal security misjudgments" (structural misjudgments) accumulate as the complexity of project changes grows.
Hypothesis Can we use an ERH-style metric to quantify thisStructural Risk Growth?
Scope GitLab Merge Request security review pipeline (using SAST/DAST results and post-merge incident data as proxies for ground truth).

2. Mapping ERH to Security Context

ERH Concept Security Context Mapping
Action ($a$) Defined as asecurity decision event for a single Merge Request (MR).
Complexity ($c(a)$) A composite metric of MR size and scope:$c(a) = \text{norm}(\log(1 + \text{lines_changed}) + \lambda \cdot \text{files_changed} + \dots)$
True Value ($V(a)$) Ground Truth. $+1$: Safe merge (no post-merge incident/unresolved high issue). $-1$: Unsafe merge (resulted in incident or high-severity issue).
Judgment System ($J(a)$) Pipeline Judge ($J_{\text{pipe}}$): Automated CI/SAST results. Human Judge ($J_{\text{human}}$): Reviewer behavior/override. Combined Judge ($J_{\text{combo}}$).
Error ($\Delta(a)$) $\Delta(a) = J(a) - V(a)$.
Ethical Prime ($P$) ACritical Misjudgment ($M(a)=1$) on a High-Importance asset ($w(a)$ in top quantile).

3. Data Schema Design

actions (Core: MR Decisions)

Column Type Description
action_id PK (MR ID) Unique identifier for the decision event
lines_added int Complexity factor
files_changed int Complexity factor
services_touched string[] Subsystems affected (via path mapping)
merged_at timestamp Time of merge (nullable)

ground_truth

Column Type Description
action_id FK Link to MR
V float [-1, 1] True Value (Post-merge security impact)
has_post\_incident bool Flag for incident discovery within observation window
unresolved\_high bool True if merged with unmitigated high/critical issues

derived_metrics (Calculated on-the-fly or materialized)

Column Type Description
action_id FK Link to MR
c float NormalizedComplexity
delta float $\Delta(a)$ Error
is_prime bool Flag indicating anEthical Prime

4. ERH Analysis Flow and Metrics

  1. Preprocessing: Ingest GitLab API data (MRs, security reports) and calculate $c(a)$, $V(a)$, $w(a)$, $J(a)$, and prime marking for all actions.
  2. ERH-style Metrics: Compute the following for each judge:
Metric Formula Description
Mistake Rate (MR) $\frac{1}{N} \sum M(a)$ Overall misjudgment frequency.
Prime Count $\Pi(x)$ Count of primes where$c(a) \leq x$. Cumulative critical error count by complexity.
Error Growth $\alpha$ Fit$ E(x)
  1. Interpretation: Check if $\alpha$ satisfies the ERH-style boundary. $\alpha \approx 0.5$ implies Riemann-healthy system with controlled risk growth. $\alpha \geq 1$ implies systematic degradation.

📈 Key Results (Expected)

The following table is a placeholder to be filled with simulation results.

Judge Type Exponent$\alpha$ ERH Satisfied? Growth Rate Interpretation
Biased TBD TBD --
Noisy TBD TBD --
Conservative TBD TBD Low risk, potentially high friction ($\alpha &lt; 0.5$)
Radical TBD TBD High risk accumulation, systematic failure ($\alpha \geq 1.0$)

📚 Documentation and Future Work

  • Simulation Framework: See simulation/README.md
  • CI/Workflows: build_thesis_gated.yml runs simulation → quantum tests → thesis build; simulation.yml fetches real data and empirical sources (HuggingFace, AITA, GitHub PR); build_thesis.yml fetches and processes case-study CSV before running Adult/Exam Cheating/Sexual Abuse case studies.
  • Installation Guide: See docs/INSTALL.md (includes venv and optional qiskit/quantum setup)
  • Theory: See ethical_riemann_hypothesis.tex
  • Tests: pytest tests/ (includes test_quantum_entanglement.py, psychohistory integration)

Applications to AI Ethics

The ERH framework provides:

  1. Quantitative Criterion: AI systems should satisfy $|E(x)| = O(\sqrt{x})$.
  2. Bias Detection: Violations of ERH indicate systematic failures.
  3. Fairness Analysis: Ethical primes highlight critical errors on vulnerable groups.

Implemented Extensions

  • Real-world case studies: Adult Income, Exam Cheating (UCI Student Performance), Sexual Abuse (synthetic fallback), COMPAS; scripts/fetch_real_data.sh fetches public sources; scripts/process_student_to_exam_cheating.py maps UCI data to exam-cheating schema; scripts/calculate_alpha_comparison.py compares real vs simulated α.
  • Quantum judgment: Optional simulation/quantum/ (local simulator or IBM Quantum); NumPy fallback when qiskit is unavailable.
  • Health monitor: E(x) vs Riemann bound monitoring (see erh-security-app backend /analysis/health and frontend).
  • Adversarial agent: simulation/adversarial.py for red-team testing (maximizing ethical-prime discovery).

Future Work

  • Apply the framework to more real-world AI systems (e.g., content moderation).
  • Develop theoretical proofs for ERH boundary conditions.
  • Explore connections to causal inference.

© Citation and License

Citation

If you use this framework in your research, please cite:

@article{ethical_riemann_hypothesis,
  title={The Ethical Riemann Hypothesis: A Mathematical Framework for Analyzing Moral Judgment Errors},
  author={[Author Name]},
  journal={[To be completed]},
  year={2025}
}

License

This project is licensed under the MIT License.

Contributing

Contributions, suggestions, and discussions are welcome.

Contact: admin@dennisleehappy.org

About

Simulations of the Ethical Riemann Hypothesis (ERH), which states that in a "healthy" moral judgment system, the error in predicting critical misjudgments grows at most like √x, where x is the complexity of the decision.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published