From 712dbf41493017d8cdaaeb8b9f5da3c539adec9c Mon Sep 17 00:00:00 2001
From: faridm69 <faridmohammadi69@gmail.com>
Date: Thu, 9 Jul 2020 10:08:45 +0200
Subject: [PATCH] [BayesInference] optional multiprocessing argument for MCMC.

---
 BayesValidRox/BayesInference/BayesInference.py     |  2 ++
 BayesValidRox/BayesInference/MCMC.py               | 14 +++++++++-----
 .../surrogate_models/RegressionFastARD.py          |  4 ++--
 BayesValidRox/surrogate_models/surrogate_models.py |  2 +-
 4 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/BayesValidRox/BayesInference/BayesInference.py b/BayesValidRox/BayesInference/BayesInference.py
index 5a8efd4f4..5befce85e 100644
--- a/BayesValidRox/BayesInference/BayesInference.py
+++ b/BayesValidRox/BayesInference/BayesInference.py
@@ -72,6 +72,7 @@ class BayesInference:
         self.Bootstrap = False
         self.BootstrapItrNr = 1
         self.BootstrapNoise = 0.05
+        self.MultiProcessMCMC = None
         
         self.Corner_title_fmt = '.3f' #'.4e'
         
@@ -678,6 +679,7 @@ class BayesInference:
             # TODO: MCMC
             initsamples = None if self.Samples is None else self.Samples
             nsteps = 1000 if self.NrofSamples is None else self.NrofSamples
+            multiprocessing = True if self.MultiProcessMCMC is None else self.MultiProcessMCMC
             MCMC_ = MCMC(self, initsamples=initsamples, nwalkers=2*self.PCEModel.NofPa,
                          nsteps = nsteps)
             self.Posterior_df = MCMC_.run_sampler(Data[0], TotalSigma2)
diff --git a/BayesValidRox/BayesInference/MCMC.py b/BayesValidRox/BayesInference/MCMC.py
index 029be7754..ae6bb7ba6 100755
--- a/BayesValidRox/BayesInference/MCMC.py
+++ b/BayesValidRox/BayesInference/MCMC.py
@@ -22,7 +22,7 @@ class MCMC():
     ntemps = 20
     """
     def __init__(self, BayesOpts, initsamples = None, nwalkers = 20, nburn = 100, 
-                 nsteps = 1000, verbose = False):
+                 nsteps = 1000, multiprocessing=True, verbose = False):
         
         self.BayesOpts      = BayesOpts
         self.initsamples    = initsamples
@@ -30,6 +30,7 @@ class MCMC():
         self.nburn          = nburn
         self.nsteps         = nsteps
         self.verbose        = verbose
+        self.mp             = multiprocessing
         
     def run_sampler(self, Observation, TotalSigma2):
         
@@ -54,12 +55,15 @@ class MCMC():
             for idxDim in range(ndim):
                 lower, upper = np.min(self.initsamples[:,idxDim]),np.max(self.initsamples[:,idxDim])
                 initsamples[:,idxDim] = st.uniform(loc=lower, scale=upper-lower).rvs(size=self.nwalkers)
+        
+        if self.mp:
+            with Pool() as pool:
+                sampler = emcee.EnsembleSampler(self.nwalkers, ndim, self.log_posterior, args=[Observation, TotalSigma2], pool=pool)
+                sampler.run_mcmc(initsamples, self.nsteps, progress=True)
             
-        with Pool() as pool:
-            sampler = emcee.EnsembleSampler(self.nwalkers, ndim, self.log_posterior, args=[Observation, TotalSigma2], pool=pool)
+        else:
+            sampler = emcee.EnsembleSampler(self.nwalkers, ndim, self.log_posterior, args=[Observation, TotalSigma2])
             sampler.run_mcmc(initsamples, self.nsteps, progress=True)
-        # sampler = emcee.EnsembleSampler(nwalkers, ndim, log_posterior, args=[Observation, TotalSigma2])
-        # sampler.run_mcmc(starting_guesses, nsteps, progress=True)
         
         # sampler.chain is of shape (nwalkers, nsteps, ndim)
         # we'll throw-out the burn-in points and reshape:
diff --git a/BayesValidRox/surrogate_models/RegressionFastARD.py b/BayesValidRox/surrogate_models/RegressionFastARD.py
index c955d0204..82825c22d 100755
--- a/BayesValidRox/surrogate_models/RegressionFastARD.py
+++ b/BayesValidRox/surrogate_models/RegressionFastARD.py
@@ -51,7 +51,7 @@ def update_precisions(Q,S,q,s,A,active,tol,n_samples,clf_bias):
     same_features  = np.sum( theta[~recompute] > 0) == 0
     
     # changes in precision for features already in model is below threshold
-    no_delta       = np.sum( abs( Anew - Arec ) > tol ) == 0
+    no_delta       = np.sum( abs( Anew - Arec ) < tol ) == 0
     
     # check convergence: if no features to add or delete and small change in 
     #                    precision for current features then terminate
@@ -267,7 +267,7 @@ class RegressionFastARD():
             rss     = np.sum( ( y - np.dot(X[:,active] , Mn) )**2 )
             beta    = n_samples - np.sum(active) + np.sum(Aa * Sdiag )
             beta   /= ( rss + np.finfo(np.float32).eps )
-
+            print("beta itr %s"%i, beta)
             # update precision parameters of coefficients
             A,converged  = update_precisions(Q,S,q,s,A,active,self.tol,
                                              n_samples,False)
diff --git a/BayesValidRox/surrogate_models/surrogate_models.py b/BayesValidRox/surrogate_models/surrogate_models.py
index b357e53e3..6ad90b639 100644
--- a/BayesValidRox/surrogate_models/surrogate_models.py
+++ b/BayesValidRox/surrogate_models/surrogate_models.py
@@ -505,7 +505,7 @@ class aPCE:
             
             elif RegMethod == 'FastARD':
                 clf_poly = RegressionFastARD(start=startBasisIndices,fit_intercept=False,compute_score=compute_score,
-                                              tol= 1e-3)
+                                              n_iter = 1000, tol= 1e-5)
                 
             elif RegMethod == 'LARS':
                 clf_poly = linear_model.Lars(fit_intercept=False)
-- 
GitLab