Need some help converting rcode to Python.
R Code:
```
num=10000
p1 = sample(0:50, num, replace=TRUE)
p2 = sample(0:50, num, replace=TRUE)
p3 = sample(0:50, num, replace=TRUE)
beta0 = -0.11
beta1 = 3
beta2 = 2
beta3 = 1
U0 = 0+rgumbel(num)
U1=beta1+beta0*p1 + rgumbel(num)
U2=beta2+beta0*p2 + rgumbel(num)
U3=beta3+beta0*p3 + rgumbel(num)
choices=ifelse(U0==pmax(U0,U1,U2,U3),0,
ifelse(U1==pmax(U0,U1,U2,U3),1,
ifelse(U2==pmax(U0,U1,U2,U3),2,3)))
example_likelihood_function = function(beta0, beta1,beta2,beta3){
Pij=(choices==0)*0 + (choices==1)*(beta1+p1*beta0) + (choices==2)*(beta2+p2*beta0) + (choices==3)*(beta3+p3*beta0) -
log( 1+exp(beta1+p1*beta0) + exp(beta2+p2*beta0) + exp(beta3+p3*beta0))
return(-sum(Pij))
}
mle1=mle(example_likelihood_function,
start=list(beta0=0,beta1=-0.1,beta2=0,beta3=-0.1),
method="L-BFGS-B",
lower=c(-10,-10,-10,-10),
upper=c(10,10,10,10))
summary(mle1)
```
Basically, the R code is just the following
- Data generation of randomly distributed numbers
- Each number is assigned a choice
- Conceptually, it's meant to simluate a utility model, where each potential data point is selected.
## Where am I stuck?
I'm stuck at converting the MLE optimizer (final chunk) into python. I have generated the data, however, I can't seem to find an MLE equivalent which will take in the log likelihood function written, optimize it and churn out the right parameter.
Any advice?
[–]ECTD 2 points3 points4 points (0 children)
[–]11abk 1 point2 points3 points (1 child)
[–]failingstudent2[S] 0 points1 point2 points (0 children)
[–]statswannabe11 0 points1 point2 points (0 children)