aria-code 4.1.3__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 (284) hide show
  1. agents/__init__.py +32 -0
  2. agents/base.py +190 -0
  3. agents/deep/__init__.py +37 -0
  4. agents/deep/calibration_loop.py +144 -0
  5. agents/deep/critic.py +125 -0
  6. agents/deep/deepen.py +193 -0
  7. agents/deep/models.py +149 -0
  8. agents/deep/pipeline.py +164 -0
  9. agents/deep/quant_fusion.py +192 -0
  10. agents/deep/themes.py +95 -0
  11. agents/deep/tiers.py +106 -0
  12. agents/financial/__init__.py +10 -0
  13. agents/financial/catalyst.py +279 -0
  14. agents/financial/debate.py +145 -0
  15. agents/financial/earnings.py +303 -0
  16. agents/financial/fundamental.py +159 -0
  17. agents/financial/macro.py +99 -0
  18. agents/financial/news.py +207 -0
  19. agents/financial/risk.py +132 -0
  20. agents/financial/sector.py +279 -0
  21. agents/financial/synthesis.py +274 -0
  22. agents/financial/technical.py +258 -0
  23. agents/portfolio_agent.py +333 -0
  24. agents/realty/__init__.py +62 -0
  25. agents/realty/asset_diagnosis.py +150 -0
  26. agents/realty/business_match.py +165 -0
  27. agents/realty/cashflow_verify.py +208 -0
  28. agents/realty/contract_rules.py +209 -0
  29. agents/realty/energy_anomaly.py +188 -0
  30. agents/realty/exit_settlement.py +207 -0
  31. agents/realty/fulfillment_risk.py +205 -0
  32. agents/realty/ops_optimize.py +159 -0
  33. agents/realty/revenue_share.py +214 -0
  34. agents/registry.py +144 -0
  35. agents/sports/__init__.py +0 -0
  36. agents/sports/football_agent.py +169 -0
  37. agents/team.py +289 -0
  38. aliyun_data_client.py +660 -0
  39. apps/README.md +12 -0
  40. apps/__init__.py +2 -0
  41. apps/channels/README.md +15 -0
  42. apps/cli/README.md +13 -0
  43. apps/cli/__init__.py +2 -0
  44. apps/cli/bootstrap.py +99 -0
  45. apps/cli/codegen_paths.py +29 -0
  46. apps/cli/commands/__init__.py +16 -0
  47. apps/cli/commands/analysis_cmds.py +288 -0
  48. apps/cli/commands/backtest_cmds.py +1887 -0
  49. apps/cli/commands/broker_cmds.py +1154 -0
  50. apps/cli/commands/business_workflow_cmds.py +289 -0
  51. apps/cli/commands/catalog.py +84 -0
  52. apps/cli/commands/data_cmds.py +405 -0
  53. apps/cli/commands/diagnostic_cmds.py +179 -0
  54. apps/cli/commands/diagnostic_ops_cmds.py +696 -0
  55. apps/cli/commands/finance_render.py +12 -0
  56. apps/cli/commands/market.py +399 -0
  57. apps/cli/commands/market_cmds.py +1276 -0
  58. apps/cli/commands/market_context.py +425 -0
  59. apps/cli/commands/market_render.py +7 -0
  60. apps/cli/commands/model_cmds.py +1579 -0
  61. apps/cli/commands/ops_cmds.py +668 -0
  62. apps/cli/commands/portfolio_cmds.py +962 -0
  63. apps/cli/commands/report.py +377 -0
  64. apps/cli/commands/scaffold_templates.py +617 -0
  65. apps/cli/commands/session_cmds.py +179 -0
  66. apps/cli/commands/session_ux_cmds.py +280 -0
  67. apps/cli/commands/team.py +588 -0
  68. apps/cli/commands/team_render.py +8 -0
  69. apps/cli/commands/ui_cmds.py +358 -0
  70. apps/cli/commands/workflow_cmds.py +279 -0
  71. apps/cli/commands/workspace_cmds.py +1414 -0
  72. apps/cli/config_paths.py +70 -0
  73. apps/cli/config_store.py +61 -0
  74. apps/cli/deterministic.py +122 -0
  75. apps/cli/direct.py +48 -0
  76. apps/cli/github_app_auth.py +135 -0
  77. apps/cli/handlers/__init__.py +11 -0
  78. apps/cli/handlers/broker_handlers.py +122 -0
  79. apps/cli/handlers/chart_handlers.py +1309 -0
  80. apps/cli/handlers/market_handlers.py +2509 -0
  81. apps/cli/handlers/realty_handlers.py +114 -0
  82. apps/cli/handlers/strategy_advice.py +82 -0
  83. apps/cli/hooks.py +180 -0
  84. apps/cli/i18n.py +284 -0
  85. apps/cli/intent.py +136 -0
  86. apps/cli/intent_router.py +217 -0
  87. apps/cli/lifecycle_hooks.py +48 -0
  88. apps/cli/main.py +29 -0
  89. apps/cli/market_metadata.py +135 -0
  90. apps/cli/market_universe.py +265 -0
  91. apps/cli/message_processing.py +257 -0
  92. apps/cli/plan_mode.py +139 -0
  93. apps/cli/plotly_html.py +15 -0
  94. apps/cli/prediction_feedback.py +202 -0
  95. apps/cli/preflight.py +497 -0
  96. apps/cli/project_aria.py +60 -0
  97. apps/cli/prompts/__init__.py +0 -0
  98. apps/cli/prompts/coding.py +658 -0
  99. apps/cli/prompts/system_prompts.py +531 -0
  100. apps/cli/prompts/ui.py +434 -0
  101. apps/cli/providers/__init__.py +1 -0
  102. apps/cli/providers/base.py +271 -0
  103. apps/cli/providers/chat_routing.py +80 -0
  104. apps/cli/providers/llm/__init__.py +1 -0
  105. apps/cli/providers/llm/ollama_stream.py +1170 -0
  106. apps/cli/providers/llm/sse_stream.py +216 -0
  107. apps/cli/providers/runtime_bridge.py +185 -0
  108. apps/cli/runtime_consumer.py +489 -0
  109. apps/cli/session_export.py +87 -0
  110. apps/cli/session_jsonl.py +207 -0
  111. apps/cli/session_store.py +112 -0
  112. apps/cli/todo_tracker.py +190 -0
  113. apps/cli/tools/__init__.py +40 -0
  114. apps/cli/tools/context.py +46 -0
  115. apps/cli/tools/file_tools.py +112 -0
  116. apps/cli/tools/market_tools.py +549 -0
  117. apps/cli/tools/notebook_tools.py +111 -0
  118. apps/cli/tools/system_tools.py +669 -0
  119. apps/cli/tools/write_tools.py +715 -0
  120. apps/cli/tradingview_bridge.py +434 -0
  121. apps/cli/update_check.py +152 -0
  122. apps/cli/utils/__init__.py +0 -0
  123. apps/cli/utils/market_detect.py +1578 -0
  124. apps/daemon/README.md +14 -0
  125. apps/vscode/README.md +115 -0
  126. apps/vscode/package.json +70 -0
  127. aria_cli.py +11636 -0
  128. aria_code-4.1.3.dist-info/METADATA +952 -0
  129. aria_code-4.1.3.dist-info/RECORD +284 -0
  130. aria_code-4.1.3.dist-info/WHEEL +5 -0
  131. aria_code-4.1.3.dist-info/entry_points.txt +2 -0
  132. aria_code-4.1.3.dist-info/licenses/LICENSE +121 -0
  133. aria_code-4.1.3.dist-info/top_level.txt +50 -0
  134. aria_daemon.py +1295 -0
  135. aria_feishu_bot.py +1359 -0
  136. aria_relay_client.py +182 -0
  137. aria_relay_server.py +405 -0
  138. aria_telegram_bot.py +202 -0
  139. ariarc.py +328 -0
  140. artifacts.py +491 -0
  141. backtest_report.py +472 -0
  142. brokers/__init__.py +72 -0
  143. brokers/base.py +207 -0
  144. brokers/capabilities.py +264 -0
  145. brokers/cn/__init__.py +10 -0
  146. brokers/cn/easytrader_broker.py +193 -0
  147. brokers/cn/futu_broker.py +194 -0
  148. brokers/cn/longbridge_broker.py +190 -0
  149. brokers/cn/tiger_broker.py +196 -0
  150. brokers/cn/xtquant_broker.py +175 -0
  151. brokers/config.py +364 -0
  152. brokers/intl/__init__.py +5 -0
  153. brokers/intl/alpaca_broker.py +183 -0
  154. brokers/intl/ibkr_broker.py +215 -0
  155. brokers/intl/webull_broker.py +156 -0
  156. brokers/paper_broker.py +259 -0
  157. brokers/planning.py +296 -0
  158. brokers/registry.py +181 -0
  159. brokers/trading.py +237 -0
  160. change_store.py +127 -0
  161. command_safety.py +19 -0
  162. computer_use_tools.py +504 -0
  163. dashboard_generator.py +578 -0
  164. data_analysis_tools.py +808 -0
  165. data_cleaner.py +483 -0
  166. data_service.py +481 -0
  167. datasources/__init__.py +23 -0
  168. datasources/base.py +166 -0
  169. datasources/router.py +221 -0
  170. datasources/sources/__init__.py +15 -0
  171. datasources/sources/akshare_source.py +269 -0
  172. datasources/sources/alpha_vantage_source.py +202 -0
  173. datasources/sources/edgar_source.py +218 -0
  174. datasources/sources/finnhub_source.py +197 -0
  175. datasources/sources/fred_source.py +219 -0
  176. datasources/sources/tushare_source.py +141 -0
  177. datasources/sources/web_scraper_source.py +278 -0
  178. datasources/sources/world_bank_source.py +205 -0
  179. datasources/sources/yfinance_source.py +152 -0
  180. demo_player.py +204 -0
  181. doctor.py +508 -0
  182. file_analysis_tools.py +734 -0
  183. finance_formulas.py +389 -0
  184. football_data_client.py +1670 -0
  185. intent_classifier.py +358 -0
  186. local_finance_tools.py +3221 -0
  187. local_llm_provider.py +552 -0
  188. macro_tools.py +368 -0
  189. market_data_client.py +1899 -0
  190. mcp_client.py +506 -0
  191. memory_manager.py +245 -0
  192. model_capability.py +416 -0
  193. notification_tools.py +248 -0
  194. packages/__init__.py +23 -0
  195. packages/aria_agents/__init__.py +5 -0
  196. packages/aria_agents/manifest.py +69 -0
  197. packages/aria_core/__init__.py +34 -0
  198. packages/aria_core/architecture.py +192 -0
  199. packages/aria_core/export.py +124 -0
  200. packages/aria_core/manifest.py +65 -0
  201. packages/aria_infra/__init__.py +15 -0
  202. packages/aria_infra/arthera.py +52 -0
  203. packages/aria_infra/doctor.py +246 -0
  204. packages/aria_infra/product.py +37 -0
  205. packages/aria_mcp/__init__.py +25 -0
  206. packages/aria_mcp/bridge.py +38 -0
  207. packages/aria_mcp/config.py +97 -0
  208. packages/aria_mcp/tools.py +61 -0
  209. packages/aria_sdk/__init__.py +19 -0
  210. packages/aria_sdk/client.py +396 -0
  211. packages/aria_sdk/providers.py +70 -0
  212. packages/aria_sdk/streaming.py +73 -0
  213. packages/aria_sdk/types.py +86 -0
  214. packages/aria_services/__init__.py +55 -0
  215. packages/aria_services/context.py +258 -0
  216. packages/aria_services/data.py +11 -0
  217. packages/aria_services/provider_health.py +189 -0
  218. packages/aria_services/registry.py +213 -0
  219. packages/aria_services/usage.py +138 -0
  220. packages/aria_skills/__init__.py +5 -0
  221. packages/aria_skills/registry.py +59 -0
  222. packages/aria_tools/__init__.py +5 -0
  223. packages/aria_tools/registry.py +128 -0
  224. packages/quant_engine/__init__.py +6 -0
  225. packages/quant_engine/sports/__init__.py +72 -0
  226. packages/quant_engine/sports/calibrator.py +353 -0
  227. packages/quant_engine/sports/dixon_coles.py +234 -0
  228. packages/quant_engine/sports/elo.py +299 -0
  229. packages/quant_engine/sports/form.py +188 -0
  230. packages/quant_engine/sports/h2h.py +195 -0
  231. packages/quant_engine/sports/ml_model.py +354 -0
  232. packages/quant_engine/sports/predictor.py +311 -0
  233. packages/quant_engine/sports/tracker.py +664 -0
  234. packages/quant_engine/stochastic/__init__.py +27 -0
  235. packages/quant_engine/stochastic/gbm_enhanced.py +195 -0
  236. packages/quant_engine/stochastic/ito_calculus.py +477 -0
  237. packages/quant_engine/stochastic/kelly_criterion.py +181 -0
  238. packages/quant_engine/stochastic/monte_carlo_advanced.py +95 -0
  239. packages/quant_engine/stochastic/options_pricing.py +573 -0
  240. packages/quant_engine/stochastic/stochastic_processes.py +90 -0
  241. plan_utils.py +194 -0
  242. plugin_loader.py +328 -0
  243. portfolio_ledger.py +262 -0
  244. privacy/__init__.py +5 -0
  245. privacy/feedback.py +123 -0
  246. project_tools.py +525 -0
  247. providers/__init__.py +30 -0
  248. providers/llm/__init__.py +19 -0
  249. providers/llm/anthropic.py +184 -0
  250. providers/llm/base.py +139 -0
  251. providers/llm/ollama.py +128 -0
  252. providers/llm/openai_compat.py +282 -0
  253. providers/llm/registry.py +358 -0
  254. realty_data_tools.py +659 -0
  255. report_generator.py +1314 -0
  256. runtime/__init__.py +103 -0
  257. runtime/agent_loop.py +1183 -0
  258. runtime/approval.py +51 -0
  259. runtime/events.py +102 -0
  260. runtime/gateway.py +128 -0
  261. runtime/lsp.py +346 -0
  262. runtime/subagent.py +258 -0
  263. runtime/tool_executor.py +104 -0
  264. runtime/tool_policy.py +106 -0
  265. safety/__init__.py +21 -0
  266. safety/permissions.py +275 -0
  267. setup_wizard.py +653 -0
  268. strategy_vault.py +420 -0
  269. ui/__init__.py +100 -0
  270. ui/banner.py +310 -0
  271. ui/completer.py +391 -0
  272. ui/console.py +271 -0
  273. ui/image_render.py +243 -0
  274. ui/input_box.py +376 -0
  275. ui/picker.py +195 -0
  276. ui/render/__init__.py +11 -0
  277. ui/render/finance.py +1480 -0
  278. ui/render/market.py +225 -0
  279. ui/render/output.py +681 -0
  280. ui/render/team.py +346 -0
  281. ui/robot.py +235 -0
  282. workspace/__init__.py +6 -0
  283. workspace/files.py +170 -0
  284. workspace/verify.py +113 -0
@@ -0,0 +1,195 @@
1
+ """
2
+ 增强型几何布朗运动 (Enhanced Geometric Brownian Motion)
3
+
4
+ 包含:
5
+ 1. 多资产相关 GBM (Multi-asset GBM with Cholesky Decomposition)
6
+ 2. 默顿跳跃扩散模型 (Merton Jump-Diffusion Model)
7
+ 3. Heston 随机波动率模型 (Heston Stochastic Volatility)
8
+ 4. 分数布朗运动 (Fractional Brownian Motion) - 选配
9
+ """
10
+
11
+ import numpy as np
12
+ import pandas as pd
13
+ from typing import Optional, Tuple, List, Union
14
+
15
+
16
+ class EnhancedGBM:
17
+ """
18
+ 增强型 GBM 仿真器
19
+ """
20
+
21
+ @staticmethod
22
+ def simulate_multi_asset(
23
+ s0: np.ndarray,
24
+ mu: np.ndarray,
25
+ sigma: np.ndarray,
26
+ corr_matrix: np.ndarray,
27
+ T: float = 1.0,
28
+ n_steps: int = 252,
29
+ n_paths: int = 1000,
30
+ seed: Optional[int] = None
31
+ ) -> np.ndarray:
32
+ """
33
+ 多资产相关 GBM 模拟
34
+
35
+ 使用 Cholesky 分解生成相关随机增量:
36
+ dS_i = μ_i S_i dt + σ_i S_i dW_i
37
+ E[dW_i dW_j] = ρ_{ij} dt
38
+
39
+ Args:
40
+ s0 : 初始价格向量, shape (n_assets,)
41
+ mu : 年化漂移率向量
42
+ sigma : 年化波动率向量
43
+ corr_matrix : 相关系数矩阵, shape (n_assets, n_assets)
44
+ T : 终止时间(年)
45
+ n_steps : 时间步数
46
+ n_paths : 路径数
47
+ seed : 随机种子
48
+
49
+ Returns:
50
+ paths: shape (n_paths, n_assets, n_steps + 1)
51
+ """
52
+ if seed is not None:
53
+ np.random.seed(seed)
54
+
55
+ n_assets = len(s0)
56
+ dt = T / n_steps
57
+
58
+ # Cholesky 分解: L @ L.T = Correlation
59
+ L = np.linalg.cholesky(corr_matrix)
60
+
61
+ paths = np.zeros((n_paths, n_assets, n_steps + 1))
62
+ paths[:, :, 0] = s0
63
+
64
+ for i in range(n_steps):
65
+ # 生成独立标准正态分布 Z ~ N(0, 1)
66
+ Z = np.random.standard_normal((n_paths, n_assets))
67
+
68
+ # 转化为相关增量 ε = Z @ L.T
69
+ epsilon = Z @ L.T
70
+
71
+ # 伊藤漂移修正
72
+ drift = (mu - 0.5 * sigma**2) * dt
73
+ diffusion = sigma * np.sqrt(dt) * epsilon
74
+
75
+ # 更新价格: S_{t+dt} = S_t * exp(drift + diffusion)
76
+ paths[:, :, i + 1] = paths[:, :, i] * np.exp(drift + diffusion)
77
+
78
+ return paths
79
+
80
+ @staticmethod
81
+ def simulate_merton_jump_diffusion(
82
+ s0: float,
83
+ mu: float,
84
+ sigma: float,
85
+ lambda_j: float,
86
+ mu_j: float,
87
+ sigma_j: float,
88
+ T: float = 1.0,
89
+ n_steps: int = 252,
90
+ n_paths: int = 1000,
91
+ seed: Optional[int] = None
92
+ ) -> np.ndarray:
93
+ """
94
+ 默顿跳跃扩散模型 (Merton Jump-Diffusion)
95
+
96
+ dS/S = (μ - λk)dt + σdW + (J - 1)dN
97
+ 其中 N 是强度为 λ 的泊松过程,ln(J) ~ N(μ_j, σ_j²)
98
+ k = E[J - 1] = exp(μ_j + 0.5σ_j²) - 1
99
+
100
+ Args:
101
+ lambda_j: 跳跃强度(每年平均跳跃次数)
102
+ mu_j : 跳跃幅度的对数均值
103
+ sigma_j : 跳跃幅度的对数标准差
104
+ """
105
+ if seed is not None:
106
+ np.random.seed(seed)
107
+
108
+ dt = T / n_steps
109
+ k = np.exp(mu_j + 0.5 * sigma_j**2) - 1
110
+
111
+ paths = np.zeros((n_paths, n_steps + 1))
112
+ paths[:, 0] = s0
113
+
114
+ for i in range(n_steps):
115
+ # 扩散部分 (Diffusion)
116
+ Z = np.random.standard_normal(n_paths)
117
+ drift = (mu - 0.5 * sigma**2 - lambda_j * k) * dt
118
+ diffusion = sigma * np.sqrt(dt) * Z
119
+
120
+ # 跳跃部分 (Jump)
121
+ # 1. 产生泊松分布的跳跃次数 (通常 dt 很小,N 主要是 0 或 1)
122
+ N = np.random.poisson(lambda_j * dt, n_paths)
123
+
124
+ # 2. 对每个跳跃计算幅度
125
+ jump_factor = np.ones(n_paths)
126
+ for path_idx in range(n_paths):
127
+ if N[path_idx] > 0:
128
+ # 总跳跃幅度 = Σ ln(J_i)
129
+ total_jump_log = np.random.normal(mu_j, sigma_j, N[path_idx]).sum()
130
+ jump_factor[path_idx] = np.exp(total_jump_log)
131
+
132
+ # 更新价格
133
+ paths[:, i + 1] = paths[:, i] * np.exp(drift + diffusion) * jump_factor
134
+
135
+ return paths
136
+
137
+ @staticmethod
138
+ def simulate_heston(
139
+ s0: float,
140
+ v0: float,
141
+ mu: float,
142
+ kappa: float,
143
+ theta: float,
144
+ sigma_v: float,
145
+ rho: float,
146
+ T: float = 1.0,
147
+ n_steps: int = 252,
148
+ n_paths: int = 1000,
149
+ seed: Optional[int] = None
150
+ ) -> Tuple[np.ndarray, np.ndarray]:
151
+ """
152
+ Heston 随机波动率模型
153
+
154
+ dS = μS dt + √v S dW1
155
+ dv = κ(θ - v)dt + σ_v √v dW2
156
+ E[dW1 dW2] = ρ dt
157
+
158
+ Args:
159
+ v0 : 初始方差 (Initial Variance)
160
+ kappa : 均值回归速度 (Mean Reversion Speed)
161
+ theta : 长期平均方差 (Long-term Variance)
162
+ sigma_v: 波动率的波动率 (Vol of Vol)
163
+ rho : 价格与波动率的相关性
164
+ """
165
+ if seed is not None:
166
+ np.random.seed(seed)
167
+
168
+ dt = T / n_steps
169
+
170
+ s_paths = np.zeros((n_paths, n_steps + 1))
171
+ v_paths = np.zeros((n_paths, n_steps + 1))
172
+ s_paths[:, 0] = s0
173
+ v_paths[:, 0] = v0
174
+
175
+ for i in range(n_steps):
176
+ # 生成相关随机变量
177
+ Z1 = np.random.standard_normal(n_paths)
178
+ Z2 = np.random.standard_normal(n_paths)
179
+ W1 = Z1
180
+ W2 = rho * Z1 + np.sqrt(1 - rho**2) * Z2
181
+
182
+ v_curr = v_paths[:, i]
183
+ # 保证方差非负 (使用 Full Truncation 或 Reflection)
184
+ v_plus = np.maximum(v_curr, 0)
185
+
186
+ # 更新方差 (Euler-Maruyama)
187
+ v_paths[:, i + 1] = v_curr + kappa * (theta - v_plus) * dt + \
188
+ sigma_v * np.sqrt(v_plus * dt) * W2
189
+
190
+ # 更新价格
191
+ s_paths[:, i + 1] = s_paths[:, i] * np.exp(
192
+ (mu - 0.5 * v_plus) * dt + np.sqrt(v_plus * dt) * W1
193
+ )
194
+
195
+ return s_paths, v_paths
@@ -0,0 +1,477 @@
1
+ """
2
+ 伊藤积分与伊藤引理实现
3
+ Itô's Lemma and Itô Stochastic Integral
4
+
5
+ 理论基础:
6
+ 设 X(t) 为伊藤过程:dX = μ(X,t)dt + σ(X,t)dW
7
+ 对光滑函数 f(X,t),伊藤引理给出:
8
+ df = (∂f/∂t + μ·∂f/∂X + ½σ²·∂²f/∂X²)dt + σ·∂f/∂X·dW
9
+
10
+ 关键应用:
11
+ 1. GBM → S(t) = S₀·exp((μ - σ²/2)t + σW(t))
12
+ 2. Black-Scholes PDE推导
13
+ 3. Feynman-Kac定理(PDE ↔ 期望)
14
+ 4. 测度变换(Girsanov定理)
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import numpy as np
20
+ import pandas as pd
21
+ from dataclasses import dataclass, field
22
+ from typing import Callable, Optional, Tuple, List
23
+ import warnings
24
+
25
+
26
+ @dataclass
27
+ class ItoProcess:
28
+ """
29
+ 伊藤过程:dX = μ(X,t)dt + σ(X,t)dW
30
+
31
+ 参数:
32
+ mu_func : 漂移系数函数 μ(x, t) -> float
33
+ sigma_func: 扩散系数函数 σ(x, t) -> float
34
+ x0 : 初始值
35
+ name : 过程名称
36
+ """
37
+ mu_func: Callable[[float, float], float]
38
+ sigma_func: Callable[[float, float], float]
39
+ x0: float = 1.0
40
+ name: str = "ItoProcess"
41
+
42
+ def simulate_path(
43
+ self,
44
+ T: float = 1.0,
45
+ n_steps: int = 252,
46
+ seed: Optional[int] = None,
47
+ method: str = "euler_maruyama"
48
+ ) -> Tuple[np.ndarray, np.ndarray]:
49
+ """
50
+ 模拟伊藤过程路径
51
+
52
+ Args:
53
+ T : 终止时间(年)
54
+ n_steps: 时间步数
55
+ seed : 随机种子
56
+ method : "euler_maruyama"(一阶)或 "milstein"(带二阶修正)
57
+
58
+ Returns:
59
+ (t_grid, X_path): 时间网格和过程路径
60
+ """
61
+ if seed is not None:
62
+ np.random.seed(seed)
63
+
64
+ dt = T / n_steps
65
+ sqrt_dt = np.sqrt(dt)
66
+ t_grid = np.linspace(0, T, n_steps + 1)
67
+ X = np.zeros(n_steps + 1)
68
+ X[0] = self.x0
69
+
70
+ dW = np.random.normal(0, sqrt_dt, n_steps)
71
+
72
+ for i in range(n_steps):
73
+ x_i = X[i]
74
+ t_i = t_grid[i]
75
+ mu_i = self.mu_func(x_i, t_i)
76
+ sigma_i = self.sigma_func(x_i, t_i)
77
+
78
+ if method == "euler_maruyama":
79
+ # Euler-Maruyama:X_{n+1} = X_n + μ·Δt + σ·ΔW
80
+ X[i + 1] = x_i + mu_i * dt + sigma_i * dW[i]
81
+
82
+ elif method == "milstein":
83
+ # Milstein 方法:加入二阶修正项 ½σ·σ'·((ΔW)² - Δt)
84
+ # σ' ≈ (σ(x+δ,t) - σ(x-δ,t)) / 2δ(数值微分)
85
+ delta = x_i * 1e-5 if x_i != 0 else 1e-5
86
+ sigma_prime = (self.sigma_func(x_i + delta, t_i) -
87
+ self.sigma_func(x_i - delta, t_i)) / (2 * delta)
88
+ milstein_correction = 0.5 * sigma_i * sigma_prime * (dW[i] ** 2 - dt)
89
+ X[i + 1] = x_i + mu_i * dt + sigma_i * dW[i] + milstein_correction
90
+
91
+ else:
92
+ raise ValueError(f"Unknown method: {method}")
93
+
94
+ return t_grid, X
95
+
96
+ def simulate_ensemble(
97
+ self,
98
+ T: float = 1.0,
99
+ n_steps: int = 252,
100
+ n_paths: int = 1000,
101
+ method: str = "euler_maruyama",
102
+ antithetic: bool = False,
103
+ ) -> np.ndarray:
104
+ """
105
+ 模拟多条路径(支持对偶变量方差缩减)
106
+
107
+ Returns:
108
+ paths: shape (n_paths, n_steps+1)
109
+ """
110
+ dt = T / n_steps
111
+ sqrt_dt = np.sqrt(dt)
112
+ paths = np.zeros((n_paths, n_steps + 1))
113
+ paths[:, 0] = self.x0
114
+
115
+ if antithetic:
116
+ half = n_paths // 2
117
+ dW_base = np.random.normal(0, sqrt_dt, (half, n_steps))
118
+ dW_all = np.vstack([dW_base, -dW_base]) # 对偶变量
119
+ else:
120
+ dW_all = np.random.normal(0, sqrt_dt, (n_paths, n_steps))
121
+
122
+ t_grid = np.linspace(0, T, n_steps + 1)
123
+
124
+ for i in range(n_steps):
125
+ x_i = paths[:, i]
126
+ t_i = t_grid[i]
127
+ mu_i = np.vectorize(self.mu_func)(x_i, t_i)
128
+ sigma_i = np.vectorize(self.sigma_func)(x_i, t_i)
129
+
130
+ if method == "euler_maruyama":
131
+ paths[:, i + 1] = x_i + mu_i * dt + sigma_i * dW_all[:, i]
132
+ elif method == "milstein":
133
+ delta = np.where(x_i != 0, np.abs(x_i) * 1e-5, 1e-5)
134
+ sig_p = np.vectorize(self.sigma_func)(x_i + delta, t_i)
135
+ sig_m = np.vectorize(self.sigma_func)(x_i - delta, t_i)
136
+ sigma_prime = (sig_p - sig_m) / (2 * delta)
137
+ corr = 0.5 * sigma_i * sigma_prime * (dW_all[:, i] ** 2 - dt)
138
+ paths[:, i + 1] = x_i + mu_i * dt + sigma_i * dW_all[:, i] + corr
139
+
140
+ return paths
141
+
142
+
143
+ class ItoCalculus:
144
+ """
145
+ 伊藤微积分工具集
146
+
147
+ 提供:
148
+ 1. 伊藤积分(离散近似)
149
+ 2. 伊藤引理(数值验证)
150
+ 3. 测度变换(Girsanov)
151
+ 4. Feynman-Kac 期望定理
152
+ """
153
+
154
+ @staticmethod
155
+ def ito_integral(
156
+ integrand: np.ndarray,
157
+ brownian_increments: np.ndarray
158
+ ) -> float:
159
+ """
160
+ 伊藤积分:∫₀ᵀ f(t)dW(t) 的离散近似
161
+
162
+ 使用左端点 Riemann-Stieltjes 求和(伊藤型):
163
+ I = Σ f(t_i) · ΔW_i
164
+
165
+ 注意:与 Stratonovich 积分的区别在于使用左端点(非中点)
166
+
167
+ Args:
168
+ integrand : f(t_i) 在各时间节点的值,shape (n,)
169
+ brownian_increments: ΔW_i = W(t_{i+1}) - W(t_i),shape (n,)
170
+
171
+ Returns:
172
+ 伊藤积分近似值
173
+ """
174
+ if len(integrand) != len(brownian_increments):
175
+ raise ValueError("integrand and brownian_increments must have same length")
176
+ return float(np.sum(integrand * brownian_increments))
177
+
178
+ @staticmethod
179
+ def ito_isometry_check(
180
+ integrand: np.ndarray,
181
+ dt: float,
182
+ n_trials: int = 10000
183
+ ) -> dict:
184
+ """
185
+ 验证伊藤等距性:E[(∫f dW)²] = E[∫f² dt] = ∫E[f²]dt
186
+
187
+ Args:
188
+ integrand: 确定性被积函数(测试用)
189
+ dt : 时间步长
190
+ n_trials : 蒙特卡罗验证次数
191
+
192
+ Returns:
193
+ {'theoretical': float, 'empirical': float, 'error_pct': float}
194
+ """
195
+ n = len(integrand)
196
+ theoretical = float(np.sum(integrand ** 2) * dt)
197
+
198
+ squared_integrals = []
199
+ for _ in range(n_trials):
200
+ dW = np.random.normal(0, np.sqrt(dt), n)
201
+ I = np.sum(integrand * dW)
202
+ squared_integrals.append(I ** 2)
203
+
204
+ empirical = float(np.mean(squared_integrals))
205
+ error_pct = abs(empirical - theoretical) / max(abs(theoretical), 1e-10) * 100
206
+
207
+ return {
208
+ "theoretical": theoretical,
209
+ "empirical": empirical,
210
+ "error_pct": error_pct,
211
+ "isometry_verified": error_pct < 5.0
212
+ }
213
+
214
+ @staticmethod
215
+ def stratonovich_to_ito(
216
+ integrand: np.ndarray,
217
+ sigma_deriv: np.ndarray,
218
+ dt: float
219
+ ) -> np.ndarray:
220
+ """
221
+ Stratonovich 积分转化为伊藤积分
222
+
223
+ 关系:∫f ∘ dW = ∫f dW + ½∫f'·f dt
224
+ 修正项 = ½·σ·(∂σ/∂X)·dt(二次变差修正)
225
+
226
+ Args:
227
+ integrand : Stratonovich 被积函数 f(t_i)
228
+ sigma_deriv: ∂σ/∂X 在各时间节点的值
229
+ dt : 时间步长
230
+
231
+ Returns:
232
+ 等价的伊藤积分被积函数
233
+ """
234
+ ito_correction = 0.5 * integrand * sigma_deriv * dt
235
+ return integrand - ito_correction
236
+
237
+
238
+ def apply_ito_lemma(
239
+ f: Callable[[float, float], float],
240
+ df_dt: Callable[[float, float], float],
241
+ df_dx: Callable[[float, float], float],
242
+ d2f_dx2: Callable[[float, float], float],
243
+ mu: Callable[[float, float], float],
244
+ sigma: Callable[[float, float], float],
245
+ x_path: np.ndarray,
246
+ t_grid: np.ndarray,
247
+ ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
248
+ """
249
+ 伊藤引理数值实现
250
+
251
+ 对伊藤过程 dX = μ(X,t)dt + σ(X,t)dW,
252
+ 函数 f(X,t) 满足:
253
+ df = [∂f/∂t + μ·∂f/∂X + ½σ²·∂²f/∂X²]dt + σ·∂f/∂X·dW
254
+ |-----------漂移项(ITO漂移)------------| |扩散项|
255
+
256
+ Args:
257
+ f : 目标函数 f(x, t)
258
+ df_dt : ∂f/∂t
259
+ df_dx : ∂f/∂X(一阶偏导)
260
+ d2f_dx2 : ∂²f/∂X²(二阶偏导)
261
+ mu : 漂移系数 μ(x, t)
262
+ sigma : 扩散系数 σ(x, t)
263
+ x_path : X 的路径,shape (n_steps+1,)
264
+ t_grid : 时间网格,shape (n_steps+1,)
265
+
266
+ Returns:
267
+ (f_path, drift_components, diffusion_components)
268
+ f_path: f(X(t)) 的路径
269
+ drift : 漂移项 dt 系数在各时间点的值
270
+ diffusion: 扩散项 dW 系数在各时间点的值
271
+
272
+ 示例(GBM 验证):
273
+ X = S(股价),f = ln(S)
274
+ μ_lnS = μ - σ²/2 ← 伊藤修正项 -σ²/2
275
+ σ_lnS = σ
276
+ → ln S(T) = ln S(0) + (μ - σ²/2)T + σW(T) ✓
277
+ """
278
+ n = len(x_path)
279
+ f_path = np.zeros(n)
280
+ drift_components = np.zeros(n)
281
+ diffusion_components = np.zeros(n)
282
+
283
+ for i in range(n):
284
+ x_i = x_path[i]
285
+ t_i = t_grid[i]
286
+ f_path[i] = f(x_i, t_i)
287
+
288
+ mu_i = mu(x_i, t_i)
289
+ sigma_i = sigma(x_i, t_i)
290
+
291
+ # 伊藤漂移:∂f/∂t + μ·∂f/∂X + ½σ²·∂²f/∂X²
292
+ drift_components[i] = (
293
+ df_dt(x_i, t_i)
294
+ + mu_i * df_dx(x_i, t_i)
295
+ + 0.5 * sigma_i ** 2 * d2f_dx2(x_i, t_i)
296
+ )
297
+ # 扩散项:σ·∂f/∂X
298
+ diffusion_components[i] = sigma_i * df_dx(x_i, t_i)
299
+
300
+ return f_path, drift_components, diffusion_components
301
+
302
+
303
+ class GirsanovTransform:
304
+ """
305
+ Girsanov 测度变换(风险中性测度)
306
+
307
+ 在风险中性世界中,股价过程从:
308
+ dS = μS dt + σS dW^P (真实世界)
309
+ 变换为:
310
+ dS = rS dt + σS dW^Q (风险中性世界)
311
+
312
+ Girsanov 密度(Radon-Nikodym 导数):
313
+ dQ/dP = exp(-θ·W(T) - ½θ²·T)
314
+ 其中 θ = (μ - r) / σ (市场价格风险)
315
+ """
316
+
317
+ def __init__(self, mu: float, r: float, sigma: float):
318
+ """
319
+ Args:
320
+ mu : 真实世界漂移率(年化)
321
+ r : 无风险利率(年化)
322
+ sigma: 波动率(年化)
323
+ """
324
+ self.mu = mu
325
+ self.r = r
326
+ self.sigma = sigma
327
+ self.theta = (mu - r) / sigma # 市场价格风险
328
+
329
+ def radon_nikodym(self, W_T: float, T: float) -> float:
330
+ """
331
+ 计算 Radon-Nikodym 导数(测度变换权重)
332
+
333
+ dQ/dP|_T = exp(-θ·W(T) - ½θ²·T)
334
+ """
335
+ return np.exp(-self.theta * W_T - 0.5 * self.theta ** 2 * T)
336
+
337
+ def real_to_risk_neutral_brownian(
338
+ self,
339
+ W_path: np.ndarray,
340
+ t_grid: np.ndarray
341
+ ) -> np.ndarray:
342
+ """
343
+ 将真实世界布朗运动 W^P 转化为风险中性布朗运动 W^Q
344
+
345
+ Girsanov 定理:W^Q(t) = W^P(t) + θ·t
346
+ """
347
+ return W_path + self.theta * t_grid
348
+
349
+ def price_option_by_measure_change(
350
+ self,
351
+ S0: float,
352
+ K: float,
353
+ T: float,
354
+ n_paths: int = 50000,
355
+ n_steps: int = 252
356
+ ) -> dict:
357
+ """
358
+ 通过 Girsanov 测度变换定价欧式看涨期权
359
+
360
+ 在 Q 测度下:S(T) = S0·exp((r - σ²/2)T + σW^Q(T))
361
+ C = e^{-rT}·E^Q[max(S(T)-K, 0)]
362
+
363
+ Returns:
364
+ {'price': float, 'delta': float, 'se': float}
365
+ """
366
+ dt = T / n_steps
367
+ sqrt_dt = np.sqrt(dt)
368
+
369
+ # 在 Q 测度下模拟(使用风险中性漂移 r - σ²/2)
370
+ dW = np.random.normal(0, sqrt_dt, (n_paths, n_steps))
371
+ log_returns = (self.r - 0.5 * self.sigma ** 2) * dt + self.sigma * dW
372
+ S_T = S0 * np.exp(np.sum(log_returns, axis=1))
373
+
374
+ payoffs = np.maximum(S_T - K, 0)
375
+ discounted = np.exp(-self.r * T) * payoffs
376
+
377
+ price = float(np.mean(discounted))
378
+ se = float(np.std(discounted) / np.sqrt(n_paths))
379
+
380
+ # 数值 Delta:ΔC/ΔS ≈ (C(S+ε) - C(S-ε)) / 2ε
381
+ eps = S0 * 0.01
382
+ payoffs_up = np.maximum(S_T * (1 + eps / S0) - K, 0)
383
+ payoffs_dn = np.maximum(S_T * (1 - eps / S0) - K, 0)
384
+ delta = float(np.exp(-self.r * T) * np.mean(payoffs_up - payoffs_dn) / (2 * eps))
385
+
386
+ return {"price": price, "delta": delta, "standard_error": se}
387
+
388
+
389
+ class FeynmanKac:
390
+ """
391
+ Feynman-Kac 定理:随机过程期望 ↔ PDE
392
+
393
+ 对于 SDE: dX = μ(X,t)dt + σ(X,t)dW,
394
+ 边值问题 u_t + μ·u_x + ½σ²·u_xx - r·u = -g(x,t)
395
+ 的解为:u(x,t) = E[∫_t^T e^{-r(s-t)} g(X_s,s)ds + e^{-r(T-t)} Φ(X_T) | X_t=x]
396
+
397
+ 应用:期权定价(Black-Scholes PDE ↔ 风险中性期望)
398
+ """
399
+
400
+ def __init__(
401
+ self,
402
+ mu: Callable,
403
+ sigma: Callable,
404
+ r: float = 0.0
405
+ ):
406
+ self.mu = mu
407
+ self.sigma = sigma
408
+ self.r = r
409
+
410
+ def solve_by_mc(
411
+ self,
412
+ x0: float,
413
+ t0: float,
414
+ T: float,
415
+ terminal_condition: Callable[[float], float],
416
+ running_cost: Optional[Callable[[float, float], float]] = None,
417
+ n_paths: int = 50000,
418
+ n_steps: int = 252,
419
+ antithetic: bool = True,
420
+ ) -> dict:
421
+ """
422
+ 用蒙特卡罗方法求解 Feynman-Kac 公式
423
+
424
+ u(x,t) ≈ (1/N) Σ [e^{-r(T-t)}·Φ(X_T^i) + ∫running_cost dt]
425
+
426
+ Args:
427
+ x0 : 初始状态
428
+ t0 : 初始时间
429
+ T : 终止时间
430
+ terminal_condition: Φ(X_T) 终端条件(如期权 payoff)
431
+ running_cost : g(X_t, t) 持续成本(默认为0)
432
+ n_paths : 路径数
433
+ n_steps : 时间步数
434
+ antithetic : 是否使用对偶变量
435
+
436
+ Returns:
437
+ {'value': float, 'se': float, 'ci_95': tuple}
438
+ """
439
+ process = ItoProcess(
440
+ mu_func=self.mu,
441
+ sigma_func=self.sigma,
442
+ x0=x0
443
+ )
444
+ tau = T - t0
445
+ paths = process.simulate_ensemble(
446
+ T=tau, n_steps=n_steps, n_paths=n_paths,
447
+ method="euler_maruyama", antithetic=antithetic
448
+ )
449
+
450
+ # 终端 payoff
451
+ X_T = paths[:, -1]
452
+ terminal_values = np.array([terminal_condition(x) for x in X_T])
453
+ discounted_terminal = np.exp(-self.r * tau) * terminal_values
454
+
455
+ # 运行成本(积分)
456
+ if running_cost is not None:
457
+ t_grid = np.linspace(t0, T, n_steps + 1)
458
+ dt = tau / n_steps
459
+ running_costs = np.zeros(n_paths)
460
+ for j in range(n_steps):
461
+ g_vals = np.array([running_cost(paths[k, j], t_grid[j]) for k in range(n_paths)])
462
+ running_costs += np.exp(-self.r * (t_grid[j] - t0)) * g_vals * dt
463
+ total_values = discounted_terminal + running_costs
464
+ else:
465
+ total_values = discounted_terminal
466
+
467
+ value = float(np.mean(total_values))
468
+ se = float(np.std(total_values) / np.sqrt(n_paths))
469
+ ci_lower = value - 1.96 * se
470
+ ci_upper = value + 1.96 * se
471
+
472
+ return {
473
+ "value": value,
474
+ "standard_error": se,
475
+ "ci_95": (ci_lower, ci_upper),
476
+ "n_paths": n_paths,
477
+ }