# 推导自然梯度下降的多项分布形式（logit为自变量）


```python
import sympy
import numpy as np
sympy.init_printing()
```


```python
theta = [sympy.symbols(f'phi_{i}') for i in range(1, 4)]
```


```python
theta
```


```python
exps = [sympy.exp(theta_i) for theta_i in theta]
```


```python
thetas = [exp/sum(exps) for exp in exps]
```


```python
thetas
```


```python
xs = [sympy.symbols(f'x_{i}') for i in range(1, 4)]
```


```python
ll = sympy.log(thetas[0]**xs[0]*thetas[1]**xs[1]*thetas[2]**xs[2])
```


```python
sympy.diff(ll, theta[2]).simplify()
```



$$\displaystyle \frac{x_{1} e^{\phi_{2}} + x_{1} e^{\phi_{3}} - x_{2} e^{\phi_{1}} - x_{3} e^{\phi_{1}}}{e^{\phi_{1}} + e^{\phi_{2}} + e^{\phi_{3}}}$$
$$\displaystyle \frac{- x_{1} e^{\phi_{2}} + x_{2} e^{\phi_{1}} + x_{2} e^{\phi_{3}} - x_{3} e^{\phi_{2}}}{e^{\phi_{1}} + e^{\phi_{2}} + e^{\phi_{3}}}$$
$$\displaystyle \frac{- x_{1} e^{\phi_{3}} - x_{2} e^{\phi_{3}} + x_{3} e^{\phi_{1}} + x_{3} e^{\phi_{2}}}{e^{\phi_{1}} + e^{\phi_{2}} + e^{\phi_{3}}}$$

# 计算信息矩阵


```python
-sympy.diff(ll, theta[2], 2).simplify().subs(xs[0], thetas[0]).subs(xs[1], thetas[1]).subs(xs[2], thetas[2]).simplify()

```


```python
-sympy.diff(ll, theta[1], 2).simplify().subs(xs[0], thetas[0]).subs(xs[1], thetas[1]).subs(xs[2], thetas[2]).simplify()

```


```python
-sympy.diff(ll, theta[0], 2).simplify().subs(xs[0], thetas[0]).subs(xs[1], thetas[1]).subs(xs[2], thetas[2]).simplify()
```

$$\displaystyle \frac{\left(e^{\phi_{2}} + e^{\phi_{3}}\right) e^{\phi_{1}}}{e^{2 \phi_{1}} + e^{2 \phi_{2}} + e^{2 \phi_{3}} + 2 e^{\phi_{1} + \phi_{2}} + 2 e^{\phi_{1} + \phi_{3}} + 2 e^{\phi_{2} + \phi_{3}}}$$

$$\displaystyle \frac{\left(e^{\phi_{1}} + e^{\phi_{3}}\right) e^{\phi_{2}}}{e^{2 \phi_{1}} + e^{2 \phi_{2}} + e^{2 \phi_{3}} + 2 e^{\phi_{1} + \phi_{2}} + 2 e^{\phi_{1} + \phi_{3}} + 2 e^{\phi_{2} + \phi_{3}}}$$




$$\displaystyle \frac{\left(e^{\phi_{1}} + e^{\phi_{2}}\right) e^{\phi_{3}}}{e^{2 \phi_{1}} + e^{2 \phi_{2}} + e^{2 \phi_{3}} + 2 e^{\phi_{1} + \phi_{2}} + 2 e^{\phi_{1} + \phi_{3}} + 2 e^{\phi_{2} + \phi_{3}}}$$

# 计算 $\partial_\theta\log P_\theta(x)$


```python
sympy.diff(ll, theta[0]).simplify()
```

$$\displaystyle \frac{x_{1} e^{\phi_{2}} + x_{1} e^{\phi_{3}} - x_{2} e^{\phi_{1}} - x_{3} e^{\phi_{1}}}{e^{\phi_{1}} + e^{\phi_{2}} + e^{\phi_{3}}}$$


```python
sympy.diff(ll, theta[1]).simplify()
```


$$\displaystyle \frac{- x_{1} e^{\phi_{2}} + x_{2} e^{\phi_{1}} + x_{2} e^{\phi_{3}} - x_{3} e^{\phi_{2}}}{e^{\phi_{1}} + e^{\phi_{2}} + e^{\phi_{3}}}$$


```python
sympy.diff(ll, theta[2]).simplify()
```

$$\displaystyle \frac{- x_{1} e^{\phi_{3}} - x_{2} e^{\phi_{3}} + x_{3} e^{\phi_{1}} + x_{3} e^{\phi_{2}}}{e^{\phi_{1}} + e^{\phi_{2}} + e^{\phi_{3}}}$$



```python
sympy.diff(ll, theta[0]).simplify()
```



a218347523bc61d42db0f56596689fd1016142c9
ipynb
Jupyter Notebook
formulation/demo.ipynb
zhanglei1172/automl
03533c4e7499126a1aa214b69656430c3836e609
MIT
formulation/demo.ipynb
zhanglei1172/automl
03533c4e7499126a1aa214b69656430c3836e609
MIT
formulation/demo.ipynb
zhanglei1172/automl
03533c4e7499126a1aa214b69656430c3836e609
MIT
Qwen/Qwen-72B
1. YES
2. YES