Marginalization "by hand" in python

52 views
Skip to first unread message

Radu Cioaca

unread,
Oct 5, 2025, 2:18:00 PMOct 5
to gtsam users
Hello I am trying to do a marginalization "by hand" on a chain-like graph structure in Python. Yet, I am getting stuck because I think some functions needed might not have the right bindings from cpp to py.

Graph is: prior>>X0>>factor>>X1>>factor>>X2 and I want to marginalize X0 for example

My approach is like:

# Build the graph
# Optimize it with LM

# Marginalizing X(0)
Sigma = marginals.jointMarginalCovariance(X(0))

# Build a new graph without X(0)
# Constrain X(1) with a new JacobianFactor (wrapped in a LinearContainer Factor) with covariance = sigma  << Here I fail because the JacobianFactor is exposed only with gtsam.gtsam.noiseModel.Diagonal as an input for the noise model so I cannot feed it the Sigma


Is this the right approach overall ? Am I missing something or is there another way?

Thank you very much, I am just willing to get control and understand marginalization better.

Regards,
Radu Cioacă

Dellaert, Frank

unread,
Oct 5, 2025, 3:27:54 PMOct 5
to Radu Cioaca, gtsam users
Indeed, JacobianFactors are restricted to diagonal noise models, even in c++.
You can  create a HessianFactor:

HessianFactor(gtsam::Key j, gtsam::Vector mu, gtsam::Matrix Sigma);

Best!
FD

From: gtsam...@googlegroups.com <gtsam...@googlegroups.com> on behalf of Radu Cioaca <cioac...@gmail.com>
Date: Sunday, October 5, 2025 at 2:18 PM
To: gtsam users <gtsam...@googlegroups.com>
Subject: [GTSAM] Marginalization "by hand" in python

You don't often get email from cioac...@gmail.com. Learn why this is important
--
You received this message because you are subscribed to the Google Groups "gtsam users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gtsam-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/gtsam-users/2663d7ba-8268-41e7-9fb4-196d79fe4280n%40googlegroups.com.

jan m

unread,
Oct 8, 2025, 9:29:40 AMOct 8
to gtsam users
Hi,

Sorry to hijack the thread. I have a question regarding creating the prior factor in a way mentioned by the original author - taking into consideration the uncertainty calculated in the previous optimization run. Namely, I have a single variable, C(0), which represents the calibration state (gtsam::Pose3) between two sensors within one sliding window of 10 poses. Now, I wish to compute a prior factor for the next optimization window based on the current solution.
To that end, I do,

marginals = gtsam::Marginals(current_graph, current_solution);
const gtsam::Matrix calibration_sigmas = marginals.marginalCovariance(C(0));
gtsam::LinearContainerFactor lcf(
gtsam::HessianFactor(C(0), current_values.localCoordinates(current_values).at(C(0)), calibration_sigmas), current_values);
new_graph.add(lcf);


is that a correct approach to constructing such prior factor? current_graph and current_values represent the graph which is used for the current optimization run and the new_graph is the one for which the prior is constructed. Is the localCoordinates() correct way of retrieving the current mean?

Many thanks for help!

Best,
Jan

Dellaert, Frank

unread,
Oct 8, 2025, 1:30:42 PMOct 8
to jan m, gtsam users
The doc web page for that factor is rather sparse but there are a bunch of examples in testLinearContainerFactor.cpp

I think you need to make a Values from the  current_solution, as error for the LCF is

double LinearContainerFactor::error(const Values& c) const {
if (!linearizationPoint_)
return 0;

// Extract subset of values for comparison
Values csub;
for (Key key : keys())
csub.insert(key, c.at(key));

// create dummy ordering for evaluation
VectorValues delta = linearizationPoint_->localCoordinates(csub);

// compute error
double error = factor_->error(delta);

return error;
}

PS that factor seems a bit inefficient for what you want. Could you just use a PriorFactor?

Best
Frank

From: gtsam...@googlegroups.com <gtsam...@googlegroups.com> on behalf of jan m <jakmic...@gmail.com>
Date: Wednesday, October 8, 2025 at 9:29 AM
To: gtsam users <gtsam...@googlegroups.com>
Subject: Re: [GTSAM] Marginalization "by hand" in python

You don't often get email from jakmic...@gmail.com. Learn why this is important
Reply all
Reply to author
Forward
0 new messages