Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Calculate conditional and marginal distributions of the multivariate normal distribution
ResourceFunction["ConditionedMultinormalDistribution"][MultinormalDistribution[μ,Σ],i→xi] returns the MultinormalDistribution obtained by conditioning coordinate i to value xi. | |
ResourceFunction["ConditionedMultinormalDistribution"][…,{i1→xi1,i2→xi2,…}] conditions on multiple coordinates. | |
ResourceFunction["ConditionedMultinormalDistribution"][…,{i1,i2,…}→{xi1,xi2,…}] can also be used. | |
ResourceFunction["ConditionedMultinormalDistribution"][…,…,{n1,n2,…}] returns the marginal distributions of coordinates ni after conditioning. | |
ResourceFunction["ConditionedMultinormalDistribution"][Inactive[MultinormalDistribution][…],…] works with SparseArray and SymmetrizedArray objects. |
Condition a 2D symbolic multivariate normal distribution on the value of the second coordinate:
| In[1]:= |
| Out[2]= |
A NormalDistribution is returned if you specify that the first marginal should be computed:
| In[3]:= |
| Out[3]= |
Generate a random MultinormalDistribution:
| In[4]:= | ![]() |
Calculate how the expected value of the second coordinate depends on the value of the first:
| In[5]:= |
| Out[5]= |
Calculate the derivative:
| In[6]:= |
| Out[6]= |
Condition on multiple indices and obtain the means of multiple marginals:
| In[7]:= |
| Out[7]= |
Generate some (x,y) data:
| In[8]:= |
| Out[9]= | ![]() |
Perform a simple linear fit by estimating the joint distribution of the data and then condition it on x:
| In[10]:= | ![]() |
| Out[10]= |
Plot the result:
| In[11]:= | ![]() |
| Out[11]= | ![]() |
MultinormalDistribution normally converts SparseArray and StructuredArray objects to regular lists:
| In[12]:= |
| Out[13]= | ![]() |
Use Inactive to keep the covariance matrix packed as a sparse array:
| In[14]:= | ![]() |
| Out[14]= |
This is significantly more computationally efficient than having the covariance unpack into a regular list:
| In[15]:= | ![]() |
| Out[15]= | ![]() |
Conditioning on variables can be done with Conditioned, but it is less convenient and results in a general ProbabilityDistribution:
| In[16]:= | ![]() |
| Out[16]= | ![]() |
The same distribution with ConditionedMultinormalDistribution:
| In[17]:= | ![]() |
| Out[17]= |
The PDFs are the same up to a minus sign under the square:
| In[18]:= | ![]() |
| Out[18]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License