```python
import numpy as np
import pandas as pd
import sympy as sym
from sympy import init_printing
from lgbayes.models import LinearGaussianBN
init_printing(use_latex=True)
%matplotlib inline

%load_ext autoreload
%autoreload 2
```

    The autoreload extension is already loaded. To reload it, use:
      %reload_ext autoreload



```python
def sample():
    u = np.random.normal(0,s_u,size=n)
    z = w_zu*u + np.random.normal(0,s_z,size=n)
    x = w_xz*z+np.random.normal(0,s_x,size=n)
    y = w_yx*x + w_yz*z + w_yu*u+ np.random.normal(0,s_y,size=n)
    return {"U":u,"Z":z,"X":x,"Y":y}
```


```python
s_u = 1
s_z = 0.3
s_x = 1
s_y = 0.5

w_zu = 2
w_xz = 0.5
w_yx = 0.5
w_yz = -1.0
w_yu = 3.0


beta_x = 0.5
beta_z = -1
sigma = 0.5
n = 1000
```


```python
data_dict = sample()
df1 = pd.DataFrame(data_dict,columns=["U","Z","X","Y"])
df1.cov()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>U</th>
      <th>Z</th>
      <th>X</th>
      <th>Y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>U</th>
      <td>0.976613</td>
      <td>1.945132</td>
      <td>1.001346</td>
      <td>1.480123</td>
    </tr>
    <tr>
      <th>Z</th>
      <td>1.945132</td>
      <td>3.964203</td>
      <td>2.033707</td>
      <td>2.878700</td>
    </tr>
    <tr>
      <th>X</th>
      <td>1.001346</td>
      <td>2.033707</td>
      <td>2.067833</td>
      <td>2.034622</td>
    </tr>
    <tr>
      <th>Y</th>
      <td>1.480123</td>
      <td>2.878700</td>
      <td>2.034622</td>
      <td>2.836955</td>
    </tr>
  </tbody>
</table>
</div>




```python
model = LinearGaussianBN()
model.add_var("X",[])
model.add_var("Y",["X"])
print(model)
model.cov
a = model.observe(["X"],["x"])
a.cov
a.mu[0].simplify()
```


```python
model.mu
```


```python
model.cov
```


```python
model.information_matrix
```


```python
model = LinearGaussianBN()
model.add_var("U",None,[0],s_u**2)
model.add_var("Z",["U"],[0,w_zu],s_z**2)
model.add_var("X",["Z"],[0,w_xz],s_x**2)
model.add_var("Y",["U","Z","X"],[0,w_yu,w_yz,w_yx],s_y**2)

print (model)
data_dict = dict(zip(model.variables,model.sample(n).T))
df = pd.DataFrame(data_dict,columns=model.variables)
df.cov()
```

    U ~ N(0 ; 1)
    Z ~ N(2*U ; 0.09)
    X ~ N(0.5*Z ; 1)
    Y ~ N(3*U + -1*Z + 0.5*X ; 0.25)
    





<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>U</th>
      <th>Z</th>
      <th>X</th>
      <th>Y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>U</th>
      <td>1.004677</td>
      <td>2.000284</td>
      <td>0.997798</td>
      <td>1.499634</td>
    </tr>
    <tr>
      <th>Z</th>
      <td>2.000284</td>
      <td>4.073505</td>
      <td>2.018449</td>
      <td>2.893577</td>
    </tr>
    <tr>
      <th>X</th>
      <td>0.997798</td>
      <td>2.018449</td>
      <td>2.016385</td>
      <td>1.946916</td>
    </tr>
    <tr>
      <th>Y</th>
      <td>1.499634</td>
      <td>2.893577</td>
      <td>1.946916</td>
      <td>2.814356</td>
    </tr>
  </tbody>
</table>
</div>




```python

```

641627ba15d0eef78c7c84186b46e442d57099ab
ipynb
Jupyter Notebook
Multivariate Gaussians.ipynb
finnhacks42/linear-gaussian-bn
63e3355bbdcb0c7218e41b1c33858b7d9917177e
MIT
Multivariate Gaussians.ipynb
finnhacks42/linear-gaussian-bn
63e3355bbdcb0c7218e41b1c33858b7d9917177e
MIT
Multivariate Gaussians.ipynb
finnhacks42/linear-gaussian-bn
63e3355bbdcb0c7218e41b1c33858b7d9917177e
MIT
Qwen/Qwen-72B
1. YES
2. YES