Section 2

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

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

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: [email protected] Submit homework to: [email protected] • All section material will be posted under 'Sections → Kevin H's Sections' on the course website: http://isites.harvard.edu/icb/icb.do?keyword=k85336&pageid=icb.page492639 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

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

View Full Document
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 Density -0.5 0.0 0.5 1.0 0.0 0.5 1.0 1.5 Histogram of x3 x3 Density -1.0 -0.5 0.0 0.5 1.0 0.0 0.5 1.0 1.5 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")
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern