scientific-computing-system 1.0.0 → 2.2.0
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.
- package/CHANGELOG.md +284 -0
- package/LICENSE +0 -0
- package/README.md +89 -218
- package/SECURITY.md +0 -0
- package/bin/scs.js +12 -2
- package/index.js +115 -1
- package/package.json +45 -1
- package/.binder/postBuild +0 -9
- package/.binder/requirements.txt +0 -10
- package/.gitattributes +0 -35
- package/.github/CODEOWNERS +0 -4
- package/.github/FUNDING.yml +0 -3
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -50
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -28
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -29
- package/.github/dependabot.yml +0 -32
- package/.github/labeler.yml +0 -50
- package/.github/workflows/benchmarks.yml +0 -97
- package/.github/workflows/bot-reviewer.yml +0 -58
- package/.github/workflows/changelog.yml +0 -130
- package/.github/workflows/cli-package-smoke.yml +0 -45
- package/.github/workflows/codeql.yml +0 -55
- package/.github/workflows/npm-publish.yml +0 -33
- package/.github/workflows/p08-cancel-noncanonical-runs.yml +0 -41
- package/.github/workflows/pr-automation.yml +0 -78
- package/.github/workflows/public-external-validation-p08-v2-glm.yml +0 -161
- package/.github/workflows/public-external-validation-p08-v2-recovery-merge-gate.yml +0 -72
- package/.github/workflows/public-external-validation-p08-v2-recovery-merge.yml +0 -113
- package/.github/workflows/public-external-validation-p08-v2-run-recovery-anatomy-gate.yml +0 -57
- package/.github/workflows/public-external-validation-p08-v2-run-recovery-merge-gate.yml +0 -63
- package/.github/workflows/public-external-validation-p08-v2-run-recovery-merge.yml +0 -89
- package/.github/workflows/public-external-validation-p08-v2-run-recovery.yml +0 -182
- package/.github/workflows/public-external-validation-p08-v3-glm-gate.yml +0 -67
- package/.github/workflows/public-external-validation-p08-v4-recovery-glm-gate.yml +0 -78
- package/.github/workflows/public-external-validation-p08-v4-recovery-glm.yml +0 -248
- package/.github/workflows/public-external-validation-p08-v5-run-recovery-glm-gate.yml +0 -66
- package/.github/workflows/public-external-validation-p08-v5-run-recovery-glm.yml +0 -218
- package/.github/workflows/pypi-registry-smoke.yml +0 -116
- package/.github/workflows/release.yml +0 -359
- package/.github/workflows/tests.yml +0 -250
- package/.github/workflows/tmp-compassion-raw-acquire.yml +0 -63
- package/.github/workflows/wsl-science-runner-smoke.yml +0 -45
- package/.pre-commit-config.yaml +0 -48
- package/CITATION.cff +0 -29
- package/CODE_OF_CONDUCT.md +0 -25
- package/CONTRIBUTING.md +0 -275
- package/ROADMAP.md +0 -155
- package/assets/logo.svg +0 -38
- package/assets/social_preview.png +0 -0
- package/benchmarks/results.json +0 -43
- package/benchmarks/robust_runner.py +0 -118
- package/benchmarks/run_benchmarks.py +0 -394
- package/cliff.toml +0 -100
- package/dashboard/app.py +0 -224
- package/docs/ARCHITECTURE.md +0 -252
- package/docs/CASE_STUDY_HUBBLE.md +0 -78
- package/docs/CASE_STUDY_QUANTUM_ML.md +0 -79
- package/docs/api.md +0 -133
- package/docs/assets/icon.svg +0 -17
- package/docs/assets/logo.svg +0 -38
- package/docs/benchmarks.md +0 -62
- package/docs/cookbook.md +0 -930
- package/docs/getting-started.md +0 -185
- package/docs/index.md +0 -111
- package/docs/maintenance.md +0 -61
- package/docs/ml_reference.md +0 -92
- package/docs/research-workflows.md +0 -82
- package/docs/stylesheets/extra.css +0 -122
- package/docs/tour_of_numerical_methods.md +0 -330
- package/docs/tutorials/core_demo.md +0 -35
- package/docs/tutorials/data_analysis_demo.md +0 -43
- package/docs/tutorials/diffeq_demo.md +0 -48
- package/docs/tutorials/fft2_demo.md +0 -67
- package/docs/tutorials/graph_demo.md +0 -45
- package/docs/tutorials/hypothesis_demo.md +0 -44
- package/docs/tutorials/hypothesis_tests_demo.md +0 -170
- package/docs/tutorials/hypothesis_with_stats_demo.md +0 -80
- package/docs/tutorials/knowledge_demo.md +0 -114
- package/docs/tutorials/linalg_demo.md +0 -103
- package/docs/tutorials/math_utils_demo.md +0 -46
- package/docs/tutorials/ml_advanced_demo.md +0 -96
- package/docs/tutorials/ml_and_viz_demo.md +0 -93
- package/docs/tutorials/ml_demo.md +0 -70
- package/docs/tutorials/modeling_demo.md +0 -82
- package/docs/tutorials/montecarlo_demo.md +0 -42
- package/docs/tutorials/nlp_attention_demo.md +0 -107
- package/docs/tutorials/nlp_bpe_demo.md +0 -103
- package/docs/tutorials/nlp_mini_gpt_demo.md +0 -110
- package/docs/tutorials/nlp_viz.md +0 -76
- package/docs/tutorials/numerical_integration_demo.md +0 -65
- package/docs/tutorials/optimization_demo.md +0 -106
- package/docs/tutorials/pandas_io_demo.md +0 -160
- package/docs/tutorials/pca_demo.md +0 -87
- package/docs/tutorials/probability_demo.md +0 -48
- package/docs/tutorials/quantum_demo.md +0 -31
- package/docs/tutorials/quick_start.md +0 -44
- package/docs/tutorials/scientific_demo.md +0 -46
- package/docs/tutorials/signals_demo.md +0 -167
- package/docs/tutorials/stats_demo.md +0 -94
- package/docs/tutorials/stiff_ode_demo.md +0 -84
- package/docs/tutorials/time_series_demo.md +0 -145
- package/examples/core_demo.py +0 -40
- package/examples/data_analysis_demo.py +0 -60
- package/examples/diffeq_demo.py +0 -34
- package/examples/discovery_pipeline_demo.py +0 -57
- package/examples/ensemble_showcase.py +0 -81
- package/examples/fft2_demo.py +0 -32
- package/examples/graph_demo.py +0 -49
- package/examples/hypothesis_custom_generator.py +0 -121
- package/examples/hypothesis_demo.py +0 -48
- package/examples/hypothesis_tests_demo.py +0 -52
- package/examples/hypothesis_with_stats_demo.py +0 -43
- package/examples/interpolate_demo.py +0 -57
- package/examples/knowledge_demo.py +0 -100
- package/examples/linalg_demo.py +0 -61
- package/examples/math_utils_demo.py +0 -53
- package/examples/ml_and_viz_demo.py +0 -47
- package/examples/modeling_demo.py +0 -110
- package/examples/montecarlo_demo.py +0 -41
- package/examples/nlp_attention_demo.py +0 -168
- package/examples/nlp_bpe_demo.py +0 -79
- package/examples/nlp_mini_gpt_demo.py +0 -76
- package/examples/nlp_viz_demo.py +0 -98
- package/examples/numerical_integration_demo.py +0 -60
- package/examples/optimization_demo.py +0 -34
- package/examples/pca_demo.py +0 -124
- package/examples/pde_demo.py +0 -65
- package/examples/plot_demo.py +0 -84
- package/examples/plotting_notebook.ipynb +0 -87
- package/examples/probability_demo.py +0 -39
- package/examples/quantum_demo.py +0 -42
- package/examples/scientific_demo.py +0 -49
- package/examples/signals_demo.py +0 -86
- package/examples/stats_demo.py +0 -40
- package/examples/tour_of_numerical_methods.ipynb +0 -497
- package/mkdocs.yml +0 -159
- package/pyproject.toml +0 -288
- package/requirements-dev.lock +0 -427
- package/requirements-dev.txt +0 -39
- package/requirements.lock +0 -6
- package/requirements.txt +0 -13
- package/scripts/build_gif.py +0 -111
- package/scripts/build_video.py +0 -214
- package/scripts/cleanup_deployments.py +0 -173
- package/scripts/crop_readme_png.py +0 -40
- package/scripts/make_pi_dashboard.py +0 -316
- package/scripts/make_pi_gif.py +0 -138
- package/scripts/publish.py +0 -203
- package/scripts/render_promo.mjs +0 -93
- package/scripts/render_readme.py +0 -211
- package/scripts/render_readme_sections.py +0 -168
- package/scripts/verify_cli_install.py +0 -99
- package/scripts/verify_cookbook.py +0 -601
- package/scripts/verify_d1_numerics.py +0 -504
- package/scripts/verify_d2_edge_cases.py +0 -442
- package/scripts/verify_d4_hypothesis.py +0 -337
- package/scripts/verify_ml_reference.py +0 -160
- package/scripts/verify_pypi_release.py +0 -97
- package/scripts/verify_release_integrity.py +0 -194
- package/src/cds/__init__.py +0 -132
- package/src/cds/__main__.py +0 -8
- package/src/cds/_version.py +0 -11
- package/src/cds/bayes/__init__.py +0 -25
- package/src/cds/bayes/conjugate.py +0 -560
- package/src/cds/cli/__init__.py +0 -104
- package/src/cds/cli/__main__.py +0 -8
- package/src/cds/cli/_handlers.py +0 -323
- package/src/cds/cli/_parser.py +0 -155
- package/src/cds/cli/_style.py +0 -208
- package/src/cds/cli/_system_info.py +0 -71
- package/src/cds/core/__init__.py +0 -5
- package/src/cds/core/_numeric.py +0 -92
- package/src/cds/core/models.py +0 -166
- package/src/cds/data_analysis/__init__.py +0 -30
- package/src/cds/data_analysis/dataset.py +0 -122
- package/src/cds/data_analysis/loader.py +0 -75
- package/src/cds/data_analysis/pandas_io.py +0 -108
- package/src/cds/data_analysis/transform.py +0 -36
- package/src/cds/data_analysis/viz.py +0 -88
- package/src/cds/data_io/__init__.py +0 -27
- package/src/cds/data_io/streaming.py +0 -314
- package/src/cds/diffeq/__init__.py +0 -37
- package/src/cds/diffeq/_implicit.py +0 -349
- package/src/cds/diffeq/solvers.py +0 -312
- package/src/cds/diffeq/symplectic.py +0 -225
- package/src/cds/fractals/__init__.py +0 -34
- package/src/cds/fractals/sets.py +0 -356
- package/src/cds/genetics/__init__.py +0 -22
- package/src/cds/genetics/sequence.py +0 -199
- package/src/cds/graph/__init__.py +0 -31
- package/src/cds/graph/algorithms.py +0 -399
- package/src/cds/hypothesis/__init__.py +0 -51
- package/src/cds/hypothesis/evaluator.py +0 -311
- package/src/cds/hypothesis/generator.py +0 -275
- package/src/cds/hypothesis/mining.py +0 -314
- package/src/cds/infotheory/__init__.py +0 -25
- package/src/cds/infotheory/measures.py +0 -276
- package/src/cds/interpolate/__init__.py +0 -156
- package/src/cds/knowledge/__init__.py +0 -45
- package/src/cds/knowledge/graph.py +0 -489
- package/src/cds/knowledge/notes.py +0 -231
- package/src/cds/knowledge/retrieval.py +0 -339
- package/src/cds/math_utils/__init__.py +0 -46
- package/src/cds/math_utils/calculus.py +0 -40
- package/src/cds/math_utils/linalg.py +0 -496
- package/src/cds/math_utils/special.py +0 -167
- package/src/cds/math_utils/svd.py +0 -261
- package/src/cds/ml/__init__.py +0 -60
- package/src/cds/ml/boosting.py +0 -304
- package/src/cds/ml/clustering.py +0 -190
- package/src/cds/ml/decomposition.py +0 -172
- package/src/cds/ml/ensemble.py +0 -152
- package/src/cds/ml/linear_models.py +0 -225
- package/src/cds/ml/metrics.py +0 -227
- package/src/cds/ml/model_selection.py +0 -150
- package/src/cds/ml/naive_bayes.py +0 -179
- package/src/cds/ml/neighbors.py +0 -162
- package/src/cds/ml/neural.py +0 -204
- package/src/cds/ml/preprocessing.py +0 -131
- package/src/cds/ml/tree.py +0 -228
- package/src/cds/ml/voting.py +0 -175
- package/src/cds/modeling/__init__.py +0 -44
- package/src/cds/modeling/_base.py +0 -210
- package/src/cds/modeling/_nodes.py +0 -526
- package/src/cds/modeling/expression.py +0 -81
- package/src/cds/modeling/fitting.py +0 -477
- package/src/cds/modeling/model.py +0 -134
- package/src/cds/modeling/solver.py +0 -187
- package/src/cds/montecarlo/__init__.py +0 -24
- package/src/cds/montecarlo/markov.py +0 -112
- package/src/cds/montecarlo/methods.py +0 -294
- package/src/cds/nlp/__init__.py +0 -170
- package/src/cds/nlp/attention.py +0 -285
- package/src/cds/nlp/autograd/__init__.py +0 -70
- package/src/cds/nlp/autograd/_grad.py +0 -120
- package/src/cds/nlp/autograd/ops.py +0 -133
- package/src/cds/nlp/autograd/tensor.py +0 -298
- package/src/cds/nlp/bpe.py +0 -449
- package/src/cds/nlp/data.py +0 -70
- package/src/cds/nlp/embed.py +0 -190
- package/src/cds/nlp/layers.py +0 -259
- package/src/cds/nlp/model.py +0 -456
- package/src/cds/nlp/optim.py +0 -163
- package/src/cds/nlp/training.py +0 -167
- package/src/cds/nlp/viz.py +0 -254
- package/src/cds/numerical_integration/__init__.py +0 -31
- package/src/cds/numerical_integration/quadrature.py +0 -480
- package/src/cds/optimization/__init__.py +0 -26
- package/src/cds/optimization/_metaheuristics.py +0 -287
- package/src/cds/optimization/constrained.py +0 -212
- package/src/cds/optimization/minimize.py +0 -425
- package/src/cds/pde/__init__.py +0 -266
- package/src/cds/plot/__init__.py +0 -49
- package/src/cds/plot/_backend.py +0 -32
- package/src/cds/plot/charts.py +0 -462
- package/src/cds/probability/__init__.py +0 -65
- package/src/cds/probability/_advanced.py +0 -283
- package/src/cds/probability/distributions.py +0 -236
- package/src/cds/probability/extra_distributions.py +0 -199
- package/src/cds/provenance/__init__.py +0 -25
- package/src/cds/provenance/manifest.py +0 -355
- package/src/cds/py.typed +0 -0
- package/src/cds/quantum/__init__.py +0 -51
- package/src/cds/quantum/circuit.py +0 -122
- package/src/cds/quantum/multi_qubit.py +0 -310
- package/src/cds/quantum/simulator.py +0 -53
- package/src/cds/scientific/__init__.py +0 -34
- package/src/cds/scientific/constants.py +0 -35
- package/src/cds/scientific/formulas.py +0 -116
- package/src/cds/sensitivity.py +0 -204
- package/src/cds/signals/__init__.py +0 -59
- package/src/cds/signals/filters.py +0 -449
- package/src/cds/signals/processing.py +0 -224
- package/src/cds/signals/stft.py +0 -177
- package/src/cds/signals/wavelet.py +0 -146
- package/src/cds/stats/__init__.py +0 -99
- package/src/cds/stats/_distributions.py +0 -102
- package/src/cds/stats/bootstrap.py +0 -188
- package/src/cds/stats/descriptive.py +0 -172
- package/src/cds/stats/hypothesis_tests.py +0 -294
- package/src/cds/stats/nonparametric.py +0 -141
- package/src/cds/stats/power.py +0 -290
- package/src/cds/stats/regression.py +0 -51
- package/src/cds/stats/time_series.py +0 -420
- package/src/cds/tools/__init__.py +0 -29
- package/src/cds/tools/adapters.py +0 -218
- package/src/cds/tools/registry.py +0 -181
- package/src/cds/uncertainty/__init__.py +0 -17
- package/src/cds/uncertainty/propagation.py +0 -233
- package/src/cds/units/__init__.py +0 -45
- package/src/cds/units/core.py +0 -199
- package/src/cds/validation/__init__.py +0 -41
- package/src/cds/validation/adequacy.py +0 -157
- package/src/cds/validation/checks.py +0 -485
- package/src/cds/validation/report.py +0 -50
- package/src/cds/wavelets/__init__.py +0 -5
- package/src/cds/wavelets/haar.py +0 -219
- package/src/cds/workflow/__init__.py +0 -77
- package/src/cds/workflow/engine.py +0 -116
- package/src/cds/workflow/gates.py +0 -120
- package/src/cds/workflow/orchestrator.py +0 -431
- package/src/cds/workflow/selection.py +0 -454
- package/src/cds/workflow/tooling.py +0 -155
- package/src/cds/workflow/types.py +0 -107
- package/tests/__init__.py +0 -0
- package/tests/conftest.py +0 -109
- package/tests/test_advanced_fitting.py +0 -247
- package/tests/test_audit_completion_coverage.py +0 -183
- package/tests/test_audit_guard_coverage.py +0 -115
- package/tests/test_audit_hardening.py +0 -138
- package/tests/test_audit_hardening_coverage.py +0 -111
- package/tests/test_bayes_v2.py +0 -208
- package/tests/test_benchmarks.py +0 -111
- package/tests/test_bootstrap.py +0 -135
- package/tests/test_cli.py +0 -51
- package/tests/test_cli_color.py +0 -114
- package/tests/test_cli_extended.py +0 -239
- package/tests/test_cli_system_info.py +0 -36
- package/tests/test_constrained_opt.py +0 -174
- package/tests/test_core.py +0 -273
- package/tests/test_data_analysis.py +0 -146
- package/tests/test_data_analysis_lazy_bridge.py +0 -68
- package/tests/test_data_analysis_pandas_io.py +0 -119
- package/tests/test_data_io.py +0 -149
- package/tests/test_dataset.py +0 -91
- package/tests/test_diffeq.py +0 -186
- package/tests/test_diffeq_implicit.py +0 -173
- package/tests/test_domain_validation.py +0 -66
- package/tests/test_edge_misc_numerical.py +0 -666
- package/tests/test_edge_ml_quantum_linalg.py +0 -191
- package/tests/test_edge_nlp.py +0 -229
- package/tests/test_edge_quantum_evaluator_cli.py +0 -601
- package/tests/test_edge_stats_optimization_signals.py +0 -406
- package/tests/test_extra_distributions.py +0 -206
- package/tests/test_fft2.py +0 -67
- package/tests/test_fitting_bootstrap.py +0 -202
- package/tests/test_fractals.py +0 -387
- package/tests/test_genetics.py +0 -159
- package/tests/test_graph.py +0 -380
- package/tests/test_hardening_privacy_provenance.py +0 -306
- package/tests/test_hypothesis.py +0 -216
- package/tests/test_hypothesis_engine.py +0 -59
- package/tests/test_hypothesis_mining.py +0 -247
- package/tests/test_hypothesis_tests.py +0 -277
- package/tests/test_infotheory.py +0 -302
- package/tests/test_interpolate.py +0 -189
- package/tests/test_knowledge.py +0 -1149
- package/tests/test_layering.py +0 -112
- package/tests/test_linalg_extended.py +0 -179
- package/tests/test_linalg_qr_cholesky.py +0 -68
- package/tests/test_markov.py +0 -155
- package/tests/test_math_utils.py +0 -157
- package/tests/test_ml_boosting.py +0 -148
- package/tests/test_ml_clustering.py +0 -92
- package/tests/test_ml_ensemble.py +0 -103
- package/tests/test_ml_knn.py +0 -95
- package/tests/test_ml_linreg.py +0 -70
- package/tests/test_ml_logreg.py +0 -84
- package/tests/test_ml_metrics.py +0 -125
- package/tests/test_ml_model_selection.py +0 -108
- package/tests/test_ml_naive_bayes.py +0 -183
- package/tests/test_ml_pca.py +0 -98
- package/tests/test_ml_preprocessing.py +0 -104
- package/tests/test_ml_tree.py +0 -89
- package/tests/test_ml_viz.py +0 -58
- package/tests/test_ml_voting.py +0 -219
- package/tests/test_modeling.py +0 -726
- package/tests/test_modeling_fit_validation.py +0 -46
- package/tests/test_montecarlo.py +0 -245
- package/tests/test_multi_qubit.py +0 -466
- package/tests/test_nlp_attention.py +0 -335
- package/tests/test_nlp_autograd.py +0 -435
- package/tests/test_nlp_bpe.py +0 -463
- package/tests/test_nlp_embed.py +0 -240
- package/tests/test_nlp_layers.py +0 -276
- package/tests/test_nlp_model.py +0 -172
- package/tests/test_nlp_training.py +0 -262
- package/tests/test_nlp_viz.py +0 -188
- package/tests/test_numerical_integration.py +0 -374
- package/tests/test_numerical_invariants.py +0 -768
- package/tests/test_numerical_oracles.py +0 -241
- package/tests/test_optimization.py +0 -139
- package/tests/test_optimization_metaheuristics.py +0 -221
- package/tests/test_pde.py +0 -294
- package/tests/test_plot_charts.py +0 -247
- package/tests/test_plot_lazy.py +0 -48
- package/tests/test_power.py +0 -226
- package/tests/test_probability.py +0 -274
- package/tests/test_probability_advanced.py +0 -226
- package/tests/test_property_invariants.py +0 -277
- package/tests/test_provenance.py +0 -215
- package/tests/test_provenance_audit_hardening.py +0 -45
- package/tests/test_public_api.py +0 -75
- package/tests/test_quantum.py +0 -195
- package/tests/test_release_contract.py +0 -105
- package/tests/test_retrieval.py +0 -255
- package/tests/test_scientific.py +0 -185
- package/tests/test_selection_audit_hardening.py +0 -55
- package/tests/test_sensitivity.py +0 -59
- package/tests/test_sensitivity_global.py +0 -73
- package/tests/test_signals.py +0 -146
- package/tests/test_signals_filters.py +0 -460
- package/tests/test_special_invariants.py +0 -222
- package/tests/test_special_numerics.py +0 -44
- package/tests/test_statistical_audit.py +0 -75
- package/tests/test_stats.py +0 -206
- package/tests/test_stats_audit_coverage.py +0 -17
- package/tests/test_stats_nonparametric.py +0 -87
- package/tests/test_stats_time_series.py +0 -352
- package/tests/test_stft.py +0 -208
- package/tests/test_streaming_analytics.py +0 -128
- package/tests/test_svd.py +0 -170
- package/tests/test_symplectic.py +0 -200
- package/tests/test_tool_adapters.py +0 -197
- package/tests/test_tools_registry.py +0 -132
- package/tests/test_uncertainty.py +0 -171
- package/tests/test_units.py +0 -139
- package/tests/test_validation.py +0 -100
- package/tests/test_validation_adequacy.py +0 -129
- package/tests/test_validation_advanced.py +0 -110
- package/tests/test_wavelet.py +0 -170
- package/tests/test_wavelets_v2.py +0 -119
- package/tests/test_workflow.py +0 -173
- package/tests/test_workflow_gates.py +0 -144
- package/tests/test_workflow_orchestrator.py +0 -516
- package/tests/test_workflow_orchestrator_blocked_review.py +0 -95
- package/tests/test_workflow_selection.py +0 -312
- package/tests/test_workflow_selection_coverage.py +0 -114
- package/tests/test_workflow_tooling.py +0 -190
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
# Statistics Tutorial
|
|
2
|
-
|
|
3
|
-
`cds.stats` covers descriptive statistics, linear regression, and a set of
|
|
4
|
-
classic hypothesis tests (t-tests, ANOVA, chi-square). Tests return a
|
|
5
|
-
`TestResult` dataclass carrying the test statistic, p-value, and degrees of
|
|
6
|
-
freedom.
|
|
7
|
-
|
|
8
|
-
## 1. Descriptive statistics
|
|
9
|
-
|
|
10
|
-
```python
|
|
11
|
-
from cds.stats import mean, median, variance, stdev
|
|
12
|
-
|
|
13
|
-
data = [23.1, 27.5, 19.8, 31.2, 25.6, 28.3, 22.0, 30.1]
|
|
14
|
-
print(f"Mean={mean(data):.2f} Median={median(data):.2f}") # 25.95 26.55
|
|
15
|
-
print(f"Var={variance(data):.2f} StdDev={stdev(data):.2f}") # 16.34 4.04
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## 2. Correlation and linear regression
|
|
19
|
-
|
|
20
|
-
Pearson correlation between two samples, and ordinary least-squares regression.
|
|
21
|
-
The `RegressionResult` exposes `slope`, `intercept`, `r_squared`, and a
|
|
22
|
-
`predict(x)` helper.
|
|
23
|
-
|
|
24
|
-
```python
|
|
25
|
-
from cds.stats import correlation, linear_regression
|
|
26
|
-
|
|
27
|
-
x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
|
|
28
|
-
y = [2.1, 4.3, 5.8, 8.1, 9.7, 12.2, 13.8, 16.1]
|
|
29
|
-
|
|
30
|
-
print(f"r = {correlation(x, y):.4f}") # r = 0.9991
|
|
31
|
-
|
|
32
|
-
reg = linear_regression(x, y)
|
|
33
|
-
print(f"y = {reg.slope:.2f}x + {reg.intercept:.2f}") # y = 1.98x + 0.10
|
|
34
|
-
print(f"R² = {reg.r_squared:.4f}") # R² = 0.9981
|
|
35
|
-
print(f"predict(10) = {reg.predict(10):.2f}") # 19.90
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## 3. Hypothesis tests
|
|
39
|
-
|
|
40
|
-
All tests return a `TestResult` with `.statistic`, `.p_value`, and `.df`.
|
|
41
|
-
|
|
42
|
-
### One-sample t-test
|
|
43
|
-
|
|
44
|
-
Is the sample mean different from a hypothesized population mean?
|
|
45
|
-
|
|
46
|
-
```python
|
|
47
|
-
from cds.stats import one_sample_ttest
|
|
48
|
-
|
|
49
|
-
r = one_sample_ttest([2.1, 2.4, 1.9, 2.2, 2.0, 2.3], popmean=2.0)
|
|
50
|
-
print(f"t={r.statistic:.4f}, p={r.p_value:.4f}, df={r.df}") # t=1.9640, p=0.1067, df=5
|
|
51
|
-
# p > 0.05 → do not reject H0 (mean ≈ 2.0)
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Two-sample t-test
|
|
55
|
-
|
|
56
|
-
Do two independent samples have equal means?
|
|
57
|
-
|
|
58
|
-
```python
|
|
59
|
-
from cds.stats import two_sample_ttest
|
|
60
|
-
|
|
61
|
-
r = two_sample_ttest([1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 4.0, 5.0])
|
|
62
|
-
print(f"t={r.statistic:.4f}, p={r.p_value:.4f}") # t=-1.0954, p=0.3153
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### One-way ANOVA
|
|
66
|
-
|
|
67
|
-
Do three or more groups share the same mean? Pass each group as a positional
|
|
68
|
-
argument.
|
|
69
|
-
|
|
70
|
-
```python
|
|
71
|
-
from cds.stats import one_way_anova
|
|
72
|
-
|
|
73
|
-
r = one_way_anova([1.0, 2.0, 3.0], [2.0, 3.0, 4.0], [3.0, 4.0, 5.0])
|
|
74
|
-
print(f"F={r.statistic:.4f}, p={r.p_value:.4f}") # F=3.0000, p=0.1250
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### Chi-square tests
|
|
78
|
-
|
|
79
|
-
Goodness-of-fit (observed vs expected counts) and independence (a 2-D
|
|
80
|
-
contingency table).
|
|
81
|
-
|
|
82
|
-
```python
|
|
83
|
-
from cds.stats import chi_square_gof, chi_square_independence
|
|
84
|
-
|
|
85
|
-
r = chi_square_gof([10.0, 20.0, 30.0], [15.0, 20.0, 25.0])
|
|
86
|
-
print(f"chi2={r.statistic:.4f}, p={r.p_value:.4f}") # chi2=2.6667, p=0.2636
|
|
87
|
-
|
|
88
|
-
r = chi_square_independence([[10.0, 20.0], [30.0, 40.0]])
|
|
89
|
-
print(f"chi2={r.statistic:.4f}, p={r.p_value:.4f}") # chi2=0.7937, p=0.3730
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
Run the full demo with `python examples/stats_demo.py`.
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
# Stiff ODE Solvers Tutorial
|
|
2
|
-
|
|
3
|
-
`cds.diffeq._implicit` adds two A-stable implicit methods to the explicit
|
|
4
|
-
solvers: **backward Euler** (θ = 1) and the **trapezoidal / Crank–Nicolson
|
|
5
|
-
method** (θ = ½). Both solve their implicit stage equation with Newton
|
|
6
|
-
iteration, using an analytic Jacobian when you supply one and central
|
|
7
|
-
differences otherwise.
|
|
8
|
-
|
|
9
|
-
## 1. Why implicit? The stiff decay problem
|
|
10
|
-
|
|
11
|
-
For `dy/dt = -1000·(y − 1)` any explicit method diverges unless
|
|
12
|
-
`dt < 2/1000`. Backward Euler stays stable at the same step:
|
|
13
|
-
|
|
14
|
-
```python
|
|
15
|
-
from cds.diffeq import backward_euler
|
|
16
|
-
|
|
17
|
-
k = 1000.0
|
|
18
|
-
sol = backward_euler(lambda t, y: -k * (y - 1.0), t0=0.0, y0=0.0, t_end=0.05, dt=0.001)
|
|
19
|
-
print(f"{sol.y[-1]:.6f}") # ≈ 1.000000 — exact is 1 − e⁻⁵⁰ ≈ 1.0
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## 2. Trapezoid (Crank–Nicolson): stable AND second-order
|
|
23
|
-
|
|
24
|
-
Backward Euler buys stability with only first-order accuracy. The trapezoidal
|
|
25
|
-
rule averages the explicit and implicit slopes for O(dt²) error while staying
|
|
26
|
-
A-stable, on smooth problems its error shrinks ~4× when you halve `dt`.
|
|
27
|
-
|
|
28
|
-
```python
|
|
29
|
-
import math
|
|
30
|
-
from cds.diffeq import backward_euler, trapezoid_method
|
|
31
|
-
|
|
32
|
-
f = lambda t, y: -y
|
|
33
|
-
be = backward_euler(f, 0.0, 1.0, 2.0, dt=0.05).y[-1]
|
|
34
|
-
cn = trapezoid_method(f, 0.0, 1.0, 2.0, dt=0.05).y[-1]
|
|
35
|
-
exact = math.exp(-2.0)
|
|
36
|
-
print(abs(cn - exact) < abs(be - exact)) # True
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## 3. Systems: the harmonic oscillator
|
|
40
|
-
|
|
41
|
-
The `_system` variants take vector states and return `(t_values, y_values)`
|
|
42
|
-
just like :func:`cds.diffeq.solve_system`.
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
import math
|
|
46
|
-
from cds.diffeq import trapezoid_method_system
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def harmonic(t, y):
|
|
50
|
-
return [y[1], -y[0]] # x'' = -x as a system
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
ts, ys = trapezoid_method_system(harmonic, 0.0, [1.0, 0.0], t_end=4 * math.pi, dt=0.01)
|
|
54
|
-
amplitude = math.hypot(ys[-1][0], ys[-1][1])
|
|
55
|
-
print(abs(amplitude - 1.0) < 0.01) # True — energy nearly conserved
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## 4. Supplying an analytic Jacobian
|
|
59
|
-
|
|
60
|
-
Newton converges in one step on linear problems either way, but a Jacobian
|
|
61
|
-
removes the finite-difference evaluations:
|
|
62
|
-
|
|
63
|
-
```python
|
|
64
|
-
from cds.diffeq import backward_euler_system
|
|
65
|
-
|
|
66
|
-
jac = lambda t, y: [[-1000.0]] # df/dy
|
|
67
|
-
ts, ys = backward_euler_system(
|
|
68
|
-
lambda t, y: [-1000.0 * (y[0] - 1.0)],
|
|
69
|
-
t0=0.0,
|
|
70
|
-
y0=[0.0],
|
|
71
|
-
t_end=0.05,
|
|
72
|
-
dt=0.001,
|
|
73
|
-
jac=jac,
|
|
74
|
-
)
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Notes
|
|
78
|
-
|
|
79
|
-
- Direction follows `sign(t_end - t0)` exactly like the explicit solvers;
|
|
80
|
-
`dt` is always a magnitude.
|
|
81
|
-
- Newton failures raise `ValueError` with a clear message ("singular
|
|
82
|
-
Jacobian" / "did not converge within max_iter") instead of returning
|
|
83
|
-
garbage.
|
|
84
|
-
- Rule of thumb: use RK45 until stiffness hurts, then reach for these.
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
# Time-Series Analysis Tutorial
|
|
2
|
-
|
|
3
|
-
`cds.stats` provides a classical toolkit for ordered, equally-spaced
|
|
4
|
-
observations: autocorrelation, partial autocorrelation, moving-average
|
|
5
|
-
smoothing, differencing, a KPSS-style stationarity test, seasonal
|
|
6
|
-
decomposition, and the Ljung-Box portmanteau test. All routines are pure
|
|
7
|
-
Python with no external dependencies.
|
|
8
|
-
|
|
9
|
-
```python
|
|
10
|
-
from cds.stats import (
|
|
11
|
-
autocorrelation,
|
|
12
|
-
autocorrelation_function,
|
|
13
|
-
partial_autocorrelation,
|
|
14
|
-
moving_average,
|
|
15
|
-
exponential_smoothing,
|
|
16
|
-
difference,
|
|
17
|
-
kpss_statistic,
|
|
18
|
-
seasonal_decompose,
|
|
19
|
-
ljung_box,
|
|
20
|
-
)
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## 1. Autocorrelation (ACF)
|
|
24
|
-
|
|
25
|
-
The sample autocorrelation at lag `k` measures how correlated a series is
|
|
26
|
-
with a delayed copy of itself. A trending series shows high positive
|
|
27
|
-
autocorrelation; an alternating series shows strong negative autocorrelation.
|
|
28
|
-
|
|
29
|
-
```python
|
|
30
|
-
# A linear ramp: each point is close to its predecessor -> r_1 near 1.
|
|
31
|
-
trend = [float(i) for i in range(20)]
|
|
32
|
-
print(f"r_1 = {autocorrelation(trend, lag=1):.3f}") # ~0.95
|
|
33
|
-
print(f"r_5 = {autocorrelation(trend, lag=5):.3f}") # still high
|
|
34
|
-
|
|
35
|
-
# Full ACF for lags 0..4 (r_0 is always 1.0).
|
|
36
|
-
acf = autocorrelation_function(trend, max_lag=4)
|
|
37
|
-
print(acf) # [1.0, 0.95, 0.90, 0.85, 0.79]
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## 2. Partial autocorrelation (PACF)
|
|
41
|
-
|
|
42
|
-
The PACF isolates the *direct* correlation at each lag, removing the effect
|
|
43
|
-
of intermediate lags via the Durbin-Levinson recursion. For a pure AR(1)
|
|
44
|
-
process `x_t = 0.8·x_{t-1}`, the PACF spikes at lag 1 then cuts off to ~0,
|
|
45
|
-
the hallmark signature for choosing ARIMA order `p`.
|
|
46
|
-
|
|
47
|
-
```python
|
|
48
|
-
# Deterministic AR(1) with phi = 0.8.
|
|
49
|
-
ar1 = [1.0]
|
|
50
|
-
for _ in range(200):
|
|
51
|
-
ar1.append(0.8 * ar1[-1])
|
|
52
|
-
|
|
53
|
-
pacf = partial_autocorrelation(ar1, max_lag=4)
|
|
54
|
-
print(f"phi_11 = {pacf[1]:.3f}") # ~0.80
|
|
55
|
-
print(f"phi_22 = {pacf[2]:.3f}") # ~0.00 (cutoff)
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## 3. Smoothing: moving average & exponential smoothing
|
|
59
|
-
|
|
60
|
-
Smoothing reveals the underlying signal in noisy data. The centered moving
|
|
61
|
-
average uses a fixed window; exponential smoothing weights recent
|
|
62
|
-
observations more heavily via `alpha ∈ (0, 1]`.
|
|
63
|
-
|
|
64
|
-
```python
|
|
65
|
-
noisy = [10.0, 0.0, 10.0, 0.0, 10.0, 0.0, 10.0, 0.0]
|
|
66
|
-
|
|
67
|
-
ma = moving_average(noisy, window=2) # centered SMA
|
|
68
|
-
ses = exponential_smoothing(noisy, alpha=0.3) # weight recent points
|
|
69
|
-
print(f"SMA variance = {sum((x - 5) ** 2 for x in ma) / len(ma):.2f}")
|
|
70
|
-
print(f"SES variance = {sum((x - 5) ** 2 for x in ses) / len(ses):.2f}")
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## 4. Differencing
|
|
74
|
-
|
|
75
|
-
Differencing removes trend and seasonality, the standard Box-Jenkins
|
|
76
|
-
preprocessing step before fitting ARIMA models. `order=k` applies the
|
|
77
|
-
difference repeatedly.
|
|
78
|
-
|
|
79
|
-
```python
|
|
80
|
-
ramp = [1.0, 3.0, 5.0, 7.0, 9.0] # y = 2t + 1
|
|
81
|
-
print(difference(ramp, lag=1, order=1)) # [2.0, 2.0, 2.0, 2.0] (constant)
|
|
82
|
-
|
|
83
|
-
quad = [float(i * i) for i in range(6)] # 0,1,4,9,16,25
|
|
84
|
-
print(difference(quad, lag=1, order=2)) # [2.0, 2.0, 2.0, 2.0] (2nd diff constant)
|
|
85
|
-
|
|
86
|
-
seasonal = [1.0, 2.0, 3.0, 1.0, 2.0, 3.0] # period 3
|
|
87
|
-
print(difference(seasonal, lag=3, order=1)) # [0.0, 0.0, 0.0]
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
## 5. Stationarity test (KPSS-style)
|
|
91
|
-
|
|
92
|
-
The KPSS statistic tests the **null of stationarity** (the opposite of the
|
|
93
|
-
ADF test). A small statistic means the series looks stationary; a large one
|
|
94
|
-
suggests a unit root. `kpss_statistic` returns a heuristic decision and an
|
|
95
|
-
approximate p-value, for formal inference consult the published asymptotic
|
|
96
|
-
critical values.
|
|
97
|
-
|
|
98
|
-
```python
|
|
99
|
-
# Oscillating around a mean -> stationary.
|
|
100
|
-
stationary = [0.1, -0.2, 0.15, -0.1, 0.2, -0.15] * 4
|
|
101
|
-
res = kpss_statistic(stationary)
|
|
102
|
-
print(f"eta = {res.statistic:.4f}, stationary = {res.is_stationary}") # True
|
|
103
|
-
|
|
104
|
-
# A random walk (cumulative sum) -> non-stationary.
|
|
105
|
-
rw = [0.0]
|
|
106
|
-
for _ in range(200):
|
|
107
|
-
rw.append(rw[-1] + (1.0 if _ % 2 else -1.0) + 0.3)
|
|
108
|
-
res2 = kpss_statistic(rw)
|
|
109
|
-
print(f"eta = {res2.statistic:.4f}") # large
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
## 6. Seasonal decomposition
|
|
113
|
-
|
|
114
|
-
Classical additive decomposition splits a series into **trend + seasonal +
|
|
115
|
-
residual**. Pass the seasonal `period` (e.g. 12 for monthly data with annual
|
|
116
|
-
cycle). The seasonal component is normalized to sum to zero over one period.
|
|
117
|
-
|
|
118
|
-
```python
|
|
119
|
-
base = [10.0, 20.0, 30.0, 40.0] # one seasonal period
|
|
120
|
-
data = base * 5 # 5 full periods
|
|
121
|
-
trend, seasonal, residual = seasonal_decompose(data, period=4)
|
|
122
|
-
|
|
123
|
-
print(f"seasonal sums to ~0: {sum(seasonal[:4]):.2f}") # 0.00
|
|
124
|
-
print(f"max |residual| = {max(abs(r) for r in residual):.2f}")
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
## 7. Ljung-Box test for autocorrelation
|
|
128
|
-
|
|
129
|
-
The Ljung-Box Q statistic tests whether *any* of the first `lags`
|
|
130
|
-
autocorrelations differ from zero. A low p-value rejects independence,
|
|
131
|
-
useful for checking whether ARIMA residuals are white noise.
|
|
132
|
-
|
|
133
|
-
```python
|
|
134
|
-
# A clean trend has strong autocorrelation.
|
|
135
|
-
strong = [float(i) for i in range(100)]
|
|
136
|
-
lb = ljung_box(strong, lags=10)
|
|
137
|
-
print(f"Q = {lb.statistic:.2f}, p = {lb.p_value:.4f}")
|
|
138
|
-
print(f"autocorrelated? {lb.has_autocorrelation}") # True
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
---
|
|
142
|
-
|
|
143
|
-
These tools compose naturally: smooth a noisy signal, difference away a
|
|
144
|
-
trend, test the result for stationarity, then decompose any remaining
|
|
145
|
-
seasonality, all from `cds.stats`, no NumPy required.
|
package/examples/core_demo.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"""Core data-models demo — Domain, Hypothesis, HypothesisStatus."""
|
|
2
|
-
|
|
3
|
-
# Import directly from the defining module (not the cds.core re-export) to
|
|
4
|
-
# give static analyzers the clearest possible view of these types.
|
|
5
|
-
from cds.core.models import Domain, Hypothesis, HypothesisStatus
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def main() -> None:
|
|
9
|
-
print("=== Domains ===")
|
|
10
|
-
# __members__.values() is the Enum API's explicit member-collection view
|
|
11
|
-
# (a Mapping's ValuesView). Functionally identical to iterating the
|
|
12
|
-
# EnumType itself (same 6 members, same order), but the return type is an
|
|
13
|
-
# unambiguously iterable mapping view — CodeQL's non-iterable-in-for-loop
|
|
14
|
-
# query can resolve it where the EnumType iterator could not.
|
|
15
|
-
for d in Domain.__members__.values():
|
|
16
|
-
print(f" {d.name} = {d.value}")
|
|
17
|
-
|
|
18
|
-
print("\n=== Constructing a Hypothesis ===")
|
|
19
|
-
h = Hypothesis(
|
|
20
|
-
id="H-001",
|
|
21
|
-
domain=Domain.COSMOLOGY,
|
|
22
|
-
research_question="Why is the Hubble expansion accelerating?",
|
|
23
|
-
statement="A cosmological constant (Λ) drives late-time acceleration.",
|
|
24
|
-
status=HypothesisStatus.TESTABLE,
|
|
25
|
-
confidence=0.62,
|
|
26
|
-
rationale="Type Ia supernova data favours accelerated expansion.",
|
|
27
|
-
assumptions=["GR holds on cosmological scales", "Λ > 0"],
|
|
28
|
-
predictions=["(m-M) vs z curve bends downward vs matter-only"],
|
|
29
|
-
tags=["dark-energy", "lambda"],
|
|
30
|
-
)
|
|
31
|
-
print(repr(h))
|
|
32
|
-
print("\n--- Markdown render ---")
|
|
33
|
-
print(h.to_markdown())
|
|
34
|
-
|
|
35
|
-
print("\n--- JSON serialisation ---")
|
|
36
|
-
print(h.model_dump_json(indent=2)[:200] + " ...")
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if __name__ == "__main__":
|
|
40
|
-
main()
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"""Data analysis demo — loading, transforming, and plotting tabular data."""
|
|
2
|
-
|
|
3
|
-
import os
|
|
4
|
-
import tempfile
|
|
5
|
-
|
|
6
|
-
from cds.data_analysis import (
|
|
7
|
-
DataSet,
|
|
8
|
-
load_csv,
|
|
9
|
-
moving_average,
|
|
10
|
-
normalize,
|
|
11
|
-
plot_bar,
|
|
12
|
-
plot_line,
|
|
13
|
-
z_score,
|
|
14
|
-
)
|
|
15
|
-
from cds.data_analysis.dataset import Scalar
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def main() -> None:
|
|
19
|
-
# Build a tiny CSV in a temp file so the demo needs no fixtures.
|
|
20
|
-
csv_text = "day,temperature,sales\n1,18.0,100\n2,19.5,110\n3,22.0,140\n4,21.5,135\n5,23.0,160\n"
|
|
21
|
-
path = os.path.join(tempfile.gettempdir(), "_cds_demo.csv")
|
|
22
|
-
with open(path, "w", encoding="utf-8") as f:
|
|
23
|
-
f.write(csv_text)
|
|
24
|
-
|
|
25
|
-
print("=== Loading CSV ===")
|
|
26
|
-
table = load_csv(path)
|
|
27
|
-
print(f"headers: {table.headers}")
|
|
28
|
-
print(f"rows: {len(table.rows)}")
|
|
29
|
-
|
|
30
|
-
# Extract numeric columns (DataTable stores everything as strings).
|
|
31
|
-
temps = [float(row[1]) for row in table.rows]
|
|
32
|
-
sales = [float(row[2]) for row in table.rows]
|
|
33
|
-
|
|
34
|
-
print("\n=== Normalisation ===")
|
|
35
|
-
norm = normalize(temps)
|
|
36
|
-
z = z_score(temps)
|
|
37
|
-
print(f"normalized temps: {[round(v, 3) for v in norm]}")
|
|
38
|
-
print(f"z-score temps : {[round(v, 3) for v in z]}")
|
|
39
|
-
|
|
40
|
-
print("\n=== Smoothing ===")
|
|
41
|
-
ma = moving_average(sales, window=2)
|
|
42
|
-
print(f"2-day moving avg of sales: {[round(v, 3) for v in ma]}")
|
|
43
|
-
|
|
44
|
-
print("\n=== ASCII Visualisation ===")
|
|
45
|
-
plot_line(temps, title="temperature")
|
|
46
|
-
plot_bar({"Mon": 100.0, "Tue": 110.0, "Wed": 140.0, "Thu": 135.0, "Fri": 160.0})
|
|
47
|
-
|
|
48
|
-
print("\n=== DataSet helper ===")
|
|
49
|
-
# DataSet expects a list of row-dicts; annotate so mypy widens the
|
|
50
|
-
# value type to Scalar (int | float | str | bool | None) instead of
|
|
51
|
-
# narrowing it to float, which would clash with the invariant list.
|
|
52
|
-
data: list[dict[str, Scalar]] = [{"day": i, "temp": float(t)} for i, t in enumerate(temps)]
|
|
53
|
-
ds = DataSet(data=data)
|
|
54
|
-
print(f"columns: {ds.columns}")
|
|
55
|
-
print(f"shape: {ds.shape}")
|
|
56
|
-
print(ds.head(3))
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if __name__ == "__main__":
|
|
60
|
-
main()
|
package/examples/diffeq_demo.py
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"""Ordinary differential equation solver demo."""
|
|
2
|
-
|
|
3
|
-
from cds.diffeq import euler_method, midpoint_method, rk4, rk45, solve_system
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def main() -> None:
|
|
7
|
-
# dy/dt = -y -> exact solution y(t) = e^-t, so y(1) = e^-1 ≈ 0.3679
|
|
8
|
-
f = lambda t, y: -y # noqa: E731
|
|
9
|
-
t0, y0, t_end = 0.0, 1.0, 1.0
|
|
10
|
-
|
|
11
|
-
print("=== Solving dy/dt = -y, y(0)=1, evaluate at t=1 (exact = e^-1 ≈ 0.3679) ===")
|
|
12
|
-
sol_euler = euler_method(f, t0=t0, y0=y0, t_end=t_end, dt=0.01)
|
|
13
|
-
sol_mid = midpoint_method(f, t0=t0, y0=y0, t_end=t_end, dt=0.01)
|
|
14
|
-
sol_rk4 = rk4(f, t0=t0, y0=y0, t_end=t_end, dt=0.1)
|
|
15
|
-
sol_rk45 = rk45(f, t0=t0, y0=y0, t_end=t_end, rtol=1e-6)
|
|
16
|
-
print(f"Euler (dt=0.01): {sol_euler.y[-1]:.6f}")
|
|
17
|
-
print(f"Midpoint(dt=0.01): {sol_mid.y[-1]:.6f}")
|
|
18
|
-
print(f"RK4 (dt=0.1) : {sol_rk4.y[-1]:.6f}")
|
|
19
|
-
print(f"RK45 adaptive : {sol_rk45.y[-1]:.6f}")
|
|
20
|
-
|
|
21
|
-
print("\n=== System: predator-prey (Lotka-Volterra) ===")
|
|
22
|
-
alpha, beta, gamma, delta = 1.1, 0.4, 0.4, 0.1
|
|
23
|
-
|
|
24
|
-
def lotka(t: float, state: list[float]) -> list[float]:
|
|
25
|
-
x, y = state
|
|
26
|
-
return [alpha * x - beta * x * y, delta * x * y - gamma * y]
|
|
27
|
-
|
|
28
|
-
ts, ys = solve_system(lotka, t0=0.0, y0=[10.0, 5.0], t_end=15.0, dt=0.05)
|
|
29
|
-
print(f"Final prey/predator state: {ys[-1]}")
|
|
30
|
-
print(f"Trajectory length: {len(ts)} points")
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if __name__ == "__main__":
|
|
34
|
-
main()
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"""Discovery pipeline demo — mine correlation hypotheses, then evaluate one statistically."""
|
|
2
|
-
|
|
3
|
-
import random
|
|
4
|
-
|
|
5
|
-
from cds.hypothesis import EvaluationData, HypothesisEvaluator, mine_correlations
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def main() -> None:
|
|
9
|
-
# --- Synthetic dataset: one strong relationship plus pure-noise columns ---
|
|
10
|
-
rng = random.Random(42)
|
|
11
|
-
n_rows = 120
|
|
12
|
-
dose = [rng.uniform(0.0, 10.0) for _ in range(n_rows)]
|
|
13
|
-
response = [2.0 * d + rng.gauss(0.0, 1.5) for d in dose]
|
|
14
|
-
data: dict[str, list[float]] = {
|
|
15
|
-
"dose": dose,
|
|
16
|
-
"response": response,
|
|
17
|
-
"lab_temperature": [rng.gauss(21.0, 1.0) for _ in range(n_rows)],
|
|
18
|
-
"instrument_drift": [rng.gauss(0.0, 2.0) for _ in range(n_rows)],
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
print("=== Mining correlation hypotheses ===")
|
|
22
|
-
mined = mine_correlations(data)
|
|
23
|
-
print(f"{len(mined)} hypothesis(ies) survived the |r| >= 0.3 and p < 0.05 filters:\n")
|
|
24
|
-
for hit in mined:
|
|
25
|
-
print(
|
|
26
|
-
f"[{hit.strength}] {hit.feature_a} ~ {hit.feature_b}: "
|
|
27
|
-
f"r={hit.correlation:+.4f}, p={hit.p_value:.3e}"
|
|
28
|
-
)
|
|
29
|
-
print(f" {hit.hypothesis.statement}")
|
|
30
|
-
print(f" confidence: {hit.hypothesis.confidence:.2f}\n")
|
|
31
|
-
|
|
32
|
-
# --- Evaluate the strongest mined hypothesis ---
|
|
33
|
-
print("=== Evaluating the strongest mined hypothesis ===")
|
|
34
|
-
print("The evaluator runs classical tests (t-test/ANOVA/chi-square), so the")
|
|
35
|
-
print("association is translated into a group comparison: mean response below")
|
|
36
|
-
print("vs above the median dose.")
|
|
37
|
-
top = mined[0]
|
|
38
|
-
doses = data[top.feature_a]
|
|
39
|
-
responses = data[top.feature_b]
|
|
40
|
-
median_dose = sorted(doses)[len(doses) // 2]
|
|
41
|
-
low_group = [r for d, r in zip(doses, responses) if d <= median_dose]
|
|
42
|
-
high_group = [r for d, r in zip(doses, responses) if d > median_dose]
|
|
43
|
-
|
|
44
|
-
evaluator = HypothesisEvaluator(alpha=0.05)
|
|
45
|
-
payload: EvaluationData = {"groups": [low_group, high_group]}
|
|
46
|
-
result = evaluator.evaluate(top.hypothesis, payload)
|
|
47
|
-
print(f"Test : {result.test_name}")
|
|
48
|
-
print(f"Statistic : {result.statistic:.4f}")
|
|
49
|
-
print(f"p-value : {result.p_value:.3e}")
|
|
50
|
-
print(f"Effect size : {result.effect_size_label} = {result.effect_size:.3f}")
|
|
51
|
-
print(f"Significant : {result.is_significant}")
|
|
52
|
-
print(f"Conclusion : {result.conclusion}")
|
|
53
|
-
print(f"Hypothesis status after evaluation: {top.hypothesis.status.value}")
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if __name__ == "__main__":
|
|
57
|
-
main()
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
"""Ensemble showcase — forest, boosting, naive Bayes and voting on seeded blobs."""
|
|
2
|
-
|
|
3
|
-
import random
|
|
4
|
-
|
|
5
|
-
from cds.ml import (
|
|
6
|
-
GaussianNaiveBayes,
|
|
7
|
-
GradientBoostingClassifier,
|
|
8
|
-
RandomForestClassifier,
|
|
9
|
-
accuracy,
|
|
10
|
-
)
|
|
11
|
-
from cds.ml.model_selection import SupervisedModel
|
|
12
|
-
from cds.ml.voting import VotingClassifier
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def make_dataset(n_per_class: int, seed: int) -> tuple[list[list[float]], list[str]]:
|
|
16
|
-
"""Two Gaussian blobs in 2-D: class A around (-1, -1), class B around (1, 1)."""
|
|
17
|
-
rng = random.Random(seed)
|
|
18
|
-
X: list[list[float]] = []
|
|
19
|
-
y: list[str] = []
|
|
20
|
-
for _ in range(n_per_class):
|
|
21
|
-
X.append([rng.gauss(-1.0, 0.9), rng.gauss(-1.0, 0.9)])
|
|
22
|
-
y.append("A")
|
|
23
|
-
X.append([rng.gauss(1.0, 0.9), rng.gauss(1.0, 0.9)])
|
|
24
|
-
y.append("B")
|
|
25
|
-
return X, y
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def report(name: str, model: SupervisedModel, X_test: list[list[float]], y_test: list[str]) -> None:
|
|
29
|
-
y_pred = [model.predict(row) for row in X_test]
|
|
30
|
-
acc = accuracy(y_test, y_pred)
|
|
31
|
-
print(f" {name:<28} accuracy = {acc:.3f}")
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def main() -> None:
|
|
35
|
-
X, y = make_dataset(n_per_class=60, seed=42)
|
|
36
|
-
|
|
37
|
-
indices = list(range(len(X)))
|
|
38
|
-
random.Random(7).shuffle(indices)
|
|
39
|
-
split = 80
|
|
40
|
-
train_idx = indices[:split]
|
|
41
|
-
test_idx = indices[split:]
|
|
42
|
-
X_train = [X[i] for i in train_idx]
|
|
43
|
-
y_train = [y[i] for i in train_idx]
|
|
44
|
-
X_test = [X[i] for i in test_idx]
|
|
45
|
-
y_test = [y[i] for i in test_idx]
|
|
46
|
-
|
|
47
|
-
print(f"=== Ensemble showcase on {len(X_train)} train / {len(X_test)} test samples ===\n")
|
|
48
|
-
print("Individual models:")
|
|
49
|
-
|
|
50
|
-
forest = RandomForestClassifier(n_trees=30, max_depth=4, seed=42)
|
|
51
|
-
boost = GradientBoostingClassifier(n_estimators=40, learning_rate=0.3, max_depth=2)
|
|
52
|
-
bayes = GaussianNaiveBayes()
|
|
53
|
-
individuals: list[tuple[str, SupervisedModel]] = [
|
|
54
|
-
("RandomForest", forest),
|
|
55
|
-
("GradientBoosting", boost),
|
|
56
|
-
("GaussianNaiveBayes", bayes),
|
|
57
|
-
]
|
|
58
|
-
for name, model in individuals:
|
|
59
|
-
model.fit(X_train, y_train)
|
|
60
|
-
report(name, model, X_test, y_test)
|
|
61
|
-
|
|
62
|
-
# --- Voting ensembles over fresh members with the same hyperparameters ---
|
|
63
|
-
print("\nVoting ensembles:")
|
|
64
|
-
hard_members: list[tuple[str, SupervisedModel]] = [
|
|
65
|
-
("forest", RandomForestClassifier(n_trees=30, max_depth=4, seed=42)),
|
|
66
|
-
("boost", GradientBoostingClassifier(n_estimators=40, learning_rate=0.3, max_depth=2)),
|
|
67
|
-
("bayes", GaussianNaiveBayes()),
|
|
68
|
-
]
|
|
69
|
-
soft_members: list[tuple[str, SupervisedModel]] = [
|
|
70
|
-
("forest", RandomForestClassifier(n_trees=30, max_depth=4, seed=42)),
|
|
71
|
-
("boost", GradientBoostingClassifier(n_estimators=40, learning_rate=0.3, max_depth=2)),
|
|
72
|
-
("bayes", GaussianNaiveBayes()),
|
|
73
|
-
]
|
|
74
|
-
hard_vote = VotingClassifier(hard_members, voting="hard").fit(X_train, y_train)
|
|
75
|
-
soft_vote = VotingClassifier(soft_members, voting="soft").fit(X_train, y_train)
|
|
76
|
-
report("VotingClassifier (hard)", hard_vote, X_test, y_test)
|
|
77
|
-
report("VotingClassifier (soft)", soft_vote, X_test, y_test)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if __name__ == "__main__":
|
|
81
|
-
main()
|
package/examples/fft2_demo.py
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"""2-D FFT demo (row-column Cooley-Tukey)."""
|
|
2
|
-
|
|
3
|
-
from cds.signals import fft2, ifft2
|
|
4
|
-
|
|
5
|
-
# A small 4x4 "image"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def main() -> None:
|
|
9
|
-
image: list[list[float | complex]] = [
|
|
10
|
-
[1.0, 2.0, 3.0, 4.0],
|
|
11
|
-
[2.0, 3.0, 4.0, 5.0],
|
|
12
|
-
[3.0, 4.0, 5.0, 6.0],
|
|
13
|
-
[4.0, 5.0, 6.0, 7.0],
|
|
14
|
-
]
|
|
15
|
-
|
|
16
|
-
print("=== 2-D FFT ===")
|
|
17
|
-
spectrum = fft2(image)
|
|
18
|
-
print("DC component (sum of all pixels):")
|
|
19
|
-
print(f" F[0][0] = {spectrum[0][0].real:.1f} (expected {sum(sum(r) for r in image):.1f})")
|
|
20
|
-
|
|
21
|
-
print("\nMagnitude spectrum |F[u][v]|:")
|
|
22
|
-
for row in spectrum:
|
|
23
|
-
print(" " + " ".join(f"{abs(x):6.2f}" for x in row))
|
|
24
|
-
|
|
25
|
-
print("\n=== Inverse 2-D FFT (round-trip) ===")
|
|
26
|
-
recovered = ifft2(spectrum)
|
|
27
|
-
max_err = max(abs(recovered[i][j].real - image[i][j]) for i in range(4) for j in range(4))
|
|
28
|
-
print(f"Max reconstruction error: {max_err:.2e}")
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if __name__ == "__main__":
|
|
32
|
-
main()
|
package/examples/graph_demo.py
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"""Graph algorithms demo — BFS/DFS, Dijkstra, Kruskal MST, topo sort, cycle detection."""
|
|
2
|
-
|
|
3
|
-
from cds.graph import (
|
|
4
|
-
Graph,
|
|
5
|
-
bfs,
|
|
6
|
-
dfs,
|
|
7
|
-
dijkstra,
|
|
8
|
-
has_cycle,
|
|
9
|
-
kruskal_mst,
|
|
10
|
-
topological_sort,
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def main() -> None:
|
|
15
|
-
# Undirected weighted graph (integer nodes).
|
|
16
|
-
g = Graph(n_vertices=6, directed=False)
|
|
17
|
-
edges = [(0, 1, 4), (0, 2, 2), (1, 2, 1), (1, 3, 5), (2, 3, 8), (2, 4, 10), (3, 4, 2)]
|
|
18
|
-
for u, v, w in edges:
|
|
19
|
-
g.add_edge(u, v, weight=w)
|
|
20
|
-
|
|
21
|
-
print("=== Traversal ===")
|
|
22
|
-
print(f"BFS from 0: {bfs(g, 0)}")
|
|
23
|
-
print(f"DFS from 0: {dfs(g, 0)}")
|
|
24
|
-
|
|
25
|
-
print("\n=== Shortest Paths (Dijkstra) ===")
|
|
26
|
-
dists, prev = dijkstra(g, 0)
|
|
27
|
-
for node in sorted(dists):
|
|
28
|
-
print(f" dist(0 -> {node}) = {dists[node]:.1f}")
|
|
29
|
-
|
|
30
|
-
print("\n=== Minimum Spanning Tree (Kruskal) ===")
|
|
31
|
-
mst_edges, total = kruskal_mst(g)
|
|
32
|
-
for e in mst_edges:
|
|
33
|
-
print(f" {e.src} -- {e.dst} (w={e.weight})")
|
|
34
|
-
print(f" MST total weight = {total}")
|
|
35
|
-
|
|
36
|
-
print("\n=== Cycle Detection (undirected) ===")
|
|
37
|
-
print(f"has_cycle: {has_cycle(g)}")
|
|
38
|
-
|
|
39
|
-
print("\n=== Topological Sort (DAG) ===")
|
|
40
|
-
dag = Graph(n_vertices=3, directed=True)
|
|
41
|
-
dag.add_edge(0, 1, weight=1)
|
|
42
|
-
dag.add_edge(1, 2, weight=1)
|
|
43
|
-
dag.add_edge(0, 2, weight=1)
|
|
44
|
-
order = topological_sort(dag)
|
|
45
|
-
print(f"order: {order}")
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if __name__ == "__main__":
|
|
49
|
-
main()
|