Rand and Jaccard index and the confusion matrix

Input: two partitions (Π^((1))), (Π^((2))) of the set 𝒜 ⊂ (ℝ^n); 
Output: Rand index R, Jaccard index J, confusion matrix S, CPU-time;
Submodules: none needed

Rand[PI1_,PI2_]:=Module[{m=Length[Flatten[PI1]],S,S1=Table[0,{i,Length[PI1]}],S2=Table[0,{j,Length[PI2]}],a,b,c,d},
time=Timing[
(* Confusion matrix *)
S=Table[0,{r,Length[PI1]},{r,Length[PI2]}];
Do[
Do[
S[[r,s]]=Length[Intersection[PI1[[r]],PI2[[s]]]]
,{r,Length[PI1]}]
,{s,Length[PI2]}];
(* Numbers a, b, c, d *)
TT=Sum[S[[i,j]]^2,{i,Length[PI1]},{j,Length[PI2]}];
Do[
S1[[i]]=Sum[S[[i,j]],{j,Length[PI2]}]
,{i,Length[PI1]}];
Do[
S2[[j]]=Sum[S[[i,j]],{i,Length[PI1]}]
,{j,Length[PI2]}];
a=.5(TT-m);
b=.5(Sum[S1[[i]]^2,{i,Length[PI1]}]-TT);
c=.5(Sum[S2[[j]]^2,{j,Length[PI2]}]-TT);
d=.5(TT+m^2-Sum[S1[[i]]^2,{i,Length[PI1]}]-Sum[S2[[j]]^2,{j,Length[PI2]}]);
];
{(a+d)/Binomial[m,2],a/(a+b+c),S,time[[1]]}
]