CUQI-DTU / CUQIpy

https://cuqi-dtu.github.io/CUQIpy/
Apache License 2.0
41 stars 7 forks source link

When FD gradient is enabled for posterior, UQ method in Bayesian problem is not able to detect that in some cases #456

Closed amal-ghamdi closed 1 month ago

amal-ghamdi commented 1 month ago

Description When FD gradient is enabled for posterior, UQ method in Bayesian problem is not able to detect that in some cases because it checks for the likelihood and the prior gradients that might not be available

Example to reproduce Provide a code example to reproduce the bug

import cuqi
import numpy as np
gaussian = cuqi.distribution.Gaussian(np.zeros(2)+0.5, 0.3**2)
x_test = cuqi.distribution.UserDefinedDistribution(logpdf_func=gaussian.logpdf, sample_func=gaussian.sample, dim=2)
A_test = cuqi.model.Model(forward=lambda x_test: x_test**2, range_geometry=2, domain_geometry=2)
y_test = cuqi.distribution.Gaussian(A_test(x_test), 0.1**2)
x_test_true = x_test.sample(1)
y_test_data = y_test(x_test=x_test_true).sample()
BP_test = cuqi.problem.BayesianProblem(x_test, y_test)
BP_test.set_data(y_test=y_test_data)
BP_test.posterior.enable_FD()
BP_test.UQ()

Output

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In [1], [line 14](vscode-notebook-cell:?execution_count=1&line=14)
     [12](vscode-notebook-cell:?execution_count=1&line=12) BP_test.set_data(y_test=y_test_data)
     [13](vscode-notebook-cell:?execution_count=1&line=13) BP_test.posterior.enable_FD()
---> [14](vscode-notebook-cell:?execution_count=1&line=14) BP_test.UQ()

File ~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:411, in BayesianProblem.UQ(self, Ns, Nb, percent, exact)
    [388](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:388) """ Run an Uncertainty Quantification (UQ) analysis on the Bayesian problem and provide a summary of the results.
    [389](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:389) 
    [390](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:390) Parameters
   (...)
    [408](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:408)     Samples from the posterior. The samples can be used to compute further statistics and plots.
    [409](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:409) """
    [410](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:410) print(f"Computing {Ns} samples")
--> [411](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:411) samples = self.sample_posterior(Ns, Nb)
    [413](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:413) print("Plotting results")
    [414](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:414) # Gibbs case

File ~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:357, in BayesianProblem.sample_posterior(self, Ns, Nb, callback)
    [354](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:354)     return self._sampleRegularizedLinearRTO(Ns, Nb, callback)
    [356](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:356) else:
--> [357](https://file+.vscode-resource.vscode-cdn.net/Users/amal/Documents/research_code/CUQI-DTU/Collab-BrainEfflux/code/ear_aqueducts/~/Documents/research_code/CUQI-DTU/CUQIpy/cuqi/problem/_problem.py:357)     raise NotImplementedError(f"Automatic sampler choice is not implemented for model: {type(self.model)}, likelihood: {type(self.likelihood.distribution)} and prior: {type(self.prior)} and dim {self.prior.dim}. Manual sampler choice can be done via the 'sampler' module. Posterior distribution can be extracted via '.posterior' of any testproblem (BayesianProblem).")

NotImplementedError: Automatic sampler choice is not implemented for model: <class 'cuqi.model._model.Model'>, likelihood: <class 'cuqi.distribution._gaussian.Gaussian'> and prior: <class 'cuqi.distribution._custom.UserDefinedDistribution'> and dim 2. Manual sampler choice can be done via the 'sampler' module. Posterior distribution can be extracted via '.posterior' of any testproblem (BayesianProblem).