[R] Ifelse leading to inconsistent result

2013-06-26 Thread Neville O'Reilly
I have used ifelse in count variables to count the number of times in a 
simulation the values of a vector of logprice fall within mutually exclusive 
ranges. However, there is a double count in the result i.e. i am getting output 
indicating values falling in mutually exclusive ranges. Here is the code and 
result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run again
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] - ifelse (minlogP  log(95), 1, ifelse (maxlogP  log 
(100), 0, 1))
  CountProf[i] - ifelse (maxlogP  log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold for a loss 
in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold for a 
profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
 mean(CountLoss) # fraction of times out of niter that stock is sold for a 
 loss in a 100 day period
[1] 0.64246
 sum(CountProf)
[1] 51857
 mean(CountProf) # fraction of times out of niter that stock is sold for a 
 profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of interations. When 
I troubleshoot by reducing the number of iterations and that size of the 
logprice, I can't reproduce the contradicion.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Ifelse leading to inconsistent result

2013-06-26 Thread Duncan Murdoch

On 26/06/2013 11:40 AM, Neville O'Reilly wrote:

I have used ifelse in count variables to count the number of times in a 
simulation the values of a vector of logprice fall within mutually exclusive 
ranges. However, there is a double count in the result i.e. i am getting output 
indicating values falling in mutually exclusive ranges. Here is the code and 
result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run again
for (i in 1:niter)
{
   r = rnorm(100,mean=.05/253,
 sd=.23/sqrt(253)) # generate 100 random normal numbers
   logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
   maxlogP = max(logPrice) # max price over next 100 days
   minlogP = min(logPrice)
   CountLoss[i] - ifelse (minlogP  log(95), 1, ifelse (maxlogP  log 
(100), 0, 1))
   CountProf[i] - ifelse (maxlogP  log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold for a loss 
in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold for a 
profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
 mean(CountLoss) # fraction of times out of niter that stock is sold for a 
loss in a 100 day period
[1] 0.64246
 sum(CountProf)
[1] 51857
 mean(CountProf) # fraction of times out of niter that stock is sold for a 
profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of interations. When 
I troubleshoot by reducing the number of iterations and that size of the 
logprice, I can't reproduce the contradicion.


I don't see a contradiction.  Both CountLoss and CountProf are less than 
niter.  The logic of your test doesn't imply that sum(CountLoss) + 
sum(CountProf) should be less than niter; e.g. a case where minlogP is 
less than log(95) and maxlogP  log(110) would be counted in both.


Duncan Murdoch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Ifelse leading to inconsistent result

2013-06-26 Thread peter dalgaard

On Jun 26, 2013, at 17:40 , Neville O'Reilly wrote:

 I have used ifelse in count variables to count the number of times in a 
 simulation the values of a vector of logprice fall within mutually exclusive 
 ranges. However, there is a double count in the result i.e. i am getting 
 output indicating values falling in mutually exclusive ranges. Here is the 
 code and result
 R script

Don't use ifelse, it just confuses the logic.

As far as I can tell, the code is equivalent (except for integer conversion) to

 CountLoss[i] - (minlogP  log(95)) | (maxlogP = log (100))
 CountProf[i] - (maxlogP = log (110))

and that doesn't look mutually exclusive to me. 

 niter = 1e5 # number of iterations is 10^5
 CountLoss = rep(0,niter)
 CountProf = rep (0,niter)
 set.seed(2009) # enables reproducibility of result if script run again
 for (i in 1:niter)
 {
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] - ifelse (minlogP  log(95), 1, ifelse (maxlogP  log 
 (100), 0, 1))
  CountProf[i] - ifelse (maxlogP  log (110),0,1)
 }
 sum(CountLoss)
 mean(CountLoss) # fraction of times out of niter that stock is sold for a 
 loss in a 100 day period
 sum(CountProf)
 mean(CountProf) # fraction of times out of niter that stock is sold for a 
 profit in a 100 day period
 
 Output
 sum(CountLoss)
 [1] 64246
 mean(CountLoss) # fraction of times out of niter that stock is sold for a 
 loss in a 100 day period
 [1] 0.64246
 sum(CountProf)
 [1] 51857
 mean(CountProf) # fraction of times out of niter that stock is sold for a 
 profit in a 100 day period
 [1] 0.51857
 
 CountLoss and CountProf should sum to less than the number of interations. 
 When I troubleshoot by reducing the number of iterations and that size of the 
 logprice, I can't reproduce the contradicion.
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Ifelse leading to inconsistent result

2013-06-26 Thread David Carlson
I don't see that you have set up mutually exclusive ranges. If we
modify your code so save the maxlogP and minlogP values:

niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
maxlogP - rep(0, ninter)
minlogP - rep(0, ninter)
set.seed(2009) # enables reproducibility of result if script run
again
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP[i] = max(logPrice) # max price over next 100 days
  minlogP[i] = min(logPrice)
  CountLoss[i] - ifelse (minlogP[i]  log(95), 1, ifelse
(maxlogP[i]  log (100), 0, 1))
  CountProf[i] - ifelse (maxlogP[i]  log (110),0,1)
}
both - which(CountLoss+CountProf1)
length(both)
# 18484
head(both)
# [1]  1  5  9 12 15 25
ifelse (minlogP[1]  log(95), 1, ifelse (maxlogP[1]  log
(100), 0, 1))
# [1] 1
ifelse (maxlogP[1]  log(110),0,1)
# [1] 1
exp(maxlogP[1])
# [1] 1204589
exp(minlogP[1])
# [1] 932747.5

Your first simulation meets both criteria along 18483 others!

-
David L Carlson
Associate Professor of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Neville O'Reilly
Sent: Wednesday, June 26, 2013 10:41 AM
To: r-help@r-project.org
Subject: [R] Ifelse leading to inconsistent result

I have used ifelse in count variables to count the number of times
in a simulation the values of a vector of logprice fall within
mutually exclusive ranges. However, there is a double count in the
result i.e. i am getting output indicating values falling in
mutually exclusive ranges. Here is the code and result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run
again
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] - ifelse (minlogP  log(95), 1, ifelse (maxlogP
 log (100), 0, 1))
  CountProf[i] - ifelse (maxlogP  log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold
for a loss in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold
for a profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
 mean(CountLoss) # fraction of times out of niter that stock is
sold for a loss in a 100 day period
[1] 0.64246
 sum(CountProf)
[1] 51857
 mean(CountProf) # fraction of times out of niter that stock is
sold for a profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of
interations. When I troubleshoot by reducing the number of
iterations and that size of the logprice, I can't reproduce the
contradicion.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Ifelse leading to inconsistent result

2013-06-26 Thread Neville O'Reilly
You are right, I inadvertently deleted part of the script - so I ended up with 
non-mutually exclusive regions. My apologies


Neville O'Reilly, Ph.D 
Associate Director, Financial Statistics  Risk Management, 
Rm 479 Hill Center, Rutgers University, 110 Freylinghuysen Rd., 
Piscataway, NJ 08854. 
848-445-7669 
http://fsrm.rutgers.edu/ 

- Original Message -
From: David Carlson dcarl...@tamu.edu
To: Neville O'Reilly norei...@stat.rutgers.edu, r-help@r-project.org
Sent: Wednesday, June 26, 2013 1:15:06 PM
Subject: RE: [R] Ifelse leading to inconsistent result

I don't see that you have set up mutually exclusive ranges. If we
modify your code so save the maxlogP and minlogP values:

niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
maxlogP - rep(0, ninter)
minlogP - rep(0, ninter)
set.seed(2009) # enables reproducibility of result if script run
again
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP[i] = max(logPrice) # max price over next 100 days
  minlogP[i] = min(logPrice)
  CountLoss[i] - ifelse (minlogP[i]  log(95), 1, ifelse
(maxlogP[i]  log (100), 0, 1))
  CountProf[i] - ifelse (maxlogP[i]  log (110),0,1)
}
both - which(CountLoss+CountProf1)
length(both)
# 18484
head(both)
# [1]  1  5  9 12 15 25
ifelse (minlogP[1]  log(95), 1, ifelse (maxlogP[1]  log
(100), 0, 1))
# [1] 1
ifelse (maxlogP[1]  log(110),0,1)
# [1] 1
exp(maxlogP[1])
# [1] 1204589
exp(minlogP[1])
# [1] 932747.5

Your first simulation meets both criteria along 18483 others!

-
David L Carlson
Associate Professor of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Neville O'Reilly
Sent: Wednesday, June 26, 2013 10:41 AM
To: r-help@r-project.org
Subject: [R] Ifelse leading to inconsistent result

I have used ifelse in count variables to count the number of times
in a simulation the values of a vector of logprice fall within
mutually exclusive ranges. However, there is a double count in the
result i.e. i am getting output indicating values falling in
mutually exclusive ranges. Here is the code and result
R script
niter = 1e5 # number of iterations is 10^5
CountLoss = rep(0,niter)
CountProf = rep (0,niter)
set.seed(2009) # enables reproducibility of result if script run
again
for (i in 1:niter)
{
  r = rnorm(100,mean=.05/253,
sd=.23/sqrt(253)) # generate 100 random normal numbers
  logPrice = log(1e6) + cumsum(r) #vector of 100 days log prices
  maxlogP = max(logPrice) # max price over next 100 days
  minlogP = min(logPrice)
  CountLoss[i] - ifelse (minlogP  log(95), 1, ifelse (maxlogP
 log (100), 0, 1))
  CountProf[i] - ifelse (maxlogP  log (110),0,1)
}
sum(CountLoss)
mean(CountLoss) # fraction of times out of niter that stock is sold
for a loss in a 100 day period
sum(CountProf)
mean(CountProf) # fraction of times out of niter that stock is sold
for a profit in a 100 day period

Output
sum(CountLoss)
[1] 64246
 mean(CountLoss) # fraction of times out of niter that stock is
sold for a loss in a 100 day period
[1] 0.64246
 sum(CountProf)
[1] 51857
 mean(CountProf) # fraction of times out of niter that stock is
sold for a profit in a 100 day period
[1] 0.51857

CountLoss and CountProf should sum to less than the number of
interations. When I troubleshoot by reducing the number of
iterations and that size of the logprice, I can't reproduce the
contradicion.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.