• No results found

Set theory

In document Statistics and Data With R (Page 112-117)

Probability and random variables

4.1 Set theory

In this section we first discuss sets and algebra of sets. Next, we discuss applications of R to set theory.

4.1.1 Sets and algebra of sets

A set is a collection of objects. These objects are called elements. We say that B is a subset of A if all the elements of B are also elements of A. We write it as B ⊂ A.

Accordingly, any set is a subset of itself. We denote sets with upper-case letters and elements of sets with lower-case letters. To indicate that a collection of elements form a set we enclose the elements, or their description, in braces. The expression

A ={a1, a2, . . . , an} (4.1) says that the set A consists of a collection of n elements, a1 through an. A set can be characterized by its individual elements—as in (4.1)—or by the properties of its elements. For example,

A ={a : a > 1} (4.2)

says that the set A consists of elements a such that a > 1. Often we need to be more explicit about the set from which the elements are drawn. For example, (4.2) describes two different sets when a is an integer, or when it is a real number. In the present context, the integers or the real numbers are the underlying spaces or the universe.

We often denote the universal set by S. We call a set with no elements the empty or the null set and denote it by ∅.

Example 4.2. An organism is either alive (a) or dead (d). Therefore we write S ={a, d} .

The set of all possible subsets of S is

P = {∅, {a} , {d} , S} .

The set of all subsets,P, consists of 22= 4 subsets. ut Elements of sets can be sets. We call the set of all possible subsets the power set and denote it byP. It can be shown that the power set of any set with n elements consists of 2n subsets.

To understand how probabilities are constructed and manipulated, we need to be familiar with set operations. We introduce these operations with the help of Venn diagrams. In the following diagrams, squares represent the universal set S.

Transitivity If A⊂ B and B ⊂ C then A ⊂ C (Figure 4.1). Thus, for any set A, A⊂ A , ∅ ⊂ A , A ⊂ S .

Example 4.3. Let C be the set of all people in South Africa, B be the set of all black people in South Africa and A the set of all black men in South Africa. Then A is a subset of B and B is a subset of C. Obviously, A is a subset of C. Here we may

identify S with C. ut

Set theory 99

Figure 4.1 Transitivity.

Equality Set A equals set B if and only if every element of A is an element of B and every element of B is an element of A. That is,

A = B if and only if A⊂ B and B ⊂ A .

Union The union of two sets A and B is a set whose elements belong to A, or to B, or to both (Figure 4.2). The union is denoted by ∪ and A ∪ B reads “A union B.”

Figure 4.2 Union.

Example 4.4. Let A be the set of all baseball, basketball and football players and B the set of all basketball and hockey players. Then A∪ B = { baseball, basketball, football, hockey players}. Note that basketball players are not counted twice. In

unions, common elements are never counted twice. ut

Associativity For any sets A, B and C

A∪ B ∪ C = (A ∪ B) ∪ C = A ∪ (B ∪ C) .

Example 4.5. Consider a small hospital with 3 wards. Let A, B and C be the sets of patients in each of these wards. Then, A∪ B ∪ C is the set of all patients in the hospital. Now take D := A∪ B. Obviously, D ∪ C is the set of all patients in the hospital. Also, for D := B∪ C, we have that A ∪ D is the set of all patients in the

hospital. ut

Commutativity Using the same reasoning as for associativity, you can easily verify that

A∪ B = B ∪ A .

Also,

A∪ ∅ = A , A ∪ S = S, and if B⊂ A then A ∪ B = A.

Example 4.6. Let

A ={oranges, tomatoes} , B ={bananas, apples} ,

S ={fruits} . Then

A∪ B = {oranges, tomatoes, bananas, apples}

and

B∪ A = {bananas, apples, oranges, tomatoes} .

Because order is not important, we can rearrange the items in A∪ B such that A ∪ B

= B∪ A. Also, the union of A with nothing gives A. And the union of A with S gives fruits because all member of the union are fruits. ut Intersection The intersection of two sets, A and B, is a set consisting of all elements

belonging to both A and B. This is written as A∩ B. From Figure 4.3, (A∩ B) ∩ C = A ∩ (B ∩ C) = A ∩ B ∩ C .

It then follows that

Figure 4.3 Intersection.

A∩ A = A , A ∩ ∅ = ∅ , A ∩ S = A . Example 4.7. Let

A ={1, 2, 3, 4} , B ={3, 4, 5, 6} , C ={4, 5, 6, 7} ,

S = the set of all integers .

Set theory 101 The elements 1, 2, 3 and 4 are members of the set A. The common elements of A∩ A are also these elements. Therefore, A∩ A = A. There are no elements common to A and ∅. Therefore, A ∩ ∅ = ∅. Finally, the elements common to A and S are the elements of A. Therefore, A∩ S = A.

Note that

A∩ B = {3, 4} . Then

(A∩ B) ∩ C = {3, 4} ∩ {4, 5, 6, 7} = {4} . Similarly,

B∩ C = {4, 5, 6} . Then

A∩ (B ∩ C) = {1, 2, 3, 4} ∩ {4, 5, 6} = {4} .

Again, note that common elements are not counted twice. ut If two sets have no elements in common then

A∩ B = ∅ .

A and B are then said to be disjoint sets (or mutually exclusive sets).

Distribution As Figure 4.4 illustrates, the distributive law for sets is

Figure 4.4 Distribution.

A∩ (B ∪ C) = A ∩ B ∪ A ∩ C . Example 4.8. Returning to Example 4.7, we have

D := B∪ C = {3, 4, 5, 6, 7}

and

A∩ D = {1, 2, 3, 4} ∩ {3, 4, 5, 6, 7}

={3, 4} . Similarly,

D := A∩ B = {3, 4} , E := A ∩ C = {4} . Therefore,

D∪ E = {3, 4} ∪ {4} = {3, 4} .

u t

Figure 4.5 Complement.

Complement The complement of the set A, denoted by A, is the set of all elements of S that are not in A (Figure 4.5). Thus,

∅ = S , S =∅ , A = A , A∪ A = S , A∩ A = ∅ ,

if B ⊂ A then B ⊃ A , if A = B then A = B .

Example 4.9. Let S be the set of all integers. Then in the defined space, ∅ has no elements. All of the elements that are not in ∅ are integers and they constitute S. In notation,∅ = S. Similarly, because S includes all of the integers, the set that has no integers in the space of all integers is empty. In notation, S =∅.

Let A = {1, 2}. Then, A is the set of all integers except 1 and 2. The set of all elements that are not in A is A ={1, 2} = A.

Let B ={1}. Then, B ⊂ A. Also, B is the set of all integers except 1 and A is the set of all integers except 1 and 2. Therefore, A⊂ B.

Finally, let C ={1, 2}. Then obviously A = C. ut

Difference The set A− B consists of all of the elements of A that are not in B.

Similarly, the set B− A consists of all elements of B that are not in A (Figure 4.6).

To distinguish the “−” operation on sets from the usual subtraction operation, we sometimes write A− B := B\A. Note that

A− B = A ∩ B = A − A ∩ B .

Figure 4.6 Difference.

Trials, events and experiments 103 To convince yourself that this indeed is the case, trace the sets B, A∩ B and A∩ B in Figure 4.6. In general, (A − B) ∪ B does not equal A. Furthermore,

(A∪ A) − A = ∅ while A ∪ (A − A) = A . Further reflection will convince you that for any A,

A− ∅ = A , A − S = ∅ , S − A = A .

Example 4.10. Let A = {1, 2, 3, 4}, B = {3, 4, 5, 6} and S the set of all integers.

Then

A− B = {1, 2} , B − A = {5, 6} . Also,

A∩ B = {1, 2, 3, 4} ∩ {all integers except 3, 4, 5, 6}

={1, 2} = A − B and

A− A ∩ B = {1, 2, 3, 4} − {3, 4}

={1, 2} = A − B .

u t Sum The sum of two sets is a new set with all the elements of both. Common elements

are counted twice. Thus,

A + B = A∪ B + A ∩ B . Therefore,

A∪ B = A + B − A ∩ B . Example 4.11. The sum of sets A and B in Example 4.4 is

A∪ B = {Baseball, basketball, football, basketball, hockey players} .

Basketball players are counted twice! ut

4.1.2 Set theory in R

The most obvious applications of set theory ideas in R relate to data manipulation and spatial analysis. Examples of common tasks are union and intersection of polygons and tests for whether points are within a polygon. R includes several packages that make such work easy.

Some of the spatially related packages are geoR, gstat, splancs and gpclib.

Review the help for these packages and you will probably find functions that do what you need. We discussed merge(), union() and intersect() in Example 2.17.

In document Statistics and Data With R (Page 112-117)