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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""
Variational Inference (VI) Engine

Implements scalable approximate Bayesian inference via optimization:
- Mean-field variational inference
- Automatic Differentiation Variational Inference (ADVI)
- Evidence Lower Bound (ELBO) optimization
- Coordinate ascent variational inference (CAVI)

Provides high-dimensional posterior approximation when MCMC is intractable.
"""

import numpy as np
from typing import Callable, Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
from scipy.stats import norm, multivariate_normal
from scipy.optimize import minimize


@dataclass
class VariationalDistribution:
    """
    Parametric variational distribution q(z | 位).

    Attributes
    ----------
    family : str
        Distribution family ('normal', 'multivariate_normal')
    parameters : dict
        Distribution parameters
    """
    family: str
    parameters: Dict[str, np.ndarray]

    def sample(self, n_samples: int = 1) -> np.ndarray:
        """Sample from variational distribution."""
        if self.family == 'normal':
            mu = self.parameters['mu']
            sigma = self.parameters['sigma']
            return np.random.normal(mu, sigma, size=(n_samples, len(mu)))
        elif self.family == 'multivariate_normal':
            mu = self.parameters['mu']
            cov = self.parameters['cov']
            return np.random.multivariate_normal(mu, cov, size=n_samples)
        else:
            raise ValueError(f"Unknown family: {self.family}")

    def log_prob(self, z: np.ndarray) -> np.ndarray:
        """Compute log probability."""
        if self.family == 'normal':
            mu = self.parameters['mu']
            sigma = self.parameters['sigma']
            return np.sum(norm.logpdf(z, loc=mu, scale=sigma), axis=-1)
        elif self.family == 'multivariate_normal':
            mu = self.parameters['mu']
            cov = self.parameters['cov']
            return multivariate_normal.logpdf(z, mean=mu, cov=cov)
        else:
            raise ValueError(f"Unknown family: {self.family}")

    def entropy(self) -> float:
        """Compute entropy H[q]."""
        if self.family == 'normal':
            sigma = self.parameters['sigma']
            # H = 0.5 * log(2蟺e蟽虏)
            return 0.5 * np.sum(np.log(2 * np.pi * np.e * sigma**2))
        elif self.family == 'multivariate_normal':
            cov = self.parameters['cov']
            d = len(cov)
            # H = 0.5 * log((2蟺e)^d |危|)
            sign, logdet = np.linalg.slogdet(cov)
            return 0.5 * (d * np.log(2 * np.pi * np.e) + logdet)
        else:
            raise ValueError(f"Unknown family: {self.family}")


class VariationalInference:
    """
    Variational Inference engine.

    Approximates posterior p(z|x) with variational distribution q(z|位)
    by maximizing Evidence Lower Bound (ELBO):

    ELBO(位) = E_q[log p(x,z)] - E_q[log q(z|位)]
            = E_q[log p(x|z)] + E_q[log p(z)] - E_q[log q(z|位)]

    Equivalently: minimize KL(q(z|位) || p(z|x))
    """

    def __init__(
        self,
        log_joint: Callable,
        variational_family: str = 'normal',
        n_samples: int = 100
    ):
        """
        Initialize variational inference.

        Parameters
        ----------
        log_joint : callable
            Log joint probability: log p(x, z)
        variational_family : str
            Variational family ('normal', 'multivariate_normal')
        n_samples : int
            Number of Monte Carlo samples for ELBO estimation
        """
        self.log_joint = log_joint
        self.variational_family = variational_family
        self.n_samples = n_samples
        self.q = None

    def elbo(
        self,
        variational_params: np.ndarray,
        param_shapes: Dict[str, Tuple],
        observed_data: Any
    ) -> float:
        """
        Compute Evidence Lower Bound (ELBO).

        ELBO = E_q[log p(x,z)] - E_q[log q(z)]

        Parameters
        ----------
        variational_params : np.ndarray
            Flattened variational parameters
        param_shapes : dict
            Shapes of each parameter
        observed_data : any
            Observed data x

        Returns
        -------
        float
            ELBO value
        """
        # Unpack parameters
        params = self._unpack_params(variational_params, param_shapes)

        # Create variational distribution
        q = VariationalDistribution(self.variational_family, params)

        # Sample from q
        z_samples = q.sample(self.n_samples)

        # Compute E_q[log p(x, z)]
        log_joint_vals = np.array([self.log_joint(z, observed_data) for z in z_samples])
        expected_log_joint = np.mean(log_joint_vals)

        # Compute E_q[log q(z)]
        log_q_vals = q.log_prob(z_samples)
        expected_log_q = np.mean(log_q_vals)

        # ELBO
        elbo_val = expected_log_joint - expected_log_q

        return elbo_val

    def neg_elbo(self, variational_params: np.ndarray, param_shapes: Dict, observed_data: Any) -> float:
        """Negative ELBO for minimization."""
        return -self.elbo(variational_params, param_shapes, observed_data)

    def fit(
        self,
        observed_data: Any,
        init_params: Dict[str, np.ndarray],
        max_iter: int = 1000,
        method: str = 'L-BFGS-B'
    ) -> VariationalDistribution:
        """
        Fit variational distribution via ELBO optimization.

        Parameters
        ----------
        observed_data : any
            Observed data
        init_params : dict
            Initial variational parameters
        max_iter : int
            Maximum optimization iterations
        method : str
            Optimization method

        Returns
        -------
        VariationalDistribution
            Optimized variational distribution
        """
        # Pack initial parameters
        flat_params, param_shapes = self._pack_params(init_params)

        # Optimize
        result = minimize(
            fun=self.neg_elbo,
            x0=flat_params,
            args=(param_shapes, observed_data),
            method=method,
            options={'maxiter': max_iter, 'disp': True}
        )

        # Unpack optimized parameters
        opt_params = self._unpack_params(result.x, param_shapes)

        # Create variational distribution
        self.q = VariationalDistribution(self.variational_family, opt_params)

        return self.q

    def _pack_params(self, params: Dict[str, np.ndarray]) -> Tuple[np.ndarray, Dict]:
        """Pack parameters into flat array."""
        flat = []
        shapes = {}
        for key, val in params.items():
            flat.append(val.flatten())
            shapes[key] = val.shape
        return np.concatenate(flat), shapes

    def _unpack_params(self, flat: np.ndarray, shapes: Dict) -> Dict[str, np.ndarray]:
        """Unpack flat array into parameters."""
        params = {}
        idx = 0
        for key, shape in shapes.items():
            size = np.prod(shape)
            params[key] = flat[idx:idx+size].reshape(shape)
            idx += size
        return params


class MeanFieldVI(VariationalInference):
    """
    Mean-Field Variational Inference.

    Assumes variational distribution factorizes:
    q(z | 位) = 鈭廮i q_i(z_i | 位_i)

    Uses coordinate ascent variational inference (CAVI) to optimize
    each factor in turn.
    """

    def __init__(
        self,
        log_joint: Callable,
        factor_families: List[str],
        n_samples: int = 100
    ):
        """
        Initialize mean-field VI.

        Parameters
        ----------
        log_joint : callable
            Log joint probability
        factor_families : list
            Distribution family for each factor
        n_samples : int
            Number of samples for ELBO
        """
        super().__init__(log_joint, 'mean_field', n_samples)
        self.factor_families = factor_families
        self.n_factors = len(factor_families)

    def fit_cavi(
        self,
        observed_data: Any,
        init_params: List[Dict[str, np.ndarray]],
        max_iter: int = 100,
        tol: float = 1e-4
    ) -> List[VariationalDistribution]:
        """
        Fit using Coordinate Ascent Variational Inference (CAVI).

        Parameters
        ----------
        observed_data : any
            Observed data
        init_params : list
            Initial parameters for each factor
        max_iter : int
            Maximum CAVI iterations
        tol : float
            Convergence tolerance

        Returns
        -------
        list
            List of optimized factor distributions
        """
        # Initialize factors
        factors = [
            VariationalDistribution(family, params)
            for family, params in zip(self.factor_families, init_params)
        ]

        prev_elbo = -np.inf

        for iteration in range(max_iter):
            # Update each factor in turn
            for i in range(self.n_factors):
                # Update factor i holding others fixed
                factors[i] = self._update_factor(i, factors, observed_data)

            # Compute ELBO
            current_elbo = self._compute_mean_field_elbo(factors, observed_data)

            # Check convergence
            if abs(current_elbo - prev_elbo) < tol:
                print(f"CAVI converged at iteration {iteration}")
                break

            prev_elbo = current_elbo

            if iteration % 10 == 0:
                print(f"Iteration {iteration}, ELBO: {current_elbo:.4f}")

        self.factors = factors
        return factors

    def _update_factor(
        self,
        factor_idx: int,
        factors: List[VariationalDistribution],
        observed_data: Any
    ) -> VariationalDistribution:
        """
        Update a single factor via optimization.

        Parameters
        ----------
        factor_idx : int
            Index of factor to update
        factors : list
            Current factor distributions
        observed_data : any
            Observed data

        Returns
        -------
        VariationalDistribution
            Updated factor
        """
        # This is a simplified version - full implementation would compute
        # conditional expectations analytically for conjugate models

        # For now, use gradient-based optimization
        current_params = factors[factor_idx].parameters

        def factor_neg_elbo(params_flat):
            # Unpack
            if self.factor_families[factor_idx] == 'normal':
                d = len(params_flat) // 2
                mu = params_flat[:d]
                log_sigma = params_flat[d:]
                sigma = np.exp(log_sigma)
                params = {'mu': mu, 'sigma': sigma}
            else:
                raise NotImplementedError

            # Create trial factor
            trial_factor = VariationalDistribution(self.factor_families[factor_idx], params)

            # Replace in factors
            trial_factors = factors.copy()
            trial_factors[factor_idx] = trial_factor

            # Compute ELBO
            elbo = self._compute_mean_field_elbo(trial_factors, observed_data)
            return -elbo

        # Pack current params
        if self.factor_families[factor_idx] == 'normal':
            params_flat = np.concatenate([
                current_params['mu'],
                np.log(current_params['sigma'])
            ])
        else:
            raise NotImplementedError

        # Optimize
        result = minimize(factor_neg_elbo, params_flat, method='L-BFGS-B')

        # Unpack
        if self.factor_families[factor_idx] == 'normal':
            d = len(result.x) // 2
            mu = result.x[:d]
            sigma = np.exp(result.x[d:])
            opt_params = {'mu': mu, 'sigma': sigma}
        else:
            raise NotImplementedError

        return VariationalDistribution(self.factor_families[factor_idx], opt_params)

    def _compute_mean_field_elbo(
        self,
        factors: List[VariationalDistribution],
        observed_data: Any
    ) -> float:
        """
        Compute ELBO for mean-field approximation.

        Parameters
        ----------
        factors : list
            Factor distributions
        observed_data : any
            Observed data

        Returns
        -------
        float
            ELBO
        """
        # Sample from each factor
        samples = []
        for factor in factors:
            samples.append(factor.sample(self.n_samples))

        # Combine samples
        z_samples = np.column_stack(samples)

        # Compute E_q[log p(x, z)]
        log_joint_vals = np.array([self.log_joint(z, observed_data) for z in z_samples])
        expected_log_joint = np.mean(log_joint_vals)

        # Compute E_q[log q(z)] = sum_i E_q[log q_i(z_i)]
        expected_log_q = 0.0
        for i, factor in enumerate(factors):
            log_q_vals = factor.log_prob(samples[i])
            expected_log_q += np.mean(log_q_vals)

        return expected_log_joint - expected_log_q


class ADVI:
    """
    Automatic Differentiation Variational Inference (ADVI).

    Transforms constrained latent variables to unconstrained space,
    then performs VI with Gaussian variational family.

    Uses reparameterization trick for low-variance gradient estimates.
    """

    def __init__(
        self,
        log_joint: Callable,
        transform_fn: Optional[Callable] = None,
        inverse_transform_fn: Optional[Callable] = None
    ):
        """
        Initialize ADVI.

        Parameters
        ----------
        log_joint : callable
            Log joint in original (possibly constrained) space
        transform_fn : callable, optional
            Transform to unconstrained space
        inverse_transform_fn : callable, optional
            Inverse transform
        """
        self.log_joint = log_joint
        self.transform_fn = transform_fn or (lambda x: x)
        self.inverse_transform_fn = inverse_transform_fn or (lambda x: x)

    def fit(
        self,
        observed_data: Any,
        latent_dim: int,
        n_samples: int = 10,
        max_iter: int = 1000,
        learning_rate: float = 0.01
    ) -> Tuple[np.ndarray, np.ndarray]:
        """
        Fit ADVI using gradient ascent on ELBO.

        Parameters
        ----------
        observed_data : any
            Observed data
        latent_dim : int
            Dimension of latent variables
        n_samples : int
            Number of samples for ELBO gradient estimation
        max_iter : int
            Maximum iterations
        learning_rate : float
            Learning rate for gradient ascent

        Returns
        -------
        tuple
            (mean, log_std) of variational distribution
        """
        # Initialize variational parameters (Gaussian in unconstrained space)
        mu = np.zeros(latent_dim)
        log_sigma = np.zeros(latent_dim)

        for iteration in range(max_iter):
            # Sample from standard normal
            epsilon = np.random.randn(n_samples, latent_dim)

            # Reparameterization: z = 渭 + 蟽 * 蔚
            sigma = np.exp(log_sigma)
            z_unconstrained = mu + sigma * epsilon

            # Transform to constrained space
            z_constrained = np.array([self.inverse_transform_fn(z) for z in z_unconstrained])

            # Compute log joint
            log_joints = np.array([self.log_joint(z, observed_data) for z in z_constrained])

            # Compute ELBO (with entropy)
            entropy = 0.5 * np.sum(np.log(2 * np.pi * np.e * sigma**2))
            elbo = np.mean(log_joints) + entropy

            # Gradient estimates (simplified - would use autograd in practice)
            grad_mu = np.mean((log_joints[:, np.newaxis] - elbo) * (z_unconstrained - mu) / (sigma**2), axis=0)
            grad_log_sigma = np.mean(
                (log_joints[:, np.newaxis] - elbo) * ((z_unconstrained - mu)**2 / sigma**2 - 1),
                axis=0
            )

            # Update parameters
            mu = mu + learning_rate * grad_mu
            log_sigma = log_sigma + learning_rate * grad_log_sigma

            if iteration % 100 == 0:
                print(f"Iteration {iteration}, ELBO: {elbo:.4f}")

        return mu, log_sigma