Calculating Probability
Moose Gimmick
Listing 2 uses the CPAN Moose module, which saves me from having to code the Distrib
constructor in Perl. However, the code instead needs to use has
(line 3) to declare and initialize the class attributes. This is just a neat gimmick in the present case; for more attributes, however, the code would be much leaner than manually creating classes.
The set()
method expects the name of a hypothesis (e.g., BB
) and its a priori probability and stores this in the object's internal values
array. To multiply a hypothesis value by a constant value, both are passed to mult()
; it finds the previously stored value in values
and multiplies it with the value passed in for $prob
.
The normalize()
method iterates over all values previously inserted into the hash, adds them up in $sum
, and then divides all the values by the sum. Thus, the new sum of all probability values after a multiplication is again 1. Each value can thus be interpreted as a probability between 0 and 1. At the bottom of the module, prob()
finds the value for the searched-for hypothesis by retrieving the hash value using the values()
method. The latter has been automatically generated by Moose and returns the value stored under the key of the desired hypothesis.
More Abstraction
If you perform several such tests for various problems, you will identify a pattern: After establishing the hypotheses, the probabilities of all defined hypotheses are always multiplied by the likelihood of newly incoming data. It makes sense (as shown in Think Bayes [3]) to define a class derived from Distrib
by the name of HypoTest
, much as in Listing 3. The class uses an update()
method to update all the values for all hypotheses, based on the probabilities of incoming data.
Listing 3
HypoTest.pm
HypoTest
also relies on classes derived from it (e.g., CardHypoTest
in Listing 4) to overload the abstract likelihood()
method and return the value for P(D|H) based on the probability of the additionally available data D under the assumption that hypothesis H is true.
Listing 4
hypotest
The HypoTest
framework in Listing 3 calls the likelihood()
method repeatedly to obtain the data probabilities under the assumption of individual hypotheses before storing them in the Distrib
distribution. The framework further provides a print()
method, which is used to output the values of all the updated probabilities for each hypothesis.
Testing Hypotheses
In Listing 4, likelihood()
accepts the letter R from the main program as $data
– to record a drawn card with red front as an additional condition. Then, based on the hypothesis also passed in (RR
, RB
, BB
), it computes how likely it is that the test candidate will look at a red surface: 1 (i.e., 100 percent) for RR
, 0.5 for RB
, and 0 for BB
.
For this calculation, the function uses a regular expression that counts the number of Rs in the hypothesis and divides the result by 2 as a floating-point value, so that Perl does not perform integer division and dump the remainder.
Finally, hypotest
outputs the probabilities for all hypotheses in the distribution using the print()
method from the HypoTest
module and correctly reports that the red-red card will appear in 2/3 of all cases:
$ ./hypotest RC 0.333333333333333 RR: 0.666666666666667 BB 0
In other words, the test candidate, who has just drawn a card with a red front and now turns over this card, will see a red back with probability of 2/3.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.