Iterative data generation

#1
I have a question about generating data based off of a loop... I am trying to output a 100 x 3 numerical matrix observations that are described by the following:

p1_t=(p1_{t-1}*w1_{t-1})/(\bar{w_{t-1}})

p2_t=(p2_{t-1}*w2_{t-1})/(\bar{w_{t-1}})

p3_t=(p3_{t-1}*w3_{t-1})/(\bar{w_{t-1}})

Where the vector W is equal to a payoff matrix

1 .5 2
2 1 .5
.5 2 1

multiplied by the vector of that period's p's

(i.e. w1_t = p1_t * 1 + p2_t * .5 + p3_t * 2 etc...)

and

\bar{w_t} = w1_t * p1_t + w2_t * p2_t + w3_t * p3_t

I am starting with initial conditions that

p1_0 = 0.1
p2_0 = 0.2
p3_0 = 1 - p1_0 - p2_0

What I have done so far (and I'm sure is completely wrong) is:

Code:
function(A){
	
	rdirichlet<-function(n,a){
		
    	 l<-length(a);
    	 x<-matrix(rgamma(l*n,a),ncol=l,byrow=TRUE);
     	sm<-x%*%rep(1,l);
     	x/as.vector(sm);
	
	 }
	 
	error <- rdirichlet(100, c(1,1,1) )
	
	p1_1<- 0.3
	p2_1<- 0.4
	p3_1<- 1 - p1_1 - p2_1
	
	w1_1 <- (p1_1 * 1 + p2_1 * 0.5 + p3_1 * 2)
	w2_1 <- (p1_1 * 2 + p2_1 * 1 + p3_1 * 0.5)
	w3_1 <- (p1_1 * 0.5 + p2_1 * 2 + p3_1 * 1)
	
	wbar_1 <- (p1_1 * w1_1 + p2_1 * w2_1 + p3_1 * w3_1)
	
	for(i in 1:100){
		
		j <- i + 1
		
		p1_1 -> p1_i
		p2_1 -> p2_i
		p3_1 -> p3_i
		
		w1_1 -> w1_i
		w2_1 -> w2_i
		w3_1 -> w3_i
		
		wbar_1 -> wbar_i
		
		p1_j <- p1_i * w1_i / wbar_i 
		p2_j <- p2_i * w2_i / wbar_i 
		p3_j <- 1 - p1_j - p2_j
		
		}
		
		for(j in 1:100){
			
			p1_j -> p1_j
			p2_j -> p2_j
			p3_j -> p3_j
			
			fq <- c(p1_j, p2_j, p3_j)
		
		}
		
		cnames <- c("Y", "B", "O")
		rnames <- c(0:100)
		
		freq <- matrix(fq, nrow=101, ncol=3, byrow=TRUE, dimnames=list(rnames, cnames) )
		
	
	return(freq)

	}

Ignore the dirichlet bit. I am going to use that to create an error term later.

Any help is much appreciated.
 
#2
I decided to change it to matrix notation and here is where I am:

Code:
function(x){
	
	payoff <- matrix(c(1.0, 2.0, 0.5, 0.5, 1.0, 2.0, 2.0, 0.5, 1.0), nrow=3)
	
	p1_0 <- 0.1
	
	p2_0 <- 0.2
	
	P <- matrix(c(p1_0, p2_0, (1 - p1_0 - p2_0)), nrow=3)
	
	W <- payoff %*% P
	
	Wbar <- sum(W * P)
	
	for(i in 1:100){
		
		P0 <- P
		
		W0 <- W
		
		Wbar0 <- Wbar
		
		P$p1 <- P[1,] * W[1,] / Wbar
		
		P$p2 <- P[2,] * W[2,] / Wbar
		
		P$p3 <- P[3,] * W[3,] / Wbar
		
		print(P)
		
		
		
		}
	
	
	
	
	
	
	
	}
Please, somebody help. I'm lost.
 
#3
Im not really familiar with the syntax so i probably cant help. But you havent described what the problem is as i read your post. That might get you more answers.
 

Dason

Ambassador to the humans
#4
Im not really familiar with the syntax so i probably cant help. But you havent described what the problem is as i read your post. That might get you more answers.
Probably! I've largely ignored the post because I'm not sure what you're doing and you don't actually post a question or tell us what's going wrong.
 
#5
My code simply doesn't work. The first code I put up will return a matrix of 100 observations but they are all the same strategies (p1_1 = p1_2 = p1_3 = ... = 0.2832618; p2_1 = p2_2 = ... = 0.3948498; and p3_1 = p3_2 = ... = 0.3218884)
and the second piece of code i put up won't even run.

I need the code to give me 100 observations of 3 strategies (the p's) according to the functions above but I can't seem to get it.


Edit:


Code:
function(x){
	#This sets up the initial conditions for the loop
	payoff <- matrix(c(1.0, 2.0, 0.5, 0.5, 1.0, 2.0, 2.0, 0.5, 1.0), nrow=3)
	
	p1_0 <- 0.1
	
	p2_0 <- 0.2
	
        #This is the initial condition for a vector that sums to 1 and is what the  
        #loop is supposed to predict future values of
	P <- matrix(c(p1_0, p2_0, (1 - p1_0 - p2_0)), nrow=3)
	
        #Should this be inside of the loop?  These functions will be used to 
        #generate the W and Wbar in each iteration (using that iteration's 
        #values)
	W <- payoff %*% P
	
	Wbar <- sum(W * P)
	
	for(i in 1:100){
		
		P0 <- P
		
		W0 <- W
		
		Wbar0 <- Wbar
		
                #This is what I am trying to generate, 100 3x1 vectors of 
                #strategies that sum to 1 and is based off of the previous   
                #iteration's values
		P[1][i] <- P[1,][i-1] * W[1,][i-1] / Wbar[i-1]
		
		P[2][i] <- P[2,][i-1] * W[2,][i-1] / Wbar[i-1]
		
		P[3][i] <- P[3,][i-1] * W[3,][i-1] / Wbar[i-1]
		

                 }


                #I then want to stack these vectors so that I have a 3x100 matrix
                #that I can use in some matrix algebra.  I'm not sure how I do 
                #this.

		print(P)
		
		
		
		
	
	
	
	
	
	
	
	}
 
Last edited:
#6
For those interested, here is the code that got me where I wanted to be:

Code:
function(x){
	
	payoff <- matrix(c(1.0, 2.0, 0.5, 0.5, 1.0, 2.0, 2.0, 0.5, 1.0), nrow=3)
	
	p1_0 <- 0.1
	
	p2_0 <- 0.2
	
	matrixBig <<- matrix(c(p1_0, p2_0, (1 - p1_0 - p2_0)), nrow=3)
	
	Pold <- matrix(c(p1_0, p2_0, (1 - p1_0 - p2_0)), nrow=3)
	
	for(i in 2:100){
		
		P <- matrix(c(p1_0, p2_0, (1 - p1_0 - p2_0)), nrow=3)
	
		W <- payoff %*% Pold
	
		Wbar <- sum(W * Pold)
		
		Ptemp <- (Pold * W) / Wbar
		
		matrixBig <<- cbind(matrixBig,Ptemp)		
		
		Pold <- Ptemp
		
		}
		
		t(matrixBig)
	
	
	
	
	
	
	
	}
Any suggestions on how to make it cleaner would be appreciated.
 

Dason

Ambassador to the humans
#7
A couple of comments on cleaning it up:

It doesn't look like it actually needs to be a function [plus you never use x so you could just have it be function() instead of function(x)].

You don't need to do a global assignment when you write to matrixBig (why are you using <<- when <- will do just fine?)

You know ahead of time how big matrixBig is going to be so you could be more efficient by just preallocating matrixBig to be how big you need it at the end. Using cbind every time through the loop slows it down. Now the loop isn't slow right now but if you wanted to do for a much longer loop this would bottleneck you unnecessarily.

Your naming conventions seem to be all over the place. Sometimes it's all lowercase, sometimes just an uppercase letter, sometimes you use an underscore and sometimes you use camelCase. It's not a huge deal when you can see all of your code at one time but if you were ever to write larger programs you would probably want to stick with a consistent convention.