3.1. Bivariate BMM Test#

3.1.1. Author: Alexandra Semposki#

3.1.1.1. Date: 10 September 2023#

import numpy as np
import matplotlib 
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

! pip install pandoc
# ! pip install Taweret    # if using Colab, uncomment to install

%matplotlib inline
Collecting pandoc
  Downloading pandoc-2.4.tar.gz (34 kB)
  Preparing metadata (setup.py) ... ?25l-
 done
?25hCollecting plumbum (from pandoc)
  Downloading plumbum-1.8.3-py3-none-any.whl.metadata (10 kB)
Collecting ply (from pandoc)
  Downloading ply-3.11-py2.py3-none-any.whl.metadata (844 bytes)
Downloading plumbum-1.8.3-py3-none-any.whl (127 kB)
Downloading ply-3.11-py2.py3-none-any.whl (49 kB)
Building wheels for collected packages: pandoc
  Building wheel for pandoc (setup.py) ... ?25l-
 \
 done
?25h  Created wheel for pandoc: filename=pandoc-2.4-py3-none-any.whl size=34792 sha256=65e5211e1362956c06788cfaea8fea2fc95ebaccd1e0ece1c013c35559ff9519
  Stored in directory: /home/runner/.cache/pip/wheels/9c/2f/9f/b1aac8c3e74b4ee327dc8c6eac5128996f9eadf586e2c0ba67
Successfully built pandoc
Installing collected packages: ply, plumbum, pandoc
Successfully installed pandoc-2.4 plumbum-1.8.3 ply-3.11
import sys
sys.path.append('../../../src/Taweret/')
from Taweret.models.samba_models import *
from Taweret.mix.gaussian import *
g = np.linspace(1e-6, 1.0, 100)
order = 3
model_1 = Loworder(order)
model_2 = Highorder(order)
true = TrueModel().evaluate(g)

exp_1 = model_1.evaluate(g)
exp_2 = model_2.evaluate(g)

var_1 = exp_1[1].flatten()
var_2 = exp_2[1].flatten()

# combine to form dict of models
models = {
    '1': model_1,
    '2': model_2
}
#example plot to test models
fig = plt.figure(figsize=(8,6), dpi=100)
ax = plt.axes()
ax.set_xlim(0.0,1.0)
ax.set_ylim(1.2,3.2)
ax.tick_params(axis='x', direction='in')
ax.tick_params(axis='y', direction='in')
ax.locator_params(nbins=8)
ax.xaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_minor_locator(AutoMinorLocator())

ax.plot(g, true[0].flatten(), 'k', label='True')
ax.plot(g, exp_1[0].flatten(), 'r--', label='Small-g')
ax.plot(g, exp_2[0].flatten(), 'b--', label='Large-g')

ax.set_title('SAMBA Models')
ax.legend()
<matplotlib.legend.Legend at 0x7f06e8581eb0>
../../_images/7185768b175be8f427598b018b21f41416de8639943f54436b3520e64978e308.png
#call mixing method and plot
mixed = Multivariate(g, models, n_models=2)
_, mixed_mean, mixed_intervals, mixed_std_dev = mixed.predict(ci=68)
#plotting bivariate BMM results on top of SAMBA models
fig = plt.figure(figsize=(8,6), dpi=100)
ax = plt.axes()
ax.set_xlim(0.0,1.0)
ax.set_ylim(1.0,3.0)
ax.tick_params(axis='x', direction='in')
ax.tick_params(axis='y', direction='in')
ax.locator_params(nbins=8)
ax.xaxis.set_minor_locator(AutoMinorLocator())
ax.yaxis.set_minor_locator(AutoMinorLocator())

ax.plot(g, true[0].flatten(), 'k', label='True')
ax.plot(g, exp_1[0].flatten(), 'r--', label='Small-g')
ax.plot(g, exp_2[0].flatten(), 'b--', label='Large-g')

ax.plot(g, exp_1[0].flatten() - var_1, 'r', linestyle='dotted')
ax.plot(g, exp_1[0].flatten() + var_1, 'r', linestyle='dotted')

ax.plot(g, exp_2[0].flatten() - var_2, 'b', linestyle='dotted')
ax.plot(g, exp_2[0].flatten() + var_2, 'b', linestyle='dotted')

ax.plot(g, mixed_mean, 'g', label='Mixed model')
ax.plot(g, mixed_intervals[0][0], 'g--')
ax.plot(g, mixed_intervals[1][0], 'g--')
ax.fill_between(g, mixed_intervals[0][0], mixed_intervals[1][0], color='green', alpha=0.2)

ax.set_title('SAMBA Models')
ax.legend()
<matplotlib.legend.Legend at 0x7f06d9bef1a0>
../../_images/9d7d9d1829c7364932e064bd872ebd16f53f86676cbf1289f5269778c7a7c97c.png

Written by: Alexandra Semposki

Last edited: 10 September 2023