0% found this document useful (0 votes)
2 views8 pages

Python Visualization

The document discusses the importance of statistical graphics for actuaries, emphasizing the limitations of Excel's charting capabilities in representing complex data structures. It introduces Python's seaborn library as a powerful tool for creating various visualizations, such as violin plots, pair plots, heatmaps, joint plots, and kernel density estimates, which facilitate better understanding of uncertainty and data relationships. The article concludes by highlighting the advantages of using code for reproducible and portable visual diagnostics over traditional manual charting methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Python Visualization

The document discusses the importance of statistical graphics for actuaries, emphasizing the limitations of Excel's charting capabilities in representing complex data structures. It introduces Python's seaborn library as a powerful tool for creating various visualizations, such as violin plots, pair plots, heatmaps, joint plots, and kernel density estimates, which facilitate better understanding of uncertainty and data relationships. The article concludes by highlighting the advantages of using code for reproducible and portable visual diagnostics over traditional manual charting methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Visualizing Uncertainty

Why Actuaries Need Statistical Graphics—And How to Build Them in


Minutes
In the previous article, we learned the syntax of the bridge. We learned how to move data
from the grid into the Python engine.

Now we answer the fundamental question: what do we do with it?

For decades, actuaries have relied on Excel’s native charting library. Excel is excellent for
business reporting: bar, line, and pie charts. These tools are designed to communicate
summarized results, often centred on averages.

But actuaries are not in the business of averages.

We are in the business of deviations, tails, correlations, and uncertainty.

We need to see outliers, asymmetry, clustering, and dependency structures. Many of the
risks we care about are invisible in standard Excel charts—not because Excel is weak
mathematically, but because its charting model is manual, static, and presentation-
oriented.

Python’s statistical visualization libraries—particularly seaborn—are built for exploratory


diagnostics. They expose statistical structures that would otherwise remain hidden.

Each pattern below serves a different diagnostic purpose, but all share one property: they
are executable, repeatable, and designed for uncertainty—not presentation.

Output Note: Seaborn plots render as images in the Excel grid. The output is automatically
set to "Excel Value" mode, displaying the visualization directly in your worksheet. Unlike
data processing steps, plots should always be rendered as values rather than kept as
Python objects.

Below are five visualization patterns that replace manual charting with scripted,
repeatable diagnostics.
The “Fat Tail” Detector: The Violin Plot
The Problem
An actuary’s nightmare is the extreme claim. A standard Excel box plot shows quartiles and
outliers, but it hides the distribution's shape. You cannot tell whether the data is skewed,
bimodal, or heavy-tailed.

The Solution
The violin plot combines a box plot with a kernel density estimate (KDE). It reveals a
smoothed estimate of where probability mass concentrates across the distribution.

The Code
df = xl("tblClaims[#All]", headers=True)

# Create the Violin Plot

[Link](figsize=(8, 6))

[Link](x='Category', y='Claim Values', data=df, inner="quartile", palette="muted")

[Link]("Violin Plot of Claim Values by Category")

[Link]()
Why This Is Engineering
You are not drawing shapes by hand. You are expressing a statistical concept in code and
letting the engine render it correctly every time. When new claims data arrives next
quarter, you refresh, not rebuild.

The Efficiency Play: The Pair Plot


The Problem
When receiving a new dataset, an actuary’s first task is exploratory data analysis. You want
to see how every variable interacts with every other variable. In Excel, this means manually
building multiple scatter plots.

The Solution
A single pair plot generates the entire matrix, scatter plots, and marginal distributions in a
single command.

The Code
pair_df = xl("tblPairs[#All]", headers=True)

[Link](pair_df, hue='Category', palette='muted')


[Link]()

Why This Is Engineering


This is leverage. Hours of manual setup collapse into a single, auditable line of code. And
when a regulator asks, 'How did you check for multicollinearity?'—you show them this
script.

The Correlation Engine: The Heatmap


The Problem
Risk concentration is rarely visible in raw numbers. Excel's conditional formatting can help,
but it is fragile (breaks when rows are inserted), manual (requires clicking through menus),
and difficult to govern (no audit trail of how colours were assigned).

The Solution
A seaborn heatmap provides precise control over colour scales, annotations, and
thresholds. More importantly, when the underlying data changes, via Power Query or other
pipelines, the visualization updates automatically.
The Code
heatmap_df = xl("tblHeatMap")

[Link](figsize=(8, 6))

[Link](heatmap_df, annot=True, fmt=".2f", cmap="YlGnBu", cbar=True, linewidths=0.5)

[Link]("Correlation Matrix")

[Link]()

Why This Matters


This turns correlation analysis into an executable diagnostic rather than a static artifact.

The Deep Dive: The Joint Plot


The Problem
Scatter plots show relationships, but they hide volume. A cluster of points might represent
10 observations or 10,000.

The Solution
The joint plot combines the joint distribution with the marginal distributions, allowing you
to assess dependency and density simultaneously.
The Code
joint_df = xl("tblJoint[#All]", headers=True)

[Link](data=joint_df, x='Feature X', y='Feature Y', kind='scatter')

[Link]()

Why Actuaries Should Care


This is how you detect structural relationships and understand where most of the exposure
lives. For example: In a severity vs. frequency plot, the marginal distributions show you
whether most claims are small/frequent or large/rare—critical for pricing layers.

The Fitting Tool: The Kernel Density Estimate (KDE)


The Problem
Histograms depend heavily on bin size. Poor choices lead to poor intuition.

The Solution
KDE provides a smooth estimate of the underlying probability density function. However,
KDE assumes continuity—for discrete claim counts or categorical data, use
histograms or bar plots instead. For actuaries preparing to fit severity or frequency
distributions, this is the first diagnostic step—not the final model.
The Code
density_df = xl("tblDensity[#All]", headers=True)

[Link](figsize=(8, 6))

[Link](data=density_df, x='Feature', fill=True, alpha=0.6)

[Link]("Probability Density Function")

[Link]()

Common Visualization Mistakes


• Returning plots as Python Objects instead of showing them - Always end with
[Link]() to display
• Forgetting figure size - Dense plots need figsize=(10, 8) or larger
• Using default colour palettes for colorblind accessibility - Use palette='colorblind'
or cmap='viridis'
• Not checking data types - Heatmaps require numeric data; convert categorical
variables first
Conclusion: The End of Click-Ops Visualization
None of the examples above required hundreds of lines of code; most required one or two
function calls.

But the real power is not just insight; it is portability.

A carefully crafted Excel chart is difficult to reuse. It must be rebuilt, copied, or manually
repaired.

A Python visualization is text.

• Want to reuse it in Q3 reporting? Copy the script.

• Want to embed it in a pricing engine? Paste the script.

• Want to standardize it across the team? Version the snippet.

This is the shift:

• The Excel user clicks pixels to build a disposable image.


• The Actuarial Engineer writes code to build a reusable diagnostic.

If a visualization cannot be regenerated from code, it is not an actuarial diagnostic—it is a


picture.

Now that we can see uncertainty, we are ready to model it.

In the following article, we will move to scipy and perform distribution fitting directly in the
grid.

You might also like