Code for permutation analysis