Geometrical Probability
In the previous chapter, we considered the classic rule for counting probabilities. Due to this rule, the probability is calculated as the ratio of the number of outcomes of interest to us to the number of all possible outcomes. But what can we do if the number of outcomes cannot be counted?
For example, assume you are randomly shooting at a target and you want to determine the probability of hitting the center area of this target.
![Target that we have to shot](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/Probability_basics_content/geom_proba_target.png)
In this case, you can’t just count all the possible outcomes because number of points that you can hit is infinite. As a result, we will have to use geometric probability.
The principle of calculating geometric probabilities is similar to the classical rule - we still assume that all possible elementary outcomes of the experiment are equally probable, but instead of counting the number of outcomes, we consider their geometric measure.
The geometric measure is determined based on the dimension of the space of elementary events:
- if the space is one-dimensional (line), then the length of the line is used as a measure;
- if two-dimensional (plane), then the area of the figure on the plane is used as a measure;
- if three-dimensional (a figure in space), then we use volume as a measure.
Thus, to solve the problem with a target, we can use the ratio of the areas of the area of interest to us and the entire target. Suppose the entire target is a circle with a radius of 2 and the region of interest is a circle in the center with a radius of 1. Then the probability of hitting the central region can be found as follows:
Code Description
- Two variables,
r_large
andr_small
, are defined to represent the radii of the larger and smaller circles, respectively. - The areas of the circles are calculated using the formula
np.pi * radius**2
, wherenp.pi
represents the mathematical constant pi. - The probability is computed by dividing the area of the smaller circle by the area of the larger circle.
- Then we are visualizing the results:
- Two circle patches are created using
plt.Circle()
, representing the larger and smaller circles. They are added to the axis object usingax.add_artist()
. - The aspect ratio of the plot is set to 'equal' using
ax.set_aspect('equal')
, ensuring that the circles appear circular. - The limits of the
x
andy
axes are adjusted usingax.set_xlim()
andax.set_ylim()
to include the circles. - Labels for the
x
andy
axes, a title for the plot, and a legend for the circles are set usingax.set_xlabel()
,ax.set_ylabel()
,ax.set_title()
, andplt.legend()
. - Grid lines are enabled using
plt.grid(True)
. - The plot is displayed using
plt.show()
.
- Two circle patches are created using
- Finally, the calculated probability is printed with 4 decimal places using f-string formatting.
Everything was clear?
Course Content
Probability Theory Basics
5. Covariance and Correlation
Probability Theory Basics
Geometrical Probability
In the previous chapter, we considered the classic rule for counting probabilities. Due to this rule, the probability is calculated as the ratio of the number of outcomes of interest to us to the number of all possible outcomes. But what can we do if the number of outcomes cannot be counted?
For example, assume you are randomly shooting at a target and you want to determine the probability of hitting the center area of this target.
![Target that we have to shot](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/Probability_basics_content/geom_proba_target.png)
In this case, you can’t just count all the possible outcomes because number of points that you can hit is infinite. As a result, we will have to use geometric probability.
The principle of calculating geometric probabilities is similar to the classical rule - we still assume that all possible elementary outcomes of the experiment are equally probable, but instead of counting the number of outcomes, we consider their geometric measure.
The geometric measure is determined based on the dimension of the space of elementary events:
- if the space is one-dimensional (line), then the length of the line is used as a measure;
- if two-dimensional (plane), then the area of the figure on the plane is used as a measure;
- if three-dimensional (a figure in space), then we use volume as a measure.
Thus, to solve the problem with a target, we can use the ratio of the areas of the area of interest to us and the entire target. Suppose the entire target is a circle with a radius of 2 and the region of interest is a circle in the center with a radius of 1. Then the probability of hitting the central region can be found as follows:
Code Description
- Two variables,
r_large
andr_small
, are defined to represent the radii of the larger and smaller circles, respectively. - The areas of the circles are calculated using the formula
np.pi * radius**2
, wherenp.pi
represents the mathematical constant pi. - The probability is computed by dividing the area of the smaller circle by the area of the larger circle.
- Then we are visualizing the results:
- Two circle patches are created using
plt.Circle()
, representing the larger and smaller circles. They are added to the axis object usingax.add_artist()
. - The aspect ratio of the plot is set to 'equal' using
ax.set_aspect('equal')
, ensuring that the circles appear circular. - The limits of the
x
andy
axes are adjusted usingax.set_xlim()
andax.set_ylim()
to include the circles. - Labels for the
x
andy
axes, a title for the plot, and a legend for the circles are set usingax.set_xlabel()
,ax.set_ylabel()
,ax.set_title()
, andplt.legend()
. - Grid lines are enabled using
plt.grid(True)
. - The plot is displayed using
plt.show()
.
- Two circle patches are created using
- Finally, the calculated probability is printed with 4 decimal places using f-string formatting.
Everything was clear?