Need some help

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Need some help

azzza
Hi!
I'm taking a course that requires some programming background, but I'm a complete novice in the field.

when asked to generate a list of 20 uniform random numbers, is it alright if I put in >randu, and just copy-paste the first 20 numbers?? Or is there, as I suspect, a better way of calling out exactly 20 uniform random numbers??

I'm also unable to solve the following problem:
We know that on average 30% of the customers who enter a store make a purchase. Suppose 200
people enter the store today. Run a simulation to see how many purchases we will have today.


Any help is greatly appreciated. i went through the Rmanual, but felt that it did not lend me the information i needed to solve the above queries.
Thanks again
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

Daniel Nordlund
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of azzza
> Sent: Sunday, October 14, 2007 10:21 PM
> To: [hidden email]
> Subject: [R] Need some help
>
>
> Hi!
> I'm taking a course that requires some programming background, but I'm a
> complete novice in the field.
>
> when asked to generate a list of 20 uniform random numbers, is it alright if
> I put in >randu, and just copy-paste the first 20 numbers?? Or is there, as
> I suspect, a better way of calling out exactly 20 uniform random numbers??
>
See ?runif

rand_nums <- runif(20)

> I'm also unable to solve the following problem:
> We know that on average 30% of the customers who enter a store make a
> purchase. Suppose 200
> people enter the store today. Run a simulation to see how many purchases we
> will have today.
>
see ?sample
> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30), replace=TRUE))

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza

Quite helpful indeed. Greatly appreciated.
Another problem I had was trying to simulate an example from my book. Simulating 1000 coin tosses, and finding the frequency of sign changes. So how will we plot this using R? (frequency of sign changes in Y axis)



Daniel Nordlund wrote
> -----Original Message-----
> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org] On Behalf
> Of azzza
> Sent: Sunday, October 14, 2007 10:21 PM
> To: r-help@r-project.org
> Subject: [R] Need some help
>
>
> Hi!
> I'm taking a course that requires some programming background, but I'm a
> complete novice in the field.
>
> when asked to generate a list of 20 uniform random numbers, is it alright if
> I put in >randu, and just copy-paste the first 20 numbers?? Or is there, as
> I suspect, a better way of calling out exactly 20 uniform random numbers??
>
See ?runif

rand_nums <- runif(20)

> I'm also unable to solve the following problem:
> We know that on average 30% of the customers who enter a store make a
> purchase. Suppose 200
> people enter the store today. Run a simulation to see how many purchases we
> will have today.
>
see ?sample
> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30), replace=TRUE))

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

jholtman
You might want to check out 'rle'.  This will give you the 'lengths'
of runs of the same value and therefore when the value changes (sign
change?) you can see how often:

> x <- sample(c(-1,1), 1000, TRUE)
> rle(x)
Run Length Encoding
  lengths: int [1:483] 2 2 1 4 3 1 1 1 1 2 ...
  values : num [1:483] -1 1 -1 1 -1 1 -1 1 -1 1 ...

Here was a sample of 1000, and there were 483 changes between the
samples.  Is this what you are looking for?

On 10/15/07, azzza <[hidden email]> wrote:

>
>
> Quite helpful indeed. Greatly appreciated.
> Another problem I had was trying to simulate an example from my book.
> Simulating 1000 coin tosses, and finding the frequency of sign changes. So
> how will we plot this using R? (frequency of sign changes in Y axis)
>
>
>
>
> Daniel Nordlund wrote:
> >
> >> -----Original Message-----
> >> From: [hidden email] [mailto:[hidden email]]
> >> On Behalf
> >> Of azzza
> >> Sent: Sunday, October 14, 2007 10:21 PM
> >> To: [hidden email]
> >> Subject: [R] Need some help
> >>
> >>
> >> Hi!
> >> I'm taking a course that requires some programming background, but I'm a
> >> complete novice in the field.
> >>
> >> when asked to generate a list of 20 uniform random numbers, is it alright
> >> if
> >> I put in >randu, and just copy-paste the first 20 numbers?? Or is there,
> >> as
> >> I suspect, a better way of calling out exactly 20 uniform random
> >> numbers??
> >>
> > See ?runif
> >
> > rand_nums <- runif(20)
> >
> >> I'm also unable to solve the following problem:
> >> We know that on average 30% of the customers who enter a store make a
> >> purchase. Suppose 200
> >> people enter the store today. Run a simulation to see how many purchases
> >> we
> >> will have today.
> >>
> > see ?sample
> >> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30),
> >> replace=TRUE))
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel Nordlund
> > Bothell, WA USA
> >
> > ______________________________________________
> > [hidden email] 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Need-some-help-tf4624513.html#a13214128
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza
Umm, yes, what you had makes a lot of sense. How would I represent that in a plot of the number of sign changes in the to  Y axis, and the toss number (from 0 to 1000) in the x-axis?



jholtman wrote
You might want to check out 'rle'.  This will give you the 'lengths'
of runs of the same value and therefore when the value changes (sign
change?) you can see how often:

> x <- sample(c(-1,1), 1000, TRUE)
> rle(x)
Run Length Encoding
  lengths: int [1:483] 2 2 1 4 3 1 1 1 1 2 ...
  values : num [1:483] -1 1 -1 1 -1 1 -1 1 -1 1 ...

Here was a sample of 1000, and there were 483 changes between the
samples.  Is this what you are looking for?

On 10/15/07, azzza <azza.khogaliali@utoronto.ca> wrote:
>
>
> Quite helpful indeed. Greatly appreciated.
> Another problem I had was trying to simulate an example from my book.
> Simulating 1000 coin tosses, and finding the frequency of sign changes. So
> how will we plot this using R? (frequency of sign changes in Y axis)
>
>
>
>
> Daniel Nordlund wrote:
> >
> >> -----Original Message-----
> >> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org]
> >> On Behalf
> >> Of azzza
> >> Sent: Sunday, October 14, 2007 10:21 PM
> >> To: r-help@r-project.org
> >> Subject: [R] Need some help
> >>
> >>
> >> Hi!
> >> I'm taking a course that requires some programming background, but I'm a
> >> complete novice in the field.
> >>
> >> when asked to generate a list of 20 uniform random numbers, is it alright
> >> if
> >> I put in >randu, and just copy-paste the first 20 numbers?? Or is there,
> >> as
> >> I suspect, a better way of calling out exactly 20 uniform random
> >> numbers??
> >>
> > See ?runif
> >
> > rand_nums <- runif(20)
> >
> >> I'm also unable to solve the following problem:
> >> We know that on average 30% of the customers who enter a store make a
> >> purchase. Suppose 200
> >> people enter the store today. Run a simulation to see how many purchases
> >> we
> >> will have today.
> >>
> > see ?sample
> >> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30),
> >> replace=TRUE))
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel Nordlund
> > Bothell, WA USA
> >
> > ______________________________________________
> > 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Need-some-help-tf4624513.html#a13214128
> Sent from the R help mailing list archive at Nabble.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.
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

jholtman
I really depends on what you want to plot.  You can plot the
cumulative count of the sign changes with:

x <- sample(c(0,1), 1000, TRUE)
plot(cumsum(x), type='l')

There are other things you can do.  You can get a rough histogram of
the length of the run by:

> stem(rle(x)$length)

  The decimal point is at the |

  1 | 00000000000000000000000000000000000000000000000000000000000000000000+201
  1 |
  2 | 00000000000000000000000000000000000000000000000000000000000000000000+60
  2 |
  3 | 00000000000000000000000000000000000000000000000000000
  3 |
  4 | 00000000000000000000000000000000000000
  4 |
  5 | 000000000
  5 |
  6 | 00000000
  6 |
  7 | 00000

With R, almost anything is possible.

On 10/15/07, azzza <[hidden email]> wrote:

>
> Umm, yes, what you had makes a lot of sense. How would I represent that in a
> plot of the number of sign changes in the to  Y axis, and the toss number
> (from 0 to 1000) in the x-axis?
>
>
>
>
> jholtman wrote:
> >
> > You might want to check out 'rle'.  This will give you the 'lengths'
> > of runs of the same value and therefore when the value changes (sign
> > change?) you can see how often:
> >
> >> x <- sample(c(-1,1), 1000, TRUE)
> >> rle(x)
> > Run Length Encoding
> >   lengths: int [1:483] 2 2 1 4 3 1 1 1 1 2 ...
> >   values : num [1:483] -1 1 -1 1 -1 1 -1 1 -1 1 ...
> >
> > Here was a sample of 1000, and there were 483 changes between the
> > samples.  Is this what you are looking for?
> >
> > On 10/15/07, azzza <[hidden email]> wrote:
> >>
> >>
> >> Quite helpful indeed. Greatly appreciated.
> >> Another problem I had was trying to simulate an example from my book.
> >> Simulating 1000 coin tosses, and finding the frequency of sign changes.
> >> So
> >> how will we plot this using R? (frequency of sign changes in Y axis)
> >>
> >>
> >>
> >>
> >> Daniel Nordlund wrote:
> >> >
> >> >> -----Original Message-----
> >> >> From: [hidden email]
> >> [mailto:[hidden email]]
> >> >> On Behalf
> >> >> Of azzza
> >> >> Sent: Sunday, October 14, 2007 10:21 PM
> >> >> To: [hidden email]
> >> >> Subject: [R] Need some help
> >> >>
> >> >>
> >> >> Hi!
> >> >> I'm taking a course that requires some programming background, but I'm
> >> a
> >> >> complete novice in the field.
> >> >>
> >> >> when asked to generate a list of 20 uniform random numbers, is it
> >> alright
> >> >> if
> >> >> I put in >randu, and just copy-paste the first 20 numbers?? Or is
> >> there,
> >> >> as
> >> >> I suspect, a better way of calling out exactly 20 uniform random
> >> >> numbers??
> >> >>
> >> > See ?runif
> >> >
> >> > rand_nums <- runif(20)
> >> >
> >> >> I'm also unable to solve the following problem:
> >> >> We know that on average 30% of the customers who enter a store make a
> >> >> purchase. Suppose 200
> >> >> people enter the store today. Run a simulation to see how many
> >> purchases
> >> >> we
> >> >> will have today.
> >> >>
> >> > see ?sample
> >> >> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30),
> >> >> replace=TRUE))
> >> >
> >> > Hope this is helpful,
> >> >
> >> > Dan
> >> >
> >> > Daniel Nordlund
> >> > Bothell, WA USA
> >> >
> >> > ______________________________________________
> >> > [hidden email] 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.
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/Need-some-help-tf4624513.html#a13214128
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >>
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem you are trying to solve?
> >
> > ______________________________________________
> > [hidden email] 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Need-some-help-tf4624513.html#a13216156
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza
Thanks Jholtman.
However, the plot didnt come out the way I envisoned it to be. On the Y axis, i should have sign changes in 1000 tosses, the range being from negative to postitive, and a straight horizontal line across  y=0. The X-axis should have the toss number, range 0-1000. I'm looking for a scatterlot preferably. Also, how do I call out the numbe rof heads for instance?

I'm glad u mentioned rle(x)....works perfect!


jholtman wrote
I really depends on what you want to plot.  You can plot the
cumulative count of the sign changes with:

x <- sample(c(0,1), 1000, TRUE)
plot(cumsum(x), type='l')

There are other things you can do.  You can get a rough histogram of
the length of the run by:

> stem(rle(x)$length)

  The decimal point is at the |

  1 | 00000000000000000000000000000000000000000000000000000000000000000000+201
  1 |
  2 | 00000000000000000000000000000000000000000000000000000000000000000000+60
  2 |
  3 | 00000000000000000000000000000000000000000000000000000
  3 |
  4 | 00000000000000000000000000000000000000
  4 |
  5 | 000000000
  5 |
  6 | 00000000
  6 |
  7 | 00000

With R, almost anything is possible.

On 10/15/07, azzza <azza.khogaliali@utoronto.ca> wrote:
>
> Umm, yes, what you had makes a lot of sense. How would I represent that in a
> plot of the number of sign changes in the to  Y axis, and the toss number
> (from 0 to 1000) in the x-axis?
>
>
>
>
> jholtman wrote:
> >
> > You might want to check out 'rle'.  This will give you the 'lengths'
> > of runs of the same value and therefore when the value changes (sign
> > change?) you can see how often:
> >
> >> x <- sample(c(-1,1), 1000, TRUE)
> >> rle(x)
> > Run Length Encoding
> >   lengths: int [1:483] 2 2 1 4 3 1 1 1 1 2 ...
> >   values : num [1:483] -1 1 -1 1 -1 1 -1 1 -1 1 ...
> >
> > Here was a sample of 1000, and there were 483 changes between the
> > samples.  Is this what you are looking for?
> >
> > On 10/15/07, azzza <azza.khogaliali@utoronto.ca> wrote:
> >>
> >>
> >> Quite helpful indeed. Greatly appreciated.
> >> Another problem I had was trying to simulate an example from my book.
> >> Simulating 1000 coin tosses, and finding the frequency of sign changes.
> >> So
> >> how will we plot this using R? (frequency of sign changes in Y axis)
> >>
> >>
> >>
> >>
> >> Daniel Nordlund wrote:
> >> >
> >> >> -----Original Message-----
> >> >> From: r-help-bounces@r-project.org
> >> [mailto:r-help-bounces@r-project.org]
> >> >> On Behalf
> >> >> Of azzza
> >> >> Sent: Sunday, October 14, 2007 10:21 PM
> >> >> To: r-help@r-project.org
> >> >> Subject: [R] Need some help
> >> >>
> >> >>
> >> >> Hi!
> >> >> I'm taking a course that requires some programming background, but I'm
> >> a
> >> >> complete novice in the field.
> >> >>
> >> >> when asked to generate a list of 20 uniform random numbers, is it
> >> alright
> >> >> if
> >> >> I put in >randu, and just copy-paste the first 20 numbers?? Or is
> >> there,
> >> >> as
> >> >> I suspect, a better way of calling out exactly 20 uniform random
> >> >> numbers??
> >> >>
> >> > See ?runif
> >> >
> >> > rand_nums <- runif(20)
> >> >
> >> >> I'm also unable to solve the following problem:
> >> >> We know that on average 30% of the customers who enter a store make a
> >> >> purchase. Suppose 200
> >> >> people enter the store today. Run a simulation to see how many
> >> purchases
> >> >> we
> >> >> will have today.
> >> >>
> >> > see ?sample
> >> >> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30),
> >> >> replace=TRUE))
> >> >
> >> > Hope this is helpful,
> >> >
> >> > Dan
> >> >
> >> > Daniel Nordlund
> >> > Bothell, WA USA
> >> >
> >> > ______________________________________________
> >> > 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.
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/Need-some-help-tf4624513.html#a13214128
> >> Sent from the R help mailing list archive at Nabble.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.
> >>
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem you are trying to solve?
> >
> > ______________________________________________
> > 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Need-some-help-tf4624513.html#a13216156
> Sent from the R help mailing list archive at Nabble.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.
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

Daniel Nordlund
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of azzza
> Sent: Monday, October 15, 2007 6:06 PM
> To: [hidden email]
> Subject: Re: [R] Need some help
>
>
>
> Thanks Jholtman.
> However, the plot didnt come out the way I envisone dit to be. On the Y
> axis, i should have sign changes in 1000 tosses, the range being from
> negative to postitive, and a straight horizontal line across  y=0. The
> X-axis should have the toss number, range 0-1000
>

You would probably get more prompt and useful help if you would provide a small, self- contained example of what you wanted (maybe small enough that you could work it by hand).  Also, if you provided some code using the help that you have already received, that would show that you are trying to solve the problem yourself and others could help with the specific R program issues that you are having.  For example, it is not clear to me if you want to count the first run of tosses as 0 or 1 sign change.  I will assume it is zero.  So with that assumption, does this get you what you want?

n <- 1000
x <- sample(c(0,1), n, TRUE)
y <- rle(x)

z <- rep(1:length(y$lengths),y$lengths)
plot(1:n,z-1)

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza

You are right, I was a bit too vague. I am trying to simulate 1000 coin Tosses. Then to write a code for the number of heads/Tails.....naturally, we would expect the proportion of heads to be 50% so the number of heads would be around 500. Secondly, I'm trying to look at the number of sign changes in 1000 tosses. The example in the book shows that the number of sign changes is WAY less than 50%. so I was  trying to look for a code that shows the number of sign changes, which someone pointed out would be to use rle(x).....my concern however is that when the rle(x) is used, the proportion of sign changes is about 50% (around 500), which would be logical to expect, however, real life experiments have shown that the number of sign changes is actually much less than 50%. The other thing I was trying to do is to plot a line graph of number of sign changes versus the number of tosses. Yes, the number of tosses should begin at zero, and at zero coin toss, the sign change is zero. The range of the number of sign changes in the Y-axis should include negative values of Y. So, for toss number zero, the sign change is 0, for the first toss, the sign change may be +3 for instance, for the 50th toss, the sign change may be -5 for instance. The plot shown in the book shows that for most of the tosses, the sign changes were negative, and this would explain how the overall number of sign changes is very low.

so basically, i was looking for a code to show the number of sign changes,  and a plot of sign changes versus toss number (from toss #0 to toss number 1000). The Y axis (number of sign changes, should include negative values of Y)

thanks


Daniel Nordlund wrote
> -----Original Message-----
> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org] On Behalf
> Of azzza
> Sent: Monday, October 15, 2007 6:06 PM
> To: r-help@r-project.org
> Subject: Re: [R] Need some help
>
>
>
> Thanks Jholtman.
> However, the plot didnt come out the way I envisone dit to be. On the Y
> axis, i should have sign changes in 1000 tosses, the range being from
> negative to postitive, and a straight horizontal line across  y=0. The
> X-axis should have the toss number, range 0-1000
>

You would probably get more prompt and useful help if you would provide a small, self- contained example of what you wanted (maybe small enough that you could work it by hand).  Also, if you provided some code using the help that you have already received, that would show that you are trying to solve the problem yourself and others could help with the specific R program issues that you are having.  For example, it is not clear to me if you want to count the first run of tosses as 0 or 1 sign change.  I will assume it is zero.  So with that assumption, does this get you what you want?

n <- 1000
x <- sample(c(0,1), n, TRUE)
y <- rle(x)

z <- rep(1:length(y$lengths),y$lengths)
plot(1:n,z-1)

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

Marc Schwartz
You might want to do some more research on "Bernoulli Trials" and "Fair
Coins", which would provide some enlightenment on why you would not
"expect" 500 sign changes in 1,000 tosses and why you should, if the
coin is "fair", expect to *approach* a 50/50 distribution of heads and
tails in a large number of tosses, but not actually observe it.

HTH,

Marc Schwartz


On Tue, 2007-10-16 at 09:05 -0700, azzza wrote:

>
> You are right, I was a bit too vague. I am trying to simulate 1000 coin
> Tosses. Then to write a code for the number of heads/Tails.....naturally, we
> would expect the proportion of heads to be 50% so the number of heads would
> be around 500. Secondly, I'm trying to look at the number of sign changes in
> 1000 tosses. The example in the book shows that the number of sign changes
> is WAY less than 50%. so I was  trying to look for a code that shows the
> number of sign changes, which someone pointed out would be to use
> rle(x).....my concern however is that when the rle(x) is used, the
> proportion of sign changes is about 50% (around 500), which would be logical
> to expect, however, real life experiments have shown that the number of sign
> changes is actually much less than 50%. The other thing I was trying to do
> is to plot a line graph of number of sign changes versus the number of
> tosses. Yes, the number of tosses should begin at zero, and at zero coin
> toss, the sign change is zero. The range of the number of sign changes in
> the Y-axis should include negative values of Y. So, for toss number zero,
> the sign change is 0, for the first toss, the sign change may be +3 for
> instance, for the 50th toss, the sign change may be -5 for instance. The
> plot shown in the book shows that for most of the tosses, the sign changes
> were negative, and this would explain how the overall number of sign changes
> is very low.
>
> so basically, i was looking for a code to show the number of sign changes,
> and a plot of sign changes versus toss number (from toss #0 to toss number
> 1000). The Y axis (number of sign changes, should include negative values of
> Y)
>
> thanks
>
>
>
> Daniel Nordlund wrote:
> >
> >> -----Original Message-----
> >> From: [hidden email] [mailto:[hidden email]]
> >> On Behalf
> >> Of azzza
> >> Sent: Monday, October 15, 2007 6:06 PM
> >> To: [hidden email]
> >> Subject: Re: [R] Need some help
> >>
> >>
> >>
> >> Thanks Jholtman.
> >> However, the plot didnt come out the way I envisone dit to be. On the Y
> >> axis, i should have sign changes in 1000 tosses, the range being from
> >> negative to postitive, and a straight horizontal line across  y=0. The
> >> X-axis should have the toss number, range 0-1000
> >>
> >
> > You would probably get more prompt and useful help if you would provide a
> > small, self- contained example of what you wanted (maybe small enough that
> > you could work it by hand).  Also, if you provided some code using the
> > help that you have already received, that would show that you are trying
> > to solve the problem yourself and others could help with the specific R
> > program issues that you are having.  For example, it is not clear to me if
> > you want to count the first run of tosses as 0 or 1 sign change.  I will
> > assume it is zero.  So with that assumption, does this get you what you
> > want?
> >
> > n <- 1000
> > x <- sample(c(0,1), n, TRUE)
> > y <- rle(x)
> >
> > z <- rep(1:length(y$lengths),y$lengths)
> > plot(1:n,z-1)
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel Nordlund
> > Bothell, WA USA
> >
> > ______________________________________________
> > [hidden email] 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.
> >
> >
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza

ok, so suppose a coin is tossed 1000 times. Each time head occurs, we win a dollar, otherwise we lose a dollar. Let S(n) be our accumulated winnings after n tosses. For instance, if the sequence HHHTT occurs in the first five tosses, then S(5) = $1.00 wheras if the sequence HTTTT occurs, S(5) =-$3. So now, we want to see how many times during the 1000tosses S9n) will go from a positive balance to a negative balanc eor the other way around. So for our simulation, S(n) is computed by adding one to S(n-1) if a head occurs, and subtracting one form S(n-1) if a tail occurs. A change in sign will occur on the nth toss in one of two ways: S(n-2)=1, S(n-1)=0 and, S(n)= -1 OR S(n-2) = -1, S(n-1)=0 and S(n)=1. This is equivalent to S(n-2)+ S(n-1)+ S(n)=0.
so now, n is the numbe rof tosses,  S(n) is the number of heads minus the number of tails in n tosses and C is the number of times S(n) changes sign.
so we initialize n=0, S(-1)=0, S(0)=0, and C(0)=0

now we should,
-generate u, a uniform number, with the increment, n=n+1 ....(our n=1000)
-if u<1/2, that is tails occur, set S(n)=S(n-1)-1, and also set S(n)=S(n-1)+1
- If S(n) +S(n-1)+S(n-2)=0, then increment C=C+1.

My issue is simulating this in R, where I need to code the number of sign changes, the frequency of heads, and to plot S(n) versus n in a line graph.



Marc Schwartz wrote
You might want to do some more research on "Bernoulli Trials" and "Fair
Coins", which would provide some enlightenment on why you would not
"expect" 500 sign changes in 1,000 tosses and why you should, if the
coin is "fair", expect to *approach* a 50/50 distribution of heads and
tails in a large number of tosses, but not actually observe it.

HTH,

Marc Schwartz


On Tue, 2007-10-16 at 09:05 -0700, azzza wrote:
>
> You are right, I was a bit too vague. I am trying to simulate 1000 coin
> Tosses. Then to write a code for the number of heads/Tails.....naturally, we
> would expect the proportion of heads to be 50% so the number of heads would
> be around 500. Secondly, I'm trying to look at the number of sign changes in
> 1000 tosses. The example in the book shows that the number of sign changes
> is WAY less than 50%. so I was  trying to look for a code that shows the
> number of sign changes, which someone pointed out would be to use
> rle(x).....my concern however is that when the rle(x) is used, the
> proportion of sign changes is about 50% (around 500), which would be logical
> to expect, however, real life experiments have shown that the number of sign
> changes is actually much less than 50%. The other thing I was trying to do
> is to plot a line graph of number of sign changes versus the number of
> tosses. Yes, the number of tosses should begin at zero, and at zero coin
> toss, the sign change is zero. The range of the number of sign changes in
> the Y-axis should include negative values of Y. So, for toss number zero,
> the sign change is 0, for the first toss, the sign change may be +3 for
> instance, for the 50th toss, the sign change may be -5 for instance. The
> plot shown in the book shows that for most of the tosses, the sign changes
> were negative, and this would explain how the overall number of sign changes
> is very low.
>
> so basically, i was looking for a code to show the number of sign changes,
> and a plot of sign changes versus toss number (from toss #0 to toss number
> 1000). The Y axis (number of sign changes, should include negative values of
> Y)
>
> thanks
>
>
>
> Daniel Nordlund wrote:
> >
> >> -----Original Message-----
> >> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org]
> >> On Behalf
> >> Of azzza
> >> Sent: Monday, October 15, 2007 6:06 PM
> >> To: r-help@r-project.org
> >> Subject: Re: [R] Need some help
> >>
> >>
> >>
> >> Thanks Jholtman.
> >> However, the plot didnt come out the way I envisone dit to be. On the Y
> >> axis, i should have sign changes in 1000 tosses, the range being from
> >> negative to postitive, and a straight horizontal line across  y=0. The
> >> X-axis should have the toss number, range 0-1000
> >>
> >
> > You would probably get more prompt and useful help if you would provide a
> > small, self- contained example of what you wanted (maybe small enough that
> > you could work it by hand).  Also, if you provided some code using the
> > help that you have already received, that would show that you are trying
> > to solve the problem yourself and others could help with the specific R
> > program issues that you are having.  For example, it is not clear to me if
> > you want to count the first run of tosses as 0 or 1 sign change.  I will
> > assume it is zero.  So with that assumption, does this get you what you
> > want?
> >
> > n <- 1000
> > x <- sample(c(0,1), n, TRUE)
> > y <- rle(x)
> >
> > z <- rep(1:length(y$lengths),y$lengths)
> > plot(1:n,z-1)
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel Nordlund
> > Bothell, WA USA
> >
> > ______________________________________________
> > 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

Nordlund, Dan (DSHS/RDA)
In reply to this post by azzza
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of azzza
> Sent: Tuesday, October 16, 2007 9:06 AM
> To: [hidden email]
> Subject: Re: [R] Need some help
>
>
>
> You are right, I was a bit too vague. I am trying to simulate
> 1000 coin
> Tosses. Then to write a code for the number of
> heads/Tails.....naturally, we
> would expect the proportion of heads to be 50% so the number
> of heads would
> be around 500. Secondly, I'm trying to look at the number of
> sign changes in
> 1000 tosses. The example in the book shows that the number of
> sign changes
> is WAY less than 50%. so I was  trying to look for a code
> that shows the
> number of sign changes, which someone pointed out would be to use
> rle(x).....my concern however is that when the rle(x) is used, the
> proportion of sign changes is about 50% (around 500), which
> would be logical
> to expect, however, real life experiments have shown that the
> number of sign
> changes is actually much less than 50%. The other thing I was
> trying to do
> is to plot a line graph of number of sign changes versus the number of
> tosses. Yes, the number of tosses should begin at zero, and
> at zero coin
> toss, the sign change is zero. The range of the number of
> sign changes in
> the Y-axis should include negative values of Y. So, for toss
> number zero,
> the sign change is 0, for the first toss, the sign change may
> be +3 for
> instance,

I guess I don't understand what you mean by "sign change".  It seems to me that on any given toss there either is or there isn't a change from heads to tails (or tails to head).  How then would it be possible to get a +3 sign change on the first toss?  Also, I might guess as to what you mean by a negative sign change but my crystal ball is in the shop :-).

Again, I would recommend that you simulate 10 coin tosses, and then by hand determine your negative and positive sign changes, and then show us the tosses, the sign changes, and the y value that you want plotted for each toss.  That would at least clarify for me (if for no one else) what the issues are.


Sorry I don't have any better advice at the moment,

Dan

Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA  98504-5204
 
 

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

jholtman
In reply to this post by azzza
If what you are asking for is to see how many times it crosses the
axis when 'accumulating' the values of the top (+1, -1), then the
following will do it - this is for 1000 and shows there are 32
crossings of the axis.

> x <- sample(c(-1,1), 1000, TRUE)
> plot(cumsum(x), type='l')
> # now if you are looking at when it crosses the axis
> cumValue <- cumsum(x)
> signChanges <- ifelse(cumValue >= 0, 1, -1)
> # number of times it crosses the axis
> sum(diff(signChanges) != 0)
[1] 32


On 10/16/07, azzza <[hidden email]> wrote:

>
> Thats what is frustrating me....I've done a lot of reading, but there doesnt
> seem to be much info on sign changes..............I  dont udnerstand why the
> poportion of sign changes is much less than 50%, despite the proportion of
> heads being equal to that of tails.
>
>
> Marc Schwartz wrote:
> >
> > You might want to do some more research on "Bernoulli Trials" and "Fair
> > Coins", which would provide some enlightenment on why you would not
> > "expect" 500 sign changes in 1,000 tosses and why you should, if the
> > coin is "fair", expect to *approach* a 50/50 distribution of heads and
> > tails in a large number of tosses, but not actually observe it.
> >
> > HTH,
> >
> > Marc Schwartz
> >
> >
> > On Tue, 2007-10-16 at 09:05 -0700, azzza wrote:
> >>
> >> You are right, I was a bit too vague. I am trying to simulate 1000 coin
> >> Tosses. Then to write a code for the number of heads/Tails.....naturally,
> >> we
> >> would expect the proportion of heads to be 50% so the number of heads
> >> would
> >> be around 500. Secondly, I'm trying to look at the number of sign changes
> >> in
> >> 1000 tosses. The example in the book shows that the number of sign
> >> changes
> >> is WAY less than 50%. so I was  trying to look for a code that shows the
> >> number of sign changes, which someone pointed out would be to use
> >> rle(x).....my concern however is that when the rle(x) is used, the
> >> proportion of sign changes is about 50% (around 500), which would be
> >> logical
> >> to expect, however, real life experiments have shown that the number of
> >> sign
> >> changes is actually much less than 50%. The other thing I was trying to
> >> do
> >> is to plot a line graph of number of sign changes versus the number of
> >> tosses. Yes, the number of tosses should begin at zero, and at zero coin
> >> toss, the sign change is zero. The range of the number of sign changes in
> >> the Y-axis should include negative values of Y. So, for toss number zero,
> >> the sign change is 0, for the first toss, the sign change may be +3 for
> >> instance, for the 50th toss, the sign change may be -5 for instance. The
> >> plot shown in the book shows that for most of the tosses, the sign
> >> changes
> >> were negative, and this would explain how the overall number of sign
> >> changes
> >> is very low.
> >>
> >> so basically, i was looking for a code to show the number of sign
> >> changes,
> >> and a plot of sign changes versus toss number (from toss #0 to toss
> >> number
> >> 1000). The Y axis (number of sign changes, should include negative values
> >> of
> >> Y)
> >>
> >> thanks
> >>
> >>
> >>
> >> Daniel Nordlund wrote:
> >> >
> >> >> -----Original Message-----
> >> >> From: [hidden email]
> >> [mailto:[hidden email]]
> >> >> On Behalf
> >> >> Of azzza
> >> >> Sent: Monday, October 15, 2007 6:06 PM
> >> >> To: [hidden email]
> >> >> Subject: Re: [R] Need some help
> >> >>
> >> >>
> >> >>
> >> >> Thanks Jholtman.
> >> >> However, the plot didnt come out the way I envisone dit to be. On the
> >> Y
> >> >> axis, i should have sign changes in 1000 tosses, the range being from
> >> >> negative to postitive, and a straight horizontal line across  y=0. The
> >> >> X-axis should have the toss number, range 0-1000
> >> >>
> >> >
> >> > You would probably get more prompt and useful help if you would provide
> >> a
> >> > small, self- contained example of what you wanted (maybe small enough
> >> that
> >> > you could work it by hand).  Also, if you provided some code using the
> >> > help that you have already received, that would show that you are
> >> trying
> >> > to solve the problem yourself and others could help with the specific R
> >> > program issues that you are having.  For example, it is not clear to me
> >> if
> >> > you want to count the first run of tosses as 0 or 1 sign change.  I
> >> will
> >> > assume it is zero.  So with that assumption, does this get you what you
> >> > want?
> >> >
> >> > n <- 1000
> >> > x <- sample(c(0,1), n, TRUE)
> >> > y <- rle(x)
> >> >
> >> > z <- rep(1:length(y$lengths),y$lengths)
> >> > plot(1:n,z-1)
> >> >
> >> > Hope this is helpful,
> >> >
> >> > Dan
> >> >
> >> > Daniel Nordlund
> >> > Bothell, WA USA
> >> >
> >> > ______________________________________________
> >> > [hidden email] 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.
> >> >
> >> >
> >>
> >
> > ______________________________________________
> > [hidden email] 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Need-some-help-tf4624513.html#a13238567
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

RKoenker
You should be reading Feller v1 (p 86, 3rd ed) to see that the number of
zero crossings in this process is proportional to sqrt(n) not n.


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820


>
> On 10/16/07, azzza <[hidden email]> wrote:
>>
>> Thats what is frustrating me....I've done a lot of reading, but  
>> there doesnt
>> seem to be much info on sign changes..............I  dont  
>> udnerstand why the
>> poportion of sign changes is much less than 50%, despite the  
>> proportion of
>> heads being equal to that of tails.
>>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

dlakelan
In reply to this post by jholtman
On Tue, Oct 16, 2007 at 02:06:47PM -0400, jim holtman wrote:
> If what you are asking for is to see how many times it crosses the
> axis when 'accumulating' the values of the top (+1, -1), then the
> following will do it - this is for 1000 and shows there are 32
> crossings of the axis.

I think what he wants is the number of times that the current sample
is different from the previous sample. (ie. imagine heads = 1 and
tails = -1 how many times does the sequence change sign?)

--
Daniel Lakeland
[hidden email]
http://www.street-artists.org/~dlakelan

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza
In reply to this post by Nordlund, Dan (DSHS/RDA)

 
ok, so suppose a coin is tossed 1000 times. Each time head occurs, we win a dollar, otherwise we lose a dollar. Let S(n) be our accumulated winnings after n tosses. For instance, if the sequence HHHTT occurs in the first five tosses, then S(5) = $1.00 wheras if the sequence HTTTT occurs, S(5) =-$3. So now, we want to see how many times during the 1000tosses S9n) will go from a positive balance to a negative balanc eor the other way around. So for our simulation, S(n) is computed by adding one to S(n-1) if a head occurs, and subtracting one form S(n-1) if a tail occurs. A change in sign will occur on the nth toss in one of two ways: S(n-2)=1, S(n-1)=0 and, S(n)= -1 OR S(n-2) = -1, S(n-1)=0 and S(n)=1. This is equivalent to S(n-2)+ S(n-1)+ S(n)=0.
so now, n is the numbe rof tosses,  S(n) is the number of heads minus the number of tails in n tosses and C is the number of times S(n) changes sign.
so we initialize n=0, S(-1)=0, S(0)=0, and C(0)=0

now we should,
-generate u, a uniform number, with the increment, n=n+1 ....(our n=1000)
-if u<1/2, that is tails occur, set S(n)=S(n-1)-1, and also set S(n)=S(n-1)+1
- If S(n) +S(n-1)+S(n-2)=0, then increment C=C+1.

My issue is simulating this in R, where I need to code the number of sign changes, the frequency of heads, and to plot S(n) versus n in a line graph.


for each coin toss, the number of sign changes could either be a positive number, zero, or a negative number.

______________________________________________
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.


Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza
In reply to this post by RKoenker
ok, so suppose a coin is tossed 1000 times. Each time head occurs, we win a dollar, otherwise we lose a dollar. Let S(n) be our accumulated winnings after n tosses. For instance, if the sequence HHHTT occurs in the first five tosses, then S(5) = $1.00 wheras if the sequence HTTTT occurs, S(5) =-$3. So now, we want to see how many times during the 1000tosses S9n) will go from a positive balance to a negative balanc eor the other way around. So for our simulation, S(n) is computed by adding one to S(n-1) if a head occurs, and subtracting one form S(n-1) if a tail occurs. A change in sign will occur on the nth toss in one of two ways: S(n-2)=1, S(n-1)=0 and, S(n)= -1 OR S(n-2) = -1, S(n-1)=0 and S(n)=1. This is equivalent to S(n-2)+ S(n-1)+ S(n)=0.
so now, n is the numbe rof tosses,  S(n) is the number of heads minus the number of tails in n tosses and C is the number of times S(n) changes sign.
so we initialize n=0, S(-1)=0, S(0)=0, and C(0)=0

now we should repeat the following steps 1000 times
-generate u, a uniform number, with the increment, n=n+1
-if u<1/2, that is tails occur, set S(n)=S(n-1)-1, and also set S(n)=S(n-1)+1
- If S(n) +S(n-1)+S(n-2)=0, then increment C=C+1.

My issue is simulating this in R, where I need to code the number of sign changes, the frequency of heads, and to plot S(n) versus n in a line graph.

 so n= the toss number, and s(n) is the accumulated winnings after n tosses. Now, each time we have a heads, we win a dollar, and each time we have a tails, we lose a dollar. So, s(n) is th sign changes in 1000 tosses. In the beginning, S(0) must be 0, and S(-1) must be zero too. ok, so if on the first toss, the  result is tails, s(1) =-1 (because  we lose a dollar for every tails). On the second toss, if we have heads , we gain a dollar, so our accumulated winnings will be s(2)=0 if on the third toss we get another head, we gain a dollar and our total winnings would be s(3)=1 and so on.

Now, you were asking what sign changes is. we will denote this by C. C will be the numbe rof times S9n) changes signs (that is from negative to positive or from positive to negative)
from positive to negative would be S(n-2)=1, S(n-1)=0 and, S(n)= -1  and form negative to positive would be S(n-2) = -1, S(n-1)=0 and S(n)=1
So in the beginning, when n=0, and s(n)=0, c must be zero.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

Marc Schwartz
In reply to this post by azzza
On Tue, 2007-10-16 at 11:53 -0700, azzza wrote:

>
>  
> ok, so suppose a coin is tossed 1000 times. Each time head occurs, we win a
> dollar, otherwise we lose a dollar. Let S(n) be our accumulated winnings
> after n tosses. For instance, if the sequence HHHTT occurs in the first five
> tosses, then S(5) = $1.00 wheras if the sequence HTTTT occurs, S(5) =-$3. So
> now, we want to see how many times during the 1000tosses S9n) will go from a
> positive balance to a negative balanc eor the other way around. So for our
> simulation, S(n) is computed by adding one to S(n-1) if a head occurs, and
> subtracting one form S(n-1) if a tail occurs. A change in sign will occur on
> the nth toss in one of two ways: S(n-2)=1, S(n-1)=0 and, S(n)= -1 OR S(n-2)
> = -1, S(n-1)=0 and S(n)=1. This is equivalent to S(n-2)+ S(n-1)+ S(n)=0.
> so now, n is the numbe rof tosses,  S(n) is the number of heads minus the
> number of tails in n tosses and C is the number of times S(n) changes sign.
> so we initialize n=0, S(-1)=0, S(0)=0, and C(0)=0
>
> now we should,
> -generate u, a uniform number, with the increment, n=n+1 ....(our n=1000)
> -if u<1/2, that is tails occur, set S(n)=S(n-1)-1, and also set
> S(n)=S(n-1)+1
> - If S(n) +S(n-1)+S(n-2)=0, then increment C=C+1.
>
> My issue is simulating this in R, where I need to code the number of sign
> changes, the frequency of heads, and to plot S(n) versus n in a line graph.
>
>
> for each coin toss, the number of sign changes could either be a positive
> number, zero, or a negative number.

I believe that Jim had the right approach in his reply here:

  https://stat.ethz.ch/pipermail/r-help/2007-October/143383.html

and Prof. Koenker has given you a reference on the theory:

  https://stat.ethz.ch/pipermail/r-help/2007-October/143385.html

HTH,

Marc

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza
In reply to this post by Nordlund, Dan (DSHS/RDA)

Ok, so n= the toss number, and s(n) is the accumulated winnings after n tosses. Now, each time we have a heads, we win a dollar, and each time we have a tails, we lose a dollar. So, s(n) is th sign changes in 1000 tosses. In the beginning, S(0) must be 0, and S(-1) must be zero too. ok, so if on the first toss, the  result is tails, s(1) =-1 (because  we lose a dollar for every tails). On the second toss, if we have heads , we gain a dollar, so our accumulated winnings will be s(2)=0 if on the third toss we get another head, we gain a dollar and our total winnings would be s(3)=1 and so on.

Now, you were asking what sign changes is. we will denote this by C. C will be the numbe rof times S9n) changes signs (that is from negative to positive or from positive to negative)
from positive to negative would be S(n-2)=1, S(n-1)=0 and, S(n)= -1  and form negative to positive would be S(n-2) = -1, S(n-1)=0 and S(n)=1
So in the beginning, when n=0, and s(n)=0, c msu tbe zero.

Hope that clarifies things.

_________________________________________________________________________________



I guess I don't understand what you mean by "sign change".  It seems to me that on any given toss there either is or there isn't a change from heads to tails (or tails to head).  How then would it be possible to get a +3 sign change on the first toss?  Also, I might guess as to what you mean by a negative sign change but my crystal ball is in the shop :-).

Again, I would recommend that you simulate 10 coin tosses, and then by hand determine your negative and positive sign changes, and then show us the tosses, the sign changes, and the y value that you want plotted for each toss.  That would at least clarify for me (if for no one else) what the issues are.


Sorry I don't have any better advice at the moment,

Dan

Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA  98504-5204
 
 

______________________________________________
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.


Reply | Threaded
Open this post in threaded view
|

Re: Need some help

azzza
In reply to this post by dlakelan

Thank you guyz. your codes gave me the results I was looking for. And thanks for the reference suggestion. Lastly,  how do I code the frequency of heads?


Daniel Lakeland wrote
On Tue, Oct 16, 2007 at 02:06:47PM -0400, jim holtman wrote:
> If what you are asking for is to see how many times it crosses the
> axis when 'accumulating' the values of the top (+1, -1), then the
> following will do it - this is for 1000 and shows there are 32
> crossings of the axis.

I think what he wants is the number of times that the current sample
is different from the previous sample. (ie. imagine heads = 1 and
tails = -1 how many times does the sequence change sign?)

--
Daniel Lakeland
dlakelan@street-artists.org
http://www.street-artists.org/~dlakelan

______________________________________________
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.
12