Skip to content

Instantly share code, notes, and snippets.

@fonsp
Last active April 21, 2026 11:34
Show Gist options
  • Select an option

  • Save fonsp/cfbc2d9138316414730fea7d067e155e to your computer and use it in GitHub Desktop.

Select an option

Save fonsp/cfbc2d9138316414730fea7d067e155e to your computer and use it in GitHub Desktop.
RxInfer Perfetto demo.jl
import Pkg; Pkg.activate(); Pkg.resolve(); Pkg.instantiate()
using Revise
using RxInfer, StableRNGs, Plots
using Markdown
## Inference example
@model function iid_estimation(y)
μ ~ Normal(mean = 0.0, precision = 0.1)
τ ~ Gamma(shape = 1.0, rate = 1.0)
y .~ Normal(mean = μ, precision = τ)
end
# Specify mean-field constraint over the joint variational posterior
constraints = @constraints begin
q(μ, τ) = q(μ)q(τ)
end
# Specify initial posteriors for variational iterations
initialization = @initialization begin
q(μ) = vague(NormalMeanPrecision)
q(τ) = vague(GammaShapeRate)
end
begin
hidden_μ = 3.1415
hidden_τ = 2.7182
distribution = NormalMeanPrecision(hidden_μ, hidden_τ)
rng = StableRNG(42)
n_observations = 600
dataset = rand(rng, distribution, n_observations)
end
results = infer(
model = iid_estimation(),
data = (y = dataset, ),
constraints = constraints,
iterations = 4,
initialization = initialization,
trace = true
)
## Getting traces from RxInfer
trace = results.model.metadata[:trace]
events = RxInfer.tracedevents(trace)
## View with Perfetto
RxInfer.perfetto_open(events)
RxInfer.perfetto_view(events)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment