Hi David,

Here is my hint:

1> set.seed(2009)
1>  r = rnorm(100, mean = .05/253, sd = .23/sqrt(253))
1>  logPrice = log(1e6) + cumsum(r)  ## 1e6 = initial price
1> logPrice
  [1] 13.80340 13.80755 13.80516 13.80164 13.82105 13.79453 13.79750
13.80408 13.78316 13.77246 13.77529
 [12] 13.79100 13.78908 13.79427 13.79268 13.76960 13.78917 13.79897
13.80548 13.80673 13.83131 13.82956
 [23] 13.82829 13.83970 13.84860 13.88746 13.90191 13.89439 13.88666
13.89621 13.92104 13.91894 13.92787
 [34] 13.91217 13.90804 13.91452 13.92865 13.95498 13.98419 13.98820
13.99140 14.00165 13.97519 13.96530
 [45] 13.95029 13.95800 13.94906 13.94807 13.91299 13.90617 13.90202
13.90779 13.89517 13.88682 13.89177
 [56] 13.88537 13.89815 13.87475 13.86916 13.88294 13.87048 13.86705
13.87571 13.86564 13.85744 13.87504
 [67] 13.88990 13.88075 13.87261 13.86023 13.85191 13.85510 13.84572
13.84649 13.85447 13.83746 13.82143
 [78] 13.80111 13.78505 13.79430 13.81151 13.81456 13.80200 13.79527
13.80288 13.76598 13.79024 13.81018
 [89] 13.80240 13.77962 13.78402 13.78493 13.80514 13.79839 13.77328
13.78443 13.78491 13.77488 13.75898
[100] 13.74589

That's probably not what you intended ;-)

Garrett

On Fri, Dec 23, 2011 at 2:43 PM, Afshartous, David <
d.afshartous@vanderbilt.edu> wrote:

>
> All,
> Below is an R-lab problem from Ch2 of Statistics and Data Analysis for
> Financial Engineering (Ruppert, 2010).   The R code runs okay but the
> answer seems strange.   Any hints much appreciated.
> Cheers,
> David
>
> David Afshartous, Ph.D.
> Research Associate Professor
> PSTATŽ: ASA Accredited Professional Statistician
> Department of Biostatistics
> Vanderbilt University Medical Center
>
>
> ## suppose Hedge fund owns $1,000,000 of stock and used $50,000 capital and
> ## $950,000 borrowed money for the purchase.
> ## Daily log returns on the stock have a mean of 0.05/year and SD of
> 0.23/year,
> ## which can be converted to per trading day by dividing by 253 and
> sqrt(253)
>
> ## the hedge fund will sell the stock for a profit of at least $100,000 if
> the value
> ## of the stock rises to at least $1,100,000 at the end of one of the
> first 100 trading days,
> ## sell it for a loss if the value falls below $950,000 at the end of one
> of the first
> ## 100 trading days, or sell after 100 trading days if the closing price
> has stayed between
> ## $950,000 and $1,1000,000.
>
> ## PROBLEM 7: What is the expected return? When answering this question,
> remember
> ## that only $50,000 was invested.  Also, the units of return are time,
> e.g.,
> ## one can express a return as a daily return or a weekly return.
>  Therefore,
> ## one must keep track of how long the hedge fund holds its position
> before selling
>
> The code below simulates whether value falls below 950,000, goes above
> 1,1000,000, or stays between. The amount lost/gained in the 1st/2nd
> scenario is a constant, whereas the amount lost/gained in the third
> scenario is random.
>
> ### simulation code; written for readability as opposed to speed
> niter = 10000     ## number of iterations
> below = rep(0, niter)   ## set up storage
> above = rep(0, niter)
> between = rep(0, niter)
> between.ending.value = NULL
> below.ending.day = NULL
> above.ending.day = NULL
> set.seed(2009)
> for (i in 1:niter) {
>  r = rnorm(100, mean = .05/253, sd = .23/sqrt(253))
>  logPrice = log(1e6) + cumsum(r)  ## 1e6 = initial price
>  minLogP = min(logPrice)  ## min price over next 45 days
>  maxLogP = max(logPrice)  ## min price over next 45 days
>  crossBelowLogP.day = ifelse( minLogP < log(950000),  min(which(logPrice <
> log(950000))), 101) ## 101 means doesn't cross
>  crossAboveLogP.day = ifelse( maxLogP > log(1100000),  min(which(logPrice
> > log(1100000))), 101)
>  if ( (minLogP >= log(950000)) & (maxLogP <= log(1100000)) ) {   ##
> Neither cross boundary
>     between[i] = 1
>     between.ending.value = append(between.ending.value, exp(logPrice[100]))
>     }
>  if  ( (minLogP < log(950000)) & (maxLogP > log(1100000))) { ## Both cross
>             ifelse (crossBelowLogP.day < crossAboveLogP.day, below[i] <-
> 1, above[i] <-1)
>             ifelse (crossBelowLogP.day < crossAboveLogP.day,
> below.ending.day <- append(below.ending.day, crossBelowLogP.day),
>                                                        above.ending.day <-
> append(above.ending.day, crossAboveLogP.day))
>           }
>  if ( (minLogP < log(950000))  & (maxLogP <= log(1100000))  ) {    ## Only
> cross below
>             below[i] = 1
>             below.ending.day = append(below.ending.day, crossBelowLogP.day)
>           }
>  if ( (minLogP >= log(950000))  & (maxLogP > log(1100000))  ) {    ## Only
> cross above
>             above[i] = 1
>             above.ending.day = append(above.ending.day, crossAboveLogP.day)
>           }
> }
> mean(above)  ## this will give the probability of the value crossing the
> above threshold
>
> When annualizing returns, the day in which a boundary needs to be
> accounted for.    The following code calculates the overall expected return
> by conditioning on each scenario:
>
> # cross above: always make constant 100,000 based on initial investment of
> 50,000
> # P_t/P_t-1 = 100,000/50,000 = 2 = gross return --> net return = 2 - 1 = 1
> ret.above = (1 + 1)^(365/(above.ending.day))  - 1    ### annualized
>
> # cross below: always lose constant 50,000 based on initial investment of
> 50,000
> # P_t/P_t-1 = 50,000/50,000 = 1 = gross return --> net return = 1 - 1 = 0
> ret.below = 0
>
> ## betwen: final value depends on realized value on 100th day
> # P_t/P_t-1 = between.ending.value/50,000 = --> net return = gross return
>  - 1
>
> net = ifelse(between.ending.value < 1000000,
>                    (1000000 - between.ending.value)/50000 - 1,  ## net
> return, gain
>                    (between.ending.value  - 1000000)/50000 - 1)   ## net
> return, loss
>
> ret.between = (1 + net)^(365/100) - 1
>
> exp.return = mean(above)*mean(ret.above)   + mean(below)*mean(ret.below)
> + mean(between) * mean(ret.between)
> 8.669104e+50???
>
> _______________________________________________
> R-SIG-Finance@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

	[[alternative HTML version deleted]]

