

In this comprehensive primer on Bayes’ Theorem for Cybersecurity Risk Analysis, you will learn the foundational concepts of Bayesian statistics and how to apply them effectively in the context of cybersecurity.
The primer is designed specifically for cybersecurity professionals looking to enhance their ability to reason under uncertainty and improve their risk analysis capabilities.
By learning and applying the information that I share in this primer, you will be heads and shoulders above your peers.
Business leaders and executives value cybersecurity professionals who can translate the complex landscape of risks and threats into clear, actionable business language. By quantifying cybersecurity risks in terms of probabilities and economic impact, you enhance your credibility and enable informed decision-making at the highest levels of the organization. In an increasingly competitive field, the ability to present cybersecurity threats in terms that resonate with business goals and financial outcomes will set you apart as a strategic advisor rather than just a technical expert. This approach positions you as a key player in aligning cybersecurity efforts with overall business strategy, making you an invaluable asset to any organization.
Key Takeaways:
Introduction to Bayes’ Theorem:
- Understand the fundamental principles of Bayes’ Theorem, a key tool in probabilistic reasoning.
- Learn how Bayes’ Theorem allows for the continuous updating of probabilities as new evidence becomes available, a critical skill in the dynamic field of cybersecurity.
Step-by-Step Application:
- By following the structured sections, readers will build their understanding incrementally, with each concept building on the previous one.
- The primer includes practical examples, such as calculating the probability of a system compromise given the detection of an unusual login attempt, and assessing the risk of a phishing attack leading to a breach.
Integration with Python Programming:
- Learn how to write a Python program that combines industry breach data, such as from the Verizon DBIR report, with internal organizational data to calculate the probability of cyber breaches.
- Explore advanced Python programming techniques, including the use of the Beta Distribution for more sophisticated probability modeling.
Advanced Scenarios and Modifications:
- Dive into advanced scenarios that show how to improve your Python program by incorporating the Beta Distribution to handle uncertain data more effectively.
- Discover various possibilities for customizing your visualizations, such as using color gradients, credibility intervals, and annotated points of interest, to enhance the clarity and impact of your risk analysis.
Practical Use Cases:
- Understand real-world applications of Bayes’ Theorem in cybersecurity, such as phishing attack detection, intrusion detection, and risk assessment for vulnerability exploitation.
- Learn how to combine internal phishing campaign data with industry data to refine your risk models and improve your organization’s cybersecurity posture.
Conclusion and Future Outlook:
- Reflect on the future of cybersecurity risk analysis and the increasing importance of Bayesian Statistics in developing more dynamic and accurate risk models.
- Consider how Bayesian methods provide a flexible and precise alternative to traditional risk matrices, offering a way to continuously update risk assessments as new data emerges.
This primer equips readers with the knowledge and practical tools needed to apply Bayes’ Theorem in cybersecurity scenarios, helping them to develop more robust, data-driven approaches to managing risks in an ever-changing threat landscape. Whether you are new to Bayesian statistics or looking to deepen your understanding, this primer offers valuable insights and practical examples to help you advance your skills.
Copyright Notice
All content on this website, including text, images, and programming code, is the sole property of Tim Layton and is protected by copyright law. © 2024 Tim Layton. All rights reserved. No part of the content on this website, including any subdomains, may be copied, reproduced, distributed, or transmitted in any form or by any means without the express written consent of Tim Layton. Unauthorized use of any content from this website is strictly prohibited and may result in legal action.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Get Immediate Access to All Python Code and Advanced Bonus Content
If you’re ready to dive deeper and want immediate access to all the Python code featured in this comprehensive primer, along with the advanced bonus section, you can purchase it now and start utilizing the code right away.
The Jupyter Notebook file is meticulously commented, with each line of code explained in detail. I’ve also included comprehensive instructions to guide you through how the code works, making it easy to follow and apply in your own projects. Everything you need is conveniently organized in a single Jupyter Notebook file, providing you with a seamless learning experience.
Introduction to Bayes’ Theorem for Cybersecurity Risk Analysis
Bayes’ Theorem is a cornerstone of Bayesian statistics and is fundamental to understanding and applying probabilistic reasoning in various fields, including cybersecurity. For new cybersecurity professionals, mastering Bayes’ Theorem is essential because it allows you to update the probability of an event as new evidence becomes available. This ability to reason under uncertainty is critical for effective cybersecurity risk analysis.
By the end of this primer, you’ll learn how to write a Python program that combines industry breach data from the annual Verizon DBIR report with your organization’s internal data to calculate the probability of a cyber breach caused by phishing emails.
Make sure to follow the primer in the order it’s presented, as each section builds on the concepts introduced in the previous one.
The program creates this visualization to help viewers quickly understand the probability of a breach from this attack vector.

In the advanced section, I walk you through how to improve your Python program to use the Beta Distribution and create this visualization.

In the bonus section at the end of this primer, I share several possibilities for modifying the Python programming code to create various charts.






What is Bayes’ Theorem?
Bayes’ Theorem is a mathematical formula that describes how to update the probability of a hypothesis based on new evidence. It is named after the Reverend Thomas Bayes, an 18th-century statistician and theologian.
As you work through the examples, don’t worry about the values assigned to the scenarios. The values used are for teaching and illustration purposes. Feel free to replace them with your data as desired.
The theorem can be expressed as:
P(A | B) = [P(B | A) * P(A)] / P(B)
Where:
- P(A | B): The posterior probability. The probability of event A (e.g., a system being compromised) given that event B (e.g., detection of an unusual login attempt) is true.
- P(B | A): The likelihood. The probability of event B occurring given that event A is true.
- P(A): The prior probability. The initial probability of event A occurring before considering event B.
- P(B): The marginal likelihood or evidence. The total probability of event B occurring under all scenarios.
Breaking Down the Formula
Let’s break down each component of the formula using a typical cybersecurity scenario.
Prior Probability (P(A))
The prior probability represents our initial belief about the likelihood of an event before considering any new evidence. For example:
- Scenario: You want to assess the likelihood that a system is compromised.
- P(A): Suppose that, based on historical data or industry reports, the probability that your system is compromised is 5% (0.05).
Likelihood (P(B | A))
The likelihood represents how likely it is to observe the evidence if the hypothesis (event A) is true. In our scenario:
- Scenario: You have detected an unusual login attempt (Event B).
- P(B | A): The probability of detecting an unusual login attempt given that the system is compromised might be 80% (0.80).
Marginal Likelihood (P(B))
The marginal likelihood represents the total probability of observing the evidence under all scenarios, both when the system is compromised and when it is not.
- Scenario: You want to consider all possible reasons for detecting an unusual login attempt.
- P(B): Let’s assume the overall probability of detecting an unusual login attempt is 10% (0.10). This accounts for situations where the system is compromised and where it is not.
Posterior Probability (P(A | B))
The posterior probability is what we want to calculate. It tells us the updated probability that the system is compromised given that we have observed the evidence (an unusual login attempt).
Example 1: Calculating the Probability of a System Compromise
Given:
- P(A) = 0.05 (The prior probability of a system compromise)
- P(B | A) = 0.80 (The likelihood of detecting an unusual login attempt if the system is compromised)
- P(B) = 0.10 (The overall probability of detecting an unusual login attempt)
Using Bayes’ Theorem:
P(A | B) = [P(B | A) * P(A)] / P(B)
P(A | B) = [0.80 * 0.05] / 0.10
Step-by-Step Calculation:
- Calculate the numerator: P(B | A) * P(A)
0.80 * 0.05 = 0.04
- Calculate the posterior probability:
P(A | B) = 0.04 / 0.10
P(A | B) = 0.40
Interpretation:
- Given the detection of an unusual login attempt, the probability that the system is compromised is 40%.
Example 2: Evaluating the Risk of a Phishing Attack
Scenario: Suppose you want to evaluate the probability that a phishing attack is successful given that a user has clicked on a suspicious link in an email.
Given:
- P(A) = 0.03 (The prior probability that any given phishing email results in a successful attack)
- P(B | A) = 0.70 (The likelihood that a user clicks on a link given that the phishing attack is successful)
- P(B) = 0.20 (The overall probability that a user clicks on a suspicious link in any email)
Calculation:
P(A | B) = [P(B | A) * P(A)] / P(B)
P(A | B) = [0.70 * 0.03] / 0.20
Step-by-Step Calculation:
- Calculate the numerator: P(B | A) * P(A)
0.70 * 0.03 = 0.021
- Calculate the posterior probability:
P(A | B) = 0.021 / 0.20
P(A | B) = 0.105
Interpretation:
- After a user clicks on a suspicious link, the probability that the phishing attack is successful increases to 10.5%.
Example 3: Assessing the Likelihood of a Data Breach
Scenario: You want to determine the probability that a data breach has occurred given that a large volume of data has been unexpectedly transmitted outside the network.
Given:
- P(A) = 0.02 (The prior probability that a data breach occurs)
- P(B | A) = 0.90 (The likelihood of observing large data transmission given that a breach has occurred)
- P(B) = 0.15 (The overall probability of large data transmission occurring)
Calculation:
P(A | B) = [P(B | A) * P(A)] / P(B)
P(A | B) = [0.90 * 0.02] / 0.15
Step-by-Step Calculation:
- Calculate the numerator: P(B | A) * P(A)
0.90 * 0.02 = 0.018
- Calculate the posterior probability:
P(A | B) = 0.018 / 0.15
P(A | B) = 0.12
Interpretation:
- If a large volume of data is transmitted outside the network, the probability that a data breach has occurred is now 12%.
Conclusion
Bayes’ Theorem provides a systematic approach to updating the probability of an event based on new evidence. In cybersecurity, where uncertainty is prevalent, Bayes’ Theorem is particularly valuable for assessing risks, making informed decisions, and improving security measures. By mastering this foundational concept, you’ll be well-equipped to apply Bayesian statistics to various cybersecurity scenarios and enhance your risk analysis capabilities.
Recommedations
I strongly suggest that you work on each of the three above examples with pencil and paper. Change out the values and run through them again.
Create one or more new scenarios that are applicaple to your environment and use the examples above as a model to work your way through the process.
You must fully understand the basics taught in this section before moving forward.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Use Case Scenario: Using Bayes’ Theorem with Industry Breach Data to Assess Cyber Attack Risks
Scenario Overview
Imagine you’re a cybersecurity analyst working for an organization that has never experienced a significant cyber breach. While this may seem like a positive situation, it presents a challenge: without historical breach data, how can you accurately assess the probability of various cyber attacks that could lead to a breach? This is particularly critical when considering attack vectors that your organization has not encountered before.
In this scenario, you can leverage industry breach data, such as the Verizon Data Breach Investigations Report (DBIR), to estimate the likelihood of different types of cyber-attacks leading to a breach. By applying Bayes’ Theorem, you can update these probabilities as you gather new evidence or detect indicators of potential attacks within your organization.
Why Bayes’ Theorem is Useful in This Context
Bayes’ Theorem is particularly valuable in scenarios where direct experience or data is limited, and you must rely on external information, such as industry reports, to inform your risk assessments. The Verizon DBIR provides comprehensive data on cyber breaches across various industries, including the frequency and impact of different attack vectors (e.g., phishing, ransomware, insider threats). By using this data in conjunction with Bayes’ Theorem, you can create a probabilistic model that estimates the risk of various cyber attacks, even if your organization has not yet experienced them.
Applying Bayes’ Theorem with DBIR Data
Step 1: Define the Probabilities
Let’s consider a specific cyber attack vector—phishing—and calculate the probability that a phishing attack could lead to a breach in your organization using Bayes’ Theorem.
P(A): The prior probability that a phishing attack will lead to a breach, based on DBIR data.
- From the DBIR, assume that 15% of reported breaches across the industry are attributed to successful phishing attacks. So, P(A) = 0.15.
P(B | A): The likelihood that your organization detects phishing emails if a phishing attack leads to a breach.
- Suppose the DBIR indicates that organizations detect phishing attempts 70% of the time when they result in a breach. So, P(B | A) = 0.70.
P(B): The overall probability of detecting phishing emails in your organization, regardless of whether they lead to a breach.
- Assume that your organization detects phishing attempts in 40% of all email communications. So, P(B) = 0.40.
Step 2: Calculate the Posterior Probability
Using Bayes’ Theorem:
P(A | B) = [P(B | A) * P(A)] / P(B)
Substituting the values:
P(A | B) = [0.70 * 0.15] / 0.40
Step 3: Perform the Calculation
Calculate the numerator:
- P(B | A) * P(A) = 0.70 * 0.15 = 0.105
Calculate the posterior probability:
- P(A | B) = 0.105 / 0.40 ≈ 0.2625
Step 4: Interpret the Results
The posterior probability, P(A | B), is approximately 0.2625 or 26.25%. This means that, given your organization’s current detection capabilities and the industry data from the DBIR, there is a 26.25% chance that a detected phishing email could lead to a cyber breach.
How This Approach is Useful
- Risk Assessment for Unencountered Attack Vectors: Even if your organization has never been breached before or has not encountered certain attack vectors, you can use industry data like the DBIR to assess the risk. This is crucial for proactive defense, enabling you to anticipate and prepare for potential threats.
- Updating Risk Models: As your organization gathers more data over time, you can continuously update the probabilities using Bayes’ Theorem. This helps in refining your risk models and making them more accurate and relevant.
- Resource Allocation: Understanding the likelihood of different attack vectors leading to a breach allows you to prioritize your cybersecurity efforts. For example, if phishing is identified as a significant risk, you can allocate more resources to phishing prevention, detection, and response strategies.
- Decision Support: The calculated probabilities can inform decision-making at various levels of the organization, from IT security teams to executive management, ensuring that cybersecurity measures are aligned with the most significant risks.
Conclusion
Bayes’ Theorem, combined with industry breach data like the Verizon DBIR, provides a powerful framework for cybersecurity risk analysis, especially when internal breach data is lacking. By applying this approach, organizations can better understand the potential impact of various cyber attacks, even those they have not yet encountered, and take proactive steps to mitigate these risks. This method allows for a more informed, data-driven approach to cybersecurity, ensuring that defenses are robust and well-targeted against the most likely threats.
Recommedations
I suggest you download a copy of the latest Verizon DBIR Report and find an attack pattern or action that interests you. In the 2024 version of the report, in Figure 57 on page 59, a summary of breaches by industry is provided as shown below this paragraph.
For example, if you were interested in the financial industry (NAICS 52), social engineering attacks lead to 251 cyber breaches with data disclosure. Select this example or something else that interests you, and work your way through the example using the four steps above. Later in the primer, I will show you how to improve your approach.

You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Enhancing Cybersecurity Risk Analysis with Internal Phishing Campaign Data and DBIR Data
Incorporating internal data into your cybersecurity risk analysis, especially data from phishing simulation campaigns, can significantly refine your understanding of how susceptible your organization is to phishing attacks. By combining this internal data with industry breach data, such as the Verizon Data Breach Investigations Report (DBIR), you can create a more accurate and tailored risk model for your organization.
The Role of Internal Phishing Campaign Data
When an organization runs internal phishing campaigns, it gathers valuable data on user behavior—specifically, how many users click on phishing emails and which types of emails are most effective at deceiving users. Over time, by conducting various types of phishing simulations, you can gain insights into:
Click Rates: The percentage of users who click on phishing emails in different scenarios.
Vulnerability to Specific Phishing Techniques: Understanding which types of phishing emails (e.g., spear-phishing, fake invoices, fake IT support) are most effective in your organization.
Trends Over Time: Observing how user behavior changes as users become more aware of phishing risks or as new types of phishing emails are introduced.
This internal data is crucial because it provides direct evidence of how your users are likely to respond to real phishing attempts, allowing you to adjust your security strategies and training programs accordingly.
Combining Internal Data with DBIR Data Using Bayes’ Theorem
Bayes’ Theorem allows you to update the probability of a phishing attack leading to a breach by incorporating both the internal data from phishing campaigns and the broader industry data from the DBIR.
Step 1: Define the Probabilities Using Combined Data
Let’s revisit the scenario where you want to calculate the probability that a phishing attack leads to a breach, given that a user has clicked on a phishing email.
P(A): The prior probability that a phishing attack leads to a breach, based on DBIR data.
- From the DBIR, you know that 15% of breaches are due to phishing attacks. So, P(A) = 0.15.
P(B | A): The likelihood that a user clicks on a phishing email given that a phishing attack leads to a breach.
- Suppose your internal phishing campaigns show that, when users are targeted, 30% of them click on phishing emails. However, combining this with DBIR data, which suggests a 70% click rate in breach cases, you might adjust this to reflect a higher risk in real attacks. For this example, let’s use P(B | A) = 0.50 to balance internal and industry data.
P(B): The overall probability of users clicking on phishing emails, considering both internal and external data.
- If your internal campaigns show a 20% click rate overall, and DBIR data shows a 40% average across the industry, you could weight these depending on their relevance. For this scenario, let’s use P(B) = 0.25.
Step 2: Apply Bayes’ Theorem
Using Bayes’ Theorem:
P(A | B) = [P(B | A) * P(A)] / P(B)
Substituting the values:
P(A | B) = [0.50 * 0.15] / 0.25
Step 3: Perform the Calculation
Calculate the numerator:
- P(B | A) * P(A) = 0.50 * 0.15 = 0.075
Calculate the posterior probability:
- P(A | B) = 0.075 / 0.25
- P(A | B) = 0.30
Step 4: Interpret the Results
The posterior probability, P(A | B), is 0.30 or 30%. This means that, given that a user clicked on a phishing email, there is a 30% chance that the phishing attack could lead to a breach.
Advantages of Using Combined Data
- Refined Risk Assessment: By combining internal data with DBIR data, you create a risk model that is both informed by industry trends and tailored to your organization’s specific context. This hybrid approach improves the accuracy of your risk assessments.
- Dynamic Learning: Internal phishing campaigns provide ongoing, real-time data about user behavior, allowing you to continuously update your risk model as users become more aware of phishing techniques or as new types of phishing emails are introduced.
- Targeted Training and Response: The insights gained from internal data help you design more effective training programs and phishing simulations, focusing on the types of phishing attacks that your users are most vulnerable to. Over time, this can reduce the overall risk of a phishing attack leading to a breach.
- Better Decision-Making: By using Bayes’ Theorem to integrate internal and external data, you can make more informed decisions about where to allocate resources, which threats to prioritize, and how to adjust your cybersecurity strategies to better protect against phishing attacks.
Conclusion
Using Bayes’ Theorem in conjunction with both internal phishing campaign data and industry breach data like the Verizon DBIR allows for a robust, data-driven approach to cybersecurity risk analysis. This method enables organizations to continually refine their understanding of phishing risks, even in the absence of historical breaches, and to proactively mitigate potential threats by tailoring defenses to the specific behaviors and vulnerabilities observed within their own environment. This approach ensures that your cybersecurity strategies are both relevant and resilient in the face of evolving threats.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Typical Use Cases for Using Bayes’ Theorem in Cybersecurity Risk Analysis
Bayes’ Theorem is a powerful tool in the realm of cybersecurity, allowing professionals to make informed decisions based on the probability of various risks and threats. By updating probabilities in light of new evidence, Bayes’ Theorem provides a dynamic approach to risk analysis that is crucial for managing the complexities of cybersecurity. Below are five key use cases where Bayes’ Theorem can significantly enhance cybersecurity risk analysis.
Phishing Attack Detection and Response
Phishing attacks remain one of the most common and dangerous threats to organizations. Bayes’ Theorem can be used to calculate the probability that a phishing email has led to a successful attack based on evidence such as user behavior and email characteristics.
- Use Case: Suppose an organization wants to assess the likelihood that a phishing email has compromised an account after a user clicked on a suspicious link. By applying Bayes’ Theorem, the cybersecurity team can update the probability of a successful phishing attack based on evidence like whether the link was clicked, if the email was flagged by the spam filter, or if the user reported it.
- Impact: This approach allows for a more targeted and efficient response, enabling the organization to prioritize incidents that are more likely to result in a breach and allocate resources accordingly.
Intrusion Detection and Response
Intrusion detection systems (IDS) generate numerous alerts, many of which are false positives. Bayes’ Theorem can be applied to filter out false positives and focus on alerts that are more likely to indicate a real threat.
- Use Case: Consider an IDS that detects unusual network traffic, which could be an indication of an intrusion. By using Bayes’ Theorem, the probability that the network is under attack can be updated based on additional evidence, such as the type of traffic, time of day, and known vulnerabilities in the system.
- Impact: This helps in reducing the noise from false positives, allowing security teams to focus on the most significant threats. It also improves the accuracy of the IDS, making it a more reliable tool for protecting the network.
Malware Detection and Prevention
Malware detection often involves analyzing patterns and behaviors associated with files or processes. Bayes’ Theorem can be used to calculate the probability that a file or process is malicious based on observed characteristics.
- Use Case: Imagine a situation where a file exhibits suspicious behavior, such as modifying system files or accessing sensitive data. Bayes’ Theorem can be used to update the probability that this file is malware, considering factors like its origin, the type of modifications, and the security posture of the system it is operating on.
- Impact: By applying Bayesian reasoning, organizations can improve their malware detection capabilities, reducing the likelihood of both false positives and false negatives. This allows for more effective malware prevention strategies and better protection of critical assets.
Risk Assessment for Vulnerability Exploitation
Organizations must constantly assess the risk of vulnerabilities being exploited by attackers. Bayes’ Theorem can help prioritize vulnerabilities by updating the probability of exploitation based on new evidence.
- Use Case: A vulnerability is discovered in a widely used software application. Bayes’ Theorem can be applied to update the probability of this vulnerability being exploited by considering factors such as the availability of an exploit, the ease of exploitation, and the presence of mitigating controls.
- Impact: This enables security teams to focus on patching the most critical vulnerabilities first, reducing the overall risk to the organization. It also allows for better resource allocation by prioritizing vulnerabilities that are more likely to be exploited.
Insider Threat Detection
Detecting insider threats is challenging due to the difficulty in distinguishing between normal and malicious behavior. Bayes’ Theorem can be used to assess the probability that an employee is engaging in malicious activity based on their behavior and access patterns.
- Use Case: Consider an employee who accesses sensitive data outside of normal working hours. Bayes’ Theorem can be applied to update the probability that this behavior is indicative of an insider threat by considering additional evidence such as the employee’s role, past behavior, and the type of data accessed.
- Impact: This approach improves the detection of insider threats by focusing on behaviors that are statistically more likely to indicate malicious intent. It also helps in reducing false positives, ensuring that legitimate actions are not flagged unnecessarily.
Conclusion
Bayes’ Theorem is a versatile tool that can be applied to various aspects of cybersecurity risk analysis. By updating probabilities in the light of new evidence, it allows organizations to make more informed decisions, prioritize resources, and enhance their overall security posture. Whether it’s detecting phishing attacks, assessing the risk of vulnerabilities, or identifying insider threats, Bayes’ Theorem provides a robust framework for managing cybersecurity risks in a dynamic and uncertain environment. As cybersecurity threats continue to evolve, the ability to reason probabilistically and make data-driven decisions will become increasingly important, making Bayes’ Theorem an essential tool for cybersecurity professionals.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Python Program # 1 For Cybersecurity Risk Analysis with Internal Phishing Campaign Data and DBIR Data Scenario
I created a Python program to illustrate how easy it is to use Bayes Theorem to calculate the probability of a data breach using industry benchmark data and internal data.
# Import necessary libraries
import matplotlib.pyplot as plt
import numpy as np
# Step 1: Define the probabilities based on internal and external data
# P(A): The prior probability that a phishing attack leads to a breach, based on DBIR data
P_A = 0.15 # 15% chance based on industry data
# P(B | A): The likelihood of clicking a phishing email given that a breach occurred
# This is a combined value considering both internal campaigns and DBIR data
P_B_given_A = 0.50 # Adjusted to reflect internal and industry data
# P(B): The overall probability of clicking on a phishing email
# Again, this is a combined value from internal data and DBIR data
P_B = 0.25 # Adjusted to reflect internal and industry data
# Step 2: Apply Bayes' Theorem to calculate P(A | B)
# P(A | B) = [P(B | A) * P(A)] / P(B)
P_A_given_B = (P_B_given_A * P_A) / P_B
# Print the calculated probability
print(f"The probability of a phishing attack leading to a breach given that a user clicked on a phishing email is {P_A_given_B:.2f} or {P_A_given_B * 100:.2f}%.")
# Step 3: Visualization of the Probability
# Data for visualization
labels = ['P(A)', 'P(B | A)', 'P(B)', 'P(A | B)']
values = [P_A, P_B_given_A, P_B, P_A_given_B]
# Create a bar chart to visualize the probabilities
plt.figure(figsize=(10, 6))
bars = plt.bar(labels, values, color=['blue', 'green', 'red', 'purple'])
# Add text labels on the bars
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2, yval, f'{yval:.2f}', ha='center', va='bottom')
# Title and labels
plt.title('Probabilities in Phishing Attack Risk Analysis')
plt.ylabel('Probability')
plt.xlabel('Probability Components')
# Show the plot
plt.show()
Explanation:
Defining the Probabilities:
- The prior probability (
P(A)) is based on industry data. - The likelihood (
P(B | A)) is a combination of internal phishing campaign data and DBIR data. - The overall probability (
P(B)) is also a combination of internal and external data.
Applying Bayes’ Theorem:
- The theorem is used to calculate the posterior probability (
P(A | B)), which is the probability that a phishing attack leads to a breach given that a user clicked on a phishing email.
Visualization:
- A bar chart is generated to visualize each probability component, helping to clearly present the relationships between the prior, likelihood, evidence, and posterior probabilities.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Revised Program # 1 With a Legend For the Visualization
I thought it would be helpful to add a legend below the chart to help viewers quickly understand the information.
# Import necessary libraries
import matplotlib.pyplot as plt
import numpy as np
# Step 1: Define the probabilities based on internal and external data
# P(A): The prior probability that a phishing attack leads to a breach, based on DBIR data
P_A = 0.15 # 15% chance based on industry data
# P(B | A): The likelihood of clicking a phishing email given that a breach occurred
# This is a combined value considering both internal campaigns and DBIR data
P_B_given_A = 0.50 # Adjusted to reflect internal and industry data
# P(B): The overall probability of clicking on a phishing email
# Again, this is a combined value from internal data and DBIR data
P_B = 0.25 # Adjusted to reflect internal and industry data
# Step 2: Apply Bayes' Theorem to calculate P(A | B)
# P(A | B) = [P(B | A) * P(A)] / P(B)
P_A_given_B = (P_B_given_A * P_A) / P_B
# Print the calculated probability
print(f"The probability of a phishing attack leading to a breach given that a user clicked \non a phishing email is {P_A_given_B:.2f} or {P_A_given_B * 100:.2f}%.")
# Step 3: Visualization of the Probability
# Data for visualization
labels = ['P(A)', 'P(B | A)', 'P(B)', 'P(A | B)']
values = [P_A, P_B_given_A, P_B, P_A_given_B]
colors = ['blue', 'green', 'red', 'purple']
# Create a bar chart to visualize the probabilities
plt.figure(figsize=(10, 6))
bars = plt.bar(labels, values, color=colors)
# Add text labels on the bars
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2, yval, f'{yval:.2f}', ha='center', va='bottom')
# Title and labels
plt.title('Probabilities for Phishing Attacks Leading to a Cyber Breach')
plt.ylabel('Probability')
plt.xlabel('Probability Components')
# Add a legend below the chart
legend_labels = [
"P(A): Prior probability that a phishing attack leads to a breach",
"P(B | A): Likelihood of clicking a phishing email given a breach occurred",
"P(B): Overall probability of clicking on a phishing email",
"P(A | B): Posterior probability that a phishing attack leads to a breach given a click"
]
plt.legend(bars, legend_labels, loc='upper center', bbox_to_anchor=(0.5, -0.15), fancybox=True, shadow=True, ncol=1)
# Show the plot
plt.show()

Be sure to connect with me on LinkedIn and subscribe to the blog so you never miss when I post new articles.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Advanced Scenario Using the Beta Distribution
Using the Beta distribution in the Python program could provide several benefits, particularly in the context of Bayesian analysis and when dealing with probabilities that are uncertain or derived from limited data. Here’s why it might be advantageous:
Modeling Uncertainty with Limited Data
- The Beta distribution is commonly used in Bayesian statistics to model the uncertainty of probabilities when dealing with limited data. For example, if you have only a small sample of phishing attempts or internal data, the Beta distribution allows you to incorporate this uncertainty into your calculations.
- It is particularly useful for representing the probability of success (e.g., the probability of a phishing email leading to a breach) when you only have a few observed successes and failures.
Flexibility in Updating Probabilities
- The Beta distribution is a conjugate prior for the binomial distribution in Bayesian analysis. This means that when you update your belief about a probability based on new evidence, the posterior distribution is also a Beta distribution. This property makes it easy to update the probability estimates as new data comes in, which is valuable for continuously refining your risk assessments.
- You can start with a prior Beta distribution reflecting your initial beliefs (e.g., based on DBIR data) and then update this distribution with your internal phishing data as it becomes available.
Incorporating Prior Knowledge
- Using the Beta distribution allows you to incorporate prior knowledge about the probability of a breach caused by phishing, which can come from historical data, expert judgment, or industry reports like the Verizon DBIR. This prior knowledge is combined with your internal data to produce a more informed posterior probability distribution.
- This is particularly useful in cybersecurity, where you might not have extensive internal data but can leverage industry benchmarks and expert insights.
Quantifying Credibility Intervals
- With the Beta distribution, you can easily compute credibility intervals (the Bayesian equivalent of confidence intervals) around your probability estimates. This allows you to quantify the uncertainty around your predictions and make more informed decisions based on the range of possible outcomes.
- For example, instead of just calculating a single probability of a breach, you could provide a range (e.g., “There’s a 95% chance that the probability of a breach is between X% and Y%”).
Improved Risk Management
- By using the Beta distribution, you can better model and understand the risks associated with phishing attacks, particularly when you have limited or evolving data. This leads to more robust risk management strategies, as you can update your assessments in real-time as more data becomes available.
Summary
Incorporating the Beta distribution into your Python program would allow you to handle uncertainty more effectively and update your probability estimates as new data becomes available. This would be especially beneficial when data is limited or uncertain, as it provides a more nuanced and flexible approach to estimating the probability of cyber breaches. Additionally, quantifying uncertainty with credibility intervals offers more comprehensive insights for decision-making in cybersecurity risk management.
If your goal is to create a more sophisticated and adaptive model for assessing cybersecurity risks, using the Beta distribution would be a valuable enhancement to the current method.
Python Progam # 2 Using Beta Distribution
Here’s a revised version of the Python program that uses the Beta distribution to model the probability of a phishing attack leading to a breach. I’ve added detailed comments throughout the code to help you understand how the Beta distribution works in this context.
# Import necessary libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta
# Step 1: Define the parameters for the Beta distribution
# These parameters are based on prior knowledge (e.g., DBIR data) and internal data
# alpha: Represents the number of successful phishing attacks that led to a breach (successes + 1)
# We'll start with a prior belief from the DBIR, assuming 15 successful breaches out of 100 attempts
alpha_prior = 15 + 1 # Add 1 to avoid a zero value for alpha
# beta: Represents the number of phishing attacks that did not lead to a breach (failures + 1)
# From DBIR, assume 85 phishing attacks did not lead to a breach out of 100 attempts
beta_prior = 85 + 1 # Add 1 to avoid a zero value for beta
# Step 2: Define the internal data from your organization's phishing campaign
# Let's say you ran a phishing campaign where 5 out of 20 users clicked on a phishing email, but only 1 led to a breach
# Update alpha with the internal data (number of successful breaches)
alpha_internal = 1 + 1 # Add 1 to the number of successes
# Update beta with the internal data (number of phishing attempts that did not lead to a breach)
beta_internal = (20 - 1) + 1 # Total attempts - successful breaches + 1
# Combine the prior knowledge with the internal data
alpha_posterior = alpha_prior + alpha_internal - 1 # Subtract 1 to correct for initial +1
beta_posterior = beta_prior + beta_internal - 1 # Subtract 1 to correct for initial +1
# Step 3: Create the Beta distribution using the posterior parameters
# The Beta distribution models the probability of a phishing attack leading to a breach
posterior_distribution = beta(alpha_posterior, beta_posterior)
# Step 4: Calculate the mean of the posterior Beta distribution
# The mean of the Beta distribution gives us the most likely probability of a breach given the data
mean_posterior = posterior_distribution.mean()
# Print the calculated probability
print(f"The mean probability of a phishing attack leading to a breach, based on the Beta distribution, is {mean_posterior:.2f} or {mean_posterior * 100:.2f}%.")
# Step 5: Visualization of the Beta Distribution
# Generate a range of x values between 0 and 1 for plotting the Beta distribution
x = np.linspace(0, 1, 100)
# Plot the Beta distribution curve
plt.figure(figsize=(10, 6))
plt.plot(x, posterior_distribution.pdf(x), 'r-', lw=2, label='Beta Distribution (Posterior)')
plt.fill_between(x, posterior_distribution.pdf(x), alpha=0.2, color='red')
# Title and labels
plt.title('Posterior Beta Distribution for Phishing Attack Breach Probability')
plt.xlabel('Probability of a Breach')
plt.ylabel('Density')
# Add legend
plt.legend(loc='upper right')
# Show the plot
plt.show()
# Step 6: Credibility Interval Calculation (Optional)
# We can calculate a 95% credibility interval (the Bayesian equivalent of a confidence interval)
credibility_interval = posterior_distribution.interval(0.95)
print(f"95% credibility interval for the breach probability: {credibility_interval[0]:.2f} to {credibility_interval[1]:.2f}")

Detailed Explanation:
Step 1: Define the Parameters for the Beta Distribution
- Alpha (α) and Beta (β): These parameters define the shape of the Beta distribution. They represent the number of successes (breaches) and failures (non-breaches). We start with a prior based on the DBIR data, assuming 15% of phishing attempts lead to breaches (α = 15+1) and 85% do not (β = 85+1).
Step 2: Update with Internal Data
- Internal Data: We simulate an internal phishing campaign where 5 out of 20 users clicked on a phishing email, but only 1 click led to a breach. We update our Alpha and Beta parameters with this new data, reflecting the combined knowledge from both external (DBIR) and internal data.
Step 3: Create the Posterior Beta Distribution
- Posterior Distribution: We create the Beta distribution using the updated (posterior) parameters. This distribution represents the updated belief about the probability of a breach after observing both internal and external data.
Step 4: Calculate the Mean of the Posterior Distribution
- Mean Probability: The mean of the Beta distribution provides the most likely probability of a breach given the observed data. This is calculated and printed as the main result.
Step 5: Visualization of the Beta Distribution
- Plotting the Distribution: We visualize the Beta distribution using
matplotlib. The curve represents the probability density of different breach probabilities, helping you understand how likely different outcomes are.
Step 6: Calculate a Credibility Interval (Optional)
- Credibility Interval: A credibility interval gives a range within which the true breach probability lies with a certain level of confidence. In this example, we calculate a 95% credibility interval, which means there’s a 95% chance that the true breach probability lies within this range.
Conclusion
Using the Beta distribution allows us to model the probability of a phishing attack leading to a breach in a more sophisticated way, incorporating both prior knowledge and new evidence. This approach is particularly useful when dealing with uncertain probabilities or limited data, providing a more nuanced understanding of risk in cybersecurity scenarios. By visualizing the distribution and calculating credibility intervals, we gain deeper insights into the potential range of outcomes, enabling more informed decision-making.
BONUS SECTION
Get Immediate Access to All Python Code and Advanced Bonus Content
If you’re ready to dive deeper and want immediate access to all the Python code featured in this comprehensive primer, along with the advanced bonus section, you can purchase it now and start utilizing the code right away.
The Jupyter Notebook file is meticulously commented, with each line of code explained in detail. I’ve also included comprehensive instructions to guide you through how the code works, making it easy to follow and apply in your own projects. Everything you need is conveniently organized in a single Jupyter Notebook file, providing you with a seamless learning experience.
You can customize the visualization in endless ways to meet your specific goals and needs. Remember, the Python source code for everything in the bonus section is also included in the Jupyter Notebook file.
Bonus # 1 – Add Dottle Line For Mean & Add an Annotation
For example, you could add a dotted line from the peak of the curve at the mean to the x-axis to highlight the most likely probability of a breach.

Bonus # 2- Color Gradients & Shading
Color Gradients and Shading:
- Use a color gradient to shade the area under the curve, with colors transitioning from one hue to another as the probability increases. This can visually emphasize the likelihood of different outcomes across the distribution.
- Apply different levels of opacity to the shading, making the area under the highest density regions more prominent.

Bonus # 3 – Credibility Intervals
Credibility Intervals:
- Visualize credibility intervals (the Bayesian equivalent of confidence intervals) by shading the region of the curve that falls within a specific interval (e.g., 95%). This can help highlight the range within which the true breach probability is most likely to lie.
- Add vertical lines at the bounds of the credibility interval to make it clearer where this interval starts and ends.

Bonus # 4 – Annotated Points of Interest
Annotated Points of Interest:
- Annotate specific points of interest on the curve, such as the median or mode of the distribution, in addition to the mean. This can provide a more comprehensive understanding of the data.
- Highlight significant deviations from the mean, such as the tails of the distribution, where rare but high-impact events might occur.

Bonus # 5 – Comparison with Empirical Data
Comparison with Empirical Data:
- Overlay empirical data points from your phishing campaigns on top of the Beta distribution curve. This can show how well the model fits your actual data and where discrepancies might lie.
- Include a secondary y-axis to represent the frequency of observed outcomes from your campaigns, aligning this with the density curve.

Bonus # 6 – Customized Legends & Labels
Customized Legends and Labels:
- Customize the legend and axis labels to reflect your specific use case or audience. For instance, you could translate technical terms into more accessible language for non-expert stakeholders.
- Use icons or images in the legend to represent different distributions or scenarios, making the visualization more visually engaging.

By incorporating these ideas, you can tailor the visualization to better fit your specific objectives, making it a more powerful tool for communicating risk and informing decisions within your organization. Whether you’re focusing on making the data more accessible, providing a deeper analysis, or creating an interactive experience, these customizations can help you achieve your goals.
Get Immediate Access to All Python Code and Advanced Bonus Content
If you’re ready to dive deeper and want immediate access to all the Python code featured in this comprehensive primer, along with the advanced bonus section, you can purchase it now and start utilizing the code right away.
The Jupyter Notebook file is meticulously commented, with each line of code explained in detail. I’ve also included comprehensive instructions to guide you through how the code works, making it easy to follow and apply in your own projects. Everything you need is conveniently organized in a single Jupyter Notebook file, providing you with a seamless learning experience.
Final Summary
I hope you have found this primer on Bayes’ Theorem both useful and insightful. As we navigate the evolving landscape of cybersecurity, I firmly believe that the future of risk analysis lies in the application of Bayesian Statistics. This approach allows us to move beyond static and often oversimplified methods like the traditional risk matrix, offering a more dynamic and nuanced way to assess and manage risks.
Bayesian Statistics empowers us to incorporate prior knowledge, continuously update our understanding with new data, and quantify uncertainty in a way that traditional models simply cannot match. As our industry becomes increasingly data-driven, I believe we will look back on the widespread use of risk matrices and question why such a rigid, one-size-fits-all approach ever became so ubiquitous.
In contrast, Bayesian methods offer the flexibility and precision required to address the complex, ever-changing nature of cybersecurity threats. By embracing these advanced statistical tools, we can develop more accurate risk assessments, make better-informed decisions, and ultimately create stronger, more resilient security postures.
The future of cybersecurity will not only involve defending against known threats but also anticipating and adapting to new ones. Bayesian Statistics provides the framework to do just that, allowing us to stay ahead of the curve and effectively protect our organizations in a world where the only constant is change.
You can connect with me on LinkedIn and join my professional network.
I share weekly insights on quantifying cyber risk in dollars, not colors — including Monte Carlo simulation, loss exceedance modeling, Cyber Value at Risk (VaR), and NIST CSF quantification. If you’re an executive, CISO, or security leader looking for practical, data-driven approaches to cyber risk, let’s connect on LinkedIn.
Copyright Notice
All content on this website, including text, images, and programming code, is the sole property of Tim Layton and is protected by copyright law. © 2024 Tim Layton. All rights reserved. No part of the content on this website, including any subdomains, may be copied, reproduced, distributed, or transmitted in any form or by any means without the express written consent of Tim Layton. Unauthorized use of any content from this website is strictly prohibited and may result in legal action.