Datasets:
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
Tags:
mathematics
information-theory
renyi-entropy
shannon-entropy
hidden-markov-models
bernoulli-convolutions
Download veloryn/source.py from PureOne/VELORYN-Boundary-Kernel-Entropy: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/datasets/PureOne/VELORYN-Boundary-Kernel-Entropy/resolve/main/veloryn/source.py
- Command line
-
hf download hf://datasets/PureOne/VELORYN-Boundary-Kernel-Entropy/veloryn/source.py
-
curl -L -o source.py https://huggingface.co/datasets/PureOne/VELORYN-Boundary-Kernel-Entropy/resolve/main/veloryn/source.py
4.7 kB
| """Rational labelled hidden-state sources with bounded integer branch clocks.""" | |
| from dataclasses import dataclass | |
| from fractions import Fraction as Q | |
| from math import lcm | |
| from .arithmetic import Field, positive_int | |
| class Edge: | |
| start: int | |
| end: int | |
| digit: tuple | |
| clock: int | |
| probability: Q | |
| class Source: | |
| def __init__(self,field:Field,states:int,edges,initial): | |
| self.field=field | |
| self.states=positive_int(states,"states") | |
| converted=[] | |
| for e in edges: | |
| if not isinstance(e,Edge): | |
| e=Edge(*e) | |
| if (isinstance(e.start,bool) or isinstance(e.end,bool) or | |
| not isinstance(e.start,int) or not isinstance(e.end,int) or | |
| not 0<=e.start<states or not 0<=e.end<states): | |
| raise ValueError("edge endpoint outside hidden-state range") | |
| positive_int(e.clock,"clock") | |
| p=Q(e.probability) | |
| if p<0 or p>1: | |
| raise ValueError("edge probabilities must lie in [0,1]") | |
| if p: | |
| converted.append(Edge(e.start,e.end,field.digit(e.digit),e.clock,p)) | |
| self.edges=tuple(converted) | |
| self.outgoing=tuple(tuple(i for i,e in enumerate(self.edges) if e.start==s) | |
| for s in range(states)) | |
| if any(sum((self.edges[i].probability for i in ids),Q(0))!=1 for ids in self.outgoing): | |
| raise ValueError("outgoing edge probabilities must sum to one at every state") | |
| self.initial=tuple(map(Q,initial)) | |
| if len(self.initial)!=states or min(self.initial)<=0 or sum(self.initial)!=1: | |
| raise ValueError("initial probabilities must be positive and sum to one; remove inactive states") | |
| self.transition=tuple(tuple(sum((e.probability for e in self.edges if e.start==i and e.end==j),Q(0)) | |
| for j in range(states)) for i in range(states)) | |
| after=tuple(sum((self.initial[i]*self.transition[i][j] for i in range(states)),Q(0)) | |
| for j in range(states)) | |
| self.stationary=(after==self.initial) | |
| self.digits=tuple(set(e.digit for e in self.edges)) | |
| self.clocks=tuple(e.clock for e in self.edges) | |
| self.kmin,self.kmax=min(self.clocks),max(self.clocks) | |
| self.denominator=lcm(*(e.probability.denominator for e in self.edges)) | |
| self.integer_weights=tuple(int(e.probability*self.denominator) for e in self.edges) | |
| def split_bound(self,n): | |
| return self.field.split_bound(n,self.digits,self.clocks) | |
| def slot_successors(self,slot,completed,n): | |
| """(position,remaining): a ready vertex, or a busy edge countdown.""" | |
| position,remaining=slot | |
| if remaining: | |
| e=self.edges[position] | |
| if remaining==1: | |
| return (((e.end,0),completed+1,e.digit,Q(1)),) | |
| return (((position,remaining-1),completed,self.field.zero,Q(1)),) | |
| if completed>=n: | |
| return () | |
| ans=[] | |
| for index in self.outgoing[position]: | |
| e=self.edges[index] | |
| if e.clock==1: | |
| ans.append(((e.end,0),completed+1,e.digit,e.probability)) | |
| else: | |
| ans.append(((index,e.clock-1),completed,self.field.zero,e.probability)) | |
| return tuple(ans) | |
| def sample_path(self,n,rng): | |
| positive_int(n,"depth") | |
| state=categorical(self.initial,rng) | |
| start=state | |
| path=[] | |
| for _ in range(n): | |
| ids=self.outgoing[state] | |
| j=categorical(tuple(self.edges[i].probability for i in ids),rng) | |
| index=ids[j] | |
| path.append(index) | |
| state=self.edges[index].end | |
| return start,tuple(path),state | |
| def path_map(self,path): | |
| u=self.field.zero | |
| clock=0 | |
| units=[] | |
| for i in path: | |
| if isinstance(i,bool) or not isinstance(i,int) or not 0<=i<len(self.edges): | |
| raise ValueError("invalid edge index") | |
| e=self.edges[i] | |
| u=self.field.jump(u,e.clock,e.digit) | |
| clock+=e.clock | |
| units.extend([self.field.zero]*(e.clock-1)) | |
| units.append(e.digit) | |
| return (clock,u),tuple(units) | |
| def categorical(probabilities,rng): | |
| denominator=lcm(*(p.denominator for p in probabilities)) | |
| r=rng.randrange(denominator) | |
| if isinstance(r,bool) or not isinstance(r,int) or not 0<=r<denominator: | |
| raise ValueError("random integer oracle violated its contract") | |
| total=0 | |
| for i,p in enumerate(probabilities): | |
| total+=int(p*denominator) | |
| if r<total: | |
| return i | |
| raise ArithmeticError("invalid categorical source") | |