Deriving Secret Sales Volumes with Bayesian Inference

Using RxInfer to decouple two signals

Retail Industry
Bayesian Inference
Active Inference
RxInfer
Julia
Author

Kobus Esterhuysen

Published

October 12, 2024

Modified

November 3, 2026

Back to Blog |  LearnableLoopAI.com |  Portfolio of Projects |  LinkedIn


versioninfo() ## Julia version
Julia Version 1.10.5
Commit 6f3fdf7b362 (2024-08-27 14:19 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)
import Pkg
Pkg.add(Pkg.PackageSpec(;name="RxInfer"))
Pkg.add("Plots")
Pkg.add("StableRNGs")
Pkg.add("LaTeXStrings")
Pkg.add("Distributions")

using RxInfer, Random, Plots, StableRNGs, LaTeXStrings, Distributions
   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
Pkg.status()
Status `~/.julia/environments/v1.10/Project.toml`
  [6e4b80f9] BenchmarkTools v1.8.0
⌃ [31c24e10] Distributions v0.25.131
  [7073ff75] IJulia v1.34.4
  [b964fa9f] LaTeXStrings v1.4.1
  [91a5bcdd] Plots v1.41.7
⌃ [86711068] RxInfer v5.5.2
⌃ [860ef19b] StableRNGs v1.0.4
Info Packages marked with ⌃ have new versions available and may be upgradable.

Deriving Secret Sales Volumes

This use case of active inference comes from the retail industry. We use the published sales volume of a company over 250 time intervals. They state that this volume is composed of two components:

  • Product sales
  • Rental sales

For strategic reasons they only publish the combined volume. Their competitor came to us with a request to try and decouple the underlying signals. This will help the competitor to decide on a strategic repositioning in the marketplace.

To test our approach, we will use a simple random walk model to simulate both the product sales volume as well as the rental sales volume. We also add these two signals to simulate the published combined sales volume. Then we will use Bayesian inference inside the RxInfer Julia package to attempt to decouple the two sales volume signals - one for product volume and the other for rental volume. During inference we will not have access to the separate product and rental sales signals, but only to their combination. If it works well enough, we will take the real published data and attempt to decouple the two sales volume signals.

The Generative Process

Working notation (continuous state)

A continuous state in \(\mathbb{R}^{D}\) is an array (italic bold), not a tuple:

  • whole state at time \(t\): \(\boldsymbol{x}^{\ast(t)}\)
  • component \(n\): \(x^{\ast(t)[n]}\) — time in round brackets, component in square brackets

Here \(D=2\): component \([1]\) = product volume, \([2]\) = rental volume. The mark \(^{\ast}\) denotes generative-process (hidden) quantities and sits on the symbol before any indices.

Code counterparts

math code
\(x^{\ast(t)[1]}\) xˣıtıI1I
\(x^{\ast(t-1)[1]}\) xˣıt₋₁ıI1I
trajectory of that component xˣːI1I
\(r^{\ast(t)}\) trajectory rˣː
\(\lambda^{\ast[1]}\) λˣI1I
\(\lambda^{[1]}\) (model) λI1I
observation random variable \(y^{(t)}\) (model) yː
realized observation \(\hat{y}^{(t)}\) (data) ŷː
process observation variance \((\sigma^{\ast}_{y})^{2}\) σˣ²ᵧ
model observation precision \(\lambda_{y}\) λ_y

Marks: ˣ = process \(*\) ; ı…ı = time member ; I…I = component entry ; ː = trajectory ; hat = realized value.

Model side (same geometry, no process mark): e.g. \(x^{(t)[1]}\) → xıtıI1I, trajectory xːI1I. The likelihood node yː is set equal to the data ŷː during inference.

Distributions: parameters of a named family follow a semicolon; sampling uses \(\sim\) (not \(=\)). Example: \(x\sim\mathcal{N}(x;\mu,\sigma^{2})\). A vertical bar \(\mid\) is reserved for conditionals.

Provision function (\(f_p\))

The provision function, provides another covariate vector. Because this is a sequential system, the provision function defines the transition between the previous state and the current state. This special case of the provision function is known as a transition function:

\[p^{\ast(t)[1]} = f_p\bigl(x^{\ast(t-1)[1]}\bigr) = x^{\ast(t-1)[1]}\]

\[p^{\ast(t)[2]} = f_p\bigl(x^{\ast(t-1)[2]}\bigr) = x^{\ast(t-1)[2]}\]

When we add system noise the next state is produced:

\[x^{\ast(t)[1]} \sim \mathcal{N}\bigl(x^{\ast(t)[1]}; p^{\ast(t)[1]}, (\lambda^{\ast[1]})^{-1}\bigr)\]

\[x^{\ast(t)[2]} \sim \mathcal{N}\bigl(x^{\ast(t)[2]}; p^{\ast(t)[2]}, (\lambda^{\ast[2]})^{-1}\bigr)\]

The *s indicate that the parameters and variables are hidden and not observed.

## provision function, provides another covariate vector
function fₚ(; xˣıt₋₁ı)
    return xˣıt₋₁ı
end
## fₚ(xˣıt₋₁ı=xˣıt₋₁ıI1I), fₚ(xˣıt₋₁ı=xˣıt₋₁ıI2I) 
fₚ (generic function with 1 method)

Response function (\(f_r\))

The response function, provides the response to the covariate vector. \[r^{\ast(t)} = f_r\bigl(x^{\ast(t)[1]}, x^{\ast(t)[2]}\bigr) = f\bigl(x^{\ast(t)[1]}, x^{\ast(t)[2]}\bigr) = x^{\ast(t)[1]} + x^{\ast(t)[2]} \]

After combining with observation noise the observation is produced:

\[y^{(t)} \sim \mathcal{N}\bigl(y^{(t)}; r^{\ast(t)}, (\sigma^{\ast}_{y})^{2}\bigr)\]

The *s indicate that the parameters and variables are hidden and not observed.

## response function, provides the response to covariate vector
function fᵣ(; f, xˣıtıI1I, xˣıtıI2I)
    return f(xˣıtıI1I, xˣıtıI2I)
end
## fᵣ(; f=+, xˣıtıI1I=7.0, xˣıtıI2I=8.0)
fᵣ (generic function with 1 method)

Next, we create the sim_batch_data(...) function that accepts an argument \(f\) which is a function that captures how the two signals are coupled or combined. We will call the sim_batch_data() function with the \(+\) function because the product sales volume is simply added to the rental sales volume.

The function will return the real (but hidden) signals \(x^{\ast(t)[1]}\) and \(x^{\ast(t)[2]}\) (used for comparison later) and their coupled/combined version \(\hat{y}\). We will use \(\hat{y}\) (the published combined sales volume) as our observations during the inference. We also assume that \(\hat{y}\) is corrupted with some observation noise.

## Data comes from either a simulation/lab (sim|lab) OR from the field (fld)
## Data are handled either in batches (batch) OR online as individual points (point)
## Batch data accumulates either
    ## along the depth/examples dimension/axis (into the screen/page), OR
        ## typical for supervised & unsupervised learning
    ## along the time dimension/axis (down the screen page)
        ## typical for sequential decision learning (reinforcement learning & active inference)
function sim_batch_data(f, T; seed, xˣıt₋₁ıI1I, xˣıt₋₁ıI2I, λˣI1I, λˣI2I, σˣ²ᵧ)
    rng = StableRNG(seed)
    pˣːI1I = Vector{Float64}(undef, T)
    xˣːI1I = Vector{Float64}(undef, T)
    pˣːI2I = Vector{Float64}(undef, T)
    xˣːI2I = Vector{Float64}(undef, T)
    rˣː = Vector{Float64}(undef, T)
    ŷː = Vector{Float64}(undef, T)
    for t in 1:T
        pˣːI1I[t] = fₚ(xˣıt₋₁ı=xˣıt₋₁ıI1I)
        xˣːI1I[t] = rand(rng, Normal(pˣːI1I[t], sqrt(1.0/λˣI1I)))
        pˣːI2I[t] = fₚ(xˣıt₋₁ı=xˣıt₋₁ıI2I)
        xˣːI2I[t] = rand(rng, Normal(pˣːI2I[t], sqrt(1.0/λˣI2I)))
        rˣː[t] = fᵣ(f=f, xˣıtıI1I=xˣːI1I[t], xˣıtıI2I=xˣːI2I[t])
        ŷː[t] = rand(rng, Normal(rˣː[t], sqrt(σˣ²ᵧ)))
        xˣıt₋₁ıI1I = xˣːI1I[t]
        xˣıt₋₁ıI2I = xˣːI2I[t]
    end
    return xˣːI1I, xˣːI2I, ŷː
end
sim_batch_data (generic function with 1 method)

First, let us create a synthetic dataset:

xˣıt₋₁ıI1I = 40.0
xˣıt₋₁ıI2I = 20.0

λˣI1I = 1.0
λˣI2I = 1.0

σˣ²ᵧ = 20.0 ##observation noise
seed = 222
T = 250
250
xˣːI1I, xˣːI2I, ŷː = sim_batch_data(
    +, T; 
    seed=seed,
    xˣıt₋₁ıI1I=xˣıt₋₁ıI1I, xˣıt₋₁ıI2I=xˣıt₋₁ıI2I, 
    λˣI1I=λˣI1I, λˣI2I=λˣI2I, 
    σˣ²ᵧ=σˣ²ᵧ);
xˣːI1I
250-element Vector{Float64}:
 38.17351536500037
 38.77424066555762
 38.0649901128261
 37.88494841327824
 38.32316266962699
 37.9709503143836
 37.92445739678425
 39.242134600059345
 39.75171798148435
 39.00489489933928
 39.6526299933459
 39.25477502050384
 39.354198334891024
  ⋮
  5.813780284901366
  5.696807759168373
  6.8102968772838
  5.229725647688231
  6.1355497052670325
  6.004826916013331
  6.285939765621888
  6.169350874135184
  5.982917077260892
  8.04753374496972
  6.996226689895888
  6.879849314673978
xˣːI2I
250-element Vector{Float64}:
 20.827514379359563
 21.836968985674375
 23.319198323841803
 23.800911955617885
 23.690922682811433
 22.885366024383806
 22.872966512102174
 21.67113389505316
 22.684780998372805
 19.758996186852205
 20.15050798220582
 20.39850685057507
 20.736518329857994
  ⋮
 33.22411677709458
 32.55498241295711
 31.023058728779805
 31.085429600320868
 32.03089890414834
 31.826225724635243
 30.535300132388816
 30.68245473546646
 30.705134833295908
 30.23491684777188
 30.10640420518613
 30.761763390055563
ŷː
250-element Vector{Float64}:
 51.055772694424824
 59.95111148432222
 55.05654278445259
 62.45589589491548
 57.256104599566946
 66.24912791331924
 63.3860861039736
 66.46907679463577
 65.17701380960303
 59.18125677519563
 62.37801819808789
 53.73889929962076
 53.50706496178381
  ⋮
 31.000477537297083
 37.315899676873684
 32.535648724925956
 34.39902507080608
 38.54427167243298
 40.46984799252074
 43.82615990686573
 38.467290944324475
 35.19221692251506
 39.40013064479152
 40.61920355195525
 39.98054648387227
legend_opts = (legendfontsize=12, legendmarkersize=10, lw=2)

p1 = plot(title="Hidden product volume and rental volume"; legend_opts...)
p1 = plot!(p1, xˣːI1I, label=L"\mathrm{Product\ sales\ volume\ } x^{\ast(t)[1]}", c="red")
p1 = plot!(p1, xˣːI2I, label=L"\mathrm{Rental\ sales\ volume\ } x^{\ast(t)[2]}", c="blue")

p2 = plot(title="Published combined volume with noise " * L"\hat{y}^{(t)} = x^{\ast(t)[1]} + x^{\ast(t)[2]}"; legend_opts...)
p2 = scatter!(p2, ŷː, ms=5, color=:purple, label=L"\hat{y}^{(t)}")
plot(p1, p2, size=(700, 600), layout=(2,1))

The Generative Model

Next, we create a probabilistic model to perform inference. We use the RxInfer package:

@model function decouple_model(f, yː, m₁₀, λ₁₀, a₁, b₁, m₂₀, λ₂₀, a₂, b₂, a_y, b_y)
    xı0ıI1I ~ Normal(mean=m₁₀, precision=λ₁₀)
    λI1I ~ Gamma(shape=a₁, rate=b₁)
    xı0ıI2I ~ Normal(mean=m₂₀, precision=λ₂₀)
    λI2I ~ Gamma(shape=a₂, rate=b₂)
    λ_y ~ Gamma(shape=a_y, rate=b_y)
    xıt₋₁ıI1I = xı0ıI1I
    xıt₋₁ıI2I = xı0ıI2I
    for t in 1:length(yː)
        xːI1I[t] ~ Normal(mean=xıt₋₁ıI1I, precision=λI1I)
        xːI2I[t] ~ Normal(mean=xıt₋₁ıI2I, precision=λI2I)
        zː[t] ~ f(xːI1I[t], xːI2I[t])
        yː[t] ~ Normal(mean=zː[t], precision=λ_y)
        xıt₋₁ıI1I = xːI1I[t]
        xıt₋₁ıI2I = xːI2I[t]
    end
end

RxInfer runs Bayesian inference as a variational optimisation procedure between the real solution and its variational proxy q. In our model specification we assumed noise components to be unknown so we need to enforce a structured mean-field assumption for the variational family of distributions q. This inevitably reduces the accuracy of the result, but makes the task easier and allows for fast and analytical message passing-based variational inference:

constraints = @constraints begin
    q(xı0ıI1I, xı0ıI2I, xːI1I, xːI2I, λI1I, λI2I, λ_y, zː) = q(xːI1I, xı0ıI1I, xːI2I, xı0ıI2I, zː)q(λI2I)q(λI1I)q(λ_y)
end
Constraints: 
  q(xı0ıI1I, xı0ıI2I, xːI1I, xːI2I, λI1I, λI2I, λ_y, zː) = q(xːI1I, xı0ıI1I, xːI2I, xı0ıI2I, zː)q(λI2I)q(λI1I)q(λ_y)

Next, we need to assign priors, initialize the messages needed, and the marginals, and then call the infer function:

m₁₀, λ₁₀ = 40.0, 0.10
m₂₀, λ₂₀ = 20.0, 0.10

## strong priors for the random walk noise components
a₁, b₁ = 0.02, 0.1var(xˣːI1I)
a₂, b₂ = 0.02, 0.1var(xˣːI2I)

## vague prior for the observation noise
a_y, b_y = 1.0, 1.0

## strong priors for messages
xːI1I_init = map(r -> NormalMeanPrecision(r, λ₁₀), reverse(range(-50, -20, length=T)))
xːI2I_init = map(r -> NormalMeanPrecision(r, λ₂₀), range(20, 50, length=T))

init = @initialization begin
    μ(xːI1I) = xːI1I_init
    μ(xːI2I) = xːI2I_init
    q(λI1I) = GammaShapeRate(a₁, b₁)
    q(λI2I) = GammaShapeRate(a₂, b₂)
    q(λ_y) = GammaShapeRate(a_y, b_y)
end

result = infer(
    model = decouple_model(
        f=+, 
        m₁₀=m₁₀, λ₁₀=λ₁₀, a₁=a₁, b₁=b₁, 
        m₂₀=m₂₀, λ₂₀=λ₂₀, a₂=a₂, b₂=b₂, 
        a_y=a_y, b_y=b_y),
    data  = (yː = ŷː,), 
    options = (limit_stack_depth = 500, ), 
    constraints = constraints, 
    initialization = init,
    iterations = 50
)
Inference results:
  Posteriors       | available for (λI1I, zː, xːI1I, λ_y, xːI2I, xı0ıI2I, xı0ıI1I, λI2I)
result.posteriors
Dict{Symbol, Vector} with 8 entries:
  :λI1I    => GammaShapeRate{Float64}[GammaShapeRate{Float64}(a=125.02, b=1596.…
  :zː      => Vector{NormalWeightedMeanPrecision{Float64}}[[NormalWeightedMeanP…
  :xːI1I   => Vector{NormalWeightedMeanPrecision{Float64}}[[NormalWeightedMeanP…
  :λ_y     => GammaShapeRate{Float64}[GammaShapeRate{Float64}(a=126.0, b=126.43…
  :xːI2I   => Vector{NormalWeightedMeanPrecision{Float64}}[[NormalWeightedMeanP…
  :xı0ıI2I => NormalWeightedMeanPrecision{Float64}[NormalWeightedMeanPrecision{…
  :xı0ıI1I => NormalWeightedMeanPrecision{Float64}[NormalWeightedMeanPrecision{…
  :λI2I    => GammaShapeRate{Float64}[GammaShapeRate{Float64}(a=125.02, b=1043.…

The inference results:

λI1I_marginals = result.posteriors[:λI1I]
λI2I_marginals = result.posteriors[:λI2I]
λ_y_marginals = result.posteriors[:λ_y]

zː_marginals = result.posteriors[:zː]
xːI1I_marginals = result.posteriors[:xːI1I]
xːI2I_marginals = result.posteriors[:xːI2I];
legend_opts = (legendfontsize=12, legendmarkersize=10, lw=2)

p1 = plot(legend=:bottomleft, title="Estimated product volume and rental volume"; legend_opts...)
p1 = plot!(p1, xˣːI1I, label=L"\mathrm{Product\ sales\ volume\ } x^{\ast(t)[1]}", c="red", linestyle=:dash)
p1 = plot!(p1, mean.(xːI1I_marginals[end]), ribbon=var.(xːI1I_marginals[end]), label=L"\mathrm{Estimated\ Product\ sales\ volume\ } x^{(t)[1]}", color=:pink)

p1 = plot!(p1, xˣːI2I, label=L"\mathrm{Rental\ sales\ volume\ } x^{\ast(t)[2]}", color=:blue, linestyle=:dash)
p1 = plot!(p1, mean.(xːI2I_marginals[end]), ribbon=var.(xːI2I_marginals[end]), label=L"\mathrm{Estimated\ Rental\ sales\ volume\ } x^{(t)[2]}", color=:lightblue)

p2 = plot(legend=:bottomright, title="Estimated combined volume"; legend_opts...)
p2 = scatter!(p2, ŷː, label="Observations (published combined volume with noise))", ms=5, alpha=0.5, color=:orange)
p2 = plot!(p2, mean.(zː_marginals[end]), ribbon=std.(zː_marginals[end]), label="Combined estimated volume", color="plum")

plot(p1, p2, size=(700, 600), layout=(2,1))

The inferred hidden signals track the their hidden counterparts initially, but then progressively deviates increasingly. We can conclude that the product volume declines consistently, but that the rental volume keeps on increasing, or at least does not show a declining trend. This kind of information could be valuable for the competitor that approached us. Based on the simulation results, they may just decide to position themselves to compete more aggresively in the product space (due to reduced competition), rather than the rental space.