Author Archives: Tamás K. Papp

Getting a nice += in LaTeX math

By: Tamás K. Papp

Re-posted from: https://tamaspapp.eu/post/latex-math-increment/

I am working on an appendix for a paper that uses MCMC, and I decided to document some change of varible calculations in the interest of reproducibility (they are quite complex, because of multivariate determinants). But how can I typeset them nicely in $\LaTeX$?

\mathtt{target} += J_f

gives
$$
\mathtt{target} += J_f
$$
which is to be expected, as + is a binary operator and = is a relation, so $\LaTeX$ is not expecting them to show up this way.

We can remedy this as

\mathtt{target} \mathrel{+}= J_f

which shows up as
$$
\mathtt{target} \mathrel{+}= J_f
$$
which is an improvement, but is still not visually appealing.

Making the + a bit smaller with

\mathrel{\raisebox{0.19ex}{$\scriptstyle+$}}=}

yields
$$
\mathtt{target} \mathrel{\raise{0.19ex}{\scriptstyle+}} = J_f
$$
which looks OK enough to preclude further tweaking. Note that MathJax does not support \raisebox, but you can use

\mathrel{\raise{0.19ex}{\scriptstyle+}} = J_f

which renders the as above.

Two tricks for change of variables in MCMC

By: Tamás K. Papp

Re-posted from: https://tamaspapp.eu/post/jacobian-chain/

Change of variables are sometimes advantageous, and occasionally inevitable for MCMC if you want efficient sampling, or to model a distribution that was obtained by a transformation. A classic example is the lognormal distribution: when

\[\log(y) \sim N(\mu, \sigma^2)\]

one has to adjust the log posterior by \(-\log y\) since

\[\frac{\partial \log(y)}{\partial y} = \frac{1}{y}\]

and

\[\log(1/y) = -\log(y).\]

In Stan, one would accomplish this as

target += -log(y)

In general, when you transform using a multivariate function \(f\), you would adjust by

\[\log\det J_f(y)\]

which is the log of the determinant of the Jacobian — some texts
simply refer to this as "the Jacobian".

The above is well-known, but the following two tricks are worth mentioning.

Chaining transformations

Suppose that you are changing a variable by using a chain of two
functions \(f \circ g\). Then

\[
\log\det J_{f \circ g}(y) = \log \bigl(\det J_f(g(y)) \cdot \det J_g(y)\bigr) \\\\
= \log\det J_f(g(y)) + \log\det J_g(y)
\]

which means that you can simply add (the log determinant of) the
Jacobians, of course evaluated at the appropriate points.

This is very useful when \(f \circ g\) is complicated and \(J_{f\circ g}\)
is tedious to derive, or if you want to use multiple \(f\)s or \(g\)s and
economize on the algebra. From the above, it is also easy to see that this
generalizes to arbitrarily long chains of functions \(f_1 \circ f_2 \circ \dots\).

This trick turned out to be very useful when I was fitting a model
where a transformation was general to both equilibrium concepts I was
using (a noncooperative game and a social planner), so I could save on
code. Of course, since
#2224 is WIP, I had to
copy-paste the code, but still saved quite a bit of work.

Transforming a subset of variables

Suppose \(x \in \mathbb{R}^m\) and \(y \in \mathbb{R}^n\) are vectors, and you are interested in transforming to

\[
z = f(x,y)
\]

where \(x\) and \(z\) have the same dimension. It is useful to think
about this transformation as

\[
g(x,y) = [f(x,y), y]^\top
\]

where \(g : \mathbb{R}^{m+n} \to \mathbb{R}^{m+n}\). Since \(y\) is mapped to itself,

\[
J_g = \begin{bmatrix}
J_{f,x} & J_{f,y} \\\\
0 & I
\end{bmatrix}
\]

has a block structure, where

\[
J_{f,x} = \frac{\partial f(x,y)}{\partial x}
\]

and similarly for \(J_{f,y}\). For the calculation of the determinant, you can safely ignore the latter, and \(\log \det I = 0\), so

\[
\log\det J_g = \log\det J_{f,x}
\]

Disabling Privacy Badger

By: Tamás K. Papp

Re-posted from: https://tamaspapp.eu/post/privacy-badger/

Firefox has been my primary browser for the last decade. I find it very fast and convenient, and use quite a few addons, including Google Scholar Button, NoScript, and uBlock Origin.

I can’t recall exactly when, but about half a year ago various sites started to break. I have not bothered to debug what’s happening, suspecting it was was a combination of various plugins, but continued to use Firefox and installed openwith to open broken pages in Chromium (yes, I know, the ultimate kludge). More sites became broken, and I found that now I am spending 99% of my time in Chromium, which I don’t like that much, but moreover, it is a resource hog: while using the plugins above I can get the CPU load of Firefox to around 1–2% when I am not using it, Chromium drains my laptop battery in effectively half the time. To make things worse, Chromium apparently can’t open links in the background like Firefox, and insists on raising the window every time I open a link from another process, which is distracting.

Finally, Julia’s Discourse forum started showing up empty, which was the last straw and I went through my plugins. It turns out hat Privacy Badger was responsible for everything: apparently it relies on heavy-handed heuristics and breaks a lot of webpages. One can report this, but instead of reporting around 10–20 pages which were broken, I simply removed the plugin. I have a lot of respect for what the Electronic Frontier Foundation does, but I am not sure that this plugin is very useful.

So finally I got back the web the way I like it. Moral of the story: temporary workarounds become permanent, and bite back.