Published
Edited
Oct 7, 2019
Importers
Insert cell
Insert cell
viewof mccTree = html`<input type=file accept="text/*">`
Insert cell
tree =Files.text(mccTree).then(t=>figtreeModule.Tree.parseNexus(t)[0])
Insert cell
Insert cell
Insert cell
population =startingPopulation(1000)
Insert cell
Insert cell
log ={
population;
return[];
}
Insert cell
function* nextGeneration(population,log){
const best = mostFitindividual(population)
const bf = best.fitness;
const newPopulation = [best,...compose(mutator(0.15,0.1),replicate,sampleWeightedArray(population.length-1)(population.map(i=>i.fitness)))(population)]
const logPoint = getLog(newPopulation);
yield logPoint;
log.push(logPoint)
if(ess(log.map(l=>l.meanCrossOvers))>1.5||log.length<100){
console.log(ess(log.map(l=>l.meanFitness)));
yield* nextGeneration(newPopulation,log);
}
}
Insert cell
mutator=(mutationRate,recombinationRate)=>(population)=>{
return population.map(i=>{
if(Math.random()<mutationRate){
i.mutate()
}
if(Math.random()<recombinationRate){
i = recombine(i,sampleWeightedArray(1)(population.map(i=>i.fitness))(population)[0])
}
return i
})
}
Insert cell
function ess(samples){
let auto =1;
let lag = 1;
let sum = 0;
while(auto>0.05&&lag<samples.length/2){
auto= autocorrelation(lag)(samples)
lag+=1;
sum+= auto;
}
return samples.length/(1+2*sum);
}
Insert cell
autocorrelation=(k)=>(samples)=>{
const samplePairs = []
for(let i=0;i<samples.length-k;i++){
samplePairs.push({x0:samples[i],x1:samples[i+k]})
}
return Math.abs(correlation(samplePairs));
}
Insert cell
function correlation(samplePairs){
const firstMean = d3.mean(samplePairs,d=>d.x0);
const secondMean = d3.mean(samplePairs,d=>d.x1);
const firstStd = d3.deviation(samplePairs,d=>d.x0);
const secondstd = d3.deviation(samplePairs,d=>d.x1);
const standardizedPairs = samplePairs.map(s=>({x0:(s.x0-firstMean)/firstStd,x1:(s.x1-secondMean)/secondstd}));
return standardizedPairs.map(s=>s.x0*s.x1).reduce((acc,el)=>acc+el)/(samplePairs.length-1)
}
Insert cell
function replicate(population){
return population.map(i=>i.replicate());
}
Insert cell
Insert cell
class Individual {
constructor(genome){
this._genome= genome;
this.transitions = transitions;
this.bottom = transitions.map(t=>t.weight*transitions.length).reduce((acc,curr)=>acc+curr,0)*10;
}
replicate(){
return new Individual(this.genome);
}
get fitness(){
const crossovers = this.crossOvers
return this.bottom - 10*this.crossOvers
}
get crossOvers(){
let crossOvers = 0;
for(const transition of this.transitions){
crossOvers+=Math.abs(this._genome.indexOf(transition.source)- this._genome.indexOf(transition.target))*transition.weight
}
return crossOvers;
}
get genome(){
return this._genome.slice();
}
mutate(){
//pick two locations at random and switch
const swaps = [Math.round(Math.random()*(this._genome.length-1)),Math.round(Math.random()*(this._genome.length-1))].sort((a,b)=>a-b);
if(swaps[0]!==swaps[1]){
this._genome = [...this._genome.slice(0,swaps[0]),this._genome[swaps[1]],...this._genome.slice(swaps[0]+1,swaps[1]),this._genome[swaps[0]],...this._genome.slice(swaps[1]+1)]
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
compose = (...fns) => input => fns.reduceRight((mem, fn) => fn(mem), input)
Insert cell
Insert cell
figtreeModule=import("https://cdn.jsdelivr.net/gh/rambaut/figtree.js@total-refactor/dist/figtree.esm.js")
Insert cell
d3 = require("d3")
Insert cell
import {errorLog} from "@mootari/tracking-and-displaying-errors"
Insert cell
errorLog()
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more