spedygiorgio / markovchain

Easy Handling Discrete Time Markov Chains
https://spedygiorgio.github.io/markovchain/
Other
105 stars 39 forks source link

add markov chain logLikelihood slot in estimates #13

Closed spedygiorgio closed 9 years ago

spedygiorgio commented 9 years ago

It would be useful to add a logLikelihood slot to the markovchainFit output list. The likelihood should be calculated by a hidden function receiving the original data and the estimated transition matrix, then retuned to the calling function. See http://stats.stackexchange.com/questions/47685/calculating-log-likelihood-for-given-mle-markov-chains for a formula

tskang commented 9 years ago

Added the logLikelihood slot and the hidden function.

spedygiorgio commented 9 years ago

great job thanks. Remember:

  1. to amend the Rd (helper documentation) on the output of the slot
  2. to amend the main vignette (an intro) with some formulas.
spedygiorgio commented 9 years ago

Sorry I am not sure the loglikelihood is correct.... for(int i = 0; i < seq.size() - 1; i ++) { for(int r = 0; r < rnames.size(); r ++) { if(rnames[r] == seq[i]) from = r; if(rnames[r] == seq[i + 1]) to = r; }
out += log(transMatr(from, to));

you are only taking into account the names but you also might need to take into account the actual transition. You migh need to run createSequenceMatrix on the seqw I suppose. Have a look to Fitting.R of clickstream package: logLikelihood=sum(x$frequency*log(x$probability))

tskang commented 9 years ago

It's the same as I basically iterate over all the transitions. Also, it is cheaper than creating the frequency matrix from the computation perspective.

tskang commented 9 years ago

I can change it to the clickstream way if you want.

spedygiorgio commented 9 years ago

If your approach is more efficient, we will keep your! Thansk again!