Created
January 1, 2011 20:02
-
-
Save Protonk/761958 to your computer and use it in GitHub Desktop.
Differences in ordinal ranking between absolute deviation and squared deviation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Very simple fitness function. Works sensibly only on 1 dimensional numeric vectors, but that is | |
| #ok for this example | |
| fitness.hw<- function(tar.hw,meas.hw,type=c("abs","sqr")) { | |
| diff.hw<-tar.hw-meas.hw | |
| if (type=="abs") { | |
| rowSums(abs(diff.hw)) | |
| } | |
| else if (type=="sqr") { | |
| rowSums(diff.hw^2) | |
| } | |
| } | |
| #Since we are looking at the large sample properties, the name "generation" is a misnomer, | |
| #all the generations are random numbers from 0-255 | |
| generation<- array(0,c(400,11,500)) | |
| pop.random<-function() {t(replicate(dim(generation)[1], round(runif(11,0,255))))} | |
| for (i in 1:500) {generation[,,i]<-pop.random()} | |
| target<-utf8ToInt("Hello World") | |
| target.mat<- t(matrix(target,nrow=11,ncol=dim(generation)[1])) | |
| abs.fitness<- matrix(0,400,500) | |
| sqr.fitness<- matrix(0,400,500) | |
| diff.rank<- matrix(0,500,400) | |
| for (i in 1:500) { | |
| abs.fitness[,i]<-fitness.hw(target.mat,generation[,,i],type="abs") | |
| sqr.fitness[,i]<-fitness.hw(target.mat,generation[,,i],type="sqr") | |
| #This is the only (relatively) tricky part. The first bit returns the index of sorted outcomes for the squared deviation | |
| #method. The second returns the index of sorted outcomes from the absolute difference method. If ordering is preserved, | |
| #the difference will be zero. If ordering is preserved in expectation, the average difference will be zero. | |
| diff.rank[i,]<- sort(sqr.fitness[,i],index.return=TRUE)$ix-sort(abs.fitness[,i],index.return=TRUE)$ix | |
| } | |
| #scatterplot and smoothed line of differences | |
| scatter.smooth(x=1:length(colMeans(diff.rank)), y=colMeans(diff.rank),main="Smoothed Difference of Rank Ordering",ylab="Rank Difference",xlab="Absolute Value Rank Fitness",pch=20) | |
| #indeed the mean of each order for 500 samples is VERY close to zero. | |
| mean(colMeans(diff.rank)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment