crca 1.4.0__py3-none-any.whl → 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. CRCA.py +172 -7
  2. MODEL_CARD.md +53 -0
  3. PKG-INFO +8 -2
  4. RELEASE_NOTES.md +17 -0
  5. STABILITY.md +19 -0
  6. architecture/hybrid/consistency_engine.py +362 -0
  7. architecture/hybrid/conversation_manager.py +421 -0
  8. architecture/hybrid/explanation_generator.py +452 -0
  9. architecture/hybrid/few_shot_learner.py +533 -0
  10. architecture/hybrid/graph_compressor.py +286 -0
  11. architecture/hybrid/hybrid_agent.py +4398 -0
  12. architecture/hybrid/language_compiler.py +623 -0
  13. architecture/hybrid/main,py +0 -0
  14. architecture/hybrid/reasoning_tracker.py +322 -0
  15. architecture/hybrid/self_verifier.py +524 -0
  16. architecture/hybrid/task_decomposer.py +567 -0
  17. architecture/hybrid/text_corrector.py +341 -0
  18. benchmark_results/crca_core_benchmarks.json +178 -0
  19. branches/crca_sd/crca_sd_realtime.py +6 -2
  20. branches/general_agent/__init__.py +102 -0
  21. branches/general_agent/general_agent.py +1400 -0
  22. branches/general_agent/personality.py +169 -0
  23. branches/general_agent/utils/__init__.py +19 -0
  24. branches/general_agent/utils/prompt_builder.py +170 -0
  25. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/METADATA +8 -2
  26. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/RECORD +303 -20
  27. crca_core/__init__.py +35 -0
  28. crca_core/benchmarks/__init__.py +14 -0
  29. crca_core/benchmarks/synthetic_scm.py +103 -0
  30. crca_core/core/__init__.py +23 -0
  31. crca_core/core/api.py +120 -0
  32. crca_core/core/estimate.py +208 -0
  33. crca_core/core/godclass.py +72 -0
  34. crca_core/core/intervention_design.py +174 -0
  35. crca_core/core/lifecycle.py +48 -0
  36. crca_core/discovery/__init__.py +9 -0
  37. crca_core/discovery/tabular.py +193 -0
  38. crca_core/identify/__init__.py +171 -0
  39. crca_core/identify/backdoor.py +39 -0
  40. crca_core/identify/frontdoor.py +48 -0
  41. crca_core/identify/graph.py +106 -0
  42. crca_core/identify/id_algorithm.py +43 -0
  43. crca_core/identify/iv.py +48 -0
  44. crca_core/models/__init__.py +67 -0
  45. crca_core/models/provenance.py +56 -0
  46. crca_core/models/refusal.py +39 -0
  47. crca_core/models/result.py +83 -0
  48. crca_core/models/spec.py +151 -0
  49. crca_core/models/validation.py +68 -0
  50. crca_core/scm/__init__.py +9 -0
  51. crca_core/scm/linear_gaussian.py +198 -0
  52. crca_core/timeseries/__init__.py +6 -0
  53. crca_core/timeseries/pcmci.py +181 -0
  54. crca_llm/__init__.py +12 -0
  55. crca_llm/client.py +85 -0
  56. crca_llm/coauthor.py +118 -0
  57. crca_llm/orchestrator.py +289 -0
  58. crca_llm/types.py +21 -0
  59. crca_reasoning/__init__.py +16 -0
  60. crca_reasoning/critique.py +54 -0
  61. crca_reasoning/godclass.py +206 -0
  62. crca_reasoning/memory.py +24 -0
  63. crca_reasoning/rationale.py +10 -0
  64. crca_reasoning/react_controller.py +81 -0
  65. crca_reasoning/tool_router.py +97 -0
  66. crca_reasoning/types.py +40 -0
  67. crca_sd/__init__.py +15 -0
  68. crca_sd/crca_sd_core.py +2 -0
  69. crca_sd/crca_sd_governance.py +2 -0
  70. crca_sd/crca_sd_mpc.py +2 -0
  71. crca_sd/crca_sd_realtime.py +2 -0
  72. crca_sd/crca_sd_tui.py +2 -0
  73. cuda-keyring_1.1-1_all.deb +0 -0
  74. cuda-keyring_1.1-1_all.deb.1 +0 -0
  75. docs/IMAGE_ANNOTATION_USAGE.md +539 -0
  76. docs/INSTALL_DEEPSPEED.md +125 -0
  77. docs/api/branches/crca-cg.md +19 -0
  78. docs/api/branches/crca-q.md +27 -0
  79. docs/api/branches/crca-sd.md +37 -0
  80. docs/api/branches/general-agent.md +24 -0
  81. docs/api/branches/overview.md +19 -0
  82. docs/api/crca/agent-methods.md +62 -0
  83. docs/api/crca/operations.md +79 -0
  84. docs/api/crca/overview.md +32 -0
  85. docs/api/image-annotation/engine.md +52 -0
  86. docs/api/image-annotation/overview.md +17 -0
  87. docs/api/schemas/annotation.md +34 -0
  88. docs/api/schemas/core-schemas.md +82 -0
  89. docs/api/schemas/overview.md +32 -0
  90. docs/api/schemas/policy.md +30 -0
  91. docs/api/utils/conversation.md +22 -0
  92. docs/api/utils/graph-reasoner.md +32 -0
  93. docs/api/utils/overview.md +21 -0
  94. docs/api/utils/router.md +19 -0
  95. docs/api/utils/utilities.md +97 -0
  96. docs/architecture/causal-graphs.md +41 -0
  97. docs/architecture/data-flow.md +29 -0
  98. docs/architecture/design-principles.md +33 -0
  99. docs/architecture/hybrid-agent/components.md +38 -0
  100. docs/architecture/hybrid-agent/consistency.md +26 -0
  101. docs/architecture/hybrid-agent/overview.md +44 -0
  102. docs/architecture/hybrid-agent/reasoning.md +22 -0
  103. docs/architecture/llm-integration.md +26 -0
  104. docs/architecture/modular-structure.md +37 -0
  105. docs/architecture/overview.md +69 -0
  106. docs/architecture/policy-engine-arch.md +29 -0
  107. docs/branches/crca-cg/corposwarm.md +39 -0
  108. docs/branches/crca-cg/esg-scoring.md +30 -0
  109. docs/branches/crca-cg/multi-agent.md +35 -0
  110. docs/branches/crca-cg/overview.md +40 -0
  111. docs/branches/crca-q/alternative-data.md +55 -0
  112. docs/branches/crca-q/architecture.md +71 -0
  113. docs/branches/crca-q/backtesting.md +45 -0
  114. docs/branches/crca-q/causal-engine.md +33 -0
  115. docs/branches/crca-q/execution.md +39 -0
  116. docs/branches/crca-q/market-data.md +60 -0
  117. docs/branches/crca-q/overview.md +58 -0
  118. docs/branches/crca-q/philosophy.md +60 -0
  119. docs/branches/crca-q/portfolio-optimization.md +66 -0
  120. docs/branches/crca-q/risk-management.md +102 -0
  121. docs/branches/crca-q/setup.md +65 -0
  122. docs/branches/crca-q/signal-generation.md +61 -0
  123. docs/branches/crca-q/signal-validation.md +43 -0
  124. docs/branches/crca-sd/core.md +84 -0
  125. docs/branches/crca-sd/governance.md +53 -0
  126. docs/branches/crca-sd/mpc-solver.md +65 -0
  127. docs/branches/crca-sd/overview.md +59 -0
  128. docs/branches/crca-sd/realtime.md +28 -0
  129. docs/branches/crca-sd/tui.md +20 -0
  130. docs/branches/general-agent/overview.md +37 -0
  131. docs/branches/general-agent/personality.md +36 -0
  132. docs/branches/general-agent/prompt-builder.md +30 -0
  133. docs/changelog/index.md +79 -0
  134. docs/contributing/code-style.md +69 -0
  135. docs/contributing/documentation.md +43 -0
  136. docs/contributing/overview.md +29 -0
  137. docs/contributing/testing.md +29 -0
  138. docs/core/crcagent/async-operations.md +65 -0
  139. docs/core/crcagent/automatic-extraction.md +107 -0
  140. docs/core/crcagent/batch-prediction.md +80 -0
  141. docs/core/crcagent/bayesian-inference.md +60 -0
  142. docs/core/crcagent/causal-graph.md +92 -0
  143. docs/core/crcagent/counterfactuals.md +96 -0
  144. docs/core/crcagent/deterministic-simulation.md +78 -0
  145. docs/core/crcagent/dual-mode-operation.md +82 -0
  146. docs/core/crcagent/initialization.md +88 -0
  147. docs/core/crcagent/optimization.md +65 -0
  148. docs/core/crcagent/overview.md +63 -0
  149. docs/core/crcagent/time-series.md +57 -0
  150. docs/core/schemas/annotation.md +30 -0
  151. docs/core/schemas/core-schemas.md +82 -0
  152. docs/core/schemas/overview.md +30 -0
  153. docs/core/schemas/policy.md +41 -0
  154. docs/core/templates/base-agent.md +31 -0
  155. docs/core/templates/feature-mixins.md +31 -0
  156. docs/core/templates/overview.md +29 -0
  157. docs/core/templates/templates-guide.md +75 -0
  158. docs/core/tools/mcp-client.md +34 -0
  159. docs/core/tools/overview.md +24 -0
  160. docs/core/utils/conversation.md +27 -0
  161. docs/core/utils/graph-reasoner.md +29 -0
  162. docs/core/utils/overview.md +27 -0
  163. docs/core/utils/router.md +27 -0
  164. docs/core/utils/utilities.md +97 -0
  165. docs/css/custom.css +84 -0
  166. docs/examples/basic-usage.md +57 -0
  167. docs/examples/general-agent/general-agent-examples.md +50 -0
  168. docs/examples/hybrid-agent/hybrid-agent-examples.md +56 -0
  169. docs/examples/image-annotation/image-annotation-examples.md +54 -0
  170. docs/examples/integration/integration-examples.md +58 -0
  171. docs/examples/overview.md +37 -0
  172. docs/examples/trading/trading-examples.md +46 -0
  173. docs/features/causal-reasoning/advanced-topics.md +101 -0
  174. docs/features/causal-reasoning/counterfactuals.md +43 -0
  175. docs/features/causal-reasoning/do-calculus.md +50 -0
  176. docs/features/causal-reasoning/overview.md +47 -0
  177. docs/features/causal-reasoning/structural-models.md +52 -0
  178. docs/features/hybrid-agent/advanced-components.md +55 -0
  179. docs/features/hybrid-agent/core-components.md +64 -0
  180. docs/features/hybrid-agent/overview.md +34 -0
  181. docs/features/image-annotation/engine.md +82 -0
  182. docs/features/image-annotation/features.md +113 -0
  183. docs/features/image-annotation/integration.md +75 -0
  184. docs/features/image-annotation/overview.md +53 -0
  185. docs/features/image-annotation/quickstart.md +73 -0
  186. docs/features/policy-engine/doctrine-ledger.md +105 -0
  187. docs/features/policy-engine/monitoring.md +44 -0
  188. docs/features/policy-engine/mpc-control.md +89 -0
  189. docs/features/policy-engine/overview.md +46 -0
  190. docs/getting-started/configuration.md +225 -0
  191. docs/getting-started/first-agent.md +164 -0
  192. docs/getting-started/installation.md +144 -0
  193. docs/getting-started/quickstart.md +137 -0
  194. docs/index.md +118 -0
  195. docs/js/mathjax.js +13 -0
  196. docs/lrm/discovery_proof_notes.md +25 -0
  197. docs/lrm/finetune_full.md +83 -0
  198. docs/lrm/math_appendix.md +120 -0
  199. docs/lrm/overview.md +32 -0
  200. docs/mkdocs.yml +238 -0
  201. docs/stylesheets/extra.css +21 -0
  202. docs_generated/crca_core/CounterfactualResult.md +12 -0
  203. docs_generated/crca_core/DiscoveryHypothesisResult.md +13 -0
  204. docs_generated/crca_core/DraftSpec.md +13 -0
  205. docs_generated/crca_core/EstimateResult.md +13 -0
  206. docs_generated/crca_core/IdentificationResult.md +17 -0
  207. docs_generated/crca_core/InterventionDesignResult.md +12 -0
  208. docs_generated/crca_core/LockedSpec.md +15 -0
  209. docs_generated/crca_core/RefusalResult.md +12 -0
  210. docs_generated/crca_core/ValidationReport.md +9 -0
  211. docs_generated/crca_core/index.md +13 -0
  212. examples/general_agent_example.py +277 -0
  213. examples/general_agent_quickstart.py +202 -0
  214. examples/general_agent_simple.py +92 -0
  215. examples/hybrid_agent_auto_extraction.py +84 -0
  216. examples/hybrid_agent_dictionary_demo.py +104 -0
  217. examples/hybrid_agent_enhanced.py +179 -0
  218. examples/hybrid_agent_general_knowledge.py +107 -0
  219. examples/image_annotation_quickstart.py +328 -0
  220. examples/test_hybrid_fixes.py +77 -0
  221. image_annotation/__init__.py +27 -0
  222. image_annotation/annotation_engine.py +2593 -0
  223. install_cuda_wsl2.sh +59 -0
  224. install_deepspeed.sh +56 -0
  225. install_deepspeed_simple.sh +87 -0
  226. mkdocs.yml +252 -0
  227. ollama/Modelfile +8 -0
  228. prompts/__init__.py +2 -1
  229. prompts/default_crca.py +9 -1
  230. prompts/general_agent.py +227 -0
  231. prompts/image_annotation.py +56 -0
  232. pyproject.toml +17 -2
  233. requirements-docs.txt +10 -0
  234. requirements.txt +21 -2
  235. schemas/__init__.py +26 -1
  236. schemas/annotation.py +222 -0
  237. schemas/conversation.py +193 -0
  238. schemas/hybrid.py +211 -0
  239. schemas/reasoning.py +276 -0
  240. schemas_export/crca_core/CounterfactualResult.schema.json +108 -0
  241. schemas_export/crca_core/DiscoveryHypothesisResult.schema.json +113 -0
  242. schemas_export/crca_core/DraftSpec.schema.json +635 -0
  243. schemas_export/crca_core/EstimateResult.schema.json +113 -0
  244. schemas_export/crca_core/IdentificationResult.schema.json +145 -0
  245. schemas_export/crca_core/InterventionDesignResult.schema.json +111 -0
  246. schemas_export/crca_core/LockedSpec.schema.json +646 -0
  247. schemas_export/crca_core/RefusalResult.schema.json +90 -0
  248. schemas_export/crca_core/ValidationReport.schema.json +62 -0
  249. scripts/build_lrm_dataset.py +80 -0
  250. scripts/export_crca_core_schemas.py +54 -0
  251. scripts/export_hf_lrm.py +37 -0
  252. scripts/export_ollama_gguf.py +45 -0
  253. scripts/generate_changelog.py +157 -0
  254. scripts/generate_crca_core_docs_from_schemas.py +86 -0
  255. scripts/run_crca_core_benchmarks.py +163 -0
  256. scripts/run_full_finetune.py +198 -0
  257. scripts/run_lrm_eval.py +31 -0
  258. templates/graph_management.py +29 -0
  259. tests/conftest.py +9 -0
  260. tests/test_core.py +2 -3
  261. tests/test_crca_core_discovery_tabular.py +15 -0
  262. tests/test_crca_core_estimate_dowhy.py +36 -0
  263. tests/test_crca_core_identify.py +18 -0
  264. tests/test_crca_core_intervention_design.py +36 -0
  265. tests/test_crca_core_linear_gaussian_scm.py +69 -0
  266. tests/test_crca_core_spec.py +25 -0
  267. tests/test_crca_core_timeseries_pcmci.py +15 -0
  268. tests/test_crca_llm_coauthor.py +12 -0
  269. tests/test_crca_llm_orchestrator.py +80 -0
  270. tests/test_hybrid_agent_llm_enhanced.py +556 -0
  271. tests/test_image_annotation_demo.py +376 -0
  272. tests/test_image_annotation_operational.py +408 -0
  273. tests/test_image_annotation_unit.py +551 -0
  274. tests/test_training_moe.py +13 -0
  275. training/__init__.py +42 -0
  276. training/datasets.py +140 -0
  277. training/deepspeed_zero2_0_5b.json +22 -0
  278. training/deepspeed_zero2_1_5b.json +22 -0
  279. training/deepspeed_zero3_0_5b.json +28 -0
  280. training/deepspeed_zero3_14b.json +28 -0
  281. training/deepspeed_zero3_h100_3gpu.json +20 -0
  282. training/deepspeed_zero3_offload.json +28 -0
  283. training/eval.py +92 -0
  284. training/finetune.py +516 -0
  285. training/public_datasets.py +89 -0
  286. training_data/react_train.jsonl +7473 -0
  287. utils/agent_discovery.py +311 -0
  288. utils/batch_processor.py +317 -0
  289. utils/conversation.py +78 -0
  290. utils/edit_distance.py +118 -0
  291. utils/formatter.py +33 -0
  292. utils/graph_reasoner.py +530 -0
  293. utils/rate_limiter.py +283 -0
  294. utils/router.py +2 -2
  295. utils/tool_discovery.py +307 -0
  296. webui/__init__.py +10 -0
  297. webui/app.py +229 -0
  298. webui/config.py +104 -0
  299. webui/static/css/style.css +332 -0
  300. webui/static/js/main.js +284 -0
  301. webui/templates/index.html +42 -0
  302. tests/test_crca_excel.py +0 -166
  303. tests/test_data_broker.py +0 -424
  304. tests/test_palantir.py +0 -349
  305. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/WHEEL +0 -0
  306. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,164 @@
1
+ # Creating Your First Agent
2
+
3
+ This tutorial walks you through creating a complete causal reasoning agent from scratch.
4
+
5
+ ## Example: Education and Income Analysis
6
+
7
+ Let's build an agent that analyzes the causal relationship between education and income.
8
+
9
+ ### Step 1: Import and Initialize
10
+
11
+ ```python
12
+ from CRCA import CRCAAgent
13
+
14
+ # Create the agent
15
+ agent = CRCAAgent(
16
+ model_name="gpt-4o-mini",
17
+ agent_name="education-income-analyzer",
18
+ max_loops=5
19
+ )
20
+ ```
21
+
22
+ ### Step 2: Define Your Causal Question
23
+
24
+ ```python
25
+ task = """
26
+ Analyze the causal relationship between education and income.
27
+
28
+ Consider the following factors:
29
+ - Years of education
30
+ - Type of degree
31
+ - Work experience
32
+ - Industry sector
33
+ - Geographic location
34
+
35
+ Determine:
36
+ 1. The direct causal effect of education on income
37
+ 2. Mediating factors (experience, industry)
38
+ 3. Confounding variables (geographic location, family background)
39
+ 4. Counterfactual scenarios (what if education increased by 2 years?)
40
+ """
41
+ ```
42
+
43
+ ### Step 3: Run the Analysis
44
+
45
+ ```python
46
+ result = agent.run(task)
47
+ print(result)
48
+ ```
49
+
50
+ The agent will automatically:
51
+ 1. Extract variables from the task
52
+ 2. Identify causal relationships
53
+ 3. Build a causal graph
54
+ 4. Perform causal inference
55
+ 5. Generate counterfactuals
56
+
57
+ ### Step 4: Examine the Causal Graph
58
+
59
+ ```python
60
+ # Get the causal graph
61
+ graph = agent.causal_graph
62
+
63
+ # View all variables
64
+ variables = graph.get_variables()
65
+ print("Variables:", variables)
66
+
67
+ # View causal relationships
68
+ relationships = graph.get_relationships()
69
+ for parent, child, strength in relationships:
70
+ print(f"{parent} -> {child}: {strength:.2f}")
71
+ ```
72
+
73
+ ### Step 5: Generate Counterfactuals
74
+
75
+ ```python
76
+ # What if education increased by 2 years?
77
+ counterfactuals = agent.generate_counterfactuals(
78
+ intervention={"education_years": "+2"},
79
+ outcome="income"
80
+ )
81
+
82
+ for cf in counterfactuals:
83
+ print(f"\nCounterfactual: {cf.description}")
84
+ print(f"Expected income change: {cf.expected_outcome}")
85
+ ```
86
+
87
+ ### Step 6: Run Deterministic Simulation
88
+
89
+ ```python
90
+ # Simulate with specific values
91
+ simulation = agent.simulate(
92
+ variables={
93
+ "education_years": 16,
94
+ "experience_years": 5,
95
+ "industry": "technology"
96
+ },
97
+ target="income"
98
+ )
99
+
100
+ print(f"Simulated income: ${simulation:,.2f}")
101
+ ```
102
+
103
+ ## Mathematical Foundation
104
+
105
+ The agent uses Structural Causal Models (SCMs) to represent causal relationships. For education and income:
106
+
107
+ $$Income = f(Education, Experience, Industry, Location, \epsilon)$$
108
+
109
+ Where $\epsilon$ represents unobserved factors.
110
+
111
+ The causal effect of education on income, controlling for confounders, is:
112
+
113
+ $$E[Income | do(Education = e)] = \sum_{z} E[Income | Education = e, Z = z] P(Z = z)$$
114
+
115
+ Where $Z$ represents confounders (experience, industry, location).
116
+
117
+ ## Complete Example
118
+
119
+ ```python
120
+ from CRCA import CRCAAgent
121
+
122
+ # Initialize agent
123
+ agent = CRCAAgent(
124
+ model_name="gpt-4o-mini",
125
+ agent_name="education-income-analyzer"
126
+ )
127
+
128
+ # Define task
129
+ task = """
130
+ Analyze how increasing education from high school to college affects income,
131
+ controlling for work experience and industry sector.
132
+ """
133
+
134
+ # Run analysis
135
+ result = agent.run(task)
136
+
137
+ # Get causal graph
138
+ graph = agent.causal_graph
139
+ print("Causal Graph Variables:", graph.get_variables())
140
+
141
+ # Generate counterfactuals
142
+ counterfactuals = agent.generate_counterfactuals(
143
+ intervention={"education": "college"},
144
+ outcome="income"
145
+ )
146
+
147
+ # Run simulation
148
+ simulation = agent.simulate(
149
+ variables={
150
+ "education": "college",
151
+ "experience": 3,
152
+ "industry": "technology"
153
+ },
154
+ target="income"
155
+ )
156
+
157
+ print(f"\nSimulated Income: ${simulation:,.2f}")
158
+ ```
159
+
160
+ ## Next Steps
161
+
162
+ - [Core Modules](../core/crcagent/overview.md) - Learn about core functionality
163
+ - [Examples](../examples/overview.md) - Explore more examples
164
+ - [API Reference](../api/crca/overview.md) - Complete API documentation
@@ -0,0 +1,144 @@
1
+ # Installation
2
+
3
+ This guide covers installing CR-CA and its dependencies.
4
+
5
+ ## Prerequisites
6
+
7
+ - Python 3.10 or higher
8
+ - pip package manager
9
+ - (Optional) Git for cloning the repository
10
+ - (Optional) Docker for containerized deployment
11
+
12
+ ## Quick Install
13
+
14
+ ### Method 1: From PyPI (Recommended)
15
+
16
+ ```bash
17
+ pip install crca
18
+ ```
19
+
20
+ ### Method 2: From Source
21
+
22
+ Clone the repository:
23
+
24
+ ```bash
25
+ git clone https://github.com/IlumCI/CR-CA.git
26
+ cd CR-CA
27
+ ```
28
+
29
+ Install dependencies:
30
+
31
+ ```bash
32
+ pip install -r requirements.txt
33
+ ```
34
+
35
+ ### Method 3: Development Install
36
+
37
+ For development with editable install:
38
+
39
+ ```bash
40
+ git clone https://github.com/IlumCI/CR-CA.git
41
+ cd CR-CA
42
+ pip install -e ".[dev]"
43
+ ```
44
+
45
+ ## Optional Dependencies
46
+
47
+ ### Policy Engine
48
+
49
+ ```bash
50
+ # Install policy engine dependencies
51
+ pip install cvxpy ruptures psutil
52
+
53
+ # Or install all optional dependencies
54
+ pip install crca[policy-engine]
55
+ ```
56
+
57
+ ### CRCA-SD
58
+
59
+ CRCA-SD dependencies are included in `requirements.txt`. Additional dependencies may be needed for specific features.
60
+
61
+ ### CRCA-CG
62
+
63
+ ```bash
64
+ # Install TUI dependencies (optional)
65
+ pip install rich
66
+
67
+ # Or install all optional dependencies
68
+ pip install crca[tui]
69
+ ```
70
+
71
+ ### CRCA-Q
72
+
73
+ ```bash
74
+ # Install trading dependencies
75
+ pip install ccxt web3
76
+
77
+ # Or install all optional dependencies
78
+ pip install crca[trading]
79
+ ```
80
+
81
+ ## Environment Variables
82
+
83
+ Create a `.env` file in the project root with required API keys:
84
+
85
+ ```bash
86
+ # Required for LLM functionality
87
+ OPENAI_API_KEY=your_openai_api_key_here
88
+
89
+ # Optional: For CRCA-Q trading
90
+ KRAKEN_API_KEY=your_kraken_key_here
91
+ KRAKEN_API_SECRET=your_kraken_secret_here
92
+
93
+ # Optional: For alternative data sources
94
+ TWITTER_BEARER_TOKEN=your_twitter_token_here
95
+ ETHERSCAN_API_KEY=your_etherscan_key_here
96
+ THEGRAPH_API_KEY=your_thegraph_key_here
97
+
98
+ # Optional: For MCP servers
99
+ MCP_SERVER_URL=http://localhost:8000
100
+ MCP_API_KEY=your_mcp_api_key_here
101
+ ```
102
+
103
+ ## Verification
104
+
105
+ Verify your installation:
106
+
107
+ ```python
108
+ from CRCA import CRCAAgent
109
+
110
+ # This should work without errors
111
+ agent = CRCAAgent(
112
+ model_name="gpt-4o-mini",
113
+ agent_name="test-agent"
114
+ )
115
+ print("Installation successful!")
116
+ ```
117
+
118
+ ## Troubleshooting
119
+
120
+ ### Windows: cvxpy Installation Issues
121
+
122
+ If you encounter issues installing `cvxpy` on Windows, you need Microsoft Visual C++ 14.0 or higher:
123
+
124
+ 1. Download [Microsoft Visual C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/)
125
+ 2. Install the "Desktop development with C++" workload
126
+ 3. Retry: `pip install cvxpy`
127
+
128
+ ### Import Errors
129
+
130
+ If you see import errors, ensure all dependencies are installed:
131
+
132
+ ```bash
133
+ pip install -r requirements.txt
134
+ ```
135
+
136
+ ### API Key Issues
137
+
138
+ Make sure your `.env` file is in the project root and contains valid API keys. The framework will automatically load environment variables from `.env`.
139
+
140
+ ## Next Steps
141
+
142
+ - [Quickstart Guide](quickstart.md) - Get started with your first agent
143
+ - [Configuration](configuration.md) - Configure your environment
144
+ - [First Agent](first-agent.md) - Create your first causal reasoning agent
@@ -0,0 +1,137 @@
1
+ # Quickstart Guide
2
+
3
+ Get started with CR-CA in minutes. This guide walks you through creating your first causal reasoning agent.
4
+
5
+ ## Basic Usage
6
+
7
+ ### 1. Import and Initialize
8
+
9
+ ```python
10
+ from CRCA import CRCAAgent
11
+
12
+ # Create an agent
13
+ agent = CRCAAgent(
14
+ model_name="gpt-4o-mini",
15
+ agent_name="my-first-agent"
16
+ )
17
+ ```
18
+
19
+ ### 2. Run a Simple Task
20
+
21
+ ```python
22
+ # Ask the agent to analyze a causal relationship
23
+ result = agent.run(
24
+ "What is the causal relationship between education and income?"
25
+ )
26
+
27
+ print(result)
28
+ ```
29
+
30
+ ### 3. Automatic Variable Extraction
31
+
32
+ CR-CA automatically extracts variables and causal relationships from natural language:
33
+
34
+ ```python
35
+ task = """
36
+ Analyze how increasing the minimum wage affects employment rates.
37
+ Consider factors like inflation, business costs, and consumer spending.
38
+ """
39
+
40
+ result = agent.run(task)
41
+
42
+ # The agent automatically:
43
+ # 1. Extracts variables (minimum_wage, employment_rate, inflation, etc.)
44
+ # 2. Identifies causal relationships
45
+ # 3. Builds a causal graph
46
+ # 4. Performs causal analysis
47
+ ```
48
+
49
+ ## Mathematical Foundation
50
+
51
+ CR-CA uses Structural Causal Models (SCMs) to represent causal relationships. An SCM is defined as:
52
+
53
+ $$(U, V, F)$$
54
+
55
+ Where:
56
+ - $U$: Exogenous (unobserved) variables
57
+ - $V$: Endogenous (observed) variables
58
+ - $F$: Structural equations defining causal relationships
59
+
60
+ The causal effect of intervention $do(X=x)$ on outcome $Y$ is computed using:
61
+
62
+ $$E[Y | do(X=x)] = \sum_{z} E[Y | X=x, Z=z] P(Z=z)$$
63
+
64
+ ## Working with Causal Graphs
65
+
66
+ ### View the Causal Graph
67
+
68
+ ```python
69
+ # After running a task, access the causal graph
70
+ graph = agent.causal_graph
71
+
72
+ # Get all variables
73
+ variables = graph.get_variables()
74
+ print(f"Variables: {variables}")
75
+
76
+ # Get causal relationships
77
+ relationships = graph.get_relationships()
78
+ for parent, child, strength in relationships:
79
+ print(f"{parent} -> {child} (strength: {strength})")
80
+ ```
81
+
82
+ ### Add Causal Relationships Manually
83
+
84
+ ```python
85
+ # Add a causal relationship
86
+ agent.add_causal_relationship(
87
+ parent="education",
88
+ child="income",
89
+ strength=0.6
90
+ )
91
+
92
+ # Add multiple relationships
93
+ agent.add_causal_relationships([
94
+ ("education", "income", 0.6),
95
+ ("experience", "income", 0.4),
96
+ ("education", "experience", 0.3)
97
+ ])
98
+ ```
99
+
100
+ ## Counterfactual Analysis
101
+
102
+ Generate "what-if" scenarios:
103
+
104
+ ```python
105
+ # Generate counterfactuals
106
+ counterfactuals = agent.generate_counterfactuals(
107
+ intervention={"education": "college"},
108
+ outcome="income"
109
+ )
110
+
111
+ for scenario in counterfactuals:
112
+ print(f"Scenario: {scenario.description}")
113
+ print(f"Expected outcome: {scenario.expected_outcome}")
114
+ ```
115
+
116
+ ## Deterministic Simulation
117
+
118
+ Run deterministic causal simulations:
119
+
120
+ ```python
121
+ # Simulate with specific variable values
122
+ simulation_result = agent.simulate(
123
+ variables={
124
+ "education": 16, # years
125
+ "experience": 5, # years
126
+ },
127
+ target="income"
128
+ )
129
+
130
+ print(f"Simulated income: ${simulation_result:.2f}")
131
+ ```
132
+
133
+ ## Next Steps
134
+
135
+ - [Configuration](configuration.md) - Configure advanced settings
136
+ - [First Agent](first-agent.md) - Build a complete example
137
+ - [Core Modules](../core/crcagent/overview.md) - Learn about core functionality
docs/index.md ADDED
@@ -0,0 +1,118 @@
1
+ # CR-CA: Causal Reasoning and Counterfactual Analysis Framework
2
+
3
+ **Version**: v1.4.0
4
+ **Repository**: [GitHub](https://github.com/IlumCI/CR-CA)
5
+ **License**: Apache-2.0
6
+
7
+ CR-CA is a comprehensive framework for causal reasoning and counterfactual analysis, combining structural causal models (SCMs) with large language model (LLM) integration. The framework enables sophisticated causal inference, automatic variable extraction from natural language, deterministic causal simulation, and comprehensive counterfactual scenario generation.
8
+
9
+ ## Quick Start
10
+
11
+ ```bash
12
+ pip install crca
13
+ ```
14
+
15
+ ```python
16
+ from CRCA import CRCAAgent
17
+
18
+ agent = CRCAAgent(
19
+ model_name="gpt-4o-mini",
20
+ agent_name="my-causal-agent"
21
+ )
22
+
23
+ result = agent.run("Analyze the causal relationship between education and income")
24
+ ```
25
+
26
+ ## Key Features
27
+
28
+ ### Core Capabilities
29
+
30
+ - **Automatic Variable Extraction**: Extract variables and causal relationships from natural language tasks
31
+ - **Dual-Mode Operation**: Switch between LLM-based analysis and deterministic simulation
32
+ - **Structural Causal Models**: Build and manage causal directed acyclic graphs (DAGs)
33
+ - **Deterministic Simulation**: Perform causal simulations with mathematical precision
34
+ - **Counterfactual Generation**: Generate "what-if" scenarios for decision support
35
+
36
+ ### Advanced Analysis
37
+
38
+ - **Batch Prediction**: Process multiple scenarios simultaneously
39
+ - **Async Operations**: Concurrent analysis with async/await support
40
+ - **Optimization Methods**: Gradient-based and dynamic programming optimization
41
+ - **Time-Series Analysis**: Granger causality, VAR estimation, and temporal modeling
42
+ - **Bayesian Inference**: Posterior distributions with credible intervals
43
+
44
+ ### Specialized Branches
45
+
46
+ - **CRCA-Q**: Quantitative trading with causal signals and portfolio optimization
47
+ - **CRCA-SD**: Socioeconomic dynamics with MPC solver and governance systems
48
+ - **CRCA-CG**: Corporate governance with CorporateSwarm and ESG scoring
49
+ - **General Agent**: Flexible general-purpose agent with personality system
50
+
51
+ ## Mathematical Foundation
52
+
53
+ CR-CA is built on rigorous mathematical foundations:
54
+
55
+ ### Structural Causal Models
56
+
57
+ A Structural Causal Model (SCM) is defined as a triple $(U, V, F)$ where:
58
+
59
+ - $U$: Exogenous (unobserved) variables
60
+ - $V$: Endogenous (observed) variables
61
+ - $F$: Structural equations defining causal relationships
62
+
63
+ ### Do-Calculus
64
+
65
+ The causal effect of intervention $do(X=x)$ on outcome $Y$ is:
66
+
67
+ $$E[Y | do(X=x)] = \sum_{z} E[Y | X=x, Z=z] P(Z=z)$$
68
+
69
+ Where $Z$ represents confounders that need to be adjusted for.
70
+
71
+ ### Counterfactual Reasoning
72
+
73
+ Counterfactual queries answer "what would have happened if" questions:
74
+
75
+ $$P(Y_{x'} | X=x, Y=y)$$
76
+
77
+ This represents the probability of outcome $Y$ under intervention $X=x'$, given that we observed $X=x$ and $Y=y$.
78
+
79
+ ## Documentation Structure
80
+
81
+ - **[Getting Started](getting-started/installation.md)**: Installation and setup guides
82
+ - **[Core Modules](core/crcagent/overview.md)**: CRCAAgent and core functionality
83
+ - **[Branches](branches/crca-q/overview.md)**: Specialized agent implementations
84
+ - **[Features](features/image-annotation/overview.md)**: Image annotation, policy engine, hybrid agent
85
+ - **[Examples](examples/overview.md)**: Code examples and tutorials
86
+ - **[Architecture](architecture/overview.md)**: System design and principles
87
+ - **[API Reference](api/crca/overview.md)**: Complete API documentation
88
+
89
+ ## Installation
90
+
91
+ ```bash
92
+ # From PyPI
93
+ pip install crca
94
+
95
+ # From source
96
+ git clone https://github.com/IlumCI/CR-CA.git
97
+ cd CR-CA
98
+ pip install -r requirements.txt
99
+ ```
100
+
101
+ ## Quick Links
102
+
103
+ - [Installation Guide](getting-started/installation.md)
104
+ - [Quickstart Tutorial](getting-started/quickstart.md)
105
+ - [API Reference](api/crca/overview.md)
106
+ - [Examples](examples/overview.md)
107
+ - [Contributing](contributing/overview.md)
108
+
109
+ ## Research & Theory
110
+
111
+ CR-CA implements concepts from:
112
+
113
+ - **Judea Pearl's Causal Hierarchy**: Association, intervention, and counterfactual reasoning
114
+ - **Structural Causal Models**: Mathematical framework for causal inference
115
+ - **Do-Calculus**: Rules for identifying causal effects from observational data
116
+ - **Counterfactual Analysis**: Reasoning about alternative scenarios
117
+
118
+ For detailed mathematical foundations, see the [Causal Reasoning](features/causal-reasoning/overview.md) section.
docs/js/mathjax.js ADDED
@@ -0,0 +1,13 @@
1
+ window.MathJax = {
2
+ tex: {
3
+ inlineMath: [['$', '$'], ['\\(', '\\)']],
4
+ displayMath: [['$$', '$$'], ['\\[', '\\]']],
5
+ processEscapes: true,
6
+ processEnvironments: true,
7
+ packages: {'[+]': ['ams', 'newcommand', 'configmacros']}
8
+ },
9
+ options: {
10
+ ignoreHtmlClass: 'tex2jax_ignore',
11
+ processHtmlClass: 'tex2jax_process'
12
+ }
13
+ };
@@ -0,0 +1,25 @@
1
+ ---
2
+ title: Discovery Proof Notes
3
+ ---
4
+
5
+ # Discovery Proof Notes (Assumptions + Correctness)
6
+
7
+ ## PC algorithm
8
+ - **Assumptions**: causal sufficiency, faithfulness, correct CI test.
9
+ - **Output**: CPDAG (equivalence class).
10
+ - **Correctness**: asymptotic correctness under assumptions (Spirtes et al.).
11
+
12
+ ## FCI algorithm
13
+ - **Assumptions**: faithfulness, correct CI test, allows latent confounding.
14
+ - **Output**: PAG (partial ancestral graph).
15
+ - **Correctness**: relies on ancestral graph Markov properties (Richardson & Spirtes).
16
+
17
+ ## GES
18
+ - **Assumptions**: correct scoring criterion (e.g., BIC), large sample.
19
+ - **Output**: CPDAG of highest score.
20
+ - **Correctness**: asymptotic consistency for score-equivalent metrics.
21
+
22
+ ## PCMCI / PCMCI+
23
+ - **Assumptions**: stationarity, lag sufficiency, correct CI test.
24
+ - **Output**: lagged causal graph hypothesis.
25
+ - **Correctness**: consistency depends on correct lag window and CI testing.
@@ -0,0 +1,83 @@
1
+ ---
2
+ title: Full Finetune (Qwen2.5-0.5B)
3
+ ---
4
+
5
+ # Full Finetune (Qwen2.5-0.5B-Instruct)
6
+
7
+ This guide targets finetuning the **Qwen2.5-0.5B-Instruct** model using **DeepSpeed ZeRO-3 CPU offload** and a **4k context length**. The 0.5B model is optimized for faster training with larger batch sizes and higher learning rates compared to larger models.
8
+
9
+ ## Requirements
10
+ - Python deps: `transformers`, `datasets`, `accelerate`, `deepspeed`
11
+ - GPU driver + CUDA compatible with your PyTorch build
12
+ - Fast SSD/NVMe recommended (CPU offload is heavy on disk I/O)
13
+
14
+ ## 1) Build the dataset (hybrid)
15
+ ```bash
16
+ python scripts/build_lrm_dataset.py \
17
+ --trace-jsonl path/to/traces.jsonl \
18
+ --output training_data/react_train.jsonl
19
+ ```
20
+
21
+ Optional: provide a public dataset config JSON:
22
+ ```bash
23
+ python scripts/build_lrm_dataset.py \
24
+ --trace-jsonl path/to/traces.jsonl \
25
+ --public-config configs/public_datasets.json \
26
+ --output training_data/react_train.jsonl
27
+ ```
28
+
29
+ ## 2) Run full finetune
30
+ ```bash
31
+ python scripts/run_full_finetune.py \
32
+ --train-file training_data/react_train.jsonl \
33
+ --output-dir lrm_qwen25_0_5b_full_finetune
34
+ ```
35
+
36
+ Notes:
37
+ - Uses `training/deepspeed_zero3_0_5b.json` (optimized for 0.5B model)
38
+ - Defaults to micro-batch 8 with gradient accumulation of 4
39
+ - Higher learning rate (3e-4) optimized for smaller model size
40
+ - Larger batch sizes possible due to smaller model memory footprint
41
+
42
+ ### Hyperparameter Comparison
43
+
44
+ | Parameter | 0.5B Model (Default) | 7B Model (Reference) |
45
+ |-----------|----------------------|----------------------|
46
+ | per_device_batch_size | 8 | 1 |
47
+ | gradient_accumulation_steps | 4 | 32 |
48
+ | learning_rate | 3e-4 | 1e-5 |
49
+ | max_seq_length | 4096 | 4096 |
50
+ | DeepSpeed config | `deepspeed_zero3_0_5b.json` | `deepspeed_zero3_offload.json` |
51
+
52
+ The 0.5B model configuration uses:
53
+ - **Larger batch sizes**: The model is ~14x smaller, allowing for batch size 8 instead of 1
54
+ - **Higher learning rates**: Smaller models can handle higher learning rates (3e-4 vs 1e-5)
55
+ - **Reduced gradient accumulation**: Larger batch size means less accumulation needed
56
+ - **Optimized DeepSpeed buckets**: Smaller bucket sizes tuned for the 0.5B model
57
+
58
+ ## 3) Evaluate traces
59
+ ```bash
60
+ python scripts/run_lrm_eval.py \
61
+ --trace path/to/trace.jsonl \
62
+ --output eval_results/lrm_eval.json
63
+ ```
64
+
65
+ ## 4) Export to HuggingFace
66
+ ```bash
67
+ python scripts/export_hf_lrm.py \
68
+ --checkpoint-dir lrm_qwen25_0_5b_full_finetune \
69
+ --output-dir hf_export/crca_lrm_0_5b
70
+ ```
71
+
72
+ ## 5) Export to Ollama (GGUF)
73
+ ```bash
74
+ python scripts/export_ollama_gguf.py \
75
+ --checkpoint-dir hf_export/crca_lrm \
76
+ --output ollama/crca_lrm.gguf \
77
+ --llama-cpp-dir /path/to/llama.cpp
78
+ ```
79
+
80
+ Then update `ollama/Modelfile` and run:
81
+ ```bash
82
+ ollama create crca-lrm -f ollama/Modelfile
83
+ ```