Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge rxinferenceand inference functions into infer #190

Merged
merged 15 commits into from
Dec 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
"function reactivemp_inference_smoothing(observations, A, B, P, Q)\n",
" n = length(observations) \n",
" \n",
" result = inference(\n",
" result = infer(\n",
" model = linear_gaussian_ssm_smoothing(n, A, B, P, Q),\n",
" data = (y = observations, ),\n",
" options = (limit_stack_depth = 500, )\n",
Expand All @@ -237,7 +237,7 @@
" x_min_t_mean, x_min_t_cov = mean_cov(q(x_t))\n",
" end\n",
" \n",
" result = rxinference(\n",
" result = infer(\n",
" model = linear_gaussian_ssm_filtering(A, B, P, Q),\n",
" data = (y_t = observations, ),\n",
" autoupdates = autoupdates,\n",
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/Tiny Benchmark.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
" x_prior_mean, x_prior_var = mean_var(q(x_next))\n",
" end\n",
"\n",
" return rxinference(\n",
" return infer(\n",
" model = filtering(c = 1.0, v = v),\n",
" datastream = datastream,\n",
" autoupdates = autoupdates,\n",
Expand Down Expand Up @@ -460,7 +460,7 @@
],
"source": [
"function run_smoothing(data, n, v)\n",
" return inference(\n",
" return infer(\n",
" model = smoothing(n, c = 1.0, v = v), \n",
" data = (y = data, ), \n",
" returnvars = KeepLast(),\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ makedocs(;
"Model specification" => "manuals/model-specification.md",
"Constraints specification" => "manuals/constraints-specification.md",
"Meta specification" => "manuals/meta-specification.md",
"Inference specification" => ["Overview" => "manuals/inference/overview.md", "Static dataset" => "manuals/inference/inference.md", "Real-time dataset / reactive inference" => "manuals/inference/rxinference.md", "Inference results postprocessing" => "manuals/inference/postprocess.md", "Manual inference specification" => "manuals/inference/manual.md"],
"Inference specification" => ["Overview" => "manuals/inference/overview.md", "Static vs Streamline inference" => "manuals/inference/infer.md", "Inference results postprocessing" => "manuals/inference/postprocess.md", "Manual inference specification" => "manuals/inference/manual.md"],
"Inference customization" => ["Defining a custom node and rules" => "manuals/custom-node.md"],
"Debugging" => "manuals/debugging.md",
"Delta node" => "manuals/delta-node.md"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/library/bethe-free-energy.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ which internalizes the factors of the model. The last two terms specify entropi

Crucially, the BFE can be iteratively optimized for each individual variational distribution in turn. Optimization of the BFE is thus more manageable than direct optimization of the VFE.

For iterative optimization of the BFE, the variational distributions must first be initialized. The `initmarginals` keyword argument to the [`inference`](@ref) and [`rxinference`](@ref) functions initializes the variational distributions of the BFE.
For iterative optimization of the BFE, the variational distributions must first be initialized. The `initmarginals` keyword argument to the [`infer`](@ref) function initializes the variational distributions of the BFE.

For disambiguation, note that the initialization of the variational distribution is a different design consideration than the choice of priors. A prior specifies a factor in the model definition, while initialization concerns factors in the variational distribution.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/manuals/constraints-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@ end
model, returnval = create_model(my_model(arguments...); constraints = constraints)
```

Alternatively, it is possible to use constraints directly in the automatic [`inference`](@ref) and [`rxinference`](@ref) functions that accepts `constraints` keyword argument.
Alternatively, it is possible to use constraints directly in the automatic [`infer`](@ref) function that accepts `constraints` keyword argument.
4 changes: 2 additions & 2 deletions docs/src/manuals/custom-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ end
Finally, we can run inference with this model and the generated dataset:

```@example create-node
result_mybernoulli = inference(
result_mybernoulli = infer(
model = coin_model_mybernoulli(length(dataset)),
data = (y = dataset, ),
)
Expand Down Expand Up @@ -312,7 +312,7 @@ As a sanity check, we can create the same model with the `RxInfer` built-in node

end

result_bernoulli = inference(
result_bernoulli = infer(
model = coin_model(length(dataset)),
data = (y = dataset, ),
)
Expand Down
6 changes: 3 additions & 3 deletions docs/src/manuals/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dataset = convert.(Int64, rand(Bernoulli(θ_real), n))

end

result = inference(
result = infer(
model = coin_model(length(dataset)),
data = (x = dataset, ),
);
Expand All @@ -56,7 +56,7 @@ vline!([θ_real], label="Real θ", title = "Inference results")
We can figure out what's wrong by looking at the Memory Addon. To obtain the trace, we have to add `addons = (AddonMemory(),)` as an argument to the inference function.

```@example addoncoin
result = inference(
result = infer(
model = coin_model(length(dataset)),
data = (x = dataset, ),
addons = (AddonMemory(),)
Expand Down Expand Up @@ -93,7 +93,7 @@ All the observations (purple, green, pink, blue) have much smaller rate paramete

end

result = inference(
result = infer(
model = coin_model(length(dataset)),
data = (x = dataset, ),
);
Expand Down
2 changes: 1 addition & 1 deletion docs/src/manuals/delta-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ end
To execute the inference procedure:

```@example delta_node_example
inference(model = delta_node_example(), meta=delta_meta, data = (z = 1.0,))
infer(model = delta_node_example(), meta=delta_meta, data = (z = 1.0,))
```

This methodology is consistent even when the delta node is associated with multiple nodes. For instance:
Expand Down
2 changes: 1 addition & 1 deletion docs/src/manuals/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ As you can see, `RxInfer` offers a model specification syntax that resembles clo
Once we have defined our model, the next step is to use `RxInfer` API to infer quantities of interests. To do this we can use a generic `inference` function that supports static datasets.

```@example coin
result = inference(
result = infer(
model = coin_model(length(dataset)),
data = (y = dataset, )
)
Expand Down
17 changes: 17 additions & 0 deletions docs/src/manuals/inference/infer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [Automatic Inference Specification](@id user-guide-inference)

`RxInfer` provides the `infer` function for quickly running and testing your model with both static and streaming datasets. To enable streaming behavior, the `infer` function accepts an `autoupdates` argument, which specifies how to update your priors for future states based on newly updated posteriors.

It's important to note that while this function covers most capabilities of the inference engine, advanced use cases may require resorting to the [Manual Inference Specification](@ref user-guide-inference-execution-manual-specification).

For details on manual inference specification, see the [Manual Inference](@ref user-guide-manual-inference) section.

```@docs
infer
InferenceResult
RxInfer.start
RxInfer.stop
@autoupdates
RxInferenceEngine
RxInferenceEvent
```
11 changes: 0 additions & 11 deletions docs/src/manuals/inference/inference.md

This file was deleted.

6 changes: 3 additions & 3 deletions docs/src/manuals/inference/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ The inference engine itself isn't aware of different algorithm types and simply

## [Automatic inference specification on static datasets](@id user-guide-inference-execution-automatic-specification-static)

`RxInfer` exports the `inference` function to quickly run and test you model with static datasets. See more information about the `inference` function on the separate [documentation section](@ref user-guide-inference).
`RxInfer` exports the `infer` function to quickly run and test you model with static datasets. See more information about the `infer` function on the separate [documentation section](@ref user-guide-inference).

## [Automatic inference specification on real-time datasets](@id user-guide-inference-execution-automatic-specification-realtime)

`RxInfer` exports the `rxinference` function to quickly run and test you model with dynamic and potentially real-time datasets. See more information about the `rxinference` function on the separate [documentation section](@ref user-guide-rxinference).
`RxInfer` supports running inference the with dynamic and potentially real-time datasets with enabled `autoupdates` keyword. See more information about the `infer` function on the separate [documentation section](@ref user-guide-inference).

## [Manual inference specification](@id user-guide-inference-execution-manual-specification)

While both `inference` and `rxinference` use most of the `RxInfer` inference engine capabilities in some situations it might be beneficial to write inference code manually. The [Manual inference](@ref user-guide-manual-inference) documentation section explains how to write your custom inference routines.
While `infer` uses most of the `RxInfer` inference engine capabilities in some situations it might be beneficial to write inference code manually. The [Manual inference](@ref user-guide-manual-inference) documentation section explains how to write your custom inference routines.
3 changes: 1 addition & 2 deletions docs/src/manuals/inference/postprocess.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# [Inference results postprocessing](@id user-guide-inference-postprocess)

Both [`inference`](@ref) and [`rxinference`](@ref) allow users to postprocess
the inference result with the `postprocess = ...` keyword argument. The inference engine
[`infer`](@ref) allow users to postprocess the inference result with the `postprocess = ...` keyword argument. The inference engine
operates on __wrapper__ types to distinguish between marginals and messages. By default
these wrapper types are removed from the inference results if no addons option is present.
Together with the enabled addons, however, the wrapper types are preserved in the
Expand Down
15 changes: 0 additions & 15 deletions docs/src/manuals/inference/rxinference.md

This file was deleted.

14 changes: 4 additions & 10 deletions docs/src/manuals/meta-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ end
model, returnval = create_model(my_model(arguments...); meta = my_meta)
```

Alternatively, it is possible to use meta directly in the automatic [`inference`](@ref) and [`rxinference`](@ref) functions that accepts `meta` keyword argument:
Alternatively, it is possible to use meta directly in the automatic [`infer`](@ref) function that accepts `meta` keyword argument:

```julia
inferred_result = inference(
model = my_model(arguments...),
meta = my_meta,
...
)

inferred_result = rxinference(
inferred_result = infer(
model = my_model(arguments...),
meta = my_meta,
...
Expand All @@ -116,7 +110,7 @@ inferred_result = rxinference(
...
end
```
If you add node-specific meta to your model this way, then you do not need to use the `meta` keyword argument in the `inference` and `rxinference` functions.
If you add node-specific meta to your model this way, then you do not need to use the `meta` keyword argument in the `infer` function.


## Create your own meta
Expand Down Expand Up @@ -179,7 +173,7 @@ y_data = 4.0
end

#do inference
inference_result = inference(
inference_result = infer(
model = gaussian_model(),
data = (y = y_data,)
)
Expand Down
228 changes: 202 additions & 26 deletions examples/advanced_examples/Active Inference Mountain car.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/advanced_examples/Advanced Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@
"\n",
"dataset = float.(rand(Bernoulli(p), 500));\n",
"\n",
"result = inference(\n",
"result = infer(\n",
" model = coin_toss_model(length(dataset)),\n",
" data = (y = dataset, )\n",
")\n",
Expand Down Expand Up @@ -978,7 +978,7 @@
"metadata": {},
"outputs": [],
"source": [
"rxresult = rxinference(\n",
"rxresult = infer(\n",
" model = online_coin_toss_model(),\n",
" data = (y = dataset, ),\n",
" autoupdates = autoupdates,\n",
Expand Down Expand Up @@ -1297,7 +1297,7 @@
}
],
"source": [
"result = inference(\n",
"result = infer(\n",
" model = test_model6(length(dataset)),\n",
" data = (y = dataset, ),\n",
" constraints = constraints6, \n",
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced_examples/Assessing People Skills.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
],
"source": [
"test_results = [0.1, 0.1, 0.1]\n",
"inference_result = inference(\n",
"inference_result = infer(\n",
" model = skill_model(),\n",
" data = (r = test_results, )\n",
")"
Expand Down
10 changes: 5 additions & 5 deletions examples/advanced_examples/Chance Constraints.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@
" m_u = zeros(T)\n",
" v_u = lambda^(-1)*ones(T)\n",
" \n",
" function infer(x_t::Float64)\n",
" function compute(x_t::Float64)\n",
" model_t = regulator_model(; T=T, lo=lo, hi=hi, epsilon=epsilon, atol=atol)\n",
" data_t = (m_u = m_u, v_u = v_u, x_t = x_t)\n",
" \n",
" result = inference(\n",
" result = infer(\n",
" model = model_t, \n",
" data = data_t,\n",
" iterations = n_its)\n",
Expand All @@ -272,7 +272,7 @@
" pol = zeros(T) # Predefine policy variable\n",
" act() = pol[1]\n",
"\n",
" return (infer, act)\n",
" return (compute, act)\n",
"end;"
]
},
Expand Down Expand Up @@ -309,15 +309,15 @@
"outputs": [],
"source": [
"(execute, observe) = initializeWorld() # Let there be a world\n",
"(infer, act) = initializeAgent() # Let there be an agent\n",
"(compute, act) = initializeAgent() # Let there be an agent\n",
"\n",
"a = Vector{Float64}(undef, N) # Actions\n",
"x = Vector{Float64}(undef, N) # States\n",
"for t = 1:N\n",
" a[t] = act()\n",
" execute(t, a[t])\n",
" x[t] = observe()\n",
" infer(x[t])\n",
" compute(x[t])\n",
"end"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@
}
],
"source": [
"results = inference(\n",
"results = infer(\n",
" model = measurement_model(nr_observations),\n",
" data = (y = measurements,),\n",
" iterations = 5,\n",
Expand Down Expand Up @@ -1164,7 +1164,7 @@
}
],
"source": [
"res = inference(\n",
"res = infer(\n",
" model = normal_square_model(1000),\n",
" data = (y = y,),\n",
" iterations = 10,\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced_examples/GP Regression by SSM.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@
}
],
"source": [
"result_32 = inference(\n",
"result_32 = infer(\n",
" model = gp_regression(n, P∞, A, Q, H, σ²_noise),\n",
" data = (y = y_data,)\n",
")"
Expand Down Expand Up @@ -506,7 +506,7 @@
}
],
"source": [
"result_52 = inference(\n",
"result_52 = infer(\n",
" model = gp_regression(n, P∞, A, Q, H, σ²_noise),\n",
" data = (y = y_data,)\n",
")"
Expand Down
Loading
Loading