2011-05-27 19 views
6

को 95% विश्वास सीमा जोड़े मैं एक परवलय लाइन आर का उपयोग कर इस सिक्के टॉस साजिश करने के लिए 95% विश्वास सीमा को संकेतित करते जोड़ना चाहते हैं:संचयी साजिश

x <- sample(c(-1,1), 60000, replace = TRUE) 
plot.ts(cumsum(x), ylim=c(-250,250)) 

यहाँ मैं के लिए क्या देख रहा हूँ का एक उदाहरण है : graph

अद्यतन: @ बिल_080 का उत्तर उत्कृष्ट है। हालांकि मैं पहले से ही 100,000 सिक्का उछालों गणना की है:

str(100ktoss) 
num [1:100000] -1 1 1 1 -1 -1 1 -1 -1 -1 ... 

और मैं वास्तव में सिर्फ इतना है कि साजिश करने के लिए 95% की सीमा जोड़ना चाहते हैं: toss

plot.ts(cumsum(100ktoss)) 

यह मेरी 100K सिक्का उछालों गणना करने के लिए कई घंटे लग गए और जब मैं @ बिल_080 के कोड के साथ कोशिश करता हूं और दोहराता हूं तो मैं स्मृति से बाहर चला जाता हूं (100,000 के लिए)।

अंतिम अपडेट: ठीक है। अंतिम समस्या मेरे पास शून्य पर क्लैंप किए गए प्रत्येक दौर की शुरुआत के साथ एक ग्राफ पर संचयी हिट के कई दौरों का एक साजिश है (वास्तव में 1 या -1 यह जीतने या हारने के आधार पर)।

>str(1.ts) 
Time-Series [1:35] from 1 to 35: 1 2 1 2 3 4 5 4 5 6 ... 
>str(2.ts) 
Time-Series [1:150] from 36 to 185: -1 0 1 0 -1 -2 -1 0 1 2 ... 

मैं इस तरह से प्रत्येक सेगमेंट में समान 95% सीमा जोड़ना चाहता हूं। अब हल किया गया:

@ बिल_080 बहुत धन्यवाद।

cum

उत्तर

9

इस प्रयास करें: यह अंत उत्पाद है। सभी लूप for लूप हैं, इसलिए आप आसानी से अधिक गणना जोड़ सकते हैं।

#Set the number of bets and number of trials and % lines 
numbet <- 6000 #6000 bets 
numtri <- 1000 #Run 1000 trials of the 6000 bets 
perlin <- 0.05 #Show the +/- 5% lines on the graph 
rantri <- 60 #The 60th trial (just a random trial to be drawn) 

#Fill a matrix where the rows are the cumulative bets and the columns are the trials 
xcum <- matrix(NA, nrow=numbet, ncol=numtri) 
for (i in 1:numtri) { 
    x <- sample(c(-1,1), numbet, replace = TRUE) 
    xcum[,i] <- cumsum(x) 
} 

#Plot the trials as transparent lines so you can see the build up 
matplot(xcum, type="l", xlab="Number of Bets", ylab="Cumulative Sum", main="Cumulative Results", col=rgb(0.01, 0.01, 0.01, 0.02)) 
grid() 

#Sort the trials of each bet so you can pick out the desired % 
xcumsor <- xcum 
for (i in 1:numbet) { 
    xcumsor[i,] <- xcum[i,order(xcum[i,])] 
} 

#Draw the upper/lower limit lines and the 50% probability line  
lines(xcumsor[, perlin*numtri], type="l", lwd=2, col=rgb(1, 0.0, 0.0)) #Lower limit 
lines(xcumsor[, 0.5*numtri], type="l", lwd=3, col=rgb(0, 1, 0.0)) #50% Line 
lines(xcumsor[, (1-perlin)*numtri], type="l", lwd=2, col=rgb(1, 0.0, 0.0)) #Upper limit 

#Show one of the trials 
lines(xcum[, rantri], type="l", lwd=1, col=rgb(1, 0.8, 0)) #Random trial 

#Draw the legend 
legend("bottomleft", legend=c("Various Trials", "Single Trial", "50% Probability", "Upper/Lower % Limts"), bg="white", lwd=c(1, 1, 3, 2), col=c("darkgray", "orange", "green", "red")) 

enter image description here

संपादित करें 1 ==================================== ======================

यदि आप +/- 5% लाइनों को खींचने की कोशिश कर रहे हैं, तो यह केवल एक वर्ग रूट फ़ंक्शन है। कोड यह रहा:

#Set the bet sequence and the % lines 
betseq <- 1:100000 #1 to 100,000 bets 
perlin <- 0.05 #Show the +/- 5% lines on the graph 

#Calculate the Upper and Lower limits using perlin 
#qnorm() gives the multiplier for the square root 
upplim <- qnorm(1-perlin)*sqrt(betseq) 
lowlim <- qnorm(perlin)*sqrt(betseq) 

#Get the range for y 
yran <- range(upplim, lowlim) 

#Plot the upper and lower limit lines 
plot(betseq, upplim, ylim=yran, type="l", xlab="", ylab="") 
lines(betseq, lowlim) 

enter image description here

संपादित 2 ================================ ==================

सही स्थानों पर पैराबोलस जोड़ने के लिए, यदि आप कोई फ़ंक्शन परिभाषित करते हैं तो यह संभवतः आसान है। ध्यान रखें कि नया फ़ंक्शन (dralim) lines का उपयोग करता है, इसलिए dralim पर कॉल करने से पहले प्लॉट मौजूद होना चाहिए। संपादित करें 1 में कोड के रूप में चर का उपयोग करना:

#Set the bet sequence and the % lines 
betseq <- 0:700 #0 to 700 bets 
perlin <- 0.05 #Show the +/- 5% lines on the graph 

#Define a function that plots the upper and lower % limit lines 
dralim <- function(stax, endx, perlin) { 
    lines(stax:endx, qnorm(1-perlin)*sqrt((stax:endx)-stax)) 
    lines(stax:endx, qnorm(perlin)*sqrt((stax:endx)-stax)) 
} 

#Build the plot area and draw the vertical dashed lines 
plot(betseq, rep(0, length(betseq)), type="l", ylim=c(-50, 50), main="", xlab="Trial Number", ylab="Cumulative Hits") 
abline(h=0) 
abline(v=35, lty="dashed") #Seg 1 
abline(v=185, lty="dashed") #Seg 2 
abline(v=385, lty="dashed") #Seg 3 
abline(v=485, lty="dashed") #Seg 4 
abline(v=585, lty="dashed") #Seg 5 

#Draw the % limit lines that correspond to the vertical dashed lines by calling the 
#new function dralim. 
dralim(0, 35, perlin) #Seg 1 
dralim(36, 185, perlin) #Seg 2 
dralim(186, 385, perlin) #Seg 3 
dralim(386, 485, perlin) #Seg 4 
dralim(486, 585, perlin) #Seg 5 
dralim(586, 701, perlin) #Seg 6 

enter image description here

+0

@ bill_080 वास्तव में, वास्तव में अच्छा। यदि आप मेरी प्रोफ़ाइल देखेंगे तो आप देखेंगे कि मेरे पास सिक्का टॉस प्लॉट्स के लिए एक चीज है और आपका एक सौंदर्य है। एकमात्र चीज यह मेरे लिए थोड़ा जटिल हो सकती है। :) –

+1

@RSoul, मैंने संबंधित कोड के साथ एक और साजिश जोड़ा। यह 5% लाइनों के लिए ऊपरी/निचली सीमा दिखाता है।यह केवल शर्त संख्या के वर्ग रूट की% संभावना है जो आप चाहते हैं कि% संभावना के लिए गुणक है। कोड में, 'qnorm()' फ़ंक्शन आपको गुणक देता है। 5% के लिए, 'qnorm (0.05) '-1.644 देता है और' qnorm (0.95) '1.644 देता है। –

+0

@ बिल_080 यह उत्कृष्ट है। वास्तव में मैं क्या चाहता था। यही कारण है कि मैंने अपने थीसिस में StackOverflow.com के उपयोगकर्ताओं को स्वीकार किया है। बहुत धन्यवाद। मैं आपकी और दिलचस्प साजिश का उपयोग कर सकता हूं। मैंने अभी तक फैसला नहीं किया है। –

संबंधित मुद्दे