S3bar ANN Model 2 Demo#

Essential Libraries#

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', fontset='cm')
from typing import Tuple, Dict, List
from IPython.display import display, Latex
from sklearn.metrics import explained_variance_score, max_error, mean_absolute_error,  mean_squared_error, r2_score, mean_absolute_percentage_error


import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import torchvision
import torchmetrics, mlxtend

import torchinfo
from torchinfo import summary

import os
import re
import time

Set the device: cuda or cpu#

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

if torch.cuda.is_available():
    torch.cuda.set_device(device)

Get the data and create some useful features as extensions#

Min and max values of stellar parameters#

C_min = 0.08763469; C_max = 0.30751308;
Mbar_min = 0.909338; Mbar_max = 2.80912;
Rbar_min = 9.93864; Rbar_max = 19.4138;
x_min = 0.1406481; x_max = 0.78176399;
Ibar_min = 4.73725112; Ibar_max = 33.86946319;
D_min = 0.0046990783; D_max = 0.0335965469;
Qbar_min = 1.31339211; Qbar_max = 14.74045551;
logQbar_min = 0.27261318; logQbar_max = 2.69059579;
S3bar_min = 1.73639896; S3bar_max = 32.10634011;

min-max scaling#

min_values = np.array([x_min, D_min, logQbar_min])
max_values = np.array([x_max, D_max, logQbar_max])

# min-max scaling function
feature_scaler = lambda data: (data - min_values) / (max_values - min_values)

Input layer features: model’s input parameters#

selected_features = ['x','D','log(Qbar)'] 

Test subample of NS configurations#

df_test = pd.read_csv("NS_data/test_data_small_sample.csv")
df_test
M Req C x Ibar D Qbar log(Qbar) S3bar EoS
0 2.190950 11.9662 0.270131 0.166659 5.928471 0.026846 1.935722 0.660480 3.089488 SK272
1 1.348410 18.3103 0.108649 0.731341 22.361405 0.007117 7.033574 1.950695 15.180773 SKI2
2 2.140930 15.1489 0.208506 0.552628 8.784199 0.018118 2.831782 1.040906 5.118462 DD2_2
3 0.990131 13.9583 0.104655 0.387451 28.397753 0.005605 11.565086 2.447991 24.845141 DS_CMF-4-Hybr
4 1.620250 14.0746 0.169841 0.371895 12.573800 0.012658 4.981185 1.605668 9.990400 DS_CMF-8-Hybr
... ... ... ... ... ... ... ... ... ... ...
397 2.200120 10.8028 0.300475 0.297565 5.216579 0.030509 1.544497 0.434698 2.207297 BSK26
398 1.972780 16.0701 0.181116 0.655830 10.062797 0.015816 3.133462 1.142139 6.054977 Rs
399 2.033340 13.9065 0.215719 0.632598 7.410619 0.021477 2.206460 0.791389 3.821924 KDE0v1
400 1.142140 15.3272 0.109939 0.565663 23.094069 0.006892 8.345383 2.121708 17.784955 PCSB1
401 2.333130 14.6437 0.235064 0.653575 6.613759 0.024064 1.899356 0.641515 3.077256 RMF3

402 rows × 10 columns

def load_stars(data_frame, batch_size = 2048, shuffle = True): 
    
    
    df_target = (data_frame['S3bar']) 
    np_features = data_frame[selected_features].to_numpy()    
    
    np_targets = df_target.to_numpy()
    np_targets = np.reshape(np_targets, (np_targets.shape[0], 1))
    
    tensor_features = torch.Tensor(feature_scaler(np_features)) 
    
    input_dimension = tensor_features.shape[1]
    
    tensor_targets = torch.Tensor(np_targets)
    final_dataset = TensorDataset(tensor_features, tensor_targets)

    dataloader = DataLoader(final_dataset, batch_size=batch_size, shuffle=shuffle, num_workers=5, pin_memory=True)

    return dataloader, input_dimension, np_features, np_targets

Dataloader#

batch_size = 512
test_dataloader, input_dimension, _, _ = load_stars(df_test, batch_size = batch_size)

Feed-forward ANN Model to perform regression#

class ModifiedSigmoid(nn.Module):
    def forward(self, x):
        return S3bar_min + (S3bar_max - S3bar_min)*torch.sigmoid(x)          
class RegressorModel(nn.Module):
    def __init__(self,input_dimension, feature_scaler):
        super().__init__()
        
        self.feature_scaler = feature_scaler
        
        self.MLP = nn.Sequential(
            
            nn.Linear(input_dimension, 120),
            nn.GELU(),
            nn.Linear(120, 75),  
            nn.GELU(),
            nn.Linear(75, 50), 
            nn.GELU(),
            nn.Linear(50, 25),  
            nn.GELU(),
            nn.Linear(25, 10),  
            nn.GELU(),
            nn.Linear(10, 1),   
            ModifiedSigmoid(), # a modification of sigmoid activation function 
        )
        # Initialize weights
        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight)
                nn.init.constant_(m.bias, 0.01)

    def forward(self, x):
        x = self.MLP(x)
        return x
    
    def predict(self, x):
        x = self.feature_scaler(x).astype('float32')
        x = torch.from_numpy(x).to(self.device)
        x = self.MLP(x)
        x = x.cpu().detach().numpy()
        return x
        
    def set_device(self, device):
        self.device = device    
model_0 = RegressorModel(input_dimension = input_dimension, feature_scaler = feature_scaler)
model_0
RegressorModel(
  (MLP): Sequential(
    (0): Linear(in_features=3, out_features=120, bias=True)
    (1): GELU(approximate='none')
    (2): Linear(in_features=120, out_features=75, bias=True)
    (3): GELU(approximate='none')
    (4): Linear(in_features=75, out_features=50, bias=True)
    (5): GELU(approximate='none')
    (6): Linear(in_features=50, out_features=25, bias=True)
    (7): GELU(approximate='none')
    (8): Linear(in_features=25, out_features=10, bias=True)
    (9): GELU(approximate='none')
    (10): Linear(in_features=10, out_features=1, bias=True)
    (11): ModifiedSigmoid()
  )
)
summary(model_0)
=================================================================
Layer (type:depth-idx)                   Param #
=================================================================
RegressorModel                           --
├─Sequential: 1-1                        --
│    └─Linear: 2-1                       480
│    └─GELU: 2-2                         --
│    └─Linear: 2-3                       9,075
│    └─GELU: 2-4                         --
│    └─Linear: 2-5                       3,800
│    └─GELU: 2-6                         --
│    └─Linear: 2-7                       1,275
│    └─GELU: 2-8                         --
│    └─Linear: 2-9                       260
│    └─GELU: 2-10                        --
│    └─Linear: 2-11                      11
│    └─ModifiedSigmoid: 2-12             --
=================================================================
Total params: 14,901
Trainable params: 14,901
Non-trainable params: 0
=================================================================

ANN Model evaluation#

Load the trained Model best weights \(\theta^\star\)#

batch_size = 512
model_path = './Model-Weights/S3bar_inference/model_2.pth' 
dataloader_test, _, np_features_test, np_targets_test = load_stars(df_test, batch_size=batch_size)
np_targets_test
array([[ 3.08948791],
       [15.18077326],
       [ 5.11846232],
       [24.84514133],
       [ 9.99040043],
       [ 6.11727969],
       [ 6.01616414],
       [13.37311538],
       [14.43530392],
       [ 5.44534505],
       [ 5.61732645],
       [ 7.30589559],
       [ 3.01324611],
       [ 7.49447274],
       [13.72098571],
       [ 7.46783394],
       [ 4.81646436],
       [ 9.98566671],
       [12.27449619],
       [ 4.39476464],
       [ 7.65942776],
       [ 7.60257261],
       [ 2.38267341],
       [ 2.3321278 ],
       [ 2.47917822],
       [ 9.97649939],
       [10.11192429],
       [ 6.36636822],
       [ 2.56187445],
       [ 4.10336046],
       [17.52931829],
       [ 5.65535746],
       [14.88592898],
       [12.66456036],
       [12.13331724],
       [ 4.47545524],
       [ 4.5494549 ],
       [ 4.64627969],
       [ 5.57482681],
       [15.24846656],
       [ 4.5932363 ],
       [ 5.27643485],
       [13.14641823],
       [ 2.74069824],
       [13.80202965],
       [ 3.7857803 ],
       [ 4.32393723],
       [13.46165887],
       [ 4.6035721 ],
       [ 6.57184848],
       [11.51452173],
       [ 6.18618608],
       [ 3.92428625],
       [ 3.28145401],
       [12.55730699],
       [ 2.16435328],
       [17.20138399],
       [ 2.1210156 ],
       [ 5.5077446 ],
       [12.3244067 ],
       [ 3.42713741],
       [ 3.44233525],
       [19.53912287],
       [ 3.00104825],
       [ 2.67654915],
       [16.69187233],
       [ 3.68574177],
       [ 4.02894458],
       [ 8.0698857 ],
       [ 2.35901615],
       [ 8.93365246],
       [17.45931897],
       [ 2.24924527],
       [ 2.08716376],
       [ 4.14746201],
       [ 3.95583604],
       [14.39729617],
       [ 5.46560912],
       [16.36021793],
       [ 7.74818429],
       [ 3.4472811 ],
       [ 6.96801058],
       [ 5.07963261],
       [ 4.78115796],
       [ 7.70127831],
       [ 2.83127867],
       [14.07045444],
       [ 3.27988241],
       [ 7.09801691],
       [ 4.02474113],
       [ 6.04018995],
       [ 2.89309445],
       [ 2.90010157],
       [23.01441895],
       [ 7.22062219],
       [10.67991323],
       [ 6.11776457],
       [ 3.51105184],
       [ 4.59268732],
       [ 8.00195991],
       [10.28687565],
       [ 6.55475498],
       [11.81590529],
       [19.22112115],
       [ 2.22075725],
       [ 5.2691044 ],
       [ 5.59617224],
       [ 5.47701976],
       [15.26283889],
       [ 2.6308438 ],
       [13.71698744],
       [ 5.98624491],
       [ 6.16198264],
       [ 3.22712341],
       [ 5.44844315],
       [10.55453961],
       [ 4.98522084],
       [ 2.37644082],
       [ 3.30860469],
       [ 3.24188297],
       [ 9.90498297],
       [ 5.51376529],
       [ 2.80392064],
       [ 8.71633529],
       [17.55041273],
       [ 4.69879383],
       [ 7.06808501],
       [ 7.89691407],
       [12.45076266],
       [ 3.53984888],
       [ 3.59751185],
       [ 5.71930279],
       [ 9.98423075],
       [17.45299644],
       [16.16416687],
       [ 2.90077519],
       [ 3.75728119],
       [20.91892182],
       [ 3.16666721],
       [ 2.62537791],
       [ 7.65553224],
       [19.45130522],
       [16.47692668],
       [ 2.16311597],
       [18.22613139],
       [10.33742031],
       [ 2.49609896],
       [ 6.00460332],
       [ 3.86062969],
       [ 7.67734234],
       [ 3.98272016],
       [ 2.41954857],
       [12.04741255],
       [13.89571844],
       [ 4.44343879],
       [ 7.63272389],
       [21.87718355],
       [ 3.46588656],
       [17.81200512],
       [ 6.84461685],
       [ 3.85543802],
       [ 4.50442824],
       [12.3228654 ],
       [ 3.53851796],
       [ 5.5543112 ],
       [ 2.85440282],
       [11.80659987],
       [ 4.21121557],
       [ 2.61716026],
       [ 2.33332216],
       [ 6.14153857],
       [ 9.01972157],
       [11.02518387],
       [ 4.79906552],
       [ 2.82716763],
       [10.76460827],
       [ 7.24154368],
       [12.82588388],
       [15.98524088],
       [ 8.4788693 ],
       [10.94012078],
       [18.10323993],
       [ 3.26043644],
       [16.09007747],
       [ 2.8801155 ],
       [ 8.49782984],
       [ 4.43620452],
       [21.24976092],
       [ 4.48151314],
       [ 2.37245518],
       [ 9.09494593],
       [ 5.62587724],
       [ 5.14741465],
       [ 6.03801474],
       [ 2.72939622],
       [13.21265577],
       [ 2.71889608],
       [ 5.56588071],
       [ 3.91189104],
       [ 5.46551466],
       [ 4.81786468],
       [ 2.54567107],
       [ 2.1469814 ],
       [ 4.14200764],
       [ 3.4793156 ],
       [ 4.39918953],
       [ 7.15367296],
       [ 6.20364912],
       [23.43860103],
       [ 6.28683646],
       [ 3.04701791],
       [ 4.94876711],
       [ 5.0947255 ],
       [ 3.69498783],
       [ 9.4602849 ],
       [ 3.90178442],
       [ 3.6812259 ],
       [ 9.06165586],
       [ 3.43408028],
       [ 2.01841858],
       [ 4.71399002],
       [10.08086631],
       [ 5.73518116],
       [ 5.49311515],
       [11.49244324],
       [ 7.32716715],
       [ 5.70709223],
       [10.78079547],
       [ 5.11816796],
       [ 2.43932891],
       [13.85560446],
       [ 7.15201715],
       [ 2.74217272],
       [ 4.61205958],
       [ 6.1310983 ],
       [ 2.20108722],
       [ 4.43437939],
       [ 3.86752882],
       [ 3.9518636 ],
       [ 2.26844984],
       [ 4.87417843],
       [ 2.64253558],
       [ 3.69181561],
       [ 8.29421076],
       [ 3.55647597],
       [ 4.02742688],
       [ 3.5267899 ],
       [ 6.26322192],
       [ 6.28899269],
       [11.31096998],
       [ 4.99468684],
       [ 6.89774155],
       [14.17534749],
       [ 6.33597301],
       [10.2533732 ],
       [ 3.39411408],
       [ 3.44857998],
       [ 5.5983287 ],
       [ 6.24764432],
       [13.63706888],
       [14.04139356],
       [ 7.65441336],
       [ 4.2574667 ],
       [ 3.07536557],
       [ 3.30181122],
       [13.5252091 ],
       [ 2.60664207],
       [ 5.67637621],
       [ 4.27593415],
       [ 2.37388587],
       [ 4.63678021],
       [11.81226103],
       [ 1.82712218],
       [ 3.80194227],
       [13.30072809],
       [20.8479736 ],
       [12.49156063],
       [ 9.99180577],
       [10.08091016],
       [11.9800703 ],
       [ 2.58429099],
       [ 7.60179021],
       [15.34907662],
       [ 5.34109639],
       [ 2.31694559],
       [ 3.36875072],
       [ 4.15647676],
       [ 3.16496588],
       [ 5.0632379 ],
       [ 4.44640159],
       [ 4.74024648],
       [ 6.20160696],
       [ 2.35617725],
       [ 3.9704472 ],
       [ 7.00598522],
       [ 3.58474779],
       [15.1922848 ],
       [ 7.52076421],
       [ 2.5272902 ],
       [ 2.84653285],
       [ 3.7064012 ],
       [ 4.23060401],
       [ 6.10930887],
       [ 5.00160067],
       [ 2.42883232],
       [ 3.21839068],
       [12.11502835],
       [ 8.70316405],
       [ 7.0833851 ],
       [10.97964887],
       [17.3214738 ],
       [ 3.69639979],
       [ 3.00881429],
       [ 3.78361694],
       [ 4.97789826],
       [ 2.40690146],
       [ 5.72079782],
       [15.74579342],
       [ 4.0160798 ],
       [14.12295935],
       [ 8.90831994],
       [ 3.33266578],
       [12.63321698],
       [ 4.4718461 ],
       [ 4.30115466],
       [ 4.12172909],
       [ 4.89488572],
       [ 2.76478368],
       [ 6.46237893],
       [ 2.94222851],
       [10.17684266],
       [10.15049278],
       [ 3.48904812],
       [ 3.65598181],
       [ 4.76763332],
       [ 3.11360881],
       [ 2.16261403],
       [ 7.81372027],
       [ 3.05374215],
       [ 5.60274128],
       [ 4.16852599],
       [18.69217365],
       [ 3.25355696],
       [ 3.92585914],
       [16.98644123],
       [21.32311466],
       [ 3.18748013],
       [ 8.14028071],
       [ 4.60979568],
       [ 6.21914884],
       [ 4.22700514],
       [ 3.20198072],
       [ 7.88939239],
       [12.68447303],
       [11.41838904],
       [ 3.93569795],
       [ 3.62776171],
       [14.40468991],
       [ 2.47703917],
       [ 2.04713608],
       [ 3.62853361],
       [24.55121017],
       [ 4.71913404],
       [ 4.15852405],
       [ 4.99395039],
       [ 2.34332296],
       [ 3.43936463],
       [ 3.73850028],
       [ 8.81810168],
       [ 4.08854126],
       [24.87333316],
       [ 5.3125407 ],
       [ 6.69854241],
       [ 2.37560823],
       [15.82981125],
       [ 2.24879321],
       [ 6.21229023],
       [ 5.05879717],
       [ 3.53575567],
       [10.35007733],
       [ 2.05221627],
       [ 3.04792076],
       [ 5.38494168],
       [ 4.27986029],
       [ 3.13801416],
       [ 3.88129661],
       [10.25140122],
       [ 4.93740824],
       [ 2.31201334],
       [ 5.22369921],
       [ 5.50228011],
       [ 8.90501009],
       [ 6.21752971],
       [13.16814917],
       [ 3.06853493],
       [ 8.08174181],
       [10.62490355],
       [ 2.20729727],
       [ 6.05497715],
       [ 3.82192404],
       [17.78495504],
       [ 3.0772557 ]])
np_features_test
array([[0.16665856, 0.02684593, 0.66048014],
       [0.73134145, 0.00711738, 1.95069503],
       [0.55262768, 0.01811834, 1.04090619],
       ...,
       [0.63259773, 0.02147664, 0.7913894 ],
       [0.56566338, 0.00689158, 2.12170842],
       [0.65357451, 0.02406422, 0.64151467]])
regressor = RegressorModel(input_dimension = input_dimension, feature_scaler = feature_scaler).to(device)
regressor.set_device(device)
regressor.load_state_dict(torch.load(model_path, map_location=torch.device(device)))
regressor.eval()
RegressorModel(
  (MLP): Sequential(
    (0): Linear(in_features=3, out_features=120, bias=True)
    (1): GELU(approximate='none')
    (2): Linear(in_features=120, out_features=75, bias=True)
    (3): GELU(approximate='none')
    (4): Linear(in_features=75, out_features=50, bias=True)
    (5): GELU(approximate='none')
    (6): Linear(in_features=50, out_features=25, bias=True)
    (7): GELU(approximate='none')
    (8): Linear(in_features=25, out_features=10, bias=True)
    (9): GELU(approximate='none')
    (10): Linear(in_features=10, out_features=1, bias=True)
    (11): ModifiedSigmoid()
  )
)

Residual Error - Fractional difference - Evaluation Measures#

def plot_residuals(residual_error, percentage_error):
    witdh, height = 16, 6
    fontsize = 20
    labelsize = 15
    lw = 5
    
    fig, ax = plt.subplots(1, 2, figsize=(witdh, height))
    # Residual error subplot 
    ax[0].plot(residual_error, lw=lw)
    ax[0].set_xlabel(r'Datapoints', fontsize=fontsize)
    ax[0].set_ylabel(r'Residual', fontsize=fontsize)
    ax[0].tick_params(axis='both', which='both', labelsize=labelsize)

    # Percentage error subplot 
    ax[1].plot(percentage_error, lw=lw)
    ax[1].set_xlabel(r'Datapoints', fontsize=fontsize)
    ax[1].set_ylabel(r'$PE \ [\%]$', fontsize=fontsize)
    ax[1].tick_params(axis='both', which='both', labelsize=labelsize)

    plt.tight_layout()
    plt.show()
    
    fig, ax = plt.subplots(1, 2, figsize=(16, 6))
    fontsize = 20
    labelsize = 15
    lw = 5

    # Residual error histogram
    ax[0].hist(residual_error, bins = 20)
    ax[0].set_ylabel(r'Datapoints', fontsize=fontsize)
    ax[0].set_xlabel(r'Residual', fontsize=fontsize)
    ax[0].tick_params(axis='both', which='both', labelsize=labelsize)

    # Percentage error histogram
    ax[1].hist(percentage_error, bins = 20)
    ax[1].set_ylabel(r'Datapoints', fontsize=fontsize)
    ax[1].set_xlabel(r'$PE \ [\%]$', fontsize=fontsize)
    ax[1].tick_params(axis='both', which='both', labelsize=labelsize)

    plt.tight_layout()
    plt.show()
def Evaluation_Measures(y_true, y_pred):    

    exp_var = explained_variance_score(y_true, y_pred)
    max_err = max_error(y_true, y_pred)
    mae = mean_absolute_error(y_true, y_pred)
    mse = mean_squared_error(y_true, y_pred)
    r2 = r2_score(y_true, y_pred)
    mape = mean_absolute_percentage_error(y_true, y_pred)
    
    measures = {'explained_variance': [exp_var], 
                'max_error': [max_err],
                'mean_absolute_error': [mae],
                'mean_squared_error': [mse],
                'r2_score': [r2],
                'mean_absolute_percentage_error': [mape]}
   
    df_eval_measures = pd.DataFrame(measures)
    
    return df_eval_measures

Evaluation measures in Test Set subsample#

np_targets_test = np_targets_test.ravel()

model_estimation_test_set = regressor.predict(np_features_test)
model_estimation_test_set = model_estimation_test_set.ravel().astype(np.float64) 
test_eval_measures = Evaluation_Measures(y_true = np_targets_test, y_pred = model_estimation_test_set)
test_eval_measures
explained_variance max_error mean_absolute_error mean_squared_error r2_score mean_absolute_percentage_error
0 0.999987 0.156162 0.006717 0.000317 0.999987 0.000683

Redidual Errors and Pecentange Errors in the test Set subsample#

residual_error_test = model_estimation_test_set - np_targets_test
percentage_error_test = (residual_error_test / np_targets_test) * 100
plot_residuals(residual_error_test, percentage_error_test)
../../_images/ebb94bfbcb94b0faf5c3f734a28b93069e1ed34205bd27999d6b2ebfcd3556e8.png ../../_images/18796c0f7471fd8edd5937dae3c966b4906e3e3d182aed2e49194aa7e1e6b9b2.png
df_test['S3bar_model'] = model_estimation_test_set
df_test['S3bar_model']
0       3.088629
1      15.186839
2       5.116338
3      24.904369
4      10.013984
         ...    
397     2.209820
398     6.052032
399     3.820173
400    17.796871
401     3.076515
Name: S3bar_model, Length: 402, dtype: float64
def S3bar_ANN_model(x,y,w):
    input_features = np.column_stack((x, y, w))
    ANN_estimation = regressor.predict(input_features)
    ANN_estimation = ANN_estimation.ravel().astype(np.float64) 
    return ANN_estimation.reshape(x.shape)
S3bar_ANN_model(df_test['x'], df_test['D'], df_test['log(Qbar)'])
array([ 3.08862925, 15.1868391 ,  5.11633778, 24.90436935, 10.01398373,
        6.11965752,  6.01632786, 13.38018322, 14.44128704,  5.44465828,
        5.61896038,  7.30884933,  3.01529813,  7.49607754, 13.69018936,
        7.46696663,  4.82605076,  9.98535061, 12.27103329,  4.3948493 ,
        7.66080666,  7.60388756,  2.38240743,  2.33251429,  2.47931504,
        9.97647572, 10.11562443,  6.36308289,  2.56133771,  4.10256004,
       17.52705574,  5.65332985, 14.89909363, 12.67594719, 12.14681053,
        4.47901154,  4.55082226,  4.64712524,  5.57509279, 15.25957394,
        4.59459019,  5.27624083, 13.18173313,  2.74070358, 13.81982231,
        3.78411627,  4.32507181, 13.47125816,  4.60604668,  6.57186031,
       11.52199268,  6.1865406 ,  3.92520618,  3.28051543, 12.56427574,
        2.16341543, 17.22198677,  2.12139988,  5.50647068, 12.33392239,
        3.42578268,  3.44082618, 19.53076744,  3.00010753,  2.67531967,
       16.7440567 ,  3.68353653,  4.02717209,  8.07121372,  2.35761356,
        8.93425846, 17.48832703,  2.25086737,  2.08599424,  4.1469593 ,
        3.9588089 , 14.5081892 ,  5.46579647, 16.36066818,  7.7413702 ,
        3.4478302 ,  6.96475029,  5.09982777,  4.78163052,  7.69987011,
        2.83138061, 14.0739727 ,  3.27739358,  7.09618568,  4.02571774,
        6.04220581,  2.89291143,  2.90096378, 23.0683403 ,  7.22144222,
       10.6796999 ,  6.11690235,  3.51843762,  4.59191227,  8.00469398,
       10.28979778,  6.56283665, 11.82244778, 19.36586189,  2.21963215,
        5.27078056,  5.59541416,  5.47842455, 15.27645302,  2.63051295,
       13.72146606,  6.03547001,  6.15471649,  3.22849798,  5.45003128,
       10.53950977,  4.98480606,  2.3759284 ,  3.3099997 ,  3.24315095,
        9.90980721,  5.51580238,  2.80273104,  8.71871948, 17.56330872,
        4.69762945,  7.0686388 ,  7.89796066, 12.45209408,  3.53794694,
        3.59671593,  5.72039032,  9.94785786, 17.33786583, 16.19673347,
        2.90399981,  3.75815129, 20.91782379,  3.16732645,  2.62587786,
        7.6518774 , 19.42069054, 16.48157692,  2.164078  , 18.20448875,
       10.34268093,  2.49601007,  6.00460339,  3.85766768,  7.67583084,
        3.98015428,  2.42033577, 12.03912735, 13.88247967,  4.44416714,
        7.6391468 , 21.85235405,  3.46461773, 17.82520485,  6.84436417,
        3.85648012,  4.50473595, 12.32801819,  3.53702593,  5.5548954 ,
        2.85548353, 11.81471825,  4.21150732,  2.61670852,  2.33432722,
        6.12910366,  9.02244091, 11.02473354,  4.80249929,  2.82812929,
       10.75094986,  7.24815273, 12.82449722, 16.06479263,  8.47129822,
       10.94496155, 18.10551643,  3.26243496, 16.10415077,  2.8795135 ,
        8.47696114,  4.43808699, 21.21380043,  4.48020554,  2.37266064,
        9.09688187,  5.61974239,  5.14608383,  6.03892422,  2.72840452,
       13.22232246,  2.72550535,  5.56601715,  3.91333055,  5.46535873,
        4.81823826,  2.54491377,  2.14758849,  4.14188385,  3.47777152,
        4.40252399,  7.15424061,  6.20329285, 23.44392967,  6.31295776,
        3.04694605,  4.94859219,  5.09264135,  3.69540763,  9.46396828,
        3.90085888,  3.67799568,  9.06083584,  3.43284273,  2.01998091,
        4.71397686, 10.08605099,  5.73341274,  5.49238825, 11.48314667,
        7.33338261,  5.70952654, 10.77910328,  5.11640549,  2.44112492,
       13.86255074,  7.16679859,  2.74601722,  4.61110687,  6.12943172,
        2.20142674,  4.43525505,  3.86659837,  3.95408773,  2.27542186,
        4.86921835,  2.64296174,  3.69098854,  8.25018787,  3.55556726,
        4.02291107,  3.52667928,  6.27138805,  6.28837585, 11.31923389,
        4.9943943 ,  6.89435482, 14.18814945,  6.33508492, 10.25922871,
        3.39312696,  3.44871521,  5.59586048,  6.24312973, 13.65777493,
       14.05635262,  7.6548872 ,  4.2573576 ,  3.07330418,  3.305089  ,
       13.53868008,  2.60760069,  5.66980553,  4.27696896,  2.37264442,
        4.63562059, 11.82280827,  1.82628119,  3.80157304, 13.29423141,
       20.84335136, 12.50006962,  9.99870777, 10.08091259, 11.98858452,
        2.5862391 ,  7.59701538, 15.36031914,  5.33975792,  2.3164618 ,
        3.36605191,  4.15815067,  3.16315508,  5.06341267,  4.44539165,
        4.74139977,  6.20207882,  2.35633898,  3.96769047,  7.00715351,
        3.58679008, 15.17469215,  7.52257729,  2.5286293 ,  2.84302425,
        3.70481825,  4.23113918,  6.10596657,  5.00921154,  2.42836142,
        3.22184896, 12.11161518,  8.70620632,  7.08645153, 10.98379326,
       17.33982277,  3.69706464,  3.01107216,  3.78161669,  4.97676182,
        2.40274858,  5.72061825, 15.79512882,  4.01433754, 14.07799721,
        8.91648006,  3.33454061, 12.64497757,  4.47521448,  4.2983675 ,
        4.12469244,  4.89498186,  2.76529932,  6.46360302,  2.9453721 ,
       10.1927557 , 10.15842819,  3.48682833,  3.65751505,  4.76580811,
        3.11539173,  2.16501594,  7.81527996,  3.05419731,  5.59940624,
        4.1684165 , 18.84833527,  3.25148106,  3.92601418, 16.98991776,
       21.29997826,  3.18908739,  8.14012337,  4.61053181,  6.21940804,
        4.22938538,  3.20170546,  7.89121437, 12.66849518, 11.42323589,
        3.93686438,  3.62863827, 14.40352154,  2.47753119,  2.04340243,
        3.62919259, 24.45956802,  4.71820736,  4.15860701,  4.99367857,
        2.3432529 ,  3.43883848,  3.73738003,  8.81822205,  4.09327507,
       24.8817749 ,  5.31143284,  6.69670105,  2.37535429, 15.86834431,
        2.24825954,  6.21214104,  5.05902958,  3.53440046, 10.35809994,
        2.05126143,  3.04776859,  5.38581848,  4.28099012,  3.14001322,
        3.88131976, 10.2620306 ,  4.93388367,  2.31191683,  5.22385979,
        5.50703621,  8.90599442,  6.21238422, 13.17512608,  3.06767988,
        8.10788059, 10.65652561,  2.20982027,  6.05203152,  3.82017255,
       17.79687119,  3.0765152 ])

Model’s fractional differences associated with the test set’s subsample#

fig,ax = plt.subplots(figsize=(14, 10),)
labels_text_size = 40
plt.xticks(fontsize=30) 
plt.yticks(fontsize=30)

xlabel = r'Absolute relative error [%]'
ylabel = r'Test Subsample PDF'

plt.xlabel(xlabel,size=labels_text_size)
plt.ylabel(ylabel,size=labels_text_size)

bins = 20
alpha = 0.75

density = True

y_reg_model = (np.abs(100*(-df_test['S3bar'] + df_test['S3bar_model'])/df_test['S3bar'])).hist(
    density=density, 
    bins=bins, 
    lw=3, 
    edgecolor='teal', 
    zorder=1, 
    histtype='step', 
    alpha=alpha, 
    label=r"ANN model (this work)", 
    color='teal',
).autoscale(enable=True, axis='both', tight=True)

max_deviation = (np.abs(100*(-df_test['S3bar'] + df_test['S3bar_model'])/df_test['S3bar'])).max()

vertical_lines = [max_deviation]  

plt.scatter(vertical_lines[0], 1.18e-3, color='teal',  marker='^', s=300, zorder=2, label=f'Max rel error: {np.round(max_deviation, 2)} %')  # Star marker

for axis in ['top','bottom','left','right']:
    ax.spines[axis].set_linewidth(3.)


plt.yscale('log')

plt.xlim(0,3.)

plt.ylim(1e-3,20.2)

plt.grid(False)
leg = plt.legend(loc="upper right",ncol=1, borderaxespad=1, prop={'size': 19}, shadow=True, fontsize="large")   
leg.get_frame().set_linewidth(3.0)
leg.get_frame().set_edgecolor('black')


plt.tight_layout()
plt.show()
../../_images/bb707ef4e5c5502738f1bb309b01f728dd99005ad8f413463bda71ebbed6dce0.png