I want to generate a random variable X by continuing generating U(0,1) random variables as long as their product falls below exp(-2). Then my random variable X would be equal to the number of U(0,1) random variables that was generated minus 1.
I tried using the while loop to do it but not sure why the code does not return anything in my console. Please help me point out what I did wrong?
p <- 1
counter <- 0
while(p < exp(-2)) {
u <- runif(1)
p <- p*u
counter <- counter+1
X <- counter - 1
print(X)
}
update
If you want to replicate the process by 100 times, you can use replicate
replicate(
100,
{
p <- 1
counter <- 0
repeat {
p <- p * runif(1)
counter <- counter + 1
if (p < exp(-2)) {
break
}
}
counter
}
)
I guess you could use repeat with condition p < exp(-2)
p <- 1
counter <- 0
repeat {
print(counter)
p <- p * runif(1)
counter <- counter + 1
if (p < exp(-2)) {
print(counter)
break
}
}
Related
I want to sum (1/3)+(3/5)+ ⋯ +(99/101) using loops in R
I have tried this and I want to check if its right and is there other solutions also if we can do it with for loops and repeat loops.
sum <- 0
i <- 1
j <- 3
while (i<=99 && j<=101 ) {
sum <- sum+i/j
i <- i+2
j <- j+2
}
print(sum)
Yes, it is correct. Although you can also do
sum <- 0
i <- 1
while (i<=99) {
sum <- sum+i/(i+2)
i <- i+2
}
sum
sum <- 0
for (i in seq(1,99,2)) {
sum <- sum+i/(i+2)
}
sum
sum <- 0
i <- 1
repeat {
sum <- sum+i/(i+2)
if (i == 99) break
i <- i + 2
}
sum
i <- seq(1,99,2)
sum(i / (i + 2))
Ah, you'd better use another name than sum for your variable.
We can use the idea of odd numbers formula where 2*n - 1 and 2*n + 1 are two consecutive odd numbers
result <- 0
for(i in 1:50){
result <- result + (2*i - 1)/(2*i + 1)
}
output
result
#>[1] 46.10465
I have this,
sum(x^i)
with i being greater than or equal to 1.
How can I create a for loop in R for this summation?
In other words, how do I format this summation in R?
If both x and i are vectors you may use for loop as -
x <- 1:10
i <- 1:10
result <- 0
for(e in i) {
result <- result + sum(x^e)
}
result
If any of x or i goes to infinity, then the result would always be infinity.
For a fixed x and infinite n,
x <- 0.1 # You may change x
s <- 0
n <- 0
while(n < 100) { #If you want inf, let n >=0 then R will freeze recommend large number...
s <- s + x^(n)
n<-n+1
}
s
Here is my code:
f.x <- function(x) {
60*x^3*(1-x)^2
}
x <- seq(0, 1, length=100)
n.samps <- 1000
n <- 0 # counter for accepted
i <- 0 # iterations
samps <- numeric(n.samps)
while (n < n.samps) {
y <- runif(1)
i <- i + 1
u <- runif(1)
if (u < f.x(y) / 2.0736) {
n <- n + 1
samps[n] <- y
}
}
I want to repeat the code above for 10 times, each time an "i" will be produced. I want to take the average of these ten "i". Instead of run the code each time, is there any way I can run one time but get 10 trials?
You can try placing your entire script into a function, and then just call it 10 times from a loop:
getValue <- function() {
x <- seq(0, 1, length=100)
n.samps <- 1000
n <- 0 # counter for accepted
i <- 0 # iterations
samps <- numeric(n.samps)
while (n < n.samps) {
y <- runif(1)
i <- i + 1
u <- runif(1)
if (u < f.x(y) / 2.0736) {
n <- n + 1
samps[n] <- y
}
}
return(i)
}
Usage:
result <- replicate(10, getValue())
I want to restrict the for cycle to only perform task if j is in some range of i (3 units, for example).
I tried the following piece of code:
a <- c(1:100)
b <- c(1:100)
k1 <- length(a)
k2 <- length(b)
for (i in 1:k1){
for (j in 1:k2){
if (j>=i-3 & j<=i+3){
c<-c(a+b)
}
}
}
What I pretended was
if i=1, j={1,2,3}, if i=6, j={1,2,3,4,5,6}
This doesn´t really work since, j and i will end up running from 1 to 100.
If I understand, the problem is that you are looping through 100 combinations of j, when only three to seven are actually useful.
If this is correct, you can loop through seven iterations of j and filter for values that are positive and within bounds:
width <- 3
for (i in seq_along(a)) {
for (j in (i-width):(i+width)) {
if (j > 0 && j <= length(b)) {
# Do something
}
}
}
When you # Do something in your code, I would advise not assigning to a variable named c.
I have generated an infinite loop and don't know how to fix it.
I essentially want to go through the data frame rnumbers and generate rstate2 with 1, -1, or 0 depending on what is in rnumbers
The function step_generator is getting stuck at the repeat function. I am not sure how to make the code put -1 in rstate2 if rnumber is less than C and then repeat an ifelse function for the next rows until a value of D or greater is obtained. Once D is obtained exit the repeat function and go back into the original for loop.
Here is my code:
rnumbers <- data.frame(replicate(5,runif(20000, 0, 1)))
dt <- c(.01)
A <- .01
B <- .0025
C <- .0003
D <- .003
E <- .05
rstate <- rnumbers # copy the structure
rstate[] <- NA # preserve structure with NA's
# Init:
rstate[1, ] <- c(0)
step_generator <- function(col, rnum){
for (i in 2:length(col) ){
if( rnum[i] < C) {
col[i] <- -1
repeat {
ifelse(rnum[i] < E, -1, if(rnum[i] >= D) {break})
}
}
else { if (rnum[i] < B) {col[i] <- -1 }
else {ifelse(rnum[i] < A, 1, 0) } }
}
return(col)
}
# Run for each column index:
for(cl in 1:5){ rstate[ , cl] <-
step_generator(rstate[,cl], rnumbers[,cl]) }
Thanks for any help.
The problem is that you are not increasing i inside repeat loop, so basically you are testing the same i all the time, and because rnum[i] < C (from if condition) it will always be rnum[i] < E since C < E, and loop never breaks.
However, if you increase i inside repeat it still will come back to value resulting from for loop, so you have to do it in different way, for example using while loop. I'm not exactly sure if I understand what you are trying to do, but basing on your description I've made this function:
step_generator <- function(col, rnum){
i <- 2
while (i <= length(col)){
if (rnum[i] < C) {
col[i] <- -1
while ((i < length(col)) & (rnum[i + 1] < D)){
i <- i + 1
col[i] <- -1
}
} else if (rnum[i] < B){
col[i] <- -1
} else if (rnum[i] < A){
col[i] <- 1
} else {
col [i] <- 0
}
i <- i + 1
}
return(col)
}