Skip to content

Commit 551e1a0

Browse files
authored
Added a Multivariate Log Gamma Function File (#7804)
* Added a Multivariate Log Gamma Function File Signed-off-by: Pranay22077 <[email protected]> * code fixes and framing issues * removed unwanted bold ---------
1 parent a82f1b5 commit 551e1a0

File tree

1 file changed

+95
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/mvlgamma

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
Title: '.mvlgamma()'
3+
Description: 'Computes the multivariate log-gamma function for tensor inputs.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
- 'Machine Learning'
8+
Tags:
9+
- 'PyTorch'
10+
- 'Tensor Operations'
11+
- 'Math Functions'
12+
- 'Probability'
13+
- 'Statistics'
14+
CatalogContent:
15+
- 'intro-to-py-torch-and-neural-networks'
16+
- 'paths/data-science'
17+
---
18+
19+
The **`torch.mvlgamma()`** function in PyTorch computes the multivariate log-gamma function for tensor inputs. It is widely used in multivariate statistics, particularly to calculate normalization constants in distributions such as the Wishart or multivariate Gamma distributions. Denoted as ${(\ln(\Gamma_p(a))\)}$, the multivariate log-gamma function generalizes the standard log-gamma function to matrix-valued arguments and is evaluated element-wise for each tensor value. It is defined for ${\(a > \frac{p-1}{2}\)}$, where ${\(p\)}$ is the specified dimension.
20+
21+
The function is commonly applied in probability distributions over matrices, with the Wishart distribution being a frequent use case.
22+
23+
The formula for the multivariate log-gamma function is:
24+
25+
$$\ln(\Gamma_p(a)) = \frac{p(p-1)}{4} \ln(\pi) + \sum_{i=1}^{p} \ln\left(\Gamma\left(a - \frac{i-1}{2}\right)\right)$$
26+
27+
where $\Gamma(\cdot)$ is the standard gamma function.
28+
29+
## Syntax
30+
31+
```pseudo
32+
torch.mvlgamma(input, p, out = None)
33+
```
34+
35+
**Parameters:**
36+
37+
- `input` (Tensor): Tensor of values for which to compute the multivariate log-gamma function.
38+
- `p` (int): Number of dimensions in the multivariate gamma function. Must be a positive integer.
39+
- `out` (Tensor, optional): Tensor to store the output. If provided, must be broadcastable to the shape of the result.
40+
41+
**Return value:**
42+
43+
Returns a tensor containing the multivariate log-gamma values for each element in `input`, evaluated element-wise according to the specified dimension `p`.
44+
45+
## Example 1: Element-wise computation
46+
47+
In this example, `torch.mvlgamma()` computes the multivariate log-gamma function for a 1D tensor:
48+
49+
```py
50+
import torch
51+
52+
# Input tensor (values must be > (p-1)/2)
53+
# Here p = 3, so input elements must be > (3-1)/2 = 1
54+
a = torch.tensor([1.5, 2.0, 3.5])
55+
p_dimension = 3
56+
57+
# Compute the multivariate log gamma function
58+
result = torch.mvlgamma(a, p=p_dimension)
59+
60+
print(f"Input tensor: {a}")
61+
print(f"Dimension p: {p_dimension}")
62+
print(f"Result (mvlgamma): {result}")
63+
```
64+
65+
The output of this code is:
66+
67+
```shell
68+
Input tensor: tensor([1.5000, 2.0000, 3.5000])
69+
Dimension p: 3
70+
Result (mvlgamma): tensor([2.1687, 1.5963, 3.8959])
71+
```
72+
73+
## Example 2: Comparison with standard lgamma
74+
75+
In this example, `torch.mvlgamma()` is computed with `p=1` to show it matches the standard `torch.lgamma()`:
76+
77+
```py
78+
import torch
79+
80+
a_scalar = torch.tensor([2.5, 4.0])
81+
p1_result = torch.mvlgamma(a_scalar, p=1)
82+
lgamma_result = torch.lgamma(a_scalar)
83+
84+
print(f"Input (p=1): {a_scalar}")
85+
print(f"mvlgamma (p=1): {p1_result}")
86+
print(f"lgamma: {lgamma_result}")
87+
```
88+
89+
The output of this code is:
90+
91+
```shell
92+
Input (p=1): tensor([2.5000, 4.0000])
93+
mvlgamma (p=1): tensor([0.2847, 1.7918])
94+
lgamma: tensor([0.2847, 1.7918])
95+
```

0 commit comments

Comments
 (0)