library(gtools)
library(Rmpfr)
library(OBsMD)
precBits=1000000
K=9
n=1000
p=mpfr(c(K:1),precBits = precBits);p=mpfr(p,precBits = precBits)/mpfr(sum(p),precBits = precBits)
#p is a vector of probabilities
zu.mpfr <- function(freq, u, precBits){
sum=mpfr(0,precBits = precBits)
n=sum(freq)
for (i in (1:length(freq))){
prod=mpfr(1,precBits = precBits)
for (j in (0:(u-1))){
prod=prod*(mpfr(freq[i]-j,precBits = precBits))/mpfr(n-j,precBits=precBits)
}
sum=sum+prod
}
return(sum)
}
etastar<-rep(mpfr(0,precBits = precBits),K-1)
etastar=c(etastar)
for (i in 1:(K-1)){
etastar[i]=sum(p^mpfr(i+1,precBits))
}
In the above codes, p
is a vector of probabilities. zu.mpfr
is \$z_u =\sum_k \prod_{j=0}^{u-1}\frac{Y_k-j}{n-j},\$ where \$u\$ can be any positive integer, and \$Y_k\$ is the observed frequency in a random sample for catagory \$k\$. The inputs for zu.mpfr
are the sample frequency counts, \$u\$, and precBits
(the preset accuracy). For the frequency, for example, if a sample is \$\{1,1,2,3,3,3,4\}\$, then the corresponding frequencies are \$\{2,1,3,1\}\$, since observation "1" appears twice, "2" appears once, "3" appears three times, and "4" appears once. The frequency is directly generated by rmultinom
in the next paragraph of codes. etastar
is a vector of length \$K-1\$. And the \$i\$-th element, \$\eta^{*}(i)\$, is \$\sum_k p_k ^{i+1}.\$ As a summary, zu.mpfr
is a value calculated from a random sample, and etastar
is a vector calculated from the probability distribution p
.
TheIn the latter codes, I used rmultinom
to get the sample
, which is the frequency counts from a random sample that generated by the probability distribution p
. Then I calculated zstar
, which is a vector of length \$K-1\$, with the \$i\$-th element zu.mpfr(sample, i+1, precBits)
. Here comes my problem. It takes too long to calculate the vector zstar
. By timing part needsit, "Time difference of 6.207007 secs" on my end. I am wondering if I can make it better. I need it faster because I need to run the simulation with thousands of iterations. I am not sure if the slowness is caused by mpfr
but I do need it to enforce accuracy.
Please let me know if any further clarification is needed.
Thank you ALL!