img.latexEquation { background:white; margin: 0px; padding: 5px; }

Friday, February 4, 2011

R Script to calculate the Median of a List of Numbers

Here is some code I wrote in the R to calculate the median of a list of numbers. Enjoy.


# read in keyboard input

#list <- scan(file = "", what = double(0), sep = ",")


list <- scan()
list

sortList <- sort(list, decreasing = FALSE)

sortList

n = length(list)

if(n %% 2 == 0) # check if the the list has an even numbered length (%% means modulo)

#medList is the median value of the list of numbers

medList = sortList[n/2 + 1]

if(n %% 2 !=0) # checks if the list has an odd length


medList = sortList[(n + 1) / 2]


medList

Thursday, February 3, 2011

How Many Numbers are Between Zero and One ?

If you said zero, you are correct if you are taking about sets of numbers that only concern whole numbers such as the integers or natural numbers. But what about fractions such as 1/3 or 2/5, or irrational numbers ? If you consider sets that contain fractions or irrational numbers, the question isn't as simple.

Common numerical sets that are used in Mathematics are the integers, natural numbers, rational numbers, irrational numbers, real numbers, and the complex numbers. We will consider the rational numbers, irrational numbers, and the real numbers for the purposes of this post.

The rational numbers are the set of numbers that can be written as a fraction. All numbers that have a terminating decimal or have a repeating pattern following the decimal point can be written as fractions and are considered to be rational numbers. Irrational numbers are any numbers that can't be written in fractional form. Finally, the real numbers is the set of all rational and irrational numbers.

If we ask the question about how many numbers are between 0 and 1 in the real number system, we have a much different answer than zero. In fact there are an uncountable number of numbers between 0 and 1. For a proof see the following link http://www.math.uic.edu/~lewis/las100/uncount.html.

If we ask the same question about rational numbers we get a different answer. There are still an infinite number of rational numbers between 0 and 1, however there is a countable number of rational numbers between 0 and 1. By countable it is meant that you could in theory list all the rational numbers between 0 and 1 which you would not be able to do with the set of real numbers between 0 and 1.

So there are a countably infinite number of rational numbers between 0 and 1, but the number of real numbers between 0 and 1 is uncountably infinite. In general, the set of all rational numbers is countably infinite and the set of all real numbers is uncountably infinite. What can we say about the irrational numbers. A statement that has been proven is that the set of irrational numbers is uncountably infinite. Using this proven statement and a theorem that says the superset of an uncountably infinite set is also uncountably infinite can be used to show that the set of real numbers is uncountably infinite (remember that the real numbers is the set that contains all rational and irrational numbers).

Monday, January 31, 2011

Binomial Coefficients in Pascal's Triangle

In Algebra II, students learn about Pascal's triangle. Below are a few of the top rows of the triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1


One of the uses of this triangle is to expand algebraic expressions called binomials.

For example:

(x+y)^4 = x^4 + 4x^3 * y + 6x^2 * y^2 + 4x * y^3 + y^4


The numbers in front of the x and y terms in the binomial expansion above are the coefficients in the fourth row of Pascal's triangle (assuming you consider the top row to be row zero).

You could also multiply and distribute this expression (x+y)(x+y)(x+y)(x+y), however this method is longer,tedious, and unnecessary if you already know about Pascal's triangle and the binomial theorem.

The coefficients in Pascal's Triangle are known as the binomial coefficients. Binomial coefficients can be written as n C r where n is the number of items in a set and r is the number of items you are "choosing" from the set. Using binomial coefficient notation, we can write the first 8 rows of Pascal's triangle as


0 C 0

1 C 0 1 C 1

2 C 0 2 C 1 2 C 2

3 C 0 3 C 1 3 C 2 3 C 3

4 C 0 4 C 1 4 C 2 4 C 3 4 C 4

5 C 0 5 C 1 5 C 2 5 C 3 5 C 4 5 C 5

6 C 0 6 C 1 6 C 2 6 C 3 6 C 4 6 C 5 6 C 6

7 C 0 7 C 1 7 C 2 7 C 3 7 C 4 7 C 5 7 C 6 7 C 7


If you use the formula n C r = n! / (n-r)! r! with all of the binomial coefficients in the above triangle, your result will be the original triangle at the top of this post.

Expanding algebraic expressions is not the only use for binomial coefficients. Binomial coefficients are also useful and ubiquitous in probability and statistics.