{'fraction_non_alphanumeric': 0.05061903984704703, 'fraction_numerical': 0.006994800531604841, 'mean_word_length': 3.704914436156209, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
A category theory framework for Bayesian learning

Inspired by the foundational works by Spivak and Fong and Cruttwell et al., we introduce a categorical framework to formalize Bayesian inference and learning. The two key ideas at play here are the notions of Bayesian inversions and the functor GL as constructed by Cruttwell et al.. In this context, we find that Bayesian learning is the simplest case of the learning paradigm. We then obtain categorical formulations of batch and sequential Bayes updates while also verifying that the two coincide in a specific example.


Introduction
A standard problem in Machine learning is to understand the relationship between two variables or random vectors. Suppose x is a random vector and y is a random variable dependent on x. The additive model assumes that there exists a function f such that where ǫ is a random variable with mean 0. Our goal then reduces to estimating the function f . To this end, we consider a parametrized family of functions {f θ } θ∈P where P is the parameter space and by means of a learning algorithm and a training data set, we traverse P to find a candidate that we believe will provide a reasonable approximation to the true function f . Several theoretical results underpin the validity of this approach -for instance the Universal Approximation Theorem.
A formulation of the above framework within Category theory was achieved in the foundational work of Fong, Spivak and Tuyeras [7]. In this paper, the authors introduce the category Learn whose objects are sets and a morphism from A to B in Learn consists of the following data. A parameter set M , an implementation I : M × A → B, an update function u : M × A × B → M and a request function r : M × A × B → A. In related work, the authors of [3], develop a more general theory by introducing the notion of a Cartesian Reverse Differential category (cf. [3, §2.2]). Associated to a CRDC -C, they construct the analog of the category Learn as a composite of the Para and Lens constructions (cf. [3, §2.1, 2.3]). In this setting, the learning algorithm will be a functor of type Para(C) → Para(Lens(C)). Our goal in this paper is to employ both these approaches to develop a categorical framework to discuss Bayesian learning.
While the approach outlined above to model the relationship between two random variables is effective, in real world situations, it is often the case that there is no such function f . Given the noisy nature of the data, it is more reasonable to model the conditional probability p(y|x) i.e. to ask the question -'Given x, what is the probability that we have y ?'.
As before, we model the conditional probability using a parametrized family of distributions -p(y|x; θ). However in contrast to our approach from before, we assume that θ is a random variable and strive to obtain a distribution over θ that agrees with the given training data. More precisely, we choose a prior distribution q(θ) on θ and update this prior to obtain the posterior distribution on θ. As opposed to gradient based approaches to learning, Bayesian machine learning updates the prior distribution on θ by exploiting Bayes Theorem. The posterior distribution is defined upto normalizing constant by the formula p(θ|y, x) ∝ p(y|x, θ)q(θ|x).
There is considerable flexibility in this approach in that we can use the posterior to obtain point estimates of the parameter θ via the MAP estimate and perform inference by integrating over the entire distribution. We continue this discussion in greater detail in §2.1 where we illustrate the essential features of Bayesian learning in the classical context using a simple example.
Our goal in this paper is to introduce a framework within category theory that allows us to formalize this set up. We draw on the theory already developed in [8], [9] and [2]. While we proceed as in [3] by associating to a parametrized function a morphism in a generalized lens category which allows for the backward transmission of information, an important observation is that the Bayes Learning framework simplifies the situation drastically. This is essentially due to the fact that with the correct setup, Bayesian inversion is a well defined dagger functor and the functor C op → Cat responsible for defining the generalized lens breaks down. We interpret this to mean that Bayes Learning is the simplest form of learning that adheres to the framework discussed in [3]. See also Remark 3. 10.
The work of Cho and Jacobs in [2] and Fritz in [8] introduces the notion of Markov categories as a suitable framework within which one can discuss ideas from Probability theory such as Bayesian inversion, disintegration, jointification, conditionalization... Hence, let C be a Markov category. We use the categories FinStoch and Stoch (cf. Example 2.3) as guiding examples for the theory we develop. Roughly speaking, the category FinStoch has finite sets as objects and Markov kernels as morphisms between them while the objects of Stoch are measurable spaces and morphisms f : X → Y imply for every x ∈ X, a probability distribution p(·|x) on Y . Central to this paper is the notion of Bayesian inversion which in the context of FinStoch with reference to a morphism p : X → Y corresponds to calculating the conditional p(X|y) for y ∈ Y using Bayes theorem to obtain a morphism p † : Y → X. Note that the inversion which is a map Y → X is not necessarily unique and in the case of FinStoch does not even always exist. To get around this issue, firstly we restrict our attention to those Markov categories C which always admit Bayesian inversion. Secondly, we make use of the symmetric monoidal category ProbStoch(C) which was introduced first by Cho and Jacobs in [2, §5] as the category of couplings or equivalently the category whose objects coincide with those of the slice category I ↓ C and morphisms are obtained by quotienting with respect to the relation of almost sure equality. Henceforth, we write PS(C) in place of ProbStoch(C) for ease of notation. As a consequence of our assumption on C, Bayesian inversion is a well defined dagger functor on PS(C). A more detailed explanation can be found in §2.2.
We can interpret the set-up in §2.1, as a parametrized function in a suitable Kleisli category. In general, we assume that the category C is equipped with the structure of an M-actegory where M is symmetric monoidal. We would like to make use of the Para M construction from [1] which generalizes [7]. For reasons explained above, we work with the category PS(C) and in Lemma 3.3, we show that PS(C) has the structure of an PS(M)-actegory provided we impose certain technical assumptions on M and its action on C. We simplify notation henceforth and write PS in place of PS.
Our definition of the functor BayesLearn in Section §3 is inspired in large part by [3]. The key to the construction of a gradient based learner in [3] is a functor GL : where we require that E is a Cartesian reverse differential category. To fully describe the learning mechanism, the authors introduce update and error endofunctors on Para(E). These three pieces when employed in unison and for appropriate choices of the update and displacement maps, can describe a wide range of optimization algorithms ([3, §3.4]).
When adapting the framework of [3] to describe Bayes learning, we see immediately that we must work with a more general notion of Lens as in [9]. To this end, we proceed as in [9, Definition 3.1] by introducing a functor Stat : PS(C) op → Cat with which we define the associated Grothendieck lens Lens Stat . As Bayesian inversion is a well defined dagger functor, we deduce the existence of a well defined functor R : PS(C) → Lens Stat .
It follows that we have a functor We refer to the functor Para PS(M) (R) as the BayesLearn functor. It captures the essential features of the Bayes learning algorithm. Indeed, given a parametrized morphism X → Y with a state on X, after choosing a prior state on the parameter object P , we obtain a morphism in PS(C) i.e. a morphism By construction, the induced morphism is given by a pair of morphisms in the forward direction and in the backwards direction by Bayesian inversion. This provides a rough description of the Bayesian learning procedure which we elaborate on in §3 and §3.3 where we also give a formulation for the Bayes predictive density. The classical formulation of Bayesian learning enables one to update a given prior distribution on a parameter space using a training data set and Bayesian inversion. In §4, we provide a category theoretic formulation of this phenomenon. Central to the discussion will be the notion of a training set. To capture the notion of a single training instance, we restrict our attention to those Markov categories which are of the form Kl(P) where P : D → D is a symmetric monoidal monad on a symmetric monoidal category D. In this context, we introduce the notion of an elementary point of an object X in C (cf. Definition 4.2). In the case of FinStoch or Stoch, these correspond to states on X which concentrate at a point x ∈ X. We show that there are two ways one can update the prior. As before, let us suppose we have a model i.e. a morphism f : M ⊙ X → Y in C. In addition we are given a prior distribution π M,0 on M and a state π X : I → X. Via Bayesian inversion with respect to π M,0 ⊙ π X and after conditionalizing, we get a channel f † joint,0 : X ⊗ Y → M . The precise details can be found in §3. Let be a list of elementary points (cf. Definition 4.2) belonging to X ⊗ Y . To obtain the posterior, we can proceed sequentially i.e. we update the prior π M,0 by the composition I Note that x 1 corresponds to a morphism I D → X and δ x1 is the image of this map in C. We then implement the inversion procedure with respect to π M,1 to get an updated channel X ⊗ Y → M and continue as before. Repeating the above step to run through the training data set will end with a state π M,n on M which in the case of FinStoch defines a distribution on M which we call the posterior.
In a similar fashion, we can also define batch updates associated to the training set T. To do so, we work with the space Z := X ⊗Y and set Z n := ⊗ n Z. A channel f : M → Z naturally gives us a channel M → Z n by the composition Here we abuse notation and write copy n M for the composition Note that the map M → Z is obtained naturally from the model f : M ⊙X → Y via the composition The Bayesian inversion with respect to π M,0 implies a channel Z n → M . The composition defines a state on M which we refer to as the batch update with respect to T and denote π M,T . It is natural in this setting to ask if there is a relationship between the sequential and batch updates or what hypothesis we must impose on C to ensure that they are equal. We leave this question open to further investigation and show that the two are equal in the case of FinStoch (cf. Examples 4.5 and 4.4).
String diagrams: In this paper, we orient our string diagrams from top to bottom and display monoidal product as moving from left to right. All string diagrams have been made using DisCoPy [4].

Bayesian Inference
Our goal in this section is to give a high level overview of the approach Bayesian learning takes in modelling the relationship between random variables of interest. We use the lecture [11] as a reference to the material in this section.
Let us illustrate the approach with an example. Let x = (x 1 , . . . , x n ) be a random vector and y be a random variable. We assume that y is related to x via the equation where ǫ is normally distributed with mean 0 and variance σ 2 and f (x) := β T x where β ∈ R n . For simplicity, we assume that the quantity σ is a known constant.
Let us suppose we are given a training set T := {(x 1 , y 1 ), . . . , (x N , y N )}. One can divide the Bayesian approach into two parts. As outlined in the introduction we treat β as a random variable and choose a prior distribution which encodes our pre-conceived beliefs about β. Then using the set T, we update the distribution on β to get its posterior distribution by applying Bayes' rule. The posterior distribution can then be used to obtain point estimates of β.
Let us assume an improper prior on β i.e. q(β) = 1. Applying Bayes' rule gives p(β|T) ∝ p(T|β)q(β) Observe that our assumption on the nature of the underlying data implies that p(y|x) is normally distributed with mean β T x and variance σ 2 . Assuming that the training data is independent, we get We assumed above that the distribution on x is known and hence in effect we condition every random variable with respect to x. Hence, and In this manner, we have updated the prior distribution on β in accordance with the given data. We now sketch how one might perform inference. Let x * be a data point that does not necessarily belong to the training set T. We would like to find p(y * |x * , T). We outline two ways on how to proceed.

Let
This is called the maximum a posteriori estimate of the parameter β. It represents a single best guess for the parameter given the training data. We then set Observe that in the context of our ongoing example, β MAP coincides with β OLS -the ordinary least squares estimate.
2. While the MAP estimate of β represents a suitable guess for the parameter, when performing inference we can do better by leveraging our knowledge of the entire posterior distribution. The true Bayesian way is to integrate over the posterior distribution β i.e.
In practice, owing to the intractability of the integral above due to the fact that the posterior distribution is not likely analytic a possible solution is to proceed by employing a suitable approximation strategy. See for instance [5] or [6].
Remark 2.1. Observe that the posterior distribution on β with regards to the training set T can also be obtained by sequential updates. This is due to the following phenomenon. Suppose T can be split into two training sets T 1 and Let q 1 be the posterior distribution p(β|T 1 ) on β obtained by updating the prior q. Likewise, let q 2 be the posterior distribution on β obtained by updating the prior q 1 using the dataset T 2 . A simple calculation shows that the distribution q 2 coincides with the posterior obtained by updating q using the entire dataset T. In §4, we formulate a categorical version of the above observation.

Bayesian inversions and PS
The theory of Markov categories provides a categorical framework within which we can discuss and formalize notions from Probability theory. We use the notation and conventions introduced in [8]. Note that this notion of Markov categories coincides with that of affine CD-categories as introduced by Cho and Jacobs in [2].

Definition 2.2.
A Markov category is a semicartesian symmetric monoidal category (C, I, ⊗) in which every object X is equipped with the structure of a commutative internal comonoid. We denote the comultiplication and counit maps by copy X : X → X ⊗ X and del X : X → I respectively and require that they satisfy certain natural conditions (cf. [8, The definition above suggest that one can think of a morphism π X : I → X in a Markov category C as a probability distribution on X. We refer to π X as defining a state on X. Likewise, a morphism f : X → Y in C will be called a channel.
For the remainder of this section we fix a Markov category C.
Example 2.3. We introduce two examples which we will continue to make reference to throughout the course of the paper.

1.
We define the category FinStoch to be the category of finite sets with channels. A morphism f : X → Y in FinStoch is given by a morphism of Given a set X, we define copy X to be the map that sends an element x ∈ X to the distribution on X × X which takes the value 1 at (x, x) and 0 everywhere else. The terminal object I is the set with a single element i.e. I = { * } and as a consequence we can identify Dist(I) with I. We define del X to be the map that sends every element x ∈ X to * . Lastly, given morphisms f : X → Y and g : Y → Z, we define the composition f ; g : X → Z to be such that for every x ∈ X, Observe that Dist defines an endofunctor on the category of Finite sets. One can check without difficulty that it is in fact a commutative monad. It follows that FinStoch coincides with the Kleisli category Kl(Dist).

2.
To deal with distributions whose supports are not necessarily finite, we introduce Stoch whose objects are measurable spaces i.e. tuples of the form (X, Σ X ) where X is a set and Σ X is a well defined σ-algebra on X.
Recall that the category of measurable spaces is endowed with a natural symmetric monoidal structure given by such that for every S ∈ Σ X , the map f (S, ) : X → [0, 1] is measurable. Furthermore, we ask that for every x, f ( , x) : Σ Y → [0, 1] is a well defined probability measure. One checks that Stoch is a symmetric monoidal category whose monoidal structure is inherited from the category of measurable spaces. The unit object I is the pair ({ * }, {{ * }, ∅}).
As before, we must define copy and delete morphisms as well as specify how to compose morphisms in Stoch. We do not define del as this is obvious from the definition of I. Given (X, Σ X ) ∈ Ob(Stoch), we define as the map that sends a pair (S, x) to 1 if (x, x) ∈ S and 0 otherwise. Lastly, given morphisms f : where we abuse notation and write f (x) for the measure Σ Y → [0, 1] given by T → f (T, x).
Definition 2.4. Let π X⊗Y : I → X ⊗ Y be a joint state. A disintegration of π X⊗Y will be a pair consisting of a channel f : X → Y and a state π X : I → X such that the following string diagrams are equal.
If every joint state admits a disintegration then we say that the category C admits conditional distributions. Likewise, given a pair f : X → Y and ψ : I → X, we can easily define a state on X × Y via the following string diagram.
We refer to this as the jointification of f and ψ.
We will require a more general version of conditionalization which permits us to consider joint distributions parametrized by another object.
Definition 2.5. Let C be a Markov category. We say that C has conditionals if for every morphism s : A → X ⊗ Y , there is t : X ⊗ A → Y such that we have the following equality of string diagrams.
Remark 2.6. In the case of FinStoch, we see that a state π X×Y : I → X × Y corresponds to a probability distribution on X × Y . Furthermore, a disintegration of π X×Y is given by the conditional distribution associated to the joint distribution as well as the state π X : I → X obtained by marginalizing y in π X×Y . We define the conditional distribution c : X → Y explicitly by setting if π X (x) is not zero and in the event that π X (x) = 0, we set c(x)( ) to be any distribution on Y .
Another category which admits conditionals is the category BorelStoch which is a subcategory of the category Stoch whose objects are standard Borel spaces.
Observe from the explicit calculation above that the disintegration of a joint distribution is not necessarily unique. This leads us to the following definition.
Definition 2.7. Let π X : I → X be a state on an object X ∈ Ob(C). Let f, g : X → Y be morphisms in C. We say that f is almost surely equal to g with respect to π X or f ∼ πX −a.s g if the following string diagrams coincide.
Observe that if π X×Y : I → X ×Y is a state and π X : I → X is the associated marginal then if f, g : X → Y are channels such that (f, π X ) and (g, π X ) are both disintegrations with respect to π X×Y then f is π X -almost surely equal to g.
Definition 2.8. Let π X : I → X be a state on an object X ∈ Ob(C). Let f : X → Y be a channel. The Bayesian inversion of f with respect to π X is a channel f † πX : Y → X such that we have the following equality of string diagrams. We say that C admits Bayesian inversions if for every state π X : I → X and f : X → Y we have a Bayesian inversion f † πX : Y → X. Note that we can rephrase the definition above in terms of disintegrations as follows. Indeed, if c and π X are as in the definition then the Bayesian inversion c † πX can be obtained by disintegrating the joint distribution π X×Y : I → Y × X obtained by swapping the integration of the pair (c, π X ). Remark 2.9. As for disintegrations, we see that Bayesian inversions are not necessarily unique. However, if c 1 , c 2 are Bayesian inversions of a channel c : X → Y with respect to a state π X : I → X then c 1 is almost surely equal to c 2 .

The category PS
A crucial requirement of our set up that allows us to define the BayesLearn functor similar to the Gradient learn functor from [3] is that Bayesian inversions must compose strictly. To this end, we must move away from working with equivalence classes of almost surely equal morphisms with respect to a given state and instead use the category PS (cf. [8,Definition 13.8]). Definition 2.10. Suppose that C is causal (cf. [8,Definition 11.31]). Then the category ProbStoch(C) is defined as follows. The objects of ProbStoch(C) consist of pairs (X, π X ) where X ∈ Ob(C) and π X : I → X is a state on X. A morphism in ProbStoch(C) between objects (X, π X ) and (Y, π Y ) consists of a map f : X → Y in C satisfying π X ; f = π Y modulo π X -a.s. equality, with composition inherited from C i.e.
As mentioned in the introduction, we write PS in place of ProbStoch for ease of notation.
Remark 2.11. Note that the categories Stoch and FinStoch are both causal. It is important to observe that if a category admits conditionals then it is causal. However the converse is not true (cf. [8, 11.34, 11.35]).
Remark 2.12. Recall from [8, Proposition 13.9(a)] that if C is causal then PS(C) is symmetric monoidal. In this case, the unit object is given by the pair (I, ι) where ι : I → I is the identity map in C.

The Para construction
Recall that our goal is to understand conditional distributions between random variables or morphisms in a Markov category that satisfies certain constraints. In this setting, we model a conditional distribution p(y|x) using a parametric function f (x; θ) while our learning algorithm updates θ using the given training set. The notion of parametrized function has a natural formulation in category theory which we call Para which was first introduced in [7]. We use the more generalized version of this construction which can be found in [1]. Observe that the type of the parameter θ need not coincide with that of the variable. To ensure that this observation is preserved in the categorical formulation, we make use of the notion of actegories.
Definition 2.13. Let (M, J, ⋆) be a symmetric monoidal category and let C be a category.

1.
We say that C is an M-actegory if we have a strong monoidal functor Φ : M → End(C) where End(C) is the category of endofunctors on C for which the monoidal product is given by composition. Given M ∈ Ob(M) and X ∈ C, we write M ⊙ X := Φ(M )(X).

2.
We say that C is a symmetric monoidal M-actegory if in addition to being an M-actegory C is endowed with natural isomorphisms satisfying coherence laws reminiscent of the laws of a costrong comonad.
which are called the mixed associator and the mixed interchanger respectively. • Ob(Para M (C)) := Ob(C).
• A 1-cell f : X → Y in Para M (C) consists of a pair (P, φ) where P ∈ Ob(M) and φ : P ⊙ X → Y is a morphism in C.
is given by a morphism α ′ : Q → P such that the following diagram commutes.
• The identity and composition in the category Para M (C)(X, Y ) are inherited from the identity and composition in M. We make use of this fact when we define the BayesLearn functor in §3.

Bayes Learn
In this section we outline the construction of the functor BayesLearn which aims to capture the essential features of Bayesian learning. However in order for us to discuss these results, we require certain preliminary ideas which allow us to better understand the actegory structure on PS(C) where C is a Markov category.

3.1
Inducing the M-actegory structure While we work with the flexibility provided by Para M , we must be careful to ensure that the categories to which we apply Para M ( ) are endowed with the structure of an M-actegory. To this end, we must make certain modifications to the category M itself as we would firstly like to ensure that parameter spaces have a well defined prior associated to them.
where the vertical morphisms are due to the natural transformation between the functors M ⊙ and N ⊙ induced by the morphism M → N . We write The following technical condition is required in the proof of Lemma 3.3. We say that C is in agreement with M if for every P ∈ M and X ∈ C, the following diagram is commutative and natural in both P and X.
copy P ⊙copy X ιP,P,X,X copy P ⊙X where ι P,P,X,X is the mixed interchanger as introduced in 2.14.
Recall from 2.1, that a categorical formulation of Bayesian learning must capture the notion of state on the parameter as well as a means to update it via Bayesian inversion. It is hence natural under these circumstances to utilize PS(M) as the category of parameters. A brief discussion of the PS construction was made in 2.2.1. Proof. Let (P, π P ) ∈ Ob(PS(C)). We define an endofunctor (P, π P ) ⊙ : PS(C) → PS(C) as follows. Firstly, let (X, π X ) be an object in PS(C). We set (P, π P ) ⊙ (X, π X ) to be the pair I where the first isomorphism is due to the M-actegory structure on C. Observe that the composition above coincides with We check that (P, π P ) ⊙ defines a functor. Suppose f : (X, π X ) → (Y, π Y ) in PS(C). The morphism π Y : I → Y coincides with the composition I πX − − → Xf − → Y wheref is a representative of the equivalence class of f in C. Applying P ⊙ gives Hence we get a sequence of morphisms Using the observation in Equation (3) and the fact that π X ;f = π Y , we deduce that Equation (4) coincides with (P, π P ) ⊙ (Y, π Y ). We have thus shown that we have a map (P, π P ) ⊙f : (P, π P ) ⊙ (X, π X ) → (P, π P ) ⊙ (Y, π Y ) in PS(C). Note that we must show that the map (P, π P ) ⊙ (X, π X ) → (P, π P ) ⊙ (Y, π Y ) we have defined above is independent of our choice off . Hence we verify that if h, g : X → Y are morphisms in C such that h ∼ πX −a.s g then (P, π P ) ⊙ h ∼ πP ⊙πX −a.s (P, π P ) ⊙ g. Since h ∼ πX −a.s g, we get that the compositions However, this implies that
is indeed a well defined functor. Furthermore, using the notation from Remark 2.12, (J, ι) ⊙ coincides with the identity on PS(C) and we have a natural isomorphism id PS(C) Lastly, let (P, π P ), (Q, π Q ) ∈ PS(M) and (X, π X ) ∈ PS(C). Since we have a strong monoidal functor P ⊙ (Q ⊙ ) ∼ − → (P ⋆ Q) ⊙ , we deduce that the following diagram is commutative.
We deduce from this and similar such arguments that we have an isomorphism given by (P, π P ) → (P, π P ) ⊙ a strong monoidal functor. This concludes the proof. 2. For every M ∈ Ob(M), X, Y ∈ Ob(A), f ∈ A(X, Y ), A ∈ Ob(S(X)) and B ∈ Ob(S(Y )), we have that The Grothendieck lens Lens S is then an M-actegory.
Note that in the statement of the above lemma, condition (2) only makes sense if we have condition (1).
Proof. Recall that the objects of Lens S are tuples of the form (X, A) where X is an object in A and A ∈ Ob(S(X)). Given M ∈ Ob(M), we define in Lens S as follows. Since M is a M-actegory, we have a morphism We apply the assumption on S to get B) in Lens S . Clearly, M ⊙ preserves identities. One checks without difficulty that M ⊙ respects compositions as well. We have thus shown that M ⊙ defines an endofunctor on Lens S . It remains to show that the morphism M → End(Lens S ) is a strong monoidal functor. Firstly, using that A is a M-actegory and for every X ∈ Ob(A), S(X) is a M-actegory, we deduce that if J is the unit object of M then J ⊙ is naturally isomorphic to the identity endofunctor. In a similar fashion, given N, M ∈ Ob(M) and (X, A) in Lens S we verify that we have a natural isomorphism of endofunctors This concludes the proof.

Bayes Learn
Let C be a Markov category which admits conditionals. Note that this is equivalent to saying that C admits Bayesian inversions. Since Bayesian inversions in general are defined up to an equivalence relation, we restrict our attention to the category PS(C) (cf. §2.2.1). In fact, in this case, Bayesian inversion defined a symmetric monoidal dagger functor on PS(C) in the sense of dagger categories. We refer the reader to [8,Remark 13.10].
Recall from [3], the basis of the gradient learning functor GL comes from a functor R : C → Lens(C) where in this case C is a Cartesian reverse differential category. In our situation, we can mirror this construction via the mechanism of Bayesian inversion and the notion of generalized lenses. We proceed below in greater detail. As above, our goal is to define a functor where Lens F is the F -lens associated to a functor PS(C) op → Cat (cf. [10]).
Given a map f : X → Y in PS(C), the natural transformation F (Y ) → F (X) is the identity functor.
Remark 3.5. Observe that our definition of Stat coincides with the definition of Stat from [9]. Indeed, since C is Markov, the unit I is a terminal object and hence there is a unique state ι : I → I. We simplify notation as above and write I in place of (I, ι). PS(C)(I, (X, π X )) hence consists of a single element corresponding to the state π X . If X ∈ PS(C) and (A, π A ), (B, π B ) ∈ Ob(Stat(X)) then a morphism f : (A, π A ) → (B, π B ) in Stat(X) coincides with a morphism of sets PS(C)(I, (X, π X )) → PS(C)((A, π A ), (B, π B )) since PS(C)(I, (X, π X )) is the singleton set. This is precisely how Stat is defined in [9].

The Grothendieck Lens
Let Lens Stat be the lens associated to the functor Stat as introduced in [10, §3.1]. More precisely, we have that • The objects of the category Lens Stat are pairs ((X, π X ), (A, π A )) where (X, π X ) ∈ PS(C) and (A, π A ) ∈ Stat(X).
One checks that this is a well defined category and is an instance of the Grothendieck construction as in [10, §3.1].
Remark 3.6. The category Lens Stat simplifies considerably in our situation.

The functor R
We define the functor R : PS(C) → Lens Stat as follows.
• If f : (X, π X ) → (Y, π Y ) is a morphism in PS(C) then the map in Lens Stat is defined to be the pair (f, f † πX ) where f † πX is the Bayesian inversion of f with respect to the state π X on X. Note that this simplification is a consequence of our discussion above.
Proposition 3.7. The functor R is well defined.
Proof. We must essentially verify that R behaves well with regards to composition. This is a consequence of the fact that in the category PS(C), Bayesian inversions are unique and their composition is indeed well defined.

The functor BayesLearn
Let M and C be Markov categories with M causal. To define BayesLearn, we now specialize to the case where C is a symmetric monoidal M-actegory which in addition is in agreement with M.
Since M is causal, PS(M) is a well defined symmetric monoidal category. By Recall that if (P, J, ⋆) is a symmetric monoidal category then a P-actegory A admits a canonical functor j P,A : A → Para P (A) given by A → J ⊙ A. Note that j P,A is the unit for the pseudo-monad defined by Para P (·). We thus have a diagram Remark 3.9. Observe that the BayesLearn functor in §3.2.4 does not have an update or displacement endofunctor as the Gradient learning functor from [3, §3.5]. This is due to the relatively simplified nature of Bayesian learning where parameter updates correspond to obtaining the posterior distribution using the prior and the likelihood and not as a result of optimizing with respect to a loss function. Equivalently, in the categorical setting, Bayesian inversion provides an update rule without recourse to a displacement or error endofunctor.
Remark 3.10. Note that when we contrast the Bayesian learning framework described above with the gradient learning framework described in [3], we see that Bayesian learning is considerably simpler. By using the Grothendieck lens in place of the standard Lens construction and ProbStoch(C) in place of C, the existence of Bayesian inversion as a dagger functor implies the breakdown of the functor Stat resulting in the simplified form of Lens Stat as described in Remark 3.6. In addition, the absence of error and update endofunctors further distinguishes the Bayes Learning framework. In this sense, we believe Bayesian learning to be the simplest case of the categorical learning as described in [3]. Hence Lens StatC in fact has the structure of Lens StatM -actegory. Since we are interested only in the BayesLearn functor and in particular its image, we do not concern ourselves with this more general action of Lens Stat M . Note also that we can endow PS(C) with the structure of Lens Stat PS(M)actegory via the projection This implies that we can also view Lens Stat C as a Lens Stat M -actegory in line with the theory from [1].

Bayes Learning algorithm
We now detail the Bayes Learning algorithm in our current setup. We preserve our assumptions on C and M from the previous section. We are given training data which consists of objects X T and Y T in C and a joint distribution ω T : I → X T ⊗ Y T . We would like to perform inference for objects X * and Y * in C. We are also provided with states π X * : I → X * and π XT : I → X T where π XT is obtained by marginalizing ω T . Note that in practice X T = X * and Y T = Y * We proceed as follows. 1. We model the given data by choosing a function f : This corresponds to a morphism f : M ⊙X T → Y T . We assume that model satisfies a technical assumption which we precise in (2). We assume we have a similar model applicable for the inference data i.e. f * : M ⊙ X * → Y * .
2. The morphism f descends to give a morphism in the category PS(C) as follows. Firstly, we endow M with a prior distribution i.e. a state π M : J → M . The morphism π M ⊙ π XT : I ≃ J ⊙ I → M ⊙ X T defines a state on M ⊙ X T . By composing with f we obtain a state on Y T i.e. a morphism π YT := (π M ⊙ π XT ); f : I → Y T . We assume that the model f and the prior π M were chosen so as to guarantee that π YT coincides with the marginal distribution on Y T from ω T . The equivalence class of f defines a morphism (M, π M ) ⊙ (X T , π XT ) → (Y T , π YT ). Equivalently, we have a map in Para PS(M) (PS(C)).
4. Recall that in §2.1, we outlined how to leverage the posterior distribution to make predictions. This can be formalized in a categorical setting as follows. Consider the composition The isomorphism above is obtained by composing the isomorphisms where (i) is due to the mixed associator, (ii) is because of the swap isomorphism, (iii) is a consequence of the associative property of the monoidal product and (iv) is obtained by applying the mixed associator again.
We conditionalize the morphsim to get a morphism By composing on the right by f * , we get This is the Bayes predictive distribution. By pre-composing with the state π XT ⊗ π YT , we get a map (X * , π X * ) → (Y * , π Y * ). We call this the full predictive distribution obtained by averaging out the predictive distributions as they vary over different instances of the training data.
Example 3.12. Let us work within the category BorelStoch which is the subcategory of Stoch from Example 2.3. We make this restriction because Borel-Stoch admits conditionals. In BorelStoch, we can view a morphism f : A → B as defining a conditional distribution p(b|a). Let X and Y be Borel spaces. Our training data consists of a list T := [(x 1 , y 1 ), . . . , (x n , y n )] of points in List(X × Y ). To align with our notation from §3.3, we write X T and Y T to be copies of X and Y and endow X T × Y T with the empirical distribution obtained from T. Our goal is to obtain an estimate of the conditional probability p(y * |x * ) for general points x * ∈ X * and y * ∈ Y * . As for the training set, let X * and Y * be copies of X and Y respectively which we use for inference. As outlined in §3.3 and §2.1, we model the given data via a parametrized function of the form f : P × X T → Y T where P ∈ BorelStoch. We endow P with a prior distribution i.e. a state π P : I → P where I = { * } is the unit object. We must update the prior p to get the posterior distribution. This is accomplished via the Bayesian inversion f † : Y T → P × X T . Note that f † is not unique. By conditionalizing, we get a map X T × Y T → P in BorelStoch which defines the posterior.
In this case, the Bayes predictive density as described in §3.3 is given by This is a consequence of how compositions are defined in Stoch i.e. via the Chapman-Kolmogorov equation cf. §2.3.

Bayes updates
While Section 3.3 describes the Bayes Learning algorithm, we observe that it does not provide a mechanism by which we can update the prior on the parameter space. Recall, via Bayesian inversion we obtain a channel (X T , π XT ) ⊗ (Y T , π YT ) → (M, π M ). However, in practice, when working in a suitable subcategory of Stoch or in FinSet, we are given a training set y 1 ), . . . , (x n , y n )} which we use to obtain the posterior distribution on M . Our goal in this section is to translate this into the categorical framework we have developed so far.
In the case of Stoch, we can represent a data point (x, y) ∈ X T × Y T as the product of a pair of morphisms I → X T and I → Y T mapping * to the probability measures that concentrate at the points x and y respectively. We use δ x ,δ y respectively to denote these maps. Given a channel c : X × Y → M in Stoch, we define a state on M via the composition This effectively defines the posterior on M given the training set T 1 where T 1 := {(x 1 , y 1 )}. The natural question to ask in this setting is how to sequentially update the posterior to achieve the required update over the entire training dataset and if one can also achieve such an update all at once. We outline a possible solution in what follows.
For the remainder of this section we work with a Markov category (C, I, ⊗) such that C = Kl(P) where P : D → D is a symmetric monoidal monad on the symmetric monoidal category (D, I D , * ). Furthermore, we suppose that C admits conditionals. As before, let (M, J, ⋆) be a symmetric monoidal category such that C is a symmetric monoidal M-actegory.
We are given a model i.e. a morphism f : M ⊙ X → Y in C where we think of M ∈ Ob(M) as the parameter space and f models a true morphism X → Y . We suppose as before that we are provided with a state π X : I → X and a prior π M,0 : J → M . Lastly, we abuse notation and write M in place of M ⊙ I when necessary. Note a prior π M : J → M implies a prior π M ⊙ id : J ⊙ I → M ⊙ I which we also refer to as π M .

Sequential updates
Recall from Definition 2.4 that the state π X and the channel f give us a morphism in C. Since C admits conditionals, it also admits Bayesian inversions. Hence, we get a morphism f † joint : Z → M ⊙ I with respect to the prior state π M,0 on M where Z := X ⊗ Y .
Note that f † joint is not unique. In the previous section, we got around this issue by working in the category PS(C). However, if we want to update the state on M sequentially then this corresponds to sequentially updating objects in PS(M) which will then require us to update the model f or more precisely its image in PS(C). Instead, we introduce Definition 4.1 to ensure that the updated priors remain well defined.
Recall that C = Kl(P) where P is a monad on the symmetric monoidal category (D, I D , * ). Since P is a monad, we have a family of maps η X : X → P(X) for every X ∈ Ob(D). Given a map a : Definition 4.1. Let f : X → Y be a morphism in C and π X : I → X be a state on X. Let y : I D → Y be a morphism in D. We say that the Bayesian inverse of f is uniquely defined at y if for any morphisms g, h : Y → X in C such that g ∼ πX −a.s h and g is a Bayesian inversion of f then η(y); g = η(y); h.
We refer to the morphism y that appears in the definition above as an elementary point of the object Y . A precise definition is as follows.
Definition 4.2. Let Y be an object of C. By an elementary point of Y we mean a morphism y : I D → Y in D. We use δ y to denote the image of y in C i.e. δ y := η(y). Example 4.3. We provide an example of Definition 4.1. Recall from Example 2.3, the category FinStoch. Note that FinStoch = Kl(Dist) where Dist : FinSet → Finset is a symmetric monoidal monad on the symmetric monoidal category Fin-Set whose objects are finite sets and morphisms are functions of sets. Let X be a finite set, π X : I → X be a state on X and f : X → Y be a morphism in FinStoch. By definition, π X corresponds to a probability distribution p X on X while f defines a conditional distribution. Let y 0 : I → Y be a morphism in FinSet. It follows that y 0 is uniquely determined by a point in Y which we abuse notation for and call y 0 as well. The Bayesian inversion g : Y → X is defined by g(y)(x) = f (x)(y)p X (x) x ′ ∈X f (x ′ )(y)p X (x ′ ) if x ′ ∈X f (x ′ )(y)p X (x ′ ) = 0 and if y is such that x ′ ∈X f (x ′ )(y)p X (x ′ ) = 0 then g(y) can be any probability distribution on X. Thus we see in this situation that the Bayesian inversion g is uniquely defined at y 0 if and only if x ′ ∈X f (x ′ )(y 0 )p X (x ′ ) = 0.
In the case of the category Stoch, things are more complicated since it is not always true that a Bayesian inversion exists. However, in certain cases where we are working with subspaces of R and both state and channel are defined using density functions then a similar calculation as above can be performed (cf.[2, Example 3.9]).
Let T := [x 1 ⊗ y 1 , . . . , x n ⊗ y n ] be a list of n elementary points of X ⊗ Y where for every i, z i := x i ⊗ y i satisfies a property to be specified below. We begin with a prior π M,0 on the parameter object M . Let us suppose that we have obtained the i-th sequential update i.e. a state π M,i on M . We define π M,i+1 as follows. By taking the Bayesian inversion of f joint with respect to π M,i , we get f † joint,i : X ⊗ Y → M. The state π M,i+1 on X ⊗ Y is given by the composition

Batch updates
To obtain an update of the prior all at once, we work with an object built from X ⊗ Y but whose points correspond to datasets of a specified cardinality.
Let n ∈ N and we set Z n := ⊗ n (X ⊗ Y ).
The model f induces a morphism where as before z i = x i ⊗ y i . We suppose T is such that the Bayesian inversion of f n joint is uniquely defined at z T . The batch update of the prior π M,0 with respect to T is given by the composition where (f n joint ) † is a Bayesian inversion of f n joint with respect to the prior π M,0 . Let π M,T : I → M denote this updated prior.
We can ask the following question. Under what conditions, can we ensure that π M,n = π M,T where π M,n is as defined at the end of §4.1 for T.  Observe from examples 4.4 and 4.5 that the sequential Bayes update and batch update coincide. This begs the following question. What conditions can we impose to relate the sequential and batch updates in the general setting of the category C used throughout this section.