```python
# import libraries and modules
import sympy as sm

from sympy.stats import Normal, density, E
```


```python
# define symbols used to create distributions
x, mean1, std1, mean2, std2 = sm.symbols('x mean1 std1 mean2 std2')
```


```python
# create univariate gaussian p(x)
p = Normal('p', mean1, std1)
p
```




$\displaystyle p$




```python
# find probability density function of p(x)
dp = density(p)(x)
dp
```




$\displaystyle \frac{\sqrt{2} e^{- \frac{\left(- mean_{1} + x\right)^{2}}{2 std_{1}^{2}}}}{2 \sqrt{\pi} std_{1}}$




```python
# create univariate gaussian q(x)
q = Normal('q', mean2, std2)
q
```




$\displaystyle q$




```python
# find probability density function of q(x)
dq = density(q)(x)
dq
```




$\displaystyle \frac{\sqrt{2} e^{- \frac{\left(- mean_{2} + x\right)^{2}}{2 std_{2}^{2}}}}{2 \sqrt{\pi} std_{2}}$




```python
# calculate KL Divergence between p(x) and q(x)
kl = E(sm.log(dp/dq))
kl
```




$\displaystyle \log{\left(\frac{std_{2} e^{- \frac{\left(- mean_{1} + x\right)^{2}}{2 std_{1}^{2}}} e^{\frac{\left(- mean_{2} + x\right)^{2}}{2 std_{2}^{2}}}}{std_{1}} \right)}$




```python
# make the formula more presentable
sm.expand(kl, force=True).subs({x:mean1, x**2:mean1**2+std1**2}).expand().collect(std2)
```




$\displaystyle - \log{\left(std_{1} \right)} + \log{\left(std_{2} \right)} - \frac{1}{2} + \frac{\frac{mean_{1}^{2}}{2} - mean_{1} mean_{2} + \frac{mean_{2}^{2}}{2} + \frac{std_{1}^{2}}{2}}{std_{2}^{2}}$



e859f9bdb718ba3a7b07329326f712c4452ee349
ipynb
Jupyter Notebook
code/KLDinSympy.ipynb
faizankshaikh/expose_klDiv
1b139240e30e4e3e6eb8f062f2d52a37fabae1fd
MIT
code/KLDinSympy.ipynb
faizankshaikh/expose_klDiv
1b139240e30e4e3e6eb8f062f2d52a37fabae1fd
MIT
code/KLDinSympy.ipynb
faizankshaikh/expose_klDiv
1b139240e30e4e3e6eb8f062f2d52a37fabae1fd
MIT
Qwen/Qwen-72B
1. YES
2. YES