Need some help

View: New views
5 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Re: Need some help

by azzza :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


THANKYOU guyz! yes, Jim's code was right (it was similar to someone elses)....I somehow missed his reply!
Yes, that was exactly what i was looking for. lastly, how do i find the frequency of heads perhaps?


Marc Schwartz wrote:
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

______________________________________________
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: Need some help

by jholtman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

sum(toss == 1) /length(toss) # assuming heads == 1

On 10/16/07, azzza <azza.khogaliali@...> wrote:

>
>
> THANKYOU guyz! yes, Jim's code was right (it was similar to someone
> elses)....I somehow missed his reply!
> Yes, that was exactly what i was looking for. lastly, how do i find the
> frequency of heads perhaps?
>
>
>
> Marc Schwartz wrote:
> >
> > 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
> >
> > ______________________________________________
> > R-help@... 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#a13241118
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@... 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@... 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: Need some help

by Nordlund, Dan (DSHS/RDA) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> -----Original Message-----
> From: r-help-bounces@...
> [mailto:r-help-bounces@...] On Behalf Of azzza
> Sent: Tuesday, October 16, 2007 12:49 PM
> To: r-help@...
> Subject: Re: [R] Need some help
>
>
>
> THANKYOU guyz! yes, Jim's code was right (it was similar to someone
> elses)....I somehow missed his reply!
> Yes, that was exactly what i was looking for. lastly, how do
> i find the
> frequency of heads perhaps?
>

Could you post the code you finally settled on?

Dan

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

______________________________________________
R-help@... 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: Need some help

by tlumley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 16 Oct 2007, azzza 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.
>


I think some earlier posters have misunderstood what you are trying to do: they thought you were interested in runs of heads of tails, but it seems to me that you are interested in whether the sample proportion is above or below 1/2.

Code to compute the number of sign changes looks like

x<-rbinom(1000,1,0.5)
proportion <- cumsum(x)/(1:1000)
above <- proportion > 1/2
nchanges<-length(rle(above)$values)

You might also want to plot the cumulative sum

plot(1:1000, cumsum(x))
abline(h=0)

Looking at the plot you should be able to see why the number of changes is relatively small: to get a sign change you need the curve to go in the right direction (50:50) but you also need to be close enough to zero.


         -thomas



> 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@...
>>> [mailto:r-help-bounces@...]
>>>>> On Behalf
>>>>> Of azzza
>>>>> Sent: Monday, October 15, 2007 6:06 PM
>>>>> To: r-help@...
>>>>> 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@... 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@... 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.
>
> ______________________________________________
> R-help@... 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.
>

Thomas Lumley Assoc. Professor, Biostatistics
tlumley@... University of Washington, Seattle

______________________________________________
R-help@... 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: Need some help

by J Dougherty :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Azza,

By "sign changes" are you looking at how long runs of similar results are
before switching, e.g. HHTHTTTH sees four changes, while HHHHTTTT sees just
one?  

JD

______________________________________________
R-help@... 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.
< Prev | 1 - 2 | Next >