# Creating an estimator with varying shock levels (SD) in R?

First time posting!

I'm trying to create a logit estimator using a looping simulation, where the loop detects the number of correct prediction (my code is below). Is it possible to change the shock in the distribution (defined as the standard deviation in the rnorm distribution) after a certain number of correct predictions? I'm trying it out with the f variable initialized below, but with little success. I was thinking that the f variable could change to 2 after a say 20 correct predictions, for example. The code below works -- with the hashes hiding a bit of working code -- but the results will not vary based on shock (the standard deviation is constant).

Thanks!

x<-1:7
y<-c(0,0,0,1,0,1,1)
n=2000
bin1<-rep(NA,n)
bin2<-rep(NA,n)
right<-NULL
b0<-rnorm(1,-4,.1)
b1<-rnorm(1,1,.1)
n=1000
ti=0
f<-1
iter<-0
bin1<-NULL
bin2<-NULL
right=-1

for (i in 1:n) {
nright<-NULL
nb0<-b0 + rnorm(1,0,1/f)
nb1<- b1+ rnorm(1,0,1/f)
predict<-((1/(1+exp(- nb0- nb1*x))))
for (j in 1:7) {
ifelse ( y[j]==1,nright[j]<-predict[j],nright[j]<-1-predict[j])     }
nright <- prod(nright)
if (nright>right) b0 <-nb0
if (nright>right) b1<- nb1
bin1[i]<-b0
bin2[i]<-b1
#   ifelse ( nright > right, iter<-0, iter<-iter+1)
#   if (iter > 50)  f<- f/2
#   if (f<.05) stop("Done")
if (nright>right) right<-nright
ti<-ti+1
}
f
ti
b0
b1

-

As it turns out, the code above does work. If you remove the hash tags in the middle portion of the code, you have a simulation-based logit estimator (producing intercepts, slopes, the number of iterations until converging on the true value, and an output saying "Done").

The estimator adjusts the standard deviation after a given number of correct predictions. The variable "ti" provides the number of iterations the logit estimator runs until the estimator converges on the true value. To manipulate the shock (standard deviation), adjust the value of the variable "f". Two examples are below:

nb0<-b0 + rnorm(1,0,1*f)
nb1<- b1+ rnorm(1,0,1*f)

nb0<-b0 + rnorm(1,0,1/f)
nb1<- b1+ rnorm(1,0,1/f)


To further clarify, below is code that plots the estimated slopes and intercepts. You'll notice that the estimates will be less accurate at the beginning, but will then converge toward the true value as shock decreases after a given number of iterations

 plot(1:n,bin1,ylim=c(-10,0), ylab="", main="intercepts")
plot(1:n,bin2,ylim=c(-2,2), ylab="", main="slopes")


Enjoy!

-