---
title: 'Lecture 1: Introduction and Basics'
author: "Statistical Computing, 36-350"
date: "Monday October 31, 2015"
---

Outline
===

- Course overview and mechanics
- Cool example
- Built-in data types
- Built-in functions and operators
- First data structures: vectors and arrays

Why good statisticians learn to program
===

- **Independence**: otherwise, you rely on someone else giving you exactly the right tool
- **Honesty**: otherwise, you end up distorting your problem to match the tools you have
- **Clarity**: turning your ideas into something a machine can do refines and clarifies your thinking
- **Fun**: these were the best of times (these were the worst of times)

How this class will work
===

- Professor Ryan Tibshirani
- TAs Yo Joong Choe, Bryan Hooi, Sangwon Hyun, Kevin Lin, Taylor Pospisil
- No programming knowledge presumed
- Some statistics knowledge presumed
- Work mostly in R; some other languages introduced
- Class will be cumulative, so keep up with the readings and assignments!

---

- Two lectures per week: concepts, methods, examples
- Weekly lab to try things out and get fast feedback (10%)
- Weekly homework to do longer and more complex things (40%)
- Midterm project (2 weeks) (20%)
- Final group project (1 month) (30%)

Class website: http://www.stat.cmu.edu/~ryantibs/statcomp-F15/.  
This will host class info, schedule, lecture notes, homework 
assignments, etc.

For announcements and discussions, sign up for the Piazza group.
Blackboard will be used to collect submissions, and keep track of grades

Cool example: statistics + darts
===

![A screenshot from the "darts" R package.](http://www.stat.cmu.edu/~ryantibs/statcomp-F15/lectures/darts.png)

<small>Statistical tools to analyze your darts game!</small>

This class in a nutshell: functional programming
===

Two basic types of things, or objects: **data** and **functions**

- **Data**: things like 7, "seven", $7.000$, and
$\left[ \begin{array}{ccc} 7 & 7 & 7 \\ 7 & 7 & 7\end{array}\right]$
- **Functions**: things like `log`, `+` (takes two arguments), 
`<` (two), `%%` (two), and `mean` (one)

> A function is a machine which turns input objects, or **arguments**, 
into an output object, or a **return value** (possibly with side effects),
according to a definite rule

---

Programming is writing functions to transform inputs into outputs

Good programming ensures the transformation is done easily and correctly

Machines are made out of machines; functions are made out of functions, 
like $f(a,b) = a^2 + b^2$

> The route to good programming is to take a big transformation and 
**break it down** into smaller ones, and then break those down, until 
you come to tasks which are easy (using built-in functions)

Before functions, data
====

At base level, all data can represented in binary format, by **bits** 
(i.e., TRUE/FALSE, YES/NO, 1/0).  Some basic data types:

- **Booleans**: Direct binary values: `TRUE` or `FALSE` in R
- **Integers**: whole numbers (positive, negative or zero), represented 
by a fixed-length block of bits
- **Characters**: fixed-length blocks of bits, with special coding;
**strings**: sequences of characters
- **Floating point numbers**: an integer times a positive integer to the
power of an integer, as in $3 \times 10^6$ or $1 \times 3^{-1}$
- **Missing or ill-defined values**: `NA`, `NaN`, etc.

Operators
====

- **Unary**: take just one argument. E.g., `-` for arithmetic negation, 
`!` for Boolean negation
- **Binary**: take two arguments. E.g., `+`, `-`, `*`, and `/` (though this 
is only a partial operator). Also, `%%` (for mod), and `^` (again partial)

Examples:

```{r}
-7
7+5
7-5
```

---

```{r}
7*5
7^5
7/5
7 %% 5
7 %/% 5
```

The R console
====

Basic interaction with R is by typing in the **console**, i.e., 
**terminal** or **command line**

You type in commands, R gives back answers (or errors)

Menus and other graphical interfaces are extras built on top of 
the console

Comparison operators
===

These are also binary operators; they take two objects, 
like numbers, and give back a Boolean. Examples:

```{r}
7 > 5
7 < 5
7 >= 7
```

---

```{r}
7 <= 5
7 == 5
7 != 5
```

<small>(Note: `==` is a comparison operator, `=` is not!)
</small>

Logical operators
===

These basic ones are `&` (and) and `|` (or). Examples:

```{r}
(5 > 7) & (6*7 == 42)
(5 > 7) | (6*7 == 42)
(5 > 7) | (6*7 == 42) & (0 != 0)
(5 > 7) | (6*7 == 42) & (0 != 0) | (9-8 >= 0)
```

<small>(The double forms `&&` and `||` are different! 
We will see them later)</small>

More types
===

The `typeof()` function returns the data type

`is.`*foo*`()` functions return Booleans for whether 
the argument is of type *foo*

`as.`*foo*`()` (tries to) "cast" its argument to 
type *foo*, to translate it sensibly into such a value

---

```{r}
typeof(7)
is.numeric(7)
is.na(7)
is.na(7/0)
is.na(0/0)
```

<small>(Why is `7/0` not `NA`, but `0/0` is?)</small>

---

```{r}
is.character(7)
is.character("7")
is.character("seven")
is.na("seven")
```

---

```{r}
as.character(5/6)
as.numeric(as.character(5/6))
6*as.numeric(as.character(5/6))
5/6 == as.numeric(as.character(5/6))
```

<small>(Why is the last evaluation `FALSE`?)</small>

Data can have names
===

We can give names to data objects; these give us **variables**

A few variables are built in:

```{r}
pi
```

Variables can be arguments to functions or operators, 
just like constants:

```{r}
pi*10
cos(pi)
```

---

Create variables with the **assignment operator**, 
`<-` or `=`, as in:

```{r}
approx.pi = 22/7
approx.pi
diameter = 10
approx.pi * diameter
```

The assignment operator also changes values:

```{r}
circumference = approx.pi * diameter
circumference
circumference = 30
circumference
```

---

The code you write will be made of variables, with descriptive
names: easier to design, easier to debug, easier to improve, and 
easier for others to read

Avoid "magic constants"; use named variables 
(you will be graded on this!)

Named variables are a first step towards **abstraction**

The R workspace
===

What variables have you defined?

```{r}
ls()
```

Getting rid of variables:

```{r}
rm("circumference")
ls()
rm(list=ls()) # Be warned! This erases everything
ls()
```

First data structure: vectors
===

A **data structure** is a grouping of relaed data values into an
object

A **vector** is a sequence of values, all of the same type, as in:

```{r}
x = c(7, 8, 10, 45)
x
is.vector(x)
```

`c()` function returns a vector containing all its arguments in 
specified order

Hence `x[1]` would be the first element, `x[4]` the 4th element, and 
`x[-4]` a vector containing *all but* the fourth element

---

`vector(length=6)` returns an empty vector of length 6; helpful for 
filling things up later

```{r}
weekly.hours = vector(length=5)
weekly.hours
weekly.hours[5] = 8
weekly.hours
```

Vector arithmetic
===

Arithmetic operator apply to vectors in a 
"componentwise" fashion:

```{r}
y = c(-7, -8, -10, -45)
x+y
x*y
```

Recycling
===

**Recycling** repeat elements in shorter vector when combined 
with a longer one. Example:

```{r}
x + c(-7,-8)
x^c(1,0,-1,0.5)
```

Single numbers are vectors of length 1 for purposes of recycling:

```{r}
2*x
```

---

Can also do componentwise comparisons with vectors:

```{r}
x > 9
```

<small>(Note: this returns a Boolean vector)</small>

Logical operators work elementwise:

```{r}
(x > 9) & (x < 20)
```

---

To compare whole vectors, best to use `identical()` or `all.equal()`:

```{r}
x == -y
identical(x,-y)
identical(c(0.5-0.3,0.3-0.1),c(0.3-0.1,0.5-0.3))
all.equal(c(0.5-0.3,0.3-0.1),c(0.3-0.1,0.5-0.3))
```

Functions on vectors
===

Lots of functions can take vectors as arguments:

- `mean()`, `median()`, `sd()`, `var()`, `max()`, `min()`, 
`length()`, and `sum()` return single numbers
- `sort()` returns a new vector
- `hist()` takes a vector of numbers and produces a histogram, 
a highly structured object, with the side effect of making a plot
- `ecdf()` similarly produces a cumulative-density-function object
- `summary()` gives a five-number summary of numerical vectors
- `any()` and `all()` are useful on Boolean vectors

Indexing vectors
===

Vector of indices:

```{r}
x[c(2,4)]
```

Vector of negative indices:

```{r}
x[c(-1,-3)]
```

<small>(Why that, and not `8 10`?)</small>

---

Boolean vector:

```{r}
x[x>9]
y[x>9]
```

`which()` gives the elements of a Boolean vector that are `TRUE`:

```{r}
places = which(x > 9)
places
y[places]
```

Named components
===

You can give names to elements or components of vectors, and
index vectors accordingly

```{r}
names(x) = c("v1","v2","v3","fred")
names(x)
x[c("fred","v1")]
```

<small>(Here R prints the labels, these are not actual 
components of `x`)</small>

---

`names(x)` is just another vector (of characters):

```{r}
names(y) = names(x)
sort(names(x))
which(names(x)=="fred")
```

Summary
===

- We write programs by composing functions to manipulate data
- The basic data types let us represent Booleans, numbers, and characters
- Data structures let us group together related values 
- Vectors let us group values of the same type
- Use variables rather a profusion of magic constants
- Name components of structures to make data more meaningful

Peculiarites of floating-point numbers
===

The more bits in the fraction part, the more precision

The R floating-point data type is a **double** (what R calls **numeric**).
Back when memory was expensive, the now-standard number of bits was twice 
the default

Finite precision $\Rightarrow$ arithmetic on doubles 
$\not=$ arithmetic on $\mathbb{R}$.

```{r}
0.45 == 3*0.15
0.45 - 3*0.15
```

---

Often ignorable, but not always

- Rounding errors tend to accumulate in long calculations
- When results should be $\approx 0$, errors can flip signs
- Usually better to use `all.equal()` than exact comparison

```{r}
(0.5 - 0.3) == (0.3 - 0.1)
all.equal(0.5-0.3, 0.3-0.1)
```

Peculiarities of integers
===

Typing a whole number in the terminal doesn't make it an integer; 
it makes it a double, whose fractional part is 0

```{r}
is.integer(7)
as.integer(7)
```

To test for being an integer in the mathematical sense, use `round()`:

```{r}
round(7) == 7
```
