Relationship Visualization

This gallery page showcases plots from the relationship module, which provide unique polar perspectives on the relationships between the core components of a forecast: true values, model predictions, and forecast errors.

These diagnostic plots are designed to reveal complex patterns such as conditional biases, heteroscedasticity, and non-linear correlations that are often difficult to see in standard Cartesian plots. This module is expected to expand with more specialized relationship diagnostics in the future.

Note

You need to run the code snippets locally to generate the plot images referenced below. Ensure the image paths in the .. image:: directives match where you save the plots.

True vs. Predicted Relationship

Uses plot_relationship() to map true values to the angular axis and normalized predicted values to the radial axis. This creates a spiral-like plot that reveals the consistency and correlation of model predictions across the entire range of true values.

 1import kdiagram.plot.relationship as kdr
 2import pandas as pd
 3import numpy as np
 4import matplotlib.pyplot as plt
 5
 6# --- Data Generation ---
 7np.random.seed(42)
 8n_points = 150
 9# Create a clear, non-linear true signal
10y_true = np.linspace(0, 10, n_points)**1.5 + np.sin(
11    np.linspace(0, 10, n_points)
12) * 2
13
14# Model 1: Good fit with some noise
15y_pred1 = y_true + np.random.normal(0, 1.5, n_points)
16# Model 2: Worse fit, under-predicts high values
17y_pred2 = y_true * 0.8 + np.random.normal(0, 2.5, n_points)
18
19# --- Plotting ---
20kdr.plot_relationship(
21    y_true,
22    y_pred1,
23    y_pred2,
24    names=["Good Model", "Biased Model"],
25    title="Gallery: True vs. Predicted Relationship",
26    theta_scale="proportional",  # Map angle to y_true value
27    acov="default",
28    s=40,
29    # Save the plot (adjust path relative to this file)
30    savefig="gallery/images/gallery_plot_relationship.png",
31)
32plt.close()
Example of a Polar Relationship Plot

Conditional Quantile Bands

Visualizes how the predicted conditional distribution (represented by quantile bands) changes as a function of the true observed value. It is a powerful tool for diagnosing heteroscedasticity.

 1import kdiagram as kd
 2import pandas as pd
 3import numpy as np
 4from scipy.stats import norm
 5import matplotlib.pyplot as plt
 6
 7# --- Data Generation with Heteroscedasticity ---
 8np.random.seed(0)
 9n_samples = 200
10y_true = np.linspace(0, 20, n_samples)**1.5
11quantiles = np.array([0.1, 0.25, 0.5, 0.75, 0.9])
12
13# Uncertainty (interval width) increases with the true value
14interval_width = 5 + (y_true / y_true.max()) * 15
15y_preds = np.zeros((n_samples, len(quantiles)))
16y_preds[:, 2] = y_true # Median
17y_preds[:, 1] = y_true - interval_width * 0.25 # Q25
18y_preds[:, 3] = y_true + interval_width * 0.25 # Q75
19y_preds[:, 0] = y_true - interval_width * 0.5  # Q10
20y_preds[:, 4] = y_true + interval_width * 0.5  # Q90
21
22# --- Plotting ---
23kd.plot_conditional_quantiles(
24    y_true,
25    y_preds,
26    quantiles,
27    bands=[80, 50], # Show 80% and 50% intervals
28    title="Conditional Uncertainty (Heteroscedasticity)",
29    savefig="gallery/images/plot_conditional_quantiles.png"
30)
31plt.close()
Example of a Conditional Quantile Plot

Error vs. True Value Relationship

Visualizes the relationship between the forecast error and the true observed value. This is a polar version of a classic residual plot, designed to diagnose conditional biases and heteroscedasticity.

 1import kdiagram as kd
 2import pandas as pd
 3import numpy as np
 4import matplotlib.pyplot as plt
 5
 6# --- Data Generation with Conditional Bias ---
 7np.random.seed(0)
 8n_samples = 200
 9y_true = np.linspace(0, 20, n_samples)**1.5
10# Model has a bias that depends on the true value (under-predicts high values)
11bias = -0.1 * y_true
12y_pred = y_true + bias + np.random.normal(0, 2, n_samples)
13
14# --- Plotting ---
15kd.plot_error_relationship(
16    y_true,
17    y_pred,
18    names=["My Model"],
19    title="Error vs. True Value (Conditional Bias)",
20    savefig="gallery/images/gallery_error_relationship.png"
21)
22plt.close()
Example of an Error vs. True Value Plot

Residual vs. Predicted Relationship

Visualizes the relationship between the forecast error (residual) and the predicted value. This is a polar version of a classic residual plot, designed to diagnose conditional biases and heteroscedasticity related to the model’s own output.

 1import kdiagram as kd
 2import pandas as pd
 3import numpy as np
 4import matplotlib.pyplot as plt
 5
 6# --- Data Generation with Heteroscedastic Errors ---
 7np.random.seed(0)
 8n_samples = 200
 9# Predictions follow a non-linear trend
10y_pred = np.linspace(0, 50, n_samples) + np.random.normal(0, 2, n_samples)
11# Error variance increases as the prediction gets larger
12error_variance = (y_pred / y_pred.max())**2 * 10
13errors = np.random.normal(0, np.sqrt(error_variance), n_samples)
14y_true = y_pred + errors
15
16# --- Plotting ---
17kd.plot_residual_relationship(
18    y_true,
19    y_pred,
20    names=["My Model"],
21    title="Residual vs. Predicted Value",
22    s=40,
23    alpha=0.6,
24    savefig="gallery/images/gallery_residual_relationship.png"
25)
26plt.close()
Example of a Residual vs. Predicted Plot