1660 lines
28 KiB
Markdown
1660 lines
28 KiB
Markdown
可以,而且结合你现在的定位,我建议把它直接做成一个真正能长期积累的项目,而不是“一堆比赛代码的文件夹”。
|
||||
|
|
|
|||
|
|
我查了 GitHub 上目前比较相关的项目后,发现有几类东西特别值得借鉴:
|
|||
|
|
|
|||
|
|
* **MCM-Resource**:最值得参考它的“按建模问题分类”的方式。它把优化、评价、预测、分类、微分方程、统计、模拟、辅助分析分开,同时还配了测试、类型检查、CI,这个思路非常适合你的工具箱。([GitHub][1])
|
|||
|
|
* **MathModelHub / CQULeaf 的 MCM-ICM 资源库**:值得参考“代码 + 历年题 + 数据分析 + 模板 + 比赛工作区”的整体组织方式。([GitHub][2])
|
|||
|
|
* **FelixYuan-YF/MCM-ICM**:适合拿来观察哪些传统模型在中文数学建模圈里被频繁整理成 Python 模块。([GitHub][3])
|
|||
|
|
* **SALib**:你尤其应该学习它的敏感性分析 API,包括 Sobol、Morris 等方法。([GitHub][4])
|
|||
|
|
* **NetworkX**:适合参考 ICM D 的网络算法 API 设计,比如最短路径、遍历、中心性等。([GitHub][5])
|
|||
|
|
* **statsmodels**:适合参考统计建模、回归、时间序列和检验的组织方法。([GitHub][6])
|
|||
|
|
* **pymoo**:适合参考优化问题的统一接口、结果对象、多目标优化和收敛分析。([GitHub][7])
|
|||
|
|
* **SymPy**:适合参考“解析数学 + 数值数学”如何统一,尤其是方程、矩阵、ODE 等求解。([GitHub][8])
|
|||
|
|
|
|||
|
|
我不建议你把这些库重新造一遍。**你的 mcm_toolkit 应该做“竞赛层封装 + 高频算法自己的简化实现 + 统一结果接口”,而不是替代 SciPy/sklearn/NetworkX。**
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 一、我建议你的第一版直接采用这个结构
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
mcm-toolkit/
|
|||
|
|
│
|
|||
|
|
├── pyproject.toml
|
|||
|
|
├── README.md
|
|||
|
|
├── LICENSE
|
|||
|
|
├── CHANGELOG.md
|
|||
|
|
│
|
|||
|
|
├── src/
|
|||
|
|
│ └── mcm_toolkit/
|
|||
|
|
│ │
|
|||
|
|
│ ├── __init__.py
|
|||
|
|
│ ├── _version.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── core/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── types.py
|
|||
|
|
│ │ ├── result.py
|
|||
|
|
│ │ ├── validation.py
|
|||
|
|
│ │ └── random.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── data/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── io.py
|
|||
|
|
│ │ ├── clean.py
|
|||
|
|
│ │ ├── transform.py
|
|||
|
|
│ │ └── sampling.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── statistics/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── descriptive.py
|
|||
|
|
│ │ ├── correlation.py
|
|||
|
|
│ │ ├── inference.py
|
|||
|
|
│ │ ├── distribution.py
|
|||
|
|
│ │ └── bootstrap.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── regression/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── linear.py
|
|||
|
|
│ │ ├── nonlinear.py
|
|||
|
|
│ │ ├── polynomial.py
|
|||
|
|
│ │ └── timeseries.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── ml/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── preprocessing.py
|
|||
|
|
│ │ ├── regression.py
|
|||
|
|
│ │ ├── classification.py
|
|||
|
|
│ │ ├── clustering.py
|
|||
|
|
│ │ ├── dimensionality.py
|
|||
|
|
│ │ └── validation.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── optimization/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── continuous.py
|
|||
|
|
│ │ ├── linear.py
|
|||
|
|
│ │ ├── integer.py
|
|||
|
|
│ │ ├── dynamic.py
|
|||
|
|
│ │ ├── heuristic.py
|
|||
|
|
│ │ └── multiobjective.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── simulation/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── monte_carlo.py
|
|||
|
|
│ │ ├── stochastic.py
|
|||
|
|
│ │ ├── markov.py
|
|||
|
|
│ │ ├── random_walk.py
|
|||
|
|
│ │ └── queueing.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── dynamics/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── ode.py
|
|||
|
|
│ │ ├── difference.py
|
|||
|
|
│ │ ├── equilibrium.py
|
|||
|
|
│ │ └── stability.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── network/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── graph.py
|
|||
|
|
│ │ ├── path.py
|
|||
|
|
│ │ ├── centrality.py
|
|||
|
|
│ │ ├── flow.py
|
|||
|
|
│ │ └── community.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── decision/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── normalize.py
|
|||
|
|
│ │ ├── weighting.py
|
|||
|
|
│ │ ├── ahp.py
|
|||
|
|
│ │ ├── topsis.py
|
|||
|
|
│ │ ├── entropy.py
|
|||
|
|
│ │ ├── grey.py
|
|||
|
|
│ │ └── dea.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── sensitivity/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── local.py
|
|||
|
|
│ │ ├── global_.py
|
|||
|
|
│ │ ├── robustness.py
|
|||
|
|
│ │ └── scenario.py
|
|||
|
|
│ │
|
|||
|
|
│ ├── visualization/
|
|||
|
|
│ │ ├── __init__.py
|
|||
|
|
│ │ ├── basic.py
|
|||
|
|
│ │ ├── statistical.py
|
|||
|
|
│ │ ├── model.py
|
|||
|
|
│ │ ├── sensitivity.py
|
|||
|
|
│ │ └── network.py
|
|||
|
|
│ │
|
|||
|
|
│ └── utils/
|
|||
|
|
│ ├── __init__.py
|
|||
|
|
│ ├── timing.py
|
|||
|
|
│ ├── metrics.py
|
|||
|
|
│ └── export.py
|
|||
|
|
│
|
|||
|
|
├── tests/
|
|||
|
|
│ ├── test_statistics.py
|
|||
|
|
│ ├── test_regression.py
|
|||
|
|
│ ├── test_optimization.py
|
|||
|
|
│ ├── test_simulation.py
|
|||
|
|
│ ├── test_network.py
|
|||
|
|
│ └── test_sensitivity.py
|
|||
|
|
│
|
|||
|
|
├── examples/
|
|||
|
|
│ ├── monte_carlo.py
|
|||
|
|
│ ├── regression.py
|
|||
|
|
│ ├── ode.py
|
|||
|
|
│ ├── optimization.py
|
|||
|
|
│ └── network.py
|
|||
|
|
│
|
|||
|
|
└── docs/
|
|||
|
|
├── algorithms/
|
|||
|
|
├── tutorials/
|
|||
|
|
└── api/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这个结构其实就是把 GitHub 上 MCM-Resource 的“模型知识分类”、成熟科学库的模块化设计和你自己的“比赛快速调用”结合起来。([GitHub][1])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二、核心层 `core`
|
|||
|
|
|
|||
|
|
这些不是数学模型,但必须有。
|
|||
|
|
|
|||
|
|
### `core/random.py`
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
set_seed(seed: int) -> None
|
|||
|
|
get_rng(seed: int | None = None) -> np.random.Generator
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### `core/validation.py`
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
check_array(x, ndim=None, finite=True)
|
|||
|
|
check_same_length(*arrays)
|
|||
|
|
check_probability(p)
|
|||
|
|
check_bounds(x, lower, upper)
|
|||
|
|
check_square_matrix(A)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### `core/types.py`
|
|||
|
|
|
|||
|
|
建议定义:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
ArrayLike
|
|||
|
|
Scalar
|
|||
|
|
Objective
|
|||
|
|
Constraint
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### `core/result.py`
|
|||
|
|
|
|||
|
|
建议做统一结果对象:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
FitResult
|
|||
|
|
OptimizationResult
|
|||
|
|
SimulationResult
|
|||
|
|
SensitivityResult
|
|||
|
|
ForecastResult
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
例如:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
result.params
|
|||
|
|
result.metrics
|
|||
|
|
result.predictions
|
|||
|
|
result.success
|
|||
|
|
result.message
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这个设计非常重要。
|
|||
|
|
|
|||
|
|
你以后不要让每个函数返回完全不同的东西。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 三、数据处理 `data`
|
|||
|
|
|
|||
|
|
这是你比赛最经常碰到的一层。
|
|||
|
|
|
|||
|
|
## `data/io.py`
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
load_csv(path)
|
|||
|
|
load_excel(path, sheet_name=0)
|
|||
|
|
save_csv(data, path)
|
|||
|
|
save_excel(data, path)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## `data/clean.py`
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
drop_missing(data, axis=0, threshold=None)
|
|||
|
|
|
|||
|
|
impute_missing(
|
|||
|
|
data,
|
|||
|
|
method="mean",
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
remove_duplicates(data)
|
|||
|
|
|
|||
|
|
detect_outliers(
|
|||
|
|
data,
|
|||
|
|
method="iqr",
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
winsorize(
|
|||
|
|
data,
|
|||
|
|
lower=0.01,
|
|||
|
|
upper=0.99,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## `data/transform.py`
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
normalize(x, axis=0)
|
|||
|
|
standardize(x, axis=0)
|
|||
|
|
|
|||
|
|
minmax_scale(
|
|||
|
|
x,
|
|||
|
|
feature_range=(0, 1),
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
log_transform(x)
|
|||
|
|
boxcox_transform(x)
|
|||
|
|
rank_transform(x)
|
|||
|
|
|
|||
|
|
one_hot_encode(data, columns)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## `data/sampling.py`
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
train_test_split(...)
|
|||
|
|
kfold_split(...)
|
|||
|
|
stratified_split(...)
|
|||
|
|
bootstrap_sample(...)
|
|||
|
|
sample_without_replacement(...)
|
|||
|
|
sample_with_replacement(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 四、统计学 `statistics`
|
|||
|
|
|
|||
|
|
这应该是你的 **S 级模块**。
|
|||
|
|
|
|||
|
|
## 描述统计
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
describe(x)
|
|||
|
|
weighted_mean(x, weights)
|
|||
|
|
weighted_variance(x, weights)
|
|||
|
|
variance(x)
|
|||
|
|
std(x)
|
|||
|
|
coefficient_of_variation(x)
|
|||
|
|
quantile(x, q)
|
|||
|
|
iqr(x)
|
|||
|
|
zscore(x)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 相关分析
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
covariance(x, y)
|
|||
|
|
correlation(x, y, method="pearson")
|
|||
|
|
spearman_correlation(x, y)
|
|||
|
|
kendall_correlation(x, y)
|
|||
|
|
correlation_matrix(X)
|
|||
|
|
partial_correlation(X, y)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 离群值
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
iqr_outliers(x, k=1.5)
|
|||
|
|
zscore_outliers(x, threshold=3.0)
|
|||
|
|
modified_zscore_outliers(x)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 统计推断
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
confidence_interval(
|
|||
|
|
x,
|
|||
|
|
confidence=0.95,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
mean_test(...)
|
|||
|
|
proportion_test(...)
|
|||
|
|
chi_square_test(...)
|
|||
|
|
t_test(...)
|
|||
|
|
anova(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 分布
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
fit_distribution(x, dist="normal")
|
|||
|
|
distribution_pdf(...)
|
|||
|
|
distribution_cdf(...)
|
|||
|
|
distribution_ppf(...)
|
|||
|
|
sample_distribution(...)
|
|||
|
|
goodness_of_fit(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 五、Bootstrap
|
|||
|
|
|
|||
|
|
这个建议你**自己认真实现一遍**。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
bootstrap_sample(
|
|||
|
|
x,
|
|||
|
|
n_resamples=10000,
|
|||
|
|
rng=None,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
bootstrap_statistic(
|
|||
|
|
x,
|
|||
|
|
statistic,
|
|||
|
|
n_resamples=10000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
bootstrap_ci(
|
|||
|
|
x,
|
|||
|
|
statistic=np.mean,
|
|||
|
|
confidence=0.95,
|
|||
|
|
n_resamples=10000,
|
|||
|
|
method="percentile",
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
以后你甚至可以:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
result = bootstrap_ci(
|
|||
|
|
data,
|
|||
|
|
statistic=np.median,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 六、回归 `regression`
|
|||
|
|
|
|||
|
|
## 线性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
fit_linear(X, y)
|
|||
|
|
|
|||
|
|
fit_weighted_linear(X, y, weights)
|
|||
|
|
|
|||
|
|
fit_ridge(X, y, alpha)
|
|||
|
|
|
|||
|
|
predict_linear(model, X)
|
|||
|
|
|
|||
|
|
linear_coefficients(model)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 多项式
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
fit_polynomial(X, y, degree=2)
|
|||
|
|
predict_polynomial(model, X)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 非线性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
fit_nonlinear(
|
|||
|
|
func,
|
|||
|
|
X,
|
|||
|
|
y,
|
|||
|
|
p0,
|
|||
|
|
bounds=None,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
curve_fit(
|
|||
|
|
func,
|
|||
|
|
x,
|
|||
|
|
y,
|
|||
|
|
p0=None,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 模型评估
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
rmse(y_true, y_pred)
|
|||
|
|
mae(y_true, y_pred)
|
|||
|
|
mape(y_true, y_pred)
|
|||
|
|
smape(y_true, y_pred)
|
|||
|
|
r2_score(y_true, y_pred)
|
|||
|
|
adjusted_r2(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 残差
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
residuals(y_true, y_pred)
|
|||
|
|
residual_statistics(...)
|
|||
|
|
residual_autocorrelation(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 七、时间序列 `regression/timeseries.py`
|
|||
|
|
|
|||
|
|
MCM C 特别值得准备。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
lag_features(series, lags)
|
|||
|
|
|
|||
|
|
moving_average(series, window)
|
|||
|
|
|
|||
|
|
weighted_moving_average(series, weights)
|
|||
|
|
|
|||
|
|
exponential_smoothing(
|
|||
|
|
series,
|
|||
|
|
alpha,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
holt_trend(
|
|||
|
|
series,
|
|||
|
|
alpha,
|
|||
|
|
beta,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
seasonal_decompose(series, period)
|
|||
|
|
|
|||
|
|
fit_arima(
|
|||
|
|
series,
|
|||
|
|
order=(p, d, q),
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
forecast(model, steps)
|
|||
|
|
|
|||
|
|
forecast_interval(model, alpha=0.05)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
statsmodels 本身已经有大量成熟的 AR/ARIMA/状态空间/指数平滑能力,所以这里建议做竞赛友好的薄封装,而不要自己重新实现整套时间序列框架。([GitHub][6])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 八、机器学习 `ml`
|
|||
|
|
|
|||
|
|
这里要控制规模。
|
|||
|
|
|
|||
|
|
## 预处理
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
scale_features(X, method="standard")
|
|||
|
|
select_features(X, y, method="correlation")
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 回归
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
fit_linear_regression(X, y)
|
|||
|
|
fit_ridge_regression(X, y)
|
|||
|
|
fit_random_forest_regressor(X, y)
|
|||
|
|
fit_gradient_boosting(X, y)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 分类
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
fit_logistic(X, y)
|
|||
|
|
fit_svm(X, y)
|
|||
|
|
fit_random_forest_classifier(X, y)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 聚类
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
kmeans(X, n_clusters)
|
|||
|
|
dbscan(X, eps, min_samples)
|
|||
|
|
hierarchical_clustering(X, n_clusters)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 降维
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
pca(X, n_components)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 验证
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
cross_validate(model, X, y, cv=5)
|
|||
|
|
grid_search(model, param_grid, X, y)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 指标
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
classification_metrics(...)
|
|||
|
|
clustering_metrics(...)
|
|||
|
|
regression_metrics(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
scikit-learn 的一个很值得你模仿的地方,就是把“模型、预测、验证、指标”组织成一套一致的接口,而不是每个算法自己搞一套参数和返回值。([GitHub][9])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 九、优化 `optimization`
|
|||
|
|
|
|||
|
|
这是你们队伍里数学手和你会大量合作的地方。
|
|||
|
|
|
|||
|
|
## 连续优化
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
minimize(
|
|||
|
|
objective,
|
|||
|
|
x0,
|
|||
|
|
bounds=None,
|
|||
|
|
constraints=None,
|
|||
|
|
method="auto",
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
maximize(
|
|||
|
|
objective,
|
|||
|
|
x0,
|
|||
|
|
bounds=None,
|
|||
|
|
constraints=None,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
底层直接调用 SciPy。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十、线性规划
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
linear_program(
|
|||
|
|
c,
|
|||
|
|
A_ub=None,
|
|||
|
|
b_ub=None,
|
|||
|
|
A_eq=None,
|
|||
|
|
b_eq=None,
|
|||
|
|
bounds=None,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
返回:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
OptimizationResult(
|
|||
|
|
x=...,
|
|||
|
|
objective=...,
|
|||
|
|
success=True,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十一、整数规划
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
integer_program(
|
|||
|
|
c,
|
|||
|
|
integrality,
|
|||
|
|
A_ub=None,
|
|||
|
|
b_ub=None,
|
|||
|
|
A_eq=None,
|
|||
|
|
b_eq=None,
|
|||
|
|
bounds=None,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十二、动态规划
|
|||
|
|
|
|||
|
|
这个建议你**一定自己做**。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
dynamic_programming(
|
|||
|
|
states,
|
|||
|
|
actions,
|
|||
|
|
transition,
|
|||
|
|
reward,
|
|||
|
|
horizon,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
value_iteration(
|
|||
|
|
states,
|
|||
|
|
actions,
|
|||
|
|
transition,
|
|||
|
|
reward,
|
|||
|
|
gamma=0.95,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
policy_iteration(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
再做几个非常通用的竞赛版本:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
knapsack_dp(...)
|
|||
|
|
shortest_path_dp(...)
|
|||
|
|
sequence_dp(...)
|
|||
|
|
resource_allocation_dp(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十三、启发式算法
|
|||
|
|
|
|||
|
|
这个模块非常适合你使用 AI 辅助开发。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
greedy_search(...)
|
|||
|
|
local_search(...)
|
|||
|
|
simulated_annealing(...)
|
|||
|
|
genetic_algorithm(...)
|
|||
|
|
particle_swarm(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
但这里我建议:
|
|||
|
|
|
|||
|
|
**第一版只自己实现 `greedy_search`、`local_search`、`simulated_annealing`、简化版 `genetic_algorithm`。**
|
|||
|
|
|
|||
|
|
PSO/NSGA-II 等复杂算法直接使用成熟库。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十四、多目标优化
|
|||
|
|
|
|||
|
|
这个模块直接借鉴 pymoo 的思想。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
weighted_sum(
|
|||
|
|
objectives,
|
|||
|
|
weights,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
pareto_front(
|
|||
|
|
solutions,
|
|||
|
|
objectives,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
pareto_filter(points)
|
|||
|
|
|
|||
|
|
crowding_distance(points)
|
|||
|
|
|
|||
|
|
hypervolume(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
如果题目真的需要 NSGA-II:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from pymoo...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
直接调用。
|
|||
|
|
|
|||
|
|
pymoo 本身已经把单目标、多目标、约束、算法、可视化和收敛分析组织成比较完整的优化框架,因此你的工具箱没必要重复造轮子。([GitHub][7])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十五、Monte Carlo `simulation`
|
|||
|
|
|
|||
|
|
**这是你的核心模块之一。**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
monte_carlo(
|
|||
|
|
sampler,
|
|||
|
|
simulator,
|
|||
|
|
n=10000,
|
|||
|
|
rng=None,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
monte_carlo_expectation(
|
|||
|
|
func,
|
|||
|
|
sampler,
|
|||
|
|
n=10000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
monte_carlo_probability(
|
|||
|
|
event,
|
|||
|
|
sampler,
|
|||
|
|
n=10000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
monte_carlo_integral(
|
|||
|
|
func,
|
|||
|
|
bounds,
|
|||
|
|
n=10000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
monte_carlo_ci(
|
|||
|
|
values,
|
|||
|
|
confidence=0.95,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
还可以:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
parameter_sweep(...)
|
|||
|
|
random_parameter_sweep(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十六、Markov
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
markov_simulate(
|
|||
|
|
transition_matrix,
|
|||
|
|
initial_state,
|
|||
|
|
steps,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
markov_steady_state(
|
|||
|
|
transition_matrix,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
markov_absorption(
|
|||
|
|
transition_matrix,
|
|||
|
|
absorbing_states,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
markov_expected_hitting_time(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十七、随机过程
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
random_walk(
|
|||
|
|
n_steps,
|
|||
|
|
p=0.5,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
brownian_motion(
|
|||
|
|
n_steps,
|
|||
|
|
dt,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
geometric_brownian_motion(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这些在金融、扩散、人口、风险等题中都可能派上用场。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十八、排队论
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
mm1_metrics(
|
|||
|
|
arrival_rate,
|
|||
|
|
service_rate,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
mmc_metrics(
|
|||
|
|
arrival_rate,
|
|||
|
|
service_rate,
|
|||
|
|
servers,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
simulate_queue(
|
|||
|
|
arrival_process,
|
|||
|
|
service_process,
|
|||
|
|
servers=1,
|
|||
|
|
horizon=1000,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 十九、微分方程 `dynamics`
|
|||
|
|
|
|||
|
|
你虽然不一定主攻 MCM A,但这个模块还是值得建立。
|
|||
|
|
|
|||
|
|
## ODE
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
solve_ode(
|
|||
|
|
derivative,
|
|||
|
|
t_span,
|
|||
|
|
y0,
|
|||
|
|
t_eval=None,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
solve_system(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 差分方程
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
solve_difference_equation(
|
|||
|
|
update,
|
|||
|
|
x0,
|
|||
|
|
steps,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 平衡点
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
find_equilibrium(
|
|||
|
|
f,
|
|||
|
|
x0,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
equilibrium_points(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 稳定性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
jacobian(f, x0)
|
|||
|
|
|
|||
|
|
eigenvalues(A)
|
|||
|
|
|
|||
|
|
local_stability(
|
|||
|
|
jacobian_matrix,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
stability_classification(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
SymPy 对方程、矩阵和 ODE 的解析/数值求解本身已经非常成熟,因此这个模块建议做“竞赛接口”,而不是再造 CAS。([GitHub][8])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十、网络 `network`
|
|||
|
|
|
|||
|
|
这部分对 ICM D 很重要。
|
|||
|
|
|
|||
|
|
## 图构建
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
build_graph(
|
|||
|
|
edges,
|
|||
|
|
directed=False,
|
|||
|
|
weighted=True,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
adjacency_matrix(G)
|
|||
|
|
|
|||
|
|
incidence_matrix(G)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 路径
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
shortest_path(G, source, target)
|
|||
|
|
|
|||
|
|
shortest_path_length(G, source, target)
|
|||
|
|
|
|||
|
|
all_pairs_shortest_path(G)
|
|||
|
|
|
|||
|
|
minimum_spanning_tree(G)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 中心性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
degree_centrality(G)
|
|||
|
|
|
|||
|
|
betweenness_centrality(G)
|
|||
|
|
|
|||
|
|
closeness_centrality(G)
|
|||
|
|
|
|||
|
|
eigenvector_centrality(G)
|
|||
|
|
|
|||
|
|
pagerank(G)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 流
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
max_flow(G, source, sink)
|
|||
|
|
|
|||
|
|
min_cut(G, source, sink)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 网络结构
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
connected_components(G)
|
|||
|
|
|
|||
|
|
density(G)
|
|||
|
|
|
|||
|
|
clustering_coefficient(G)
|
|||
|
|
|
|||
|
|
average_path_length(G)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 社区
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
community_detection(G)
|
|||
|
|
|
|||
|
|
modularity(G, communities)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这些正好对应 NetworkX 已提供的很多核心图算法类型。([GitHub][5])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十一、评价决策 `decision`
|
|||
|
|
|
|||
|
|
这个模块我建议保留,但优先级低于统计/优化/模拟。
|
|||
|
|
|
|||
|
|
## 标准化
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
benefit_normalize(X)
|
|||
|
|
cost_normalize(X)
|
|||
|
|
vector_normalize(X)
|
|||
|
|
zscore_normalize(X)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 权重
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
equal_weights(n)
|
|||
|
|
|
|||
|
|
entropy_weights(X)
|
|||
|
|
|
|||
|
|
ahp_weights(pairwise_matrix)
|
|||
|
|
|
|||
|
|
critic_weights(X)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 综合评价
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
topsis(
|
|||
|
|
X,
|
|||
|
|
weights=None,
|
|||
|
|
benefit_columns=None,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
grey_relational_analysis(X, reference)
|
|||
|
|
|
|||
|
|
fuzzy_comprehensive_evaluation(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## DEA
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
dea_efficiency(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十二、AHP
|
|||
|
|
|
|||
|
|
如果做就做成比较简单的:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
ahp_weights(
|
|||
|
|
matrix,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
ahp_consistency_ratio(
|
|||
|
|
matrix,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十三、灰色模型
|
|||
|
|
|
|||
|
|
虽然它更常见于中文建模赛事,但做成一个很小的模块没问题。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
gm11_fit(
|
|||
|
|
series,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
gm11_predict(
|
|||
|
|
model,
|
|||
|
|
steps,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
gm11_fit_predict(
|
|||
|
|
series,
|
|||
|
|
steps,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十四、敏感性分析
|
|||
|
|
|
|||
|
|
这个模块我建议直接做成你的招牌模块。
|
|||
|
|
|
|||
|
|
## 局部敏感性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
finite_difference_sensitivity(
|
|||
|
|
model,
|
|||
|
|
params,
|
|||
|
|
step=1e-4,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
local_sensitivity(
|
|||
|
|
model,
|
|||
|
|
params,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
elasticity(
|
|||
|
|
model,
|
|||
|
|
params,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## OAT
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
one_at_a_time(
|
|||
|
|
model,
|
|||
|
|
parameter,
|
|||
|
|
values,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 参数扫描
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
parameter_sweep(
|
|||
|
|
model,
|
|||
|
|
parameter_ranges,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
grid_sweep(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 鲁棒性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
robustness_test(
|
|||
|
|
model,
|
|||
|
|
scenarios,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
perturbation_test(
|
|||
|
|
model,
|
|||
|
|
parameters,
|
|||
|
|
perturbation=0.1,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 全局敏感性
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
sobol_sample(...)
|
|||
|
|
sobol_analyze(...)
|
|||
|
|
|
|||
|
|
morris_sample(...)
|
|||
|
|
morris_analyze(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这部分非常建议直接参考 SALib 的思路,而不是自己发明接口。SALib 已经实现了 Sobol、Morris、FAST 等经典方法。([GitHub][4])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十五、可视化 `visualization`
|
|||
|
|
|
|||
|
|
你最后一定会非常高频使用。
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
plot_line(...)
|
|||
|
|
plot_multi_line(...)
|
|||
|
|
|
|||
|
|
plot_scatter(...)
|
|||
|
|
plot_regression(...)
|
|||
|
|
|
|||
|
|
plot_histogram(...)
|
|||
|
|
plot_boxplot(...)
|
|||
|
|
|
|||
|
|
plot_heatmap(...)
|
|||
|
|
plot_correlation_matrix(...)
|
|||
|
|
|
|||
|
|
plot_residuals(...)
|
|||
|
|
plot_prediction_interval(...)
|
|||
|
|
|
|||
|
|
plot_convergence(...)
|
|||
|
|
plot_parameter_sensitivity(...)
|
|||
|
|
|
|||
|
|
plot_monte_carlo_distribution(...)
|
|||
|
|
|
|||
|
|
plot_network(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
还有一个非常有用的:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
save_figure(
|
|||
|
|
fig,
|
|||
|
|
path,
|
|||
|
|
dpi=300,
|
|||
|
|
formats=("png", "pdf"),
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十六、模型验证
|
|||
|
|
|
|||
|
|
我非常建议单独建立:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
analysis/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
或者把它放到 `utils/metrics.py`。
|
|||
|
|
|
|||
|
|
最基本:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
rmse(...)
|
|||
|
|
mae(...)
|
|||
|
|
mape(...)
|
|||
|
|
smape(...)
|
|||
|
|
r2_score(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
然后:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
cross_validation(...)
|
|||
|
|
residual_analysis(...)
|
|||
|
|
error_analysis(...)
|
|||
|
|
prediction_interval(...)
|
|||
|
|
confidence_interval(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
再做:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
compare_models(
|
|||
|
|
models,
|
|||
|
|
X,
|
|||
|
|
y,
|
|||
|
|
metrics=("rmse", "mae", "r2"),
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这个函数比赛时特别好用。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十七、最终我建议你再加入一个 `model_compare`
|
|||
|
|
|
|||
|
|
比如:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
result = compare_models(
|
|||
|
|
{
|
|||
|
|
"linear": linear_model,
|
|||
|
|
"polynomial": polynomial_model,
|
|||
|
|
"random_forest": rf_model,
|
|||
|
|
},
|
|||
|
|
X,
|
|||
|
|
y,
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
结果自动返回:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
RMSE MAE R²
|
|||
|
|
linear ...
|
|||
|
|
poly ...
|
|||
|
|
randomforest ...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这样你们数学手提出:
|
|||
|
|
|
|||
|
|
> “我们要不要用复杂模型?”
|
|||
|
|
|
|||
|
|
你直接跑一次。
|
|||
|
|
|
|||
|
|
这比争论半小时有效。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十八、最关键的:不要所有函数都自己写
|
|||
|
|
|
|||
|
|
我建议你把整个项目里的函数分成三个等级。
|
|||
|
|
|
|||
|
|
## 🟢 A:强烈建议自己实现
|
|||
|
|
|
|||
|
|
这些最适合你通过 AI 辅助学习:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
bootstrap
|
|||
|
|
Monte Carlo
|
|||
|
|
random walk
|
|||
|
|
Markov
|
|||
|
|
dynamic programming
|
|||
|
|
greedy
|
|||
|
|
local search
|
|||
|
|
simulated annealing
|
|||
|
|
basic genetic algorithm
|
|||
|
|
finite difference
|
|||
|
|
sensitivity analysis
|
|||
|
|
parameter sweep
|
|||
|
|
normalization
|
|||
|
|
TOPSIS
|
|||
|
|
entropy weighting
|
|||
|
|
AHP consistency
|
|||
|
|
GM(1,1)
|
|||
|
|
模型评价
|
|||
|
|
模型比较
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这些实现之后,你会真正理解算法。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🟡 B:自己封装,底层调用成熟库
|
|||
|
|
|
|||
|
|
例如:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
linear regression
|
|||
|
|
nonlinear optimization
|
|||
|
|
ODE
|
|||
|
|
ARIMA
|
|||
|
|
SVM
|
|||
|
|
Random Forest
|
|||
|
|
KMeans
|
|||
|
|
PCA
|
|||
|
|
linear programming
|
|||
|
|
integer programming
|
|||
|
|
graph algorithms
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
即:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def linear_program(...):
|
|||
|
|
return scipy.optimize.linprog(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
你学习的是:
|
|||
|
|
|
|||
|
|
> **怎样把成熟工具变成符合 MCM 工作流的接口。**
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🔴 C:不要自己造轮子
|
|||
|
|
|
|||
|
|
例如:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
完整 Transformer
|
|||
|
|
完整 PyTorch training framework
|
|||
|
|
完整 NSGA-II
|
|||
|
|
完整 NetworkX
|
|||
|
|
完整 statsmodels
|
|||
|
|
完整 SciPy optimizer
|
|||
|
|
完整 symbolic algebra system
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
直接调用成熟库。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 二十九、你的 `__init__.py` 应该怎么设计
|
|||
|
|
|
|||
|
|
我建议不是:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from .statistics import *
|
|||
|
|
from .optimization import *
|
|||
|
|
from .simulation import *
|
|||
|
|
...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
全部塞进顶层。
|
|||
|
|
|
|||
|
|
而是:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from . import data
|
|||
|
|
from . import statistics
|
|||
|
|
from . import regression
|
|||
|
|
from . import optimization
|
|||
|
|
from . import simulation
|
|||
|
|
from . import dynamics
|
|||
|
|
from . import network
|
|||
|
|
from . import sensitivity
|
|||
|
|
from . import visualization
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
然后再提供少量最高频快捷函数:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from .simulation import monte_carlo
|
|||
|
|
from .statistics import bootstrap_ci
|
|||
|
|
from .optimization import minimize
|
|||
|
|
from .sensitivity import parameter_sweep
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
于是比赛的时候你可以:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
import mcm_toolkit as mcm
|
|||
|
|
|
|||
|
|
mcm.monte_carlo(...)
|
|||
|
|
mcm.bootstrap_ci(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
也可以:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from mcm_toolkit import statistics
|
|||
|
|
statistics.correlation_matrix(...)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这样既方便,又不会污染 namespace。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 三十、`__all__` 也可以保留
|
|||
|
|
|
|||
|
|
例如:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
__all__ = [
|
|||
|
|
"monte_carlo",
|
|||
|
|
"bootstrap_ci",
|
|||
|
|
"minimize",
|
|||
|
|
"solve_ode",
|
|||
|
|
"parameter_sweep",
|
|||
|
|
"topsis",
|
|||
|
|
"pareto_front",
|
|||
|
|
]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
注意:
|
|||
|
|
|
|||
|
|
**`__all__` 是“公开 API 清单”,不是“把所有东西导出来的按钮”。**
|
|||
|
|
|
|||
|
|
这一点你从第一版就养成习惯,后面会舒服很多。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 三十一、依赖不要一上来装几十个
|
|||
|
|
|
|||
|
|
我建议:
|
|||
|
|
|
|||
|
|
```toml
|
|||
|
|
[project]
|
|||
|
|
dependencies = [
|
|||
|
|
"numpy",
|
|||
|
|
"scipy",
|
|||
|
|
"pandas",
|
|||
|
|
"matplotlib",
|
|||
|
|
]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
然后 extras:
|
|||
|
|
|
|||
|
|
```toml
|
|||
|
|
[project.optional-dependencies]
|
|||
|
|
|
|||
|
|
ml = [
|
|||
|
|
"scikit-learn",
|
|||
|
|
"statsmodels",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
network = [
|
|||
|
|
"networkx",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
sensitivity = [
|
|||
|
|
"SALib",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
symbolic = [
|
|||
|
|
"sympy",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
advanced-optimization = [
|
|||
|
|
"pymoo",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
excel = [
|
|||
|
|
"openpyxl",
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
dev = [
|
|||
|
|
"pytest",
|
|||
|
|
"ruff",
|
|||
|
|
"pyright",
|
|||
|
|
]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这样非常符合你之前强调的**轻量化**思路,而且结构上也参考了 MCM-Resource 对 core / algorithm / optional dependencies 的分层。([GitHub][1])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 三十二、我甚至建议你的项目不要只有 `mcm_toolkit`
|
|||
|
|
|
|||
|
|
最终 GitHub 仓库可以:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
mcm-toolkit/
|
|||
|
|
│
|
|||
|
|
├── src/
|
|||
|
|
│ └── mcm_toolkit/
|
|||
|
|
│
|
|||
|
|
├── examples/
|
|||
|
|
├── tests/
|
|||
|
|
├── docs/
|
|||
|
|
│
|
|||
|
|
├── problems/
|
|||
|
|
│ ├── 2025_C/
|
|||
|
|
│ ├── 2025_D/
|
|||
|
|
│ └── ...
|
|||
|
|
│
|
|||
|
|
└── notebooks/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这样你每做一道题,就沉淀一次:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
题目
|
|||
|
|
↓
|
|||
|
|
发现某个模型
|
|||
|
|
↓
|
|||
|
|
实现通用函数
|
|||
|
|
↓
|
|||
|
|
写 test
|
|||
|
|
↓
|
|||
|
|
加入 toolkit
|
|||
|
|
↓
|
|||
|
|
在 examples 写一个例子
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**一年之后,你真正拥有的不是 20 道题的代码,而是一套自己的建模计算框架。**
|
|||
|
|
|
|||
|
|
这也是我觉得现在 GitHub 上这些 MCM 资源项目里,最值得你吸收的思想:把“历年题解”进一步抽象成“模型 + 算法 + 可复用代码 + 测试 + 文档”。([GitHub][1])
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# 三十三、我会把你的 v0.1 最终压缩成这套核心 API
|
|||
|
|
|
|||
|
|
如果只看**第一版必须完成的 60 个左右函数**,我建议就是:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
数据
|
|||
|
|
├── load_csv
|
|||
|
|
├── load_excel
|
|||
|
|
├── clean_missing
|
|||
|
|
├── impute_missing
|
|||
|
|
├── detect_outliers
|
|||
|
|
├── normalize
|
|||
|
|
├── standardize
|
|||
|
|
└── train_test_split
|
|||
|
|
|
|||
|
|
统计
|
|||
|
|
├── describe
|
|||
|
|
├── correlation
|
|||
|
|
├── correlation_matrix
|
|||
|
|
├── zscore
|
|||
|
|
├── confidence_interval
|
|||
|
|
├── t_test
|
|||
|
|
├── anova
|
|||
|
|
├── fit_distribution
|
|||
|
|
├── bootstrap_sample
|
|||
|
|
└── bootstrap_ci
|
|||
|
|
|
|||
|
|
回归
|
|||
|
|
├── fit_linear
|
|||
|
|
├── fit_polynomial
|
|||
|
|
├── fit_nonlinear
|
|||
|
|
├── predict
|
|||
|
|
├── rmse
|
|||
|
|
├── mae
|
|||
|
|
├── mape
|
|||
|
|
└── r2_score
|
|||
|
|
|
|||
|
|
优化
|
|||
|
|
├── minimize
|
|||
|
|
├── linear_program
|
|||
|
|
├── integer_program
|
|||
|
|
├── dynamic_programming
|
|||
|
|
├── greedy_search
|
|||
|
|
├── local_search
|
|||
|
|
├── simulated_annealing
|
|||
|
|
├── genetic_algorithm
|
|||
|
|
├── pareto_front
|
|||
|
|
└── weighted_sum
|
|||
|
|
|
|||
|
|
模拟
|
|||
|
|
├── monte_carlo
|
|||
|
|
├── monte_carlo_probability
|
|||
|
|
├── monte_carlo_integral
|
|||
|
|
├── random_walk
|
|||
|
|
├── markov_simulate
|
|||
|
|
├── markov_steady_state
|
|||
|
|
├── mm1_metrics
|
|||
|
|
└── parameter_sweep
|
|||
|
|
|
|||
|
|
动力学
|
|||
|
|
├── solve_ode
|
|||
|
|
├── solve_difference_equation
|
|||
|
|
├── find_equilibrium
|
|||
|
|
├── jacobian
|
|||
|
|
└── local_stability
|
|||
|
|
|
|||
|
|
网络
|
|||
|
|
├── build_graph
|
|||
|
|
├── shortest_path
|
|||
|
|
├── minimum_spanning_tree
|
|||
|
|
├── degree_centrality
|
|||
|
|
├── betweenness_centrality
|
|||
|
|
├── closeness_centrality
|
|||
|
|
├── pagerank
|
|||
|
|
└── max_flow
|
|||
|
|
|
|||
|
|
评价
|
|||
|
|
├── entropy_weights
|
|||
|
|
├── ahp_weights
|
|||
|
|
├── topsis
|
|||
|
|
├── grey_relational_analysis
|
|||
|
|
└── dea_efficiency
|
|||
|
|
|
|||
|
|
敏感性
|
|||
|
|
├── local_sensitivity
|
|||
|
|
├── finite_difference_sensitivity
|
|||
|
|
├── one_at_a_time
|
|||
|
|
├── parameter_sweep
|
|||
|
|
├── robustness_test
|
|||
|
|
├── sobol_analyze
|
|||
|
|
└── morris_analyze
|
|||
|
|
|
|||
|
|
可视化
|
|||
|
|
├── plot_line
|
|||
|
|
├── plot_scatter
|
|||
|
|
├── plot_histogram
|
|||
|
|
├── plot_boxplot
|
|||
|
|
├── plot_heatmap
|
|||
|
|
├── plot_correlation_matrix
|
|||
|
|
├── plot_residuals
|
|||
|
|
├── plot_convergence
|
|||
|
|
└── plot_sensitivity
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这套东西已经足够构成一个**相当完整的 MCM/ICM 编程手工具箱**。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 最后一个我认为非常适合你的设计
|
|||
|
|
|
|||
|
|
你之前说的:
|
|||
|
|
|
|||
|
|
> “让 AI 帮我写模块,然后我读 AI 的代码继续学习。”
|
|||
|
|
|
|||
|
|
我建议正式把它写进开发规范。
|
|||
|
|
|
|||
|
|
每一个函数都强制要求:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
1. 数学定义
|
|||
|
|
2. 适用场景
|
|||
|
|
3. 参数说明
|
|||
|
|
4. Python 实现
|
|||
|
|
5. 最小示例
|
|||
|
|
6. 单元测试
|
|||
|
|
7. 时间复杂度
|
|||
|
|
8. 数值稳定性说明
|
|||
|
|
9. 常见错误
|
|||
|
|
10. MCM/ICM 使用案例
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
例如你让 Codex/Claude Code 帮你写 `monte_carlo()`,不要只得到:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def monte_carlo(...):
|
|||
|
|
...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
而是得到:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
monte_carlo
|
|||
|
|
├── 数学原理
|
|||
|
|
├── 实现
|
|||
|
|
├── test
|
|||
|
|
├── example
|
|||
|
|
└── MCM案例
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这样你每开发一个函数,就相当于**同时完成一次算法学习 + 一次 Python 训练 + 一次竞赛知识积累**。
|
|||
|
|
|
|||
|
|
而且从你现在的情况看,我会优先让你开始写:
|
|||
|
|
|
|||
|
|
**`statistics → regression → simulation → optimization → sensitivity → network`**
|
|||
|
|
|
|||
|
|
这六块最能形成你作为“编程手”的核心能力。对于你们这种**数学竞赛建模手 + 英语写作手 + 计算/AI 编程手**的三人组合,这个方向比继续单独刷 PyTorch 更有价值。
|
|||
|
|
|
|||
|
|
[1]: https://github.com/dreamnight16/MCM-Resource "GitHub - dreamnight16/MCM-Resource: 数学建模资源库 (MCM Resource) — 8大类模型 × 27个算法 × 4个赛事指南 · GitHub"
|
|||
|
|
[2]: https://github.com/Juccy-12/MathModelHub "GitHub - Juccy-12/MathModelHub: 美赛/ICM综合准备资源库。包含可复用的数学建模Python代码、LaTeX/Word模板、数据分析笔记本和历年赛题组织。旨在优化竞赛流程,促进可复现研究。A comprehensive toolkit for MCM/ICM preparation. Features reusable Python code for mathematical modeling, LaTeX/Word templates, data analysis notebooks, and organized past problems. Aimed at streamlining the contest workflow and fostering reproducible research. · GitHub"
|
|||
|
|
[3]: https://github.com/FelixYuan-YF/MCM-ICM "GitHub - FelixYuan-YF/MCM-ICM: 美赛常用模型的Python实现,以及历年Outstanding获奖文章 · GitHub"
|
|||
|
|
[4]: https://github.com/salib/salib?utm_source=chatgpt.com "GitHub - SALib/SALib: Sensitivity Analysis Library in Python. Contains Sobol, Morris, FAST, and other methods. · GitHub"
|
|||
|
|
[5]: https://github.com/networkx/networkx/blob/main/doc/reference/introduction.md?utm_source=chatgpt.com "networkx/doc/reference/introduction.md at main · networkx/networkx · GitHub"
|
|||
|
|
[6]: https://github.com/statsmodels/statsmodels?utm_source=chatgpt.com "GitHub - statsmodels/statsmodels: Statsmodels: statistical modeling and econometrics in Python · GitHub"
|
|||
|
|
[7]: https://github.com/anyoptimization/pymoo/blob/main/docs/source/index.rst?utm_source=chatgpt.com "pymoo/docs/source/index.rst at main · anyoptimization/pymoo · GitHub"
|
|||
|
|
[8]: https://github.com/sympy/sympy/blob/master/doc/src/guides/solving/index.md?utm_source=chatgpt.com "sympy/doc/src/guides/solving/index.md at master · sympy/sympy · GitHub"
|
|||
|
|
[9]: https://github.com/scikit-learn/scikit-learn/blob/main/doc/modules/model_evaluation.rst?utm_source=chatgpt.com "scikit-learn/doc/modules/model_evaluation.rst at main · scikit-learn/scikit-learn · GitHub"
|