Skip to content

CFA and SEM with PyMC - #698

Merged
fonnesbeck merged 17 commits into
pymc-devs:mainfrom
NathanielF:cfa_sem
Sep 25, 2024
Merged

CFA and SEM with PyMC#698
fonnesbeck merged 17 commits into
pymc-devs:mainfrom
NathanielF:cfa_sem

Conversation

@NathanielF

@NathanielF NathanielF commented Sep 1, 2024

Copy link
Copy Markdown
Contributor

CFA and SEM with PyMC

Related to this issue here I'm adding a PR to demonstrate the functionality of CFA and SEM models using PyMC
#695

Helpful links


📚 Documentation preview 📚: https://pymc-examples--698.org.readthedocs.build/en/698/

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
@NathanielF
NathanielF marked this pull request as ready for review September 8, 2024 16:21
@NathanielF

Copy link
Copy Markdown
Contributor Author

Think this is ready for review @drbenvincent , @cluhmann , @AlexAndorra . Any feedback/pushback welcome.

@NathanielF

Copy link
Copy Markdown
Contributor Author

Giving this another nudge!

Thanks!

@drbenvincent

Copy link
Copy Markdown
Contributor

FYI: I'm totally overloaded at the moment, so going to struggle with a review. Will see if things ease up a bit next week.

@NathanielF

Copy link
Copy Markdown
Contributor Author

No worries.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:46Z
----------------------------------------------------------------

Perhaps add reference to psychometrics in the title to make it more specific, and aid in discoverability.


NathanielF commented on 2024-09-25T11:38:08Z
----------------------------------------------------------------

Good idea.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:47Z
----------------------------------------------------------------

nitpick: data set should not have a hyphen; data set or dataset, no?


NathanielF commented on 2024-09-25T11:38:41Z
----------------------------------------------------------------

changed.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:47Z
----------------------------------------------------------------

Maybe hyperlink SEM and CFA to some introductory materials on the topics (even a Wikipedia link would be good)


NathanielF commented on 2024-09-25T11:38:54Z
----------------------------------------------------------------

Added links to wikipedia.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:48Z
----------------------------------------------------------------

There are lots of UserWarnings in the notebook. Consider filtering them out.


@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:49Z
----------------------------------------------------------------

The last line of the equation set contains a typo -- should be \psi_n rather than \psi_3


NathanielF commented on 2024-09-25T11:39:29Z
----------------------------------------------------------------

Fixed. Thanks, good catch!

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:50Z
----------------------------------------------------------------

Line #29.        chol, _, _ = pm.LKJCholeskyCov("chol_cov", n=2, eta=2, sd_dist=sd_dist, compute_corr=True)

Why do you set compute_corr=True and discard the correlations?


NathanielF commented on 2024-09-25T11:40:37Z
----------------------------------------------------------------

It's not used here in this model, but useful to have computed for analysing the correlations between the constructs post model fit. I demonstrate this below for the full measurement model....

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:50Z
----------------------------------------------------------------

Cool plots!


NathanielF commented on 2024-09-25T11:45:43Z
----------------------------------------------------------------

Thanks!

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:51Z
----------------------------------------------------------------

Line #81.            draws=10000,

Why so many draws? Bad autocorrelation?


NathanielF commented on 2024-09-25T11:42:40Z
----------------------------------------------------------------

Yes, it seemed to help generally boost the ESS. The model samples pretty fast so it was easy enough to just boost the samples.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:52Z
----------------------------------------------------------------

typo: reflected


NathanielF commented on 2024-09-25T11:42:50Z
----------------------------------------------------------------

Fixed.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:53Z
----------------------------------------------------------------

typo in first sentence: individual


NathanielF commented on 2024-09-25T11:42:59Z
----------------------------------------------------------------

Fixed.

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:53Z
----------------------------------------------------------------

Typo: each


NathanielF commented on 2024-09-25T11:43:11Z
----------------------------------------------------------------

Fixed

@review-notebook-app

review-notebook-app Bot commented Sep 25, 2024

Copy link
Copy Markdown

View / edit / reply to this conversation on ReviewNB

fonnesbeck commented on 2024-09-25T03:08:54Z
----------------------------------------------------------------

What about measures of relative fit among the models? What do the LOO/WAIC values look like, and are they helpful in selecting the appropriate level of complexity?


NathanielF commented on 2024-09-25T11:45:26Z
----------------------------------------------------------------

Added the global fit comparison for the SEM and full measurement models. The LOO metrics are quite close, but the full measurement model "wins" on this score. I added a note to say we need to consider the value of the trade-off between expressive power of the SEMs and the questions we can answer in the context of simple global fit comparisons.

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

Copy link
Copy Markdown
Contributor Author

Good idea.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

changed.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Added links to wikipedia.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Fixed. Thanks, good catch!


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

It's not used here in this model, but useful to have computed for analysing the correlations between the constructs post model fit. I demonstrate this below for the full measurement model....


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Yes, it seemed to help generally boost the ESS. The model samples pretty fast so it was easy enough to just boost the samples.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Fixed.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Fixed.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Fixed


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Added the global fit comparison for the SEM and full measurement models. The LOO metrics are quite close, but the full measurement model "wins" on this score. I added a note to say we need to consider the value of the trade-off between expressive power of the SEMs and the questions we can answer in the context of simple global fit comparisons.


View entire conversation on ReviewNB

Copy link
Copy Markdown
Contributor Author

Thanks!


View entire conversation on ReviewNB

@NathanielF

Copy link
Copy Markdown
Contributor Author

Thanks @fonnesbeck I think i've addressed most of the points you raised. But let me know if you think I should add anything else.

Note I also re-parameterised the tau terms in the final SEM model which seemed to fix the divergences i was getting.

@fonnesbeck
fonnesbeck self-requested a review September 25, 2024 13:40

@fonnesbeck fonnesbeck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@NathanielF

Copy link
Copy Markdown
Contributor Author

Sweet!

@NathanielF

Copy link
Copy Markdown
Contributor Author

Think you'll have to merge that for me @fonnesbeck, i don't have that power.

@fonnesbeck
fonnesbeck merged commit ba7a936 into pymc-devs:main Sep 25, 2024
@NathanielF

Copy link
Copy Markdown
Contributor Author

Woo! Thanks @fonnesbeck

fonnesbeck pushed a commit to fonnesbeck/pymc-examples that referenced this pull request Dec 20, 2024
* First commit CFA and SEM

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* test pre-commit hooks

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* updated for pre-commit

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* updated git index

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update with full run

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* add some text write up and a pair plot

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update with full run and text

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* Run with explained variance calculations

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* Add sem structure section

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* full run with lavaan syntax noted

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update with draft conclusion

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* tigher writing

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* tidying

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* emphasis on exchangeability

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update plot sizes

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* add pearl reference

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* updating with Chris's comments and better parameterisation

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

---------

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
fonnesbeck pushed a commit to fonnesbeck/pymc-examples that referenced this pull request Jan 19, 2025
* First commit CFA and SEM

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* test pre-commit hooks

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* updated for pre-commit

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* updated git index

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update with full run

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* add some text write up and a pair plot

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update with full run and text

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* Run with explained variance calculations

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* Add sem structure section

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* full run with lavaan syntax noted

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update with draft conclusion

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* tigher writing

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* tidying

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* emphasis on exchangeability

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* update plot sizes

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* add pearl reference

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

* updating with Chris's comments and better parameterisation

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>

---------

Signed-off-by: Nathaniel <NathanielF@users.noreply.github.com>
YichengYang-Ethan added a commit to YichengYang-Ethan/pymc-examples that referenced this pull request Aug 17, 2026
An external review raised two blockers; both reproduce.

First, `approx.hist` is not the full-data negative ELBO the notebook said
it was. PyMC normalizes twice: MinibatchRV scales the observed logp up by
N/b, then OPVI divides the objective by the same constant because
`scale_cost_to_minibatch` defaults on. What is recorded is the batch's own
log-likelihood sum plus (b/N) KL, which scales linearly with b — measured
directly by alternating 1000- and 400-row blocks and reading the ratio of
mean recorded loss: 0.4126 against the 0.400 the batch sizes predict.
Multiplying by N/b restores the full-data scale (0.9998 against a full-data
reference over 1200 steps). The notebook now derives this, plots and
analyses `hist * N/b`, and reports 188 nats of stage-2 reduction rather
than the 0.60 that the batch scale produced.

Second, the shard geometry made blocks ragged: 288 of 1024 rows plus eight
of 636, which put a deterministic sawtooth into the loss and left
`len(loader)` matching neither the block count nor the yields. Ten shards
of 30,000 rows with row groups equal to the batch size give exactly 300
equal blocks, `len(loader) == 300`, and a loss series whose only structure
is optimization and noise. The per-step standardized contrast now has unit
spread (0.99, from 1.07) and the epoch-alignment result survives on the
clean series: the spread at epoch-aligned horizons is more than twenty
times smaller than at 1.5 epochs.

Third, provenance. The stored outputs were produced in an environment
whose pymc-extras metadata predated the merge and whose PyMC violated the
merged package's own floor. Everything here was re-executed against
pymc-extras at 8db1880 (the pymc-devs#698 merge commit), PyMC 6.2.0, PyTensor
3.2.4, and the install note now gives readers that exact command.

Also corrected: the CUSUM warning overstated its case (the recursion sits
at zero while improvement exceeds the allowance; the real failure is that
it cannot tell convergence from improvement too slow to resolve); the
fixed replay is described as cyclic finite-sum optimization rather than
the unbiased N/B identity; the hurdle alone does not prevent sufficient-
statistic collapse; the nu floor is a statement about support, not prior
belief (P(nu<=2) is 4e-6); the row-scale generator arrays are released
before fitting and the in-memory preprocessing is labelled as such; the
shrinkage claim in the model section is withdrawn; and the predictive
check is described as one batch, one step ahead.

Cold runs in the pinned environment: 11.0 / 10.0 / 9.6 s, peak RSS 619 MB.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants