Volume-aware reaction networks, stochastic paths and closed finite chemical master equations
moon add jqlssg/moonkinetics@0.1.0import { "jqlssg/moonkinetics" @k }
pkgtype(kind: "executable")///|
fn main raise {
let net = @k.Network::new(
["A", "B"],
[
@k.Reaction::new([1, 0], [0, 1], 2.0),
@k.Reaction::new([0, 1], [1, 0], 1.0),
],
volume=20.0,
)
let path = net.simulate([20, 0], 2.0, seed=2026)
for state in path.count_states() {
assert_eq(state[0] + state[1], 20)
}
let replay = @k.path_from_json(net, path.to_json().stringify())
assert_eq(replay.count_states(), path.count_states())
println(path.to_json().stringify())
println(net.log_likelihood(path))
}moon run cmd/biochemical --target js
moon run cmd/population --target js
moon run cmd/epidemic --target js| 能力 | 入口 | 返回与约束 |
|---|---|---|
| 模型 | Reaction::new, Network::new, with_rate | 校验、复制数组;内部字段私有,getter 返回副本 |
| 计量 | stoichiometry, enabled, fire, orders | 整数计数;fire 先校验,不修改调用者数组 |
| 单位转换 | concentrations, rounded_counts | 计数/V;反向显式四舍五入,不宣称可逆 |
| 动力学 | fluxes, drift, propensities, count_drift | 浓度与计数语义不混用 |
| 局部分析 | jacobian, diffusion, conservation_basis | 零浓度安全解析导数;S diag(a) Sᵀ;浮点消元守恒基,非整数证明 |
| 确定性基线 | integrate | 固定最大步长 RK4;负 stage、时钟不前进、预算耗尽均报错 |
| 随机路径 | simulate, Path::from_events, sample | Direct Gillespie;带通道事件重放;右连续阶梯采样 |
| 观测/估计 | log_likelihood, path_score, event_counts, integrated_propensities | 全观测、通道已知路径;包含末尾无事件生存项,score 需各常数>0 |
| 多路径 | ensemble, Moments | Welford 均值/样本协方差、并行合并、标准误;失败不部分更新 |
| 有限概率 | generator, Generator::evolve, mean_counts, expectation | 显式闭合集;Poisson uniformization;尾项上界只描述截断误差 |
| 交换 | network_from_json, path_from_json, to_json, to_csv | 精确 schema、未知/重复 key 拒绝;CSV 不嵌入物种名,避免公式注入 |
let net = @k.Network::new(["A"], [@k.Reaction::new([1], [0], 2.0)])
let g = net.generator([[0], [1]]) // 闭合集:1→0
let d = g.evolve([0.0, 1.0], 1.0)
let masses = d.masses() // 约 [1-exp(-2), exp(-2)]
let truncation = d.tail_bound() // 不包含 IEEE 浮点舍入误差{"schema":1,"species":["A","B"],"volume":2,"reactions":[{"input":[2,0],"output":[0,1],"rate":3}]}moon info
moon fmt
moon fmt --check
moon check --target wasm-gc --deny-warn
moon build --target wasm-gc --deny-warn
moon test --target wasm-gc --deny-warn
moon run cmd/quickstart --target js
python scripts/reference.py --target js
python scripts/audit.pypub struct Distribution {
// private fields
}pub struct Ensemble {
// private fields
}pub struct Generator {
// private fields
}fn Generator::derivative(self : Generator, probabilities : Array[Double]) -> Array[Double] raise KineticsErrorfn Generator::evolve(self : Generator, initial : Array[Double], time : Double, tolerance? : Double, max_terms? : Int, max_work? : Int) -> Distribution raise KineticsErrorfn Generator::expectation(self : Generator, distribution : Distribution, observable : Array[Double]) -> (Double, Double) raise KineticsErrorfn Generator::mean_counts(self : Generator, distribution : Distribution) -> (Array[Double], Array[Double]) raise KineticsErrorpub struct Moments {
// private fields
}fn Network::concentrations(self : Network, counts : Array[Int]) -> Array[Double] raise KineticsErrorfn Network::conservation_basis(self : Network, tolerance? : Double) -> Array[Array[Double]] raise KineticsErrorfn Network::conservation_residual(self : Network, weights : Array[Double]) -> Array[Double] raise KineticsErrorfn Network::drift(self : Network, concentrations : Array[Double]) -> Array[Double] raise KineticsErrorfn Network::fire(self : Network, state : Array[Int], channel : Int) -> Array[Int] raise KineticsErrorfn Network::fluxes(self : Network, concentrations : Array[Double]) -> Array[Double] raise KineticsErrorfn Network::integrated_propensities(self : Network, path : Path) -> Array[Double] raise KineticsErrorfn Network::rounded_counts(self : Network, concentrations : Array[Double]) -> Array[Int] raise KineticsErrorfn Network::simulate(self : Network, initial : Array[Int], horizon : Double, seed? : Int, max_events? : Int) -> Path raise KineticsErrorfn Network::validate_concentrations(self : Network, state : Array[Double]) -> Unit raise KineticsErrorpub struct Path {
// private fields
}pub struct Random {
// private fields
}fn Reaction::new(input : Array[Int], output : Array[Int], rate : Double) -> Reaction raise KineticsErrorpub struct Trace {
// private fields
}Install
Download zipVolume-aware reaction networks, stochastic paths and closed finite chemical master equations