Hi, I had a java implementation for your code, it may help others. and I compare the java-implementation and the r-implementation, bias is very small.