import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import TwoSlopeNorm
def cf_combine(x, y):
"""MYCIN certainty factor combination formula"""
if x > 0 and y > 0:
return x + y - x*y
elif x < 0 and y < 0:
return x + y + x*y
else:
min_abs = min(abs(x), abs(y))
if min_abs == 1: # Handle division by zero case
return np.nan
return (x + y)/(1 - min_abs)
# Create a grid of x and y values
n = 200
x = np.linspace(-1, 1, n)
y = np.linspace(-1, 1, n)
X, Y = np.meshgrid(x, y)
# Calculate Z values using the combination formula
Z = np.zeros((n, n))
for i in range(n):
for j in range(n):
Z[i, j] = cf_combine(X[i, j], Y[i, j])
# Create figure and axes with 1:1 aspect ratio
fig, ax = plt.subplots(figsize=(8, 8))
# Contour plot
levels = np.linspace(-1, 1, 21)
im = ax.contour(X, Y, Z, levels=levels, cmap='RdBu')
ax.clabel(im, inline=True, fontsize=8)
ax.set_title('Contour Plot of MYCIN Certainty Factor Combination')
ax.grid(True, linestyle='--', alpha=0.6)
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
ax.set_aspect('equal') # Ensure 1:1 aspect ratio
plt.tight_layout()
plt.savefig('mycin_cf_combine.svg')
plt.show()