Update my_pages/rashomon_effect.py
Browse files- my_pages/rashomon_effect.py +56 -64
my_pages/rashomon_effect.py
CHANGED
|
@@ -5,89 +5,80 @@ from utils import add_navigation, add_instruction_text
|
|
| 5 |
|
| 6 |
plt.style.use('dark_background')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def render():
|
| 9 |
add_navigation("txt_rashomon_effect", "txt_developer_decisions")
|
| 10 |
|
| 11 |
add_instruction_text(
|
| 12 |
"""
|
| 13 |
Consider the following data about individuals who did (green) or didn't (red) repay their loans. <br>
|
| 14 |
-
Which model out of these three will you choose to
|
| 15 |
"""
|
| 16 |
)
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
st.markdown(
|
| 19 |
-
""
|
| 20 |
-
<style>
|
| 21 |
-
button[kind="primary"] {
|
| 22 |
-
background: green!important;
|
| 23 |
-
}
|
| 24 |
-
</style>
|
| 25 |
-
""",
|
| 26 |
unsafe_allow_html=True,
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
income = np.array([80, 85, 97, 91, 78, 102, 84, 88, 81, 40, 45, 51, 34, 47, 38, 39, 97, 91, 38, 32])
|
| 30 |
credit = np.array([970, 880, 1020, 910, 805, 800, 804, 708, 810, 370, 470, 309, 450, 304, 380, 501, 370, 301, 1080, 902])
|
| 31 |
labels = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1])
|
| 32 |
-
|
| 33 |
colors = ['green' if label == 1 else 'red' for label in labels]
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
def plot_scatter(x, y, colors, title="", boundary_type=None, highlight_point=None):
|
| 37 |
-
fig, ax = plt.subplots(figsize=(2, 2))
|
| 38 |
-
ax.scatter(x, y, c=colors, alpha=0.6)
|
| 39 |
-
ax.set_xlabel("Annual Income")
|
| 40 |
-
ax.set_ylabel("Credit Score")
|
| 41 |
-
# ax.set_title(title)
|
| 42 |
-
|
| 43 |
-
fig.patch.set_alpha(0)
|
| 44 |
-
ax.patch.set_alpha(0)
|
| 45 |
-
|
| 46 |
-
# Decision boundary
|
| 47 |
-
if boundary_type is not None:
|
| 48 |
-
if boundary_type == "vertical":
|
| 49 |
-
ax.axvline(65, color='blue')
|
| 50 |
-
ax.fill_betweenx(np.arange(min(y), max(y)), 65, max(x), alpha=0.1, color='green')
|
| 51 |
-
ax.fill_betweenx(np.arange(min(y), max(y)), min(x), 65, alpha=0.1, color='red')
|
| 52 |
-
elif boundary_type == "horizontal":
|
| 53 |
-
ax.axhline(650, color='blue')
|
| 54 |
-
ax.fill_between(np.arange(min(x), max(x)), 650, max(y), alpha=0.1, color='green')
|
| 55 |
-
ax.fill_between(np.arange(min(x), max(x)), min(y), 650, alpha=0.1, color='red')
|
| 56 |
-
elif boundary_type == "slant":
|
| 57 |
-
slope = -10.677966 # From (94, 350) and (35, 980)
|
| 58 |
-
intercept = 1353.7288
|
| 59 |
-
x_sorted = np.sort(x)
|
| 60 |
-
y_line = slope * x_sorted + intercept
|
| 61 |
-
ax.plot(x_sorted, y_line, color='blue')
|
| 62 |
-
ax.fill_between(x_sorted, y_line, max(y), alpha=0.1, color='green')
|
| 63 |
-
ax.fill_between(x_sorted, min(y), y_line, alpha=0.1, color='red')
|
| 64 |
-
|
| 65 |
-
# Highlight specific point
|
| 66 |
-
if highlight_point is not None:
|
| 67 |
-
ax.scatter(*highlight_point, c='green', edgecolors='yellow', s=200, zorder=5, linewidths=4)
|
| 68 |
-
|
| 69 |
-
ax.spines['right'].set_visible(False)
|
| 70 |
-
ax.spines['top'].set_visible(False)
|
| 71 |
-
ax.set_xticks([])
|
| 72 |
-
ax.set_yticks([])
|
| 73 |
-
|
| 74 |
-
return fig
|
| 75 |
-
|
| 76 |
-
|
| 77 |
graph_selected, highlight_point = None, None
|
| 78 |
if "graph_selected" in st.session_state:
|
| 79 |
graph_selected = st.session_state.graph_selected
|
| 80 |
highlight_point = st.session_state.highlight_point
|
| 81 |
|
| 82 |
-
# Top scatter plot (centered to match smaller width)
|
| 83 |
-
col1, col2, col3 = st.columns([1.5, 1, 1.5])
|
| 84 |
-
with col2:
|
| 85 |
-
st.pyplot(plot_scatter(income, credit, colors, title="Original Data"))
|
| 86 |
-
|
| 87 |
col1, col2, col3, col4, col5 = st.columns([0.5, 1, 1, 1, 0.5])
|
| 88 |
with col2:
|
| 89 |
st.pyplot(plot_scatter(income, credit, colors, boundary_type="vertical", highlight_point=highlight_point))
|
| 90 |
-
st.markdown("Accuracy: 90%")
|
| 91 |
if graph_selected=="vertical":
|
| 92 |
button_click_v = st.button("Choose Model 1", type="primary")
|
| 93 |
else:
|
|
@@ -98,7 +89,7 @@ def render():
|
|
| 98 |
st.rerun()
|
| 99 |
with col3:
|
| 100 |
st.pyplot(plot_scatter(income, credit, colors, boundary_type="slant", highlight_point=highlight_point))
|
| 101 |
-
st.markdown("Accuracy: 90%")
|
| 102 |
if graph_selected=="slant":
|
| 103 |
button_click_s = st.button("Choose Model 2", type="primary")
|
| 104 |
else:
|
|
@@ -109,7 +100,7 @@ def render():
|
|
| 109 |
st.rerun()
|
| 110 |
with col4:
|
| 111 |
st.pyplot(plot_scatter(income, credit, colors, boundary_type="horizontal", highlight_point=highlight_point))
|
| 112 |
-
st.markdown("Accuracy: 90%")
|
| 113 |
if graph_selected=="horizontal":
|
| 114 |
button_click_h = st.button("Choose Model 3", type="primary")
|
| 115 |
else:
|
|
@@ -119,11 +110,12 @@ def render():
|
|
| 119 |
st.session_state.graph_selected = "horizontal"
|
| 120 |
st.rerun()
|
| 121 |
|
|
|
|
| 122 |
if "graph_selected" in st.session_state:
|
| 123 |
-
multiplicity_message = "
|
| 124 |
-
|
| 125 |
-
|
| 126 |
st.markdown(
|
| 127 |
-
f"<div style='text-align:center; color:#c0392b; font-size:20px;
|
| 128 |
unsafe_allow_html=True,
|
| 129 |
)
|
|
|
|
| 5 |
|
| 6 |
plt.style.use('dark_background')
|
| 7 |
|
| 8 |
+
def plot_scatter(x, y, colors, title="", boundary_type=None, highlight_point=None):
|
| 9 |
+
fig, ax = plt.subplots(figsize=(2, 2))
|
| 10 |
+
ax.scatter(x, y, c=colors, alpha=0.6)
|
| 11 |
+
ax.set_xlabel("Annual Income")
|
| 12 |
+
ax.set_ylabel("Credit Score")
|
| 13 |
+
# ax.set_title(title)
|
| 14 |
+
|
| 15 |
+
fig.patch.set_alpha(0)
|
| 16 |
+
ax.patch.set_alpha(0)
|
| 17 |
+
|
| 18 |
+
# Decision boundary
|
| 19 |
+
if boundary_type is not None:
|
| 20 |
+
if boundary_type == "vertical":
|
| 21 |
+
ax.axvline(65, color='blue')
|
| 22 |
+
ax.fill_betweenx(np.arange(min(y), max(y)), 65, max(x), alpha=0.1, color='green')
|
| 23 |
+
ax.fill_betweenx(np.arange(min(y), max(y)), min(x), 65, alpha=0.1, color='red')
|
| 24 |
+
elif boundary_type == "horizontal":
|
| 25 |
+
ax.axhline(650, color='blue')
|
| 26 |
+
ax.fill_between(np.arange(min(x), max(x)), 650, max(y), alpha=0.1, color='green')
|
| 27 |
+
ax.fill_between(np.arange(min(x), max(x)), min(y), 650, alpha=0.1, color='red')
|
| 28 |
+
elif boundary_type == "slant":
|
| 29 |
+
slope = -10.677966 # From (94, 350) and (35, 980)
|
| 30 |
+
intercept = 1353.7288
|
| 31 |
+
x_sorted = np.sort(x)
|
| 32 |
+
y_line = slope * x_sorted + intercept
|
| 33 |
+
ax.plot(x_sorted, y_line, color='blue')
|
| 34 |
+
ax.fill_between(x_sorted, y_line, max(y), alpha=0.1, color='green')
|
| 35 |
+
ax.fill_between(x_sorted, min(y), y_line, alpha=0.1, color='red')
|
| 36 |
+
|
| 37 |
+
# Highlight specific point
|
| 38 |
+
if highlight_point is not None:
|
| 39 |
+
ax.scatter(*highlight_point, c='green', edgecolors='yellow', s=200, zorder=5, linewidths=4)
|
| 40 |
+
|
| 41 |
+
ax.spines['right'].set_visible(False)
|
| 42 |
+
ax.spines['top'].set_visible(False)
|
| 43 |
+
ax.set_xticks([])
|
| 44 |
+
ax.set_yticks([])
|
| 45 |
+
|
| 46 |
+
return fig
|
| 47 |
+
|
| 48 |
def render():
|
| 49 |
add_navigation("txt_rashomon_effect", "txt_developer_decisions")
|
| 50 |
|
| 51 |
add_instruction_text(
|
| 52 |
"""
|
| 53 |
Consider the following data about individuals who did (green) or didn't (red) repay their loans. <br>
|
| 54 |
+
Which model out of these three will you choose to judge loan applications?
|
| 55 |
"""
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
#### Rashomon Set Definition
|
| 59 |
+
rashomon_set_message = "The existence of multiple models that achieve similar accuracy, i.e., multiple interpretations of the data, is known as the Rashomon effect. "
|
| 60 |
+
"We call the models below part of a 'Rashomon set'."
|
| 61 |
st.markdown(
|
| 62 |
+
f"<div style='text-align:center; color:#c0392b; font-size:20px; font-weight:bold; margin:14px 0;'>{rashomon_set_message}</div>",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
unsafe_allow_html=True,
|
| 64 |
)
|
| 65 |
|
| 66 |
+
#### Setup data to plot
|
| 67 |
income = np.array([80, 85, 97, 91, 78, 102, 84, 88, 81, 40, 45, 51, 34, 47, 38, 39, 97, 91, 38, 32])
|
| 68 |
credit = np.array([970, 880, 1020, 910, 805, 800, 804, 708, 810, 370, 470, 309, 450, 304, 380, 501, 370, 301, 1080, 902])
|
| 69 |
labels = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1])
|
|
|
|
| 70 |
colors = ['green' if label == 1 else 'red' for label in labels]
|
| 71 |
|
| 72 |
+
#### Plot three graphs to represent three models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
graph_selected, highlight_point = None, None
|
| 74 |
if "graph_selected" in st.session_state:
|
| 75 |
graph_selected = st.session_state.graph_selected
|
| 76 |
highlight_point = st.session_state.highlight_point
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
col1, col2, col3, col4, col5 = st.columns([0.5, 1, 1, 1, 0.5])
|
| 79 |
with col2:
|
| 80 |
st.pyplot(plot_scatter(income, credit, colors, boundary_type="vertical", highlight_point=highlight_point))
|
| 81 |
+
st.markdown("<div style='text-align: center;'>Accuracy: 90%</div>")
|
| 82 |
if graph_selected=="vertical":
|
| 83 |
button_click_v = st.button("Choose Model 1", type="primary")
|
| 84 |
else:
|
|
|
|
| 89 |
st.rerun()
|
| 90 |
with col3:
|
| 91 |
st.pyplot(plot_scatter(income, credit, colors, boundary_type="slant", highlight_point=highlight_point))
|
| 92 |
+
st.markdown("<div style='text-align: center;'>Accuracy: 90%</div>")
|
| 93 |
if graph_selected=="slant":
|
| 94 |
button_click_s = st.button("Choose Model 2", type="primary")
|
| 95 |
else:
|
|
|
|
| 100 |
st.rerun()
|
| 101 |
with col4:
|
| 102 |
st.pyplot(plot_scatter(income, credit, colors, boundary_type="horizontal", highlight_point=highlight_point))
|
| 103 |
+
st.markdown("<div style='text-align: center;'>Accuracy: 90%</div>")
|
| 104 |
if graph_selected=="horizontal":
|
| 105 |
button_click_h = st.button("Choose Model 3", type="primary")
|
| 106 |
else:
|
|
|
|
| 110 |
st.session_state.graph_selected = "horizontal"
|
| 111 |
st.rerun()
|
| 112 |
|
| 113 |
+
#### Multiplicity Definition
|
| 114 |
if "graph_selected" in st.session_state:
|
| 115 |
+
multiplicity_message = "Depending on the model choice, notice the highlighted individual who doesn't get loan, but would have gotten loan under a different model. "
|
| 116 |
+
"These conflicting predictions are called multiplicity.<br><br>"
|
| 117 |
+
"Clearly, the choice of model directly impacts individuals!"
|
| 118 |
st.markdown(
|
| 119 |
+
f"<div style='text-align:center; color:#c0392b; font-size:20px; margin:14px 0;'>{multiplicity_message}</div>",
|
| 120 |
unsafe_allow_html=True,
|
| 121 |
)
|