wordhit

版本 1.1.0.0 (11.3 KB) 作者: Ben Petschel
determines probabilities and expected times to produce words in a random character stream
200.0 次下载
更新时间 2011/7/22

查看许可证

Consider the following problems:

- is HH or TH more likely to appear first in a series of coin flips?
- how long does it take on average for a monkey to type out the phrase "to be or not to be"?

WORDHIT solves the general problem for any list of reasonably-sized words. For example,

wordhit('HH','TH') % returns [1/4,3/4]
[P,T]=wordhit('HH','TH') % T = [0.5,2.5]
T./P % conditional hitting times [2,10/3]
sum(T) % total hitting time [3]

% optional symbolic probability values (requires Symbolic Toolbox)
[~,t]=wordhit(repmat('H',1,5),'',sym('p')) % (1+p+p^2+p^3+p^4)/p^5

The algorithm works by determining the transition matrix of the Markov chain where each state represents a given combination of matches to the first few elements of each word, with the absorbing states being where a full word is matched. This allows the hitting probabilities and hitting times to be expressed as limits of matrix powers.

The values can be challenging to compute even for some relatively small examples, so several methods have been implemented.

For some examples the results are accurate to values close to REALMIN, e.g.

log2(wordhit(repmat('H',1,1000),'TH')) % get [-1000,0]

For other examples, a modified Kramer's method is more accurate due to poor conditioning of the transition matrix:

[~,t]=wordhit(repmat('H',1,500)) % accuracy warning
[~,t]=wordhit(repmat('H',1,500),'','kramer') % get 2^501 as expected
[~,t]=wordhit('to be or not to be','','kramer') % 5.815e25
% adjust for unequal key probabilites:
[~,t]=wordhit('to be or not to be','',[32*ones(1,26),190]/2800,'kramer') % 1.225e31

See the help file for more examples and extensive documentation of the options, theory and methods.

Let me know if you find any bugs or interesting examples.

引用格式

Ben Petschel (2025). wordhit (https://www.mathworks.com/matlabcentral/fileexchange/31748-wordhit), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2010a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Systems of Nonlinear Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.1.0.0

(previously htseqprob.m) - added calculation of hitting times; support for alphabets of size > 2; symbolic inputs; additional methods

1.0.0.0