Tuesday, December 4, 2007

Math Resources at SJSU, Part 1

I would think that one of the benefits of being a faculty at SJSU would be that one can talk to other colleagues. I find a lot of people using math in general, or statistics in particular, and they do not talk to each other.

Various things occur to me, but the most innocuous thing I can do is to collect some information. So, here is a start.

Bud Gerstman
    Epidemiology Kept Simple
    Supplementary (Unofficial) Web Site for Basic Biostatistics Statistics for Public Health Practice
    StatPrimer 6.4
    Data Analysis With EpiInfo and EpiData Analysis

bus2 090 - Business Statistics (7 sections * 45 students)
hs 167 - Biostatistics (5 sections, 1 * 50 and 4 * 24 students)
ise 130 - engineering statistics (1 section, 75 students)
ise 131 - statistical process control (1 section, 60 students)

Thursday, September 13, 2007

I have been playing with R for one of the classes I am taking this semester. I thought it would be interesting to model a covariant coin in R. It is certainly not trivial. It should not be hard, but R is a quirky environment and a lot of things seem way to complicated in it.

For example, right now, I am trying to figure out how to get the number of items in a vector. Heaven forbid there should be a function like count(). Would that just be too easy? Well, I'll figure it out and it will probably seem about as obvious as using "scalar(@list)" in perl.

Anyway.

So, here are some random things I have found. I do not know the best way to find them, but I found them somehow. So, here they are.

* How does one flip a fair coin?

* How can one flip a uniformly unfair coin?

* How can one flip two coins and have a covariance occur?


How does one flip a fair coin?



# I tried variations of rnorm, such as:
> rnorm(50)
[1] -0.33805550 -0.86197178 -1.12771806 0.72742962 0.03927931 1.38426924 0.92175284 0.79204301 -0.69652307 1.03105752
[11] 1.69153791 -1.74026417 -0.01350978 0.21599462 -0.08565426 -0.26373930 -1.10840322 0.59548152 2.24852825 -0.21409398
[21] -0.28769993 -0.83067256 -1.11048383 -1.46584158 -1.67773248 -1.22302638 -0.23192819 -0.58792094 0.58878786 0.32494373
[31] 0.61243277 -0.64834829 -0.85173197 -0.04764028 0.77408856 0.07826967 -1.18135820 0.15234143 0.50881794 0.47849280
[41] -0.71861064 -0.28603871 1.58796660 -1.47128448 0.79252309 -0.23991624 0.66082809 -2.06752866 0.26841476 -0.36200074

# Maybe I can test each of these values? Use some complicated code to test whether it is greater than or less than 0? It turns
# out to be simpler than that.
#
> round(runif(1))
[1] 1
> round(runif(1))
[1] 1
> round(runif(1))
[1] 0
> round(runif(1))
[1] 0

# This is flipping a coin each time.
#
# runif generates a random number or a uniform distribution between two numbers. The first parameter is the number of numbers
# The second is the min, defaults to 0, and the third is the max, defaults to 1.
#
# The round() functions rounds, as you would expect.
#
# This is flipping a coin 1000 times.
#
sum(round(runif(1000)))
[1] 491
sum(round(runif(1000)))
[1] 526

# There is another way to flip a coin also.
#
> sample(c(0,1), 50, replace=TRUE)
[1] 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 0 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 1 1

# This takes 50 samples from the vector = (0,1). This works especially well for dice with different numbers of sides.
#
# First, 50 rolls of a 6-sided die, and then 50 rolls of an 8-sided die.
#
> sample(c(1:6), 50, replace=TRUE)
[1] 4 6 1 6 1 4 1 2 5 6 4 1 4 4 5 4 4 2 4 1 2 6 3 2 5 3 4 2 4 4 2 6 4 6 5 5 5 1 3 4 5 5 3 5 1 2 3 5 2 6
> sample(c(1:8), 50, replace=TRUE)
[1] 7 1 8 8 8 8 4 4 8 8 1 3 6 1 5 7 2 7 5 8 7 2 5 2 3 7 4 2 7 6 8 8 7 1 8 4 6 3 2 1 3 5 7 2 3 6 5 6 2 8


With thanks to: http://homepage.psy.utexas.edu/Homepage/Class/Psy394U/Cormack/DYIStats/readings/ChXcounting4.pdf



How can one flip a uniformly unfair coin?



# One can give a vector of probability to the sample. One gives a probability to each choice for the sample. The probabilities
# do not need to add up to 1, but it would be good if they did. They at least need to be non-negative.
#
# Throw an unfair coin weighted for heads, which is 0.
#
> sample(c(0,1), 50, replace=TRUE, prob=c(0.75, 0.25))
[1] 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0

# Throw an unfair die, weighted on 6. If 6 is most likely, 1 is least likely, with the others in between.
#
> myprobs = c(1/6-13/100, 1/6-3/100, 1/6-3/100, 1/6-3/100, 1/6-3/100, 1/6+25/100)
>
> sample(c(1:6), 50, replace=TRUE, prob=myprobs)
[1] 2 3 6 1 6 5 2 6 6 6 3 6 6 4 6 6 5 5 2 3 6 3 1 4 6 6 6 6 4 2 3 6 6 2 6 6 6 3 6 2 5 6 6 3 3 2 4 4 6 6
> sample(c(1:6), 50, replace=TRUE, prob=myprobs)
[1] 6 5 3 4 4 2 4 6 5 6 6 6 6 5 6 5 6 3 1 5 6 6 4 6 6 6 6 6 2 3 6 4 2 5 1 3 3 6 6 6 6 6 6 4 6 5 4 6 6 2
> sample(c(1:6), 50, replace=TRUE, prob=myprobs)
[1] 1 6 2 4 3 6 2 5 6 6 6 4 6 6 4 6 6 6 2 2 2 3 4 2 5 1 6 6 6 3 6 6 4 6 6 5 5 6 4 6 6 6 4 1 6 3 2 6 4 6



Again, a thank you to http://homepage.psy.utexas.edu/Homepage/Class/Psy394U/Cormack/DYIStats/readings/ChXcounting4.pdf



How can one flip two coins and have a covariance occur?



This can happen if one coin "admires" the other and follows it. Imagine you are throwing a quarter and a nickel. The quarter is true. But about 50% of the
time, the nickel is able to follow the quarter. The other 50% of the time, the coin is true. There should be a covariance. How can one do this in R?


# Flip a quarter 100 times.
#
quarters = round(runif(100))
#
# Flip a coin to see if the nickel "wins". If it wins, it follows the quarter. If it loses, it flips and takes that value.
#
> nickels = numeric()
> for (tails in quarters) {
+ if (round(runif(1))) {
+ nickels = c( nickels, round(runif(1)) )
+ } else {
+ nickels = c( nickels, tails )
+ }
+ }
> cov(quarters, nickels)
[1] 0.1278288