Basic Rules of Discrete Probability

In this reading, we’ll introduce discrete and continuous probability, walk through basic probability notation, and describe a few common rules of discrete probability.

Types of Probability

When working with probability in the real world, it’s common to see probability broken down into two categories: discrete probability and continuous probability.

Discrete Probability

Discrete probability deals with discrete variables – that is, variables that have countable values, like integers. Examples of discrete variables include the number of fish in a lake and the number of hobbies that an adult in the U.S. enjoys. 

Therefore, the probability of discrete variables describes the probability of occurrence of each specific value of a discrete variable. As an example, the probability of there being exactly 142 fish in the lake. In another example, the probability of a particular person enjoying exactly 4 hobbies. 

Each possible value of these discrete variables has its own respective non-zero probability of occurring in a dataset. 

Continuous Probability

Continuous probability deals with continuous variables – that is, variables that have infinite and uncountable values. Examples of continuous variables include an individual’s weight and how long it takes to run a kilometer.

Similarly to discrete probability, the probability of continuous variables describes the probability of occurrence of each specific value of a continuous variable. However, these probability values are always close to zero for continuous variables – this is because of the infinite set of outcomes! Any individual outcome, like 2.0000001, is highly unlikely.

We’ll cover more on continuous probability later in this course, but we’ll focus on discrete probability for now.

Probability Notation

In order to learn the basics of discrete probability, it’s important to understand the basics of probability notation.

To symbolize the probability of a discrete event occurring, we use the following notation: P(A) = 0.5

This reads as “the probability of Event A occurring is equal to 0.5.” This can be interpreted as a 50 percent chance of Event A occurring.

Let’s consider a common real-world example: flipping a coin. Below, we write the probability of a flipped standard coin landing heads-up.

P(Heads) = 0.5

To shorten this, we will commonly represent the outcome (Heads) with a single letter. In this case, we are shorting Heads as a capital H.

P(H) = 0.5

Let’s take a look at an example with more possible outcomes: rolling a standard six-sided die. Each of the six outcomes are equally likely to occur when the die is rolled.

P(1) = P(2) = P(3) = P(4) = P(5) = P(6) = 1/6 or about 0.167.

It’s important to note that these examples are theoretical. They are based on what we know about coins and dice, rather than recording actual observations from the real world. In reality, the theoretical probability will most likely not be exactly the same as the actual number of times the event occurs. For example, we might roll a 4 slightly more often, or slightly less often, than 1 in 6 times. So based on recorded data, we might find: P(4) = # of rolls where 4 occurred / # of total rolls = 3/20 = 0.15

However, whether we are working with theoretical or recorded probabilities, the following must be true:

  1. The probability of each outcome must be between 0 and 1: 0 ≤ P(x) ≤ 1.
  2. The sum of the probabilities of each outcome must equal 1: PP(x) = 1.

Common Discrete Probability Rules

When we work with discrete probability, we’re often interested in the more complex questions than the likelihood of a single event occurring in a single trial or draw. Sometimes, we’re interested in the probability that one of a set of non-mutually-exclusive events occurs or in the probability that multiple events occur simultaneously.

In order to answer these questions, we need to understand some of the basic rules of discrete probability. Two common rules are the additive rule and the multiplicative rule.

Additive Rule

The additive rule states that for two events in a single-trial probability experiment, the probability of either event occurring is equal to the sum of their individual probabilities, as long as those events are mutually exclusive. 

Consider the following theoretical examples from our coin and die examples:

P(H or T) = P(H) + P(T) = 0.5 + 0.5 = 1

P(1 or 4) = P(1) + P(4) = ⅙ + ⅙ = ⅓

This rule can be generally stated as:

P(A or B) = P(A) + P(B)

The above rule holds when the events are mutually exclusive, because both events cannot occur – a single coin flip can only bring up heads or tails, not both. And the single roll of a die cannot land on the number 1 and the number 4.

But what about if both events can occur? Let’s look at an example including a standard deck of cards. Assuming that we’re interested in the probability that a single drawn card is a king, denoted by K, or is of the heart suit, denoted by H.

The probabilities of each of these single events are below:

P(K) = 4/52

P(H) = 13/52

If these events were mutually exclusive, meaning there’s no card that is simultaneously a king and of a heart suit, we’d just add P(K) and P(H) together. However, there is a single card that is a king and of a heart suit. 

Its probability is denoted below:

P(K and H) = 1/52

In order to correctly calculate the probability of a single drawn card being a king or of the heart suit, we need to subtract the probability of drawing the king of hearts card. This is because that card is counted as both part of P(K) and P(H) – we’re simply making sure to not count it twice.

P(K or H) = P(K) + P(H) – P(K and H) = 4/52 + 13/52 – 1/52 = 16 / 52

This rule can be generally stated as:

P(A or B) = P(A) + P(B) – P(A and B)

The additive rule can be used to help us understand the probability of a single event of a set of events occurring. Depending on whether the set of events are mutually exclusive from one another, we might need to subtract the probability that they both occur.

Multiplicative Rule

The multiplicative rule states that the probability of two events both occurring is equal to the probability of the first event occurring multiplied by the probability of the second event occurring, as long as those events are independent.

Let us combine our coin and die examples by using the multiplicative rule to compute the probability that a flip of the coin lands heads-up and the die lands with the number 4 facing up.

P(H and 4) = P(H) * P(4) = ½ * ⅙ = 1/12

This rule can be generally stated as:

P(A and B) = P(A) * P(B)

The above rule holds when the two events are independent of one another. The outcome of flipping a coin has absolutely no impact on the outcome of rolling a die.

However, if the events are not independent of one another, then the multiplicative rule states that the probability of two events both occurring is equal to the probability of the first event occurring multiplied by the probability of the second event occurring given that the first event occurred. This last condition accounts for the dependence of the two events.

This rule can be generally stated as:

P(A and B) = P(A) * P(B|A)

Let’s consider our playing card example. Assuming that you want to know the probability of drawing a card that is both a king and of the hearts suit. In order to determine this, you need to know the probability of a card being a king and the probability of the card being of the hearts suit given that it is a king.

These probabilities are denoted below:

P(K) = 4/52

P(H|K) = ¼

Because these events are not independent from one another, we need to multiply these two probabilities by one another to compute P(K and H).

P(K and H) = P(K) * P(H|K) = P(4/52) * P(¼) = 1/52

The multiplicative rule can be used to help us understand the probability of two events occurring. Depending on whether the set of events are independent from one another, we might use the probability of the second event occurring if the first event has occurred, too.

Linux: using netstat -tulpn

The netstat -tulpn command is used to display active network connections and their corresponding processes on a system. Here’s a breakdown of each option:

  • -t: Display TCP connections.
  • -u: Display UDP connections.
  • -l: Show only listening sockets.
  • -p: Show the PID (process ID) and name of the program to which each socket belongs.
  • -n: Show numerical addresses instead of resolving hostnames.

So, netstat -tulpn will list all TCP and UDP connections, show the listening sockets, display the associated process with each connection (along with its PID), and show numerical addresses instead of resolving hostnames. This can be useful for network troubleshooting and monitoring purposes.

How to fix “network ‘default’ is not active” error in libvirt

root@debian01:~# virsh net-list –all

Name State Autostart Persistent

default inactive no yes

root@debian01:~# virsh net-start default
Network default started

root@debian01:~# virsh net-autostart default
Network default marked as autostarted

root@debian01:~# virsh net-list –all

Name State Autostart Persistent

default active yes yes

root@debian01:~#

Xen open-source hypervisor command line reference

Xen is a popular open-source hypervisor that allows for running multiple virtual machines on a single physical host. Here are some common command-line references for managing Xen:

  1. Starting and Stopping Xen:
    • xl create <config_file>: Start a virtual machine defined in the specified configuration file.
    • xl destroy <domain_name>: Forcefully shutdown a virtual machine.
    • xl shutdown <domain_name>: Gracefully shutdown a virtual machine.
    • xl list: List all running domains.
    • xl console <domain_name>: Connect to the console of a running virtual machine.
  2. Managing Virtual Machine Configurations:
    • xl list: List all virtual machines and their states.
    • xl info: Display information about the Xen hypervisor.
    • xl config-list: List all defined VM configurations.
    • xl config-edit <domain_name>: Edit the configuration file of a virtual machine.
    • xl save <domain_name> <state_file>: Save the state of a virtual machine to a file.
    • xl restore <state_file>: Restore a virtual machine from a saved state.
  3. Resource Management:
    • xl mem-set <domain_name> <memory_in_mb>: Set the memory allocation for a virtual machine.
    • xl vcpu-set <domain_name> <num_vcpus>: Set the number of virtual CPUs for a virtual machine.
  4. Networking:
    • Xen usually relies on Linux networking configuration for virtual networking. You can use brctl or ip commands for managing bridges and interfaces.
  5. Snapshot Management:
    • Xen doesn’t have built-in snapshot management like some other hypervisors. You can achieve similar functionality by saving the state of a VM and restoring it later.
  6. XenStore:
    • XenStore is a shared configuration database used by Xen. You can interact with it using the xenstore command. Example:perlCopy codexenstore-ls xenstore-read /local/domain/<domain_id>/memory/target
  7. Debugging and Troubleshooting:
    • xl dmesg: Display Xen hypervisor debug messages.
    • xl top: Display real-time information about the system’s virtualization.
    • xl debug-keys: Print the list of available debug key combinations.

These are some of the basic commands for managing Xen virtual machines and resources. For more detailed information and advanced usage, you can refer to the official documentation for Xen or consult the man pages for the xl command.

KVM (Kernel-based Virtual Machine) is a virtualization solution

KVM (Kernel-based Virtual Machine) is a virtualization solution for Linux that enables users to run multiple virtual machines (VMs) on a single physical host. Below are some common command-line references for managing KVM:

  1. Creating a Virtual Machine:
    • virt-install: Command-line tool for creating new virtual machines. Example:cssCopy codevirt-install --name=myvm --memory=2048 --vcpus=2 --disk path=/var/lib/libvirt/images/myvm.qcow2,size=20 --cdrom /path/to/install.iso --network bridge=br0
  2. Managing Virtual Machines:
    • virsh: Command-line interface for managing virtual machines. Some useful commands include:
      • virsh list: List all running VMs.
      • virsh start <domain>: Start a VM.
      • virsh shutdown <domain>: Shutdown a VM gracefully.
      • virsh destroy <domain>: Forcefully shutdown a VM.
      • virsh reboot <domain>: Reboot a VM.
      • virsh suspend <domain>: Suspend a VM.
      • virsh resume <domain>: Resume a suspended VM.
  3. Managing Virtual Networks:
    • virsh net-list: List all defined virtual networks.
    • virsh net-start <network>: Start a virtual network.
    • virsh net-destroy <network>: Destroy a virtual network.
    • virsh net-edit <network>: Edit a virtual network configuration.
  4. Cloning Virtual Machines:
    • You can use virt-clone or qemu-img to clone virtual machine disk images. Example with qemu-img:cssCopy codeqemu-img create -f qcow2 -b source_image.qcow2 cloned_image.qcow2
  5. Snapshot Management:
    • virsh snapshot-create <domain>: Create a snapshot of a VM.
    • virsh snapshot-list <domain>: List snapshots of a VM.
    • virsh snapshot-revert <domain> <snapshot>: Revert a VM to a specific snapshot.
    • virsh snapshot-delete <domain> <snapshot>: Delete a snapshot of a VM.
  6. Virtual Disk Management:
    • qemu-img: Command-line tool for managing disk images. Examples:
      • qemu-img info disk_image.qcow2: Get information about a disk image.
      • qemu-img resize disk_image.qcow2 +10G: Resize a disk image.
      • qemu-img convert -f qcow2 -O raw disk_image.qcow2 disk_image.raw: Convert disk image formats.

These are some of the basic commands for managing KVM virtual machines and resources. For more detailed information and advanced usage, you can refer to the official documentation for KVM and associated tools.