def fold_ancestral(F):
    """
    Takes an unfolded two locus frequency spectrum and folds it (assume don't konw ancestral state)
    """
    ns = len(F[:, (0), (0)]) - 1
    for ii in range(ns + 1):
        for jj in range(ns + 1):
            for kk in range(ns + 1):
                if F.mask[ii, jj, kk]:
                    continue
                p = ii + jj
                q = ii + kk
                if p > ns / 2 and q > ns / 2:
                    F[ns - ii - jj - kk, kk, jj] += F[ii, jj, kk]
                    F.mask[ii, jj, kk] = True
                elif p > ns / 2:
                    F[kk, ns - ii - jj - kk, ii] += F[ii, jj, kk]
                    F.mask[ii, jj, kk] = True
                elif q > ns / 2:
                    F[jj, ii, ns - ii - jj - kk] += F[ii, jj, kk]
                    F.mask[ii, jj, kk] = True
    return F
