Section 2 - Statistics 107, Section #2 TF: Kevin Liu Huang...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Statistics 107, Section #2 TF: Kevin Liu Huang February 8, 2012 Section: Thursdays, 1-2pm in SC-110 Office Hours: Fridays, 11am-12pm in SC-601 E-mail: klhuang@college.harvard.edu Submit homework to: stat107kevinliuhuang@gmail.com • All section material will be posted under 'Sections → Kevin H's Sections' on the course website: Topics for Section - Introduction - For loops - Functions - Random sampling from a distribution - Yearly returns monte carlo sampling - Portfolio optimization Practice Problem: 1) for Loop Example: What is the sum of all of the even numbers from 1 to 100 (inclusive)? Let’s get R to calculate this for us: > total=0 > for(i in 1:100){ + if(i%%2 == 0){ + total=total+i + } + } > total [1] _______ 2) Creating a function in R. Here’s an example of a function to solve the same type of problem as #1 above: > sum.evens = function(end, start=1){ + total=0 + for(i in start:end){ + if(i%%2 == 0){ + total=total+i + } + } + return(total) + } > sum.evens(20) [1] 110 > sum.evens(20,start=5) [1] 104
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Histogram of x1 x1 Density -0.4 -0.2 0.0 0.2 0.4 0.6 0.0 0.5 1.0 1.5 2.0 Histogram of x2 x2 -0.5 0.0 0.5 1.0 Histogram of x3 x3 -1.0 -0.5 0.0 0.5 1.0 3) Sampling from Specified Distribution Let’s do a simulation similar to what Prof Parzen did in lecture on Tueday. Let’s imagine we built a portfolio comprised of 50% Coca-Cola (KO) and 50% McDonalds (MCD) (two of the largest companies in the market). Let’s start by downloading their stock prices for the last ~25 years (we will be assuming the last 25 years is a good reflection of future returns on the companies). In fact, the start date will be Jan 28, 1986 (what happened on that date?) > library(quantmod) >library(logspline) > getSymbols("KO", from = "1986-01-28", to = "2012-02-01") > getSymbols("MCD", from = "1986-01-28", to = "2012-02-01")
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Section 2 - Statistics 107, Section #2 TF: Kevin Liu Huang...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online