0
# Variational Inference Results
1
2
Container for Automatic Differentiation Variational Inference (ADVI) results and approximate posterior samples. The CmdStanVB class provides access to variational parameters and generated posterior samples.
3
4
## Capabilities
5
6
### Variational Parameters
7
8
Access the mean-field or full-rank variational parameters that define the approximate posterior.
9
10
```python { .api }
11
def variational_params_np(self):
12
"""
13
Get variational parameters as NumPy array.
14
15
Returns:
16
np.ndarray: Variational parameter values (means and standard deviations)
17
"""
18
19
def variational_params_pd(self):
20
"""
21
Get variational parameters as pandas DataFrame.
22
23
Returns:
24
pd.DataFrame: Parameters with names as index
25
"""
26
27
def variational_params_dict(self):
28
"""
29
Get variational parameters as dictionary.
30
31
Returns:
32
dict: Mapping from parameter names to arrays
33
"""
34
```
35
36
### Posterior Samples
37
38
Access samples drawn from the variational approximation.
39
40
```python { .api }
41
def variational_sample(self):
42
"""
43
Get posterior sample from variational approximation.
44
45
Returns:
46
np.ndarray: Draws from approximate posterior (draws, parameters)
47
"""
48
49
def variational_sample_pd(self):
50
"""
51
Get posterior sample as pandas DataFrame.
52
53
Returns:
54
pd.DataFrame: Draws with parameter names as columns
55
"""
56
```
57
58
### Variable Access
59
60
```python { .api }
61
def stan_variable(self, var):
62
"""
63
Get posterior sample for specific Stan variable.
64
65
Parameters:
66
- var (str): Variable name
67
68
Returns:
69
np.ndarray: Draws for the variable
70
"""
71
72
def stan_variables(self):
73
"""
74
Get all Stan variables as dictionary.
75
76
Returns:
77
dict: Mapping from variable names to draw arrays
78
"""
79
```
80
81
### File Operations
82
83
```python { .api }
84
def save_csvfiles(self, dir=None):
85
"""
86
Save CSV output files to directory.
87
88
Parameters:
89
- dir (str or PathLike, optional): Target directory
90
91
Returns:
92
None
93
"""
94
```
95
96
## Properties
97
98
```python { .api }
99
# Variational information
100
vb.columns # int: Number of parameters
101
vb.column_names # Tuple[str, ...]: Parameter names
102
vb.eta # float: Final step size
103
vb.metadata # InferenceMetadata: Run configuration and timing
104
```
105
106
## Usage Examples
107
108
```python
109
# Run variational inference
110
vb = model.variational(data=data, algorithm="meanfield", draws=2000)
111
112
# Access variational parameters
113
var_params = vb.variational_params_dict()
114
print("Variational means:", var_params)
115
116
# Get posterior samples
117
posterior_samples = vb.variational_sample()
118
theta_draws = vb.stan_variable("theta")
119
120
print(f"Posterior mean for theta: {theta_draws.mean()}")
121
print(f"Posterior std for theta: {theta_draws.std()}")
122
```