Bayesian Deep Learning

Bayesian data analysis & Neural network


"Don't hope that events will turn out the way you want, welcome events in whichever way they happen" - Epictetus

This sentense sums up what we're trying to achieve through data science and here bayesian data analysis : implement models who can adapt to the data you feed it on input, no matter its size as long as it is consistent with the nature of the data the model has been trained with.

When we think about time series forecasting, it is common to use Neural Network in order to forecast our predictions by using either ANN or LSTM/GRU RNNs in order to make single-step or multi-step forecasting.
Yet, even though time series forecasting gives pretty good results with common deep learning approaches, it is still important to quantify the uncertainty on te predictions made by such models. This is why we're gonna implement a neural network with a quantification of the prediction uncertainty : a Bayesian neural network.

The theory on this topic shall be exposed in another article, the main thing to keep in mind is that the Bayesian neural network implemented here is based on a principle called : Variational Inference.
Basically, the intuition behind this concept is we're trying to approach the true posterior distribution \(p(y|x)\) with an approximation \(q(x)\)

The model will be implemented with Tensorflow Probability. It is a new framework designed to incorporate probabilistic programming in machine learning and deep learning models. Here we'll implement a bayesian neural network.

The project will consist in 2 parts :

  1. Regression with Tensorflow probability on artificial data
  2. Application of variational inference on real world data : stock forecasting

To begin this project, we'll need to precise 2 variables we'll instanciate for the rest of the project besides all the necessary known packages of Pandas and NumPy :

import tensorflow_probability as tfp
								
tfd = tfp.distributions

Let's create our artificial data :

w0 = 0.125
b0 = 5.
x_range = [-20,60]
def load_dataset(n=150, n_tst=150):
  np.random.seed(42)
  def s(x):
    g = (x - x_range[0]) / (x_range[1] - x_range[0])
    return 3 * (0.25 + g**2.)
  x = (x_range[1] - x_range[0]) * np.random.randn(n) + x_range[0]
  eps = np.random.randn(n) * s(x)
  y = (w0 * x * (1. + np.sin(x)) + b0) + eps
  x = x[...,np.newaxis]
  x_tst = np.linspace(*x_range, num=n_tst).astype(np.float32)
  x_tst = x_tst[...,np.newaxis]
  return y, x, x_tst
y, x, x_tst = load_dataset()
plt.figure()
plt.scatter(x,y)
plt.show()


We get the following dataset :

In order to approximate the distribution of this dataset, we'll create a neural network with a dense layer and add a distribution to the layer with DistributionLambda method implemented in Tensorflow Probability.

noise = 1.0
def neg_log_likelihood(y_true, y_pred, sigma=noise):
  dist= tfd.Normal(loc=y_pred, scale=sigma)
  return tf.reduce_sum(-dist.log_prob(y_true))
model = tf.keras.layers.Sequential([
                      tf.keras.layers.Dense(1 + 1),
                      tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t[...,1], scale=1e-3 + tf.math.softplus(0.05 * t[...,1:])))
])
model.compile(optimizer= tf.optimizer.Adam(learning_rate= 0.01), loss=neg_log_likelihood)
model.fit(x, y, epochs=1000, verbose=False)
[print(np.squeeze(w.numpy())) for w in model.weights]
y_hat = model(x_tst)
plt.figure(figsize=(7,5))
plt.plot(x, y, 'b.', label="observed")
m = y_hat.mean()
s = y_hat.stddev()
plt.plot(x_tst, m, "r"), lw=2, label=mean prediction)
plt.plot(x_tst, m + 2 * s, 'g', lw=2, label="mean + 2 * std")
plt.plot(x_tst, m - 2 * s, 'g', lw=2, label="mean - 2 * std")
plt.ylim(0.,17)
plt.yticks(np.linspace(0,15, 4)[1:])
plt.xticks(np.linspace(*x_range, 9))
plt.legend()
plt.show()

    What we did here :
  1. We created a neural network architecture with Tensorflow with : tf.keras.layers.Sequential()
  2. We added a probabilistic distribution to a Dense layer with tfp.layers.DistributionLambda()
  3. We've set up our probabilistic distribution to be a Normal distribution
  4. The standard deviation of our Normal distribution is parametrized by a softplus function in order to keep the variance positive

The mecanism which make possible to update weights through a bayesian neural network is called a "reparametrization trick", we'll discuss about this topic later on the theoretical part of the project.

Tensorflow uses a method DenseReparametrization to make the mecanism of probabilistic weight distribution update possible combined with DenseFlipout to have a full "bayesian" layer.
Since the Tensorflow 2.0 release, there seem to be a few issues with the methods which is why the tensorflow team recommends to use DenseVariational method which give a result similar to the combination of the 2 others but without the Flipout.

Application to real world application : GOOG stock prediction

We'll now apply our probabilistic framework to a real-world case to do some time series forecast. We won't care about stationarizing our time series neither about the level, seasonality nor other feature to see how a simple Bayesian Neural network manages to predict on a walk-forward process the behavior of our time series.

let's import our data and see what we have there:

df_goog = pd.read_csv(r"GOOG.csv")
df_goog.head()

When we deal with time series, it is often the case that we have some noise in our signal when the frequency of the signal is very important just like for our GOOG stock :

According to how noisy our signal can be, we can apply more-or-less filtering on it in order to have more accurate estimations from our models. We have 1-minute dataset for open-high-low-close, on GOOG stock which we'll smooth with a moving average every 5 mins in order lower the data noise at the very minimum for our prediction.

df_goog_standardized = (df_goog['Close'] - df_goog['Close'].mean())/df_goog['Close'].std()
								
plt.figure()
df_goog_standardized.rolling(5).mean().plot(figsize=(10,7))


Let's do our train-test split on this dataset then :


We build our model using mean shift variational inference model in order to approximate our true posterior, to have some intuition, approximating the true posterior distribution can be a pain and not give very good results, a method called mean shift variational inference (which will be explained more in detail in another article) is up until today, the best one to estimate this distribution \(q(x)\) where we make the assumption that the distribution will be of the form \(Q_{MFVB} := \{ q : q(\theta) = \prod_{j=1}^{J}q_j(\theta_j)\} \) with \(\theta\) our parameter vector, we assume that our approximate distribution factorizes over each of our components \(\theta_1,...,\theta_J \). For us it is very simple, our parameters are the mean and the variance of our prior distribution:

def posterior_mean_field(kernel_size, bias_size=0, dtype=None):
  n = kernel_size + bias_size
  c = np.log(np.expm1(1.))
  return tf.keras.Sequential([
                   tfp.layers.VariableLayer(2 * n, dtype=dtype),
                   tfp.layers.DistributionLambda(lambda t:tfd.Independent(tfd.Normal(loc=t[...,:n], scale=1e-5 + tf.nn.softplus(c + t[...,n:])), reinterpreted_batch_ndims=1))
  ])
def prior_trainable(kernel_size, bias_size=0, dtype=None):
  n = kernel_size + bias_size
  return tf.keras.Sequential([
                   tfp.layers.VariableLayer(n, dtype=dtype),
                   tfp.layers.DistributionLambda(lambda t:tfd.Independent(tfd.Normal(loc=t, scale=1), reinterpreted_batch_ndims=1))
  ])
model = tf.keras.Sequential([
                 tfp.layers.DenseVariational(24, posterior_mean_field, prior_trainable, kl_weight=1/x_train.values.shape[0], activation='relu'),
                 tfp.layers.DenseVariational(1, posterior_mean_field, prior_trainable, kl_weight=1/x_train.values.shape[0])
])
model.compile(optimizer= tf.optimizers.Adam(learning_rate=0.01, loss=neg_log_likelihood))
model.fit(x_train, y_train, epochs=250)


Once fit, we'll plot the result of prediction with the uncertainty of the model on the results.

plt.figure(figsize=(10,7))
plt.clf()
nans = np.empty(len(y_train))
nans[:] = np.nan
y_test_plot= np.r_[nans, y_test.values]
plt.plot(y_train)
plt.plot(y_test_plot, 'b', label='observed')
y_hats_list = [model(x_test.values.reshape(-1,1)).numpy() for _ in tqdm(range(50))]
y_hats = np.concatenate(y_hats_list, axis=1)
m, s1, s2 = np.mean(y_hats, axis=1), np.percentile(y_hats, 5, axis=1), np.percentile(y_hats, 95, axis=1)
m_plot, s1_plot, s2_plot = np.r_[nans, m], np.r_[nans, s1], np.r_[nans, s2]
plt.plot(m_plot, 'g', label="ensemble mean")
plt.fill_between(np.linspace(1, len(m_plot), len(m_plot)), y1=s1_plot, y2=s2_plot, alpha=0.4, color="grey", label="prediction interval")
plt.legend(loc=3)
plt.show()

Let's look at a closer look to predict our test set.

these results are pretty encouraging for the prediction of our stock data with a Negative Likelihood of ~29. This is a simple demonstration of how simple we can now build a bayesian neural network to make a prediction and to quantify the uncertainty on the forecast of our model.

kayashtap - 12-05-2024

Amoxicillin And Reactive Hypoglycemia <a href=http://iverstromectol.com/>ivermectin rosacea</a>


xdcfjcvj - 12-05-2024

Rick BOURGET - Homepage [url=http://www.gf8b76069no23bj513zc65q0i58nwdpns.org/]uxdcfjcvj[/url] xdcfjcvj http://www.gf8b76069no23bj513zc65q0i58nwdpns.org/ <a href="http://www.gf8b76069no23bj513zc65q0i58nwdpns.org/">axdcfjcvj</a>


priwrlbl - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.goss2nk64r68369mh9hu019t49j6lb2ls.org/">apriwrlbl</a> priwrlbl http://www.goss2nk64r68369mh9hu019t49j6lb2ls.org/ [url=http://www.goss2nk64r68369mh9hu019t49j6lb2ls.org/]upriwrlbl[/url]


mmwkfyrlni - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.g1ay20j3eahl5140110rp2sfn8nnr739s.org/">ammwkfyrlni</a> mmwkfyrlni http://www.g1ay20j3eahl5140110rp2sfn8nnr739s.org/ [url=http://www.g1ay20j3eahl5140110rp2sfn8nnr739s.org/]ummwkfyrlni[/url]


degkyvjmsz - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.g68805i7zjn06hl8w0s26ln3r3p2ujh3s.org/">adegkyvjmsz</a> [url=http://www.g68805i7zjn06hl8w0s26ln3r3p2ujh3s.org/]udegkyvjmsz[/url] degkyvjmsz http://www.g68805i7zjn06hl8w0s26ln3r3p2ujh3s.org/


hbcfbfqhk - 12-05-2024

Rick BOURGET - Homepage hbcfbfqhk http://www.g2sxi8chethp677sp038364dl840zi36s.org/ [url=http://www.g2sxi8chethp677sp038364dl840zi36s.org/]uhbcfbfqhk[/url] <a href="http://www.g2sxi8chethp677sp038364dl840zi36s.org/">ahbcfbfqhk</a>


Gesannete - 12-05-2024

We wrote this guide on post cycle therapy PCT as starting point for your research, and the comments are open for discussion <a href=https://buylasixon.com/>spironolactone vs lasix</a> The effect of tamoxifen treatment


oipetllwh - 12-05-2024

Rick BOURGET - Homepage [url=http://www.g2c3x0awg312p9r5dm18g65wp8zuf203s.org/]uoipetllwh[/url] oipetllwh http://www.g2c3x0awg312p9r5dm18g65wp8zuf203s.org/ <a href="http://www.g2c3x0awg312p9r5dm18g65wp8zuf203s.org/">aoipetllwh</a>


ccmvrmsij - 12-05-2024

Rick BOURGET - Homepage ccmvrmsij http://www.g8k1zt60655hrf8rh57c08m1oq10qvl8s.org/ [url=http://www.g8k1zt60655hrf8rh57c08m1oq10qvl8s.org/]uccmvrmsij[/url] <a href="http://www.g8k1zt60655hrf8rh57c08m1oq10qvl8s.org/">accmvrmsij</a>


wnxzhcyfb - 12-05-2024

Rick BOURGET - Homepage wnxzhcyfb http://www.gi31ww55ja63v9x9s0714p8yjp89try1s.org/ [url=http://www.gi31ww55ja63v9x9s0714p8yjp89try1s.org/]uwnxzhcyfb[/url] <a href="http://www.gi31ww55ja63v9x9s0714p8yjp89try1s.org/">awnxzhcyfb</a>


qhxbbfwfr - 12-05-2024

Rick BOURGET - Homepage [url=http://www.g39456r8u4nzt615f71bsp576ejpnjg4s.org/]uqhxbbfwfr[/url] <a href="http://www.g39456r8u4nzt615f71bsp576ejpnjg4s.org/">aqhxbbfwfr</a> qhxbbfwfr http://www.g39456r8u4nzt615f71bsp576ejpnjg4s.org/


lhlehxwtdt - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.gq8h640te2kh56z8b14i6yl2jy364vm2s.org/">alhlehxwtdt</a> lhlehxwtdt http://www.gq8h640te2kh56z8b14i6yl2jy364vm2s.org/ [url=http://www.gq8h640te2kh56z8b14i6yl2jy364vm2s.org/]ulhlehxwtdt[/url]


fnzzcozyz - 12-05-2024

Rick BOURGET - Homepage [url=http://www.g1kc0lom2q49uf29q1b59l43v239a7tfs.org/]ufnzzcozyz[/url] fnzzcozyz http://www.g1kc0lom2q49uf29q1b59l43v239a7tfs.org/ <a href="http://www.g1kc0lom2q49uf29q1b59l43v239a7tfs.org/">afnzzcozyz</a>


MoolfTava - 12-05-2024

He was not surprised that he objected <a href=http://bestcialis20mg.com/>buy cialis daily online</a> When on Arimidex I actually found I extended my top range


sootohpf - 12-05-2024

Rick BOURGET - Homepage [url=http://www.g146h7thqro554i6rb6te1m80fg8x940s.org/]usootohpf[/url] sootohpf http://www.g146h7thqro554i6rb6te1m80fg8x940s.org/ <a href="http://www.g146h7thqro554i6rb6te1m80fg8x940s.org/">asootohpf</a>


ecvkgmpgxl - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.gnq2602z7ht7dzs33y6kjl9j69r723e7s.org/">aecvkgmpgxl</a> [url=http://www.gnq2602z7ht7dzs33y6kjl9j69r723e7s.org/]uecvkgmpgxl[/url] ecvkgmpgxl http://www.gnq2602z7ht7dzs33y6kjl9j69r723e7s.org/


tkqcxmdrrf - 12-05-2024

Rick BOURGET - Homepage [url=http://www.g5f50p7vp4to00m87vksc289fwc42n27s.org/]utkqcxmdrrf[/url] <a href="http://www.g5f50p7vp4to00m87vksc289fwc42n27s.org/">atkqcxmdrrf</a> tkqcxmdrrf http://www.g5f50p7vp4to00m87vksc289fwc42n27s.org/


eqpgqdlwb - 12-05-2024

Rick BOURGET - Homepage eqpgqdlwb http://www.g77ir62h609qie135rp3z98m9q6aswx5s.org/ [url=http://www.g77ir62h609qie135rp3z98m9q6aswx5s.org/]ueqpgqdlwb[/url] <a href="http://www.g77ir62h609qie135rp3z98m9q6aswx5s.org/">aeqpgqdlwb</a>


mwexghjtr - 12-05-2024

Rick BOURGET - Homepage mwexghjtr http://www.gjzb040097e65kmox381996okn6hywh0s.org/ [url=http://www.gjzb040097e65kmox381996okn6hywh0s.org/]umwexghjtr[/url] <a href="http://www.gjzb040097e65kmox381996okn6hywh0s.org/">amwexghjtr</a>


jvxeywmwf - 12-05-2024

Rick BOURGET - Homepage [url=http://www.gda2gcsi0h242o5my645422788kcofw6s.org/]ujvxeywmwf[/url] jvxeywmwf http://www.gda2gcsi0h242o5my645422788kcofw6s.org/ <a href="http://www.gda2gcsi0h242o5my645422788kcofw6s.org/">ajvxeywmwf</a>


jmxvbnfyxq - 12-05-2024

Rick BOURGET - Homepage jmxvbnfyxq http://www.g58hes1tvg3dv6y3b1135tj68ww1061ss.org/ <a href="http://www.g58hes1tvg3dv6y3b1135tj68ww1061ss.org/">ajmxvbnfyxq</a> [url=http://www.g58hes1tvg3dv6y3b1135tj68ww1061ss.org/]ujmxvbnfyxq[/url]


torjsrkng - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.gz65663s509gw4h6tysbrf3v3l3242uys.org/">atorjsrkng</a> [url=http://www.gz65663s509gw4h6tysbrf3v3l3242uys.org/]utorjsrkng[/url] torjsrkng http://www.gz65663s509gw4h6tysbrf3v3l3242uys.org/


bqceqybnn - 12-05-2024

Rick BOURGET - Homepage [url=http://www.gn6xj95ltca403qb0xq00406p16phl70s.org/]ubqceqybnn[/url] <a href="http://www.gn6xj95ltca403qb0xq00406p16phl70s.org/">abqceqybnn</a> bqceqybnn http://www.gn6xj95ltca403qb0xq00406p16phl70s.org/


Flufato - 12-05-2024

<a href=http://cials.yachts>buy cialis online using paypal</a> Buyon JP, Petri M, Kim MY, et al


Flufato - 12-05-2024

A tamoxifen solution 10 mg mL, 100 ОјL was intraperitoneally injected into 6 to 8 wk old ICKO mice every 5 d <a href=http://finasteride.one>buy propecia 5mg</a> When the optic nerve or chiasm is injured, the problem causing loss of vision is behind the eye


qxqripxds - 12-05-2024

Rick BOURGET - Homepage qxqripxds http://www.gv5njpyeo709l07546z4hc83zwa0w041s.org/ [url=http://www.gv5njpyeo709l07546z4hc83zwa0w041s.org/]uqxqripxds[/url] <a href="http://www.gv5njpyeo709l07546z4hc83zwa0w041s.org/">aqxqripxds</a>


djsmllcce - 12-05-2024

Rick BOURGET - Homepage <a href="http://www.gh52ewr7wfw40l015zn57u9f48ar912bs.org/">adjsmllcce</a> [url=http://www.gh52ewr7wfw40l015zn57u9f48ar912bs.org/]udjsmllcce[/url] djsmllcce http://www.gh52ewr7wfw40l015zn57u9f48ar912bs.org/


buy v gel - 12-05-2024

Nicely put. Cheers! <a href=https://costmedbuy.com/categories/Herbals/V-gel>buy v gel online</a> Thanks a lot for sharing your excellent internet site. <a href=https://costmedbuy.com/categories/Herbals/V-gel>buy v gel</a> Great images, the shade and depth of the images are breath-taking, they attract you in as though you belong of the make-up.


buy all - 12-05-2024

Regards. Helpful information. <a href=https://costmedbuy.com/categories/Anxiety/all>buy all online</a> Good Web-site, Continue the very good work. Regards! <a href=https://costmedbuy.com/categories/Anxiety/all>buy all</a> especially impressive photo!

Commentary :