Datasets:
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
Tags:
mathematics
information-theory
renyi-entropy
shannon-entropy
hidden-markov-models
bernoulli-convolutions
Download veloryn/entropy.py from PureOne/VELORYN-Boundary-Kernel-Entropy: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/datasets/PureOne/VELORYN-Boundary-Kernel-Entropy/resolve/main/veloryn/entropy.py
- Command line
-
hf download hf://datasets/PureOne/VELORYN-Boundary-Kernel-Entropy/veloryn/entropy.py
-
curl -L -o entropy.py https://huggingface.co/datasets/PureOne/VELORYN-Boundary-Kernel-Entropy/resolve/main/veloryn/entropy.py
4.23 kB
| """Deterministic integer-order and randomized Shannon entropy-rate intervals.""" | |
| from fractions import Fraction as Q | |
| import random | |
| from .arithmetic import positive_int | |
| from .numerics import log_bounds, spectral_bounds, sqrt_upper | |
| from .compiler import collision_matrix, target_mass | |
| def renyi_interval(source,depth,order,relative_precision=Q(1,10**10),max_states=500000): | |
| matrix,diagnostics=collision_matrix(source,depth,order,max_states) | |
| root=spectral_bounds(matrix,relative_precision) | |
| a=log_bounds(root["lower"])[0] | |
| b=log_bounds(root["upper"])[1] | |
| divisor=depth*(order-1) | |
| spectral_lo,spectral_hi=-b/divisor,-a/divisor | |
| split=source.split_bound(depth) | |
| error=log_bounds(source.states*split)[1]/depth | |
| # Edge alphabet size bounds the number of n-edge histories. | |
| beta_upper=source.field.embeddings(128)[3].hi | |
| ceiling=min(log_bounds(len(source.edges))[1],source.kmax*log_bounds(beta_upper)[1]) | |
| lo=max(Q(0),spectral_lo-error) | |
| hi=min(ceiling,spectral_hi) | |
| if lo>hi: | |
| raise ArithmeticError("inconsistent certified bounds") | |
| return {"order":order,"depth":depth,"interval_nats_per_branch":(lo,hi), | |
| "width":hi-lo,"spectral_proxy_interval":(spectral_lo,spectral_hi), | |
| "split_bound":split,"boundary_fibre_log_error":error, | |
| "moment_matrix":matrix,"spectral_radius":root,"diagnostics":diagnostics, | |
| "probabilistic":False,"source_stationary":source.stationary} | |
| def shannon_interval(source,depth,samples,confidence=Q(99,100),seed=None, | |
| numerical_width=Q(1,10**8),max_states=500000,rng=None): | |
| positive_int(depth,"depth");positive_int(samples,"samples") | |
| if not source.stationary: | |
| raise ValueError("the Shannon finite-block bracket requires an exactly stationary source") | |
| confidence,numerical_width=Q(confidence),Q(numerical_width) | |
| if not 0<confidence<1 or numerical_width<=0: | |
| raise ValueError("invalid confidence or numerical width") | |
| if seed is not None and rng is not None: | |
| raise ValueError("supply a seed or a random oracle, not both") | |
| mode=("seeded pseudorandom demonstration" if seed is not None else | |
| "supplied random-integer oracle" if rng is not None else "SystemRandom sampling") | |
| rng=rng if rng is not None else random.Random(seed) if seed is not None else random.SystemRandom() | |
| minp=min(e.probability for e in source.edges) | |
| B=max(Q(1),log_bounds(1/minp)[1]) | |
| logtail=log_bounds(2/(1-confidence))[1] | |
| # Choose square-root precision from requested numerical width, not a fixed floor. | |
| bits=max(80,numerical_width.denominator.bit_length()-numerical_width.numerator.bit_length()+8) | |
| margin=sqrt_upper(B*B*logtail/(2*samples),bits) | |
| lower=upper=Q(0) | |
| widths=[] | |
| for _ in range(samples): | |
| start,path,end=source.sample_path(depth,rng) | |
| mass,diag=target_mass(source,path,max_states,start=start,end=end) | |
| if mass<=0: | |
| raise ArithmeticError("sampled endpoint has zero mass") | |
| lo,hi=log_bounds(1/mass,depth*numerical_width) | |
| lower+=lo/depth;upper+=hi/depth | |
| widths.append(diag["max_layer_states"]) | |
| lower/=samples;upper/=samples | |
| bias=log_bounds(source.states*source.split_bound(depth))[1]/depth | |
| beta_upper=source.field.embeddings(128)[3].hi | |
| ceiling=min(log_bounds(len(source.edges))[1],source.kmax*log_bounds(beta_upper)[1]) | |
| lo=max(Q(0),lower-margin-bias) | |
| hi=min(ceiling,upper+margin) | |
| if lo>hi: | |
| # Bad concentration events can in principle create an empty intersection. | |
| # Report failure rather than inventing a valid confidence interval. | |
| return {"failure":True,"reason":"empty confidence/bound intersection","sampling_mode":mode} | |
| return {"order":1,"depth":depth,"samples":samples,"confidence":confidence, | |
| "interval_nats_per_branch":(lo,hi),"width":hi-lo, | |
| "sampling_mode":mode,"independent_exact_draws_required":True, | |
| "sampled_quantity":"H(X_n,Z_n | Z_0)/n", | |
| "surprisal_range_upper":B,"hoeffding_margin":margin, | |
| "finite_block_bias_upper":bias,"mean_surprisal_interval":(lower,upper), | |
| "maximum_fibre_layer_states":max(widths),"probabilistic":True} | |