measure_pharma_024.py VOYNICH RESEARCH ARCHIVE | 16 SEPTEMBER 2026 2 } def folds(folios,k=5): fs=sorted(folios); rng=np.random.default_rng(SEED); rng.shuffle(fs) return {f:i%k for i,f in enumerate(fs)} BASE=['n_tokens','resolved_rate'] REP=['payload_repeat','max_payload_mult','frame_repeat','max_frame_mult','exact_repeat','max_exact_mult' ,'adjacent_frame','adjacent_payload','longest_frame_run'] SER=['variable_frame_types','variable_frame_mass','single_atom_step_rate','has_bounded_series'] def fit_eval(train,test,features): pipe=Pipeline([('imp',SimpleImputer()),('scale',StandardScaler()),('lr',LogisticRegression(C=.3,max_ iter=3000,class_weight='balanced',random_state=SEED))]) pipe.fit(train[features],train.y); p=pipe.predict_proba(test[features])[:,1] return p def fitted(train,features): pipe=Pipeline([('imp',SimpleImputer()),('scale',StandardScaler()),('lr',LogisticRegression(C=.3,max_ iter=3000,class_weight='balanced',random_state=SEED))]) return pipe.fit(train[features],train.y) def analyze(d,w): d=d[(d.witness==w)&d.section.isin(['Herbal','Pharma'])].copy(); d['y']=(d.section=='Pharma').astype(int) fmap=folds(d.folio.unique()); d['fold']=d.folio.map(fmap) rows=[]; preds=[] models={'BASE':BASE,'REPETITION':BASE+REP,'SERIES':BASE+SER,'FULL':BASE+REP+SER} for fold in range(5): tr=d[d.fold!=fold]; te=d[d.fold==fold] if te.y.nunique()<2: continue for name,feats in models.items(): p=fit_eval(tr,te,feats); ll=log_loss(te.y,p,labels=[0,1])/np.log(2); auc=roc_auc_score(te.y,p) rows.append({'witness':w,'fold':fold,'model':name,'n_test':len(te),'bits_logloss':ll,'auc':auc}) for ix,pp in zip(te.index,p): preds.append({'witness':w,'fold':fold,'model':name,'index':int(ix) ,'y':int(d.loc[ix,'y']),'p':float(pp)}) r=pd.DataFrame(rows); p=pd.DataFrame(preds) wide=r.pivot(index=['witness','fold'],columns='model',values='bits_logloss').reset_index() wide['gain_series_vs_base']=wide.BASE-wide.SERIES; wide['gain_full_vs_rep']=wide.REPETITION-wide.FUL L # Conditional test: shuffle series vectors among equal coarse line-length bins in each held-out set. observed=float(wide.gain_full_vs_rep.mean()); null=[] cached=[] for fold in sorted(d.fold.unique()): tr=d[d.fold!=fold]; te=d[d.fold==fold].copy() if te.y.nunique()<2:continue prep=fitted(tr,BASE+REP).predict_proba(te[BASE+REP])[:,1] llrep=log_loss(te.y,prep,labels=[0,1])/np.log(2) full_model=fitted(tr,BASE+REP+SER) bins=pd.cut(te.n_tokens,[-np.inf,3,5,8,12,np.inf],labels=False).to_numpy() cached.append((te,llrep,full_model,bins)) for b in range(1000): gains=[] for te0,llrep,full_model,bins in cached: te=te0.copy() for col in SER: vals=te[col].to_numpy().copy() for z in np.unique(bins): ii=np.where(bins==z)[0]; vals[ii]=vals[RNG.permutation(ii)] te[col]=vals pf=full_model.predict_proba(te[BASE+REP+SER])[:,1]; llf=log_loss(te.y,pf,labels=[0,1])/np.log(2) ; gains.append(llrep-llf) null.append(np.mean(gains)) pv=(1+sum(x>=observed for x in null))/(len(null)+1) return r,wide,p,{'witness':w,'observed_full_over_repetition_bits':observed,'permutation_p':pv,'null_ mean':float(np.mean(null)),'n_lines':len(d),'n_folios':d.folio.nunique(),'pharma_lines':int(d.y.sum())}