subroutine calc_rdmbiasfac(p_spawn_rdmfac, p_gen, SignCurr, RDMBiasFacCurr)
real(dp), intent(in) :: p_gen
real(dp), intent(in) :: SignCurr
real(dp), intent(out) :: RDMBiasFacCurr
real(dp), intent(in) :: p_spawn_rdmfac
real(dp) :: p_notlist_rdmfac, p_spawn, p_not_spawn, p_max_walktospawn
character(len=*), parameter :: t_r = 'calc_rdmbiasfac'
! We eventually turn this real bias factor into an integer to be passed
! around with the spawned children and their parents - this only works
! with 64 bit at the moment.
if (n_int == 4) call stop_all(t_r, 'The bias factor currently does not work with 32 bit integers.')
! Otherwise calculate the 'sign' of Di we are eventually going to add
! in as Di.Dj. Because we only add in Di.Dj when we successfully spawn
! from Di.Dj, we need to unbias (scale up) Di by the probability of this
! happening. We need the probability that the determinant i, with
! population n_i, will spawn on j. We only consider one instance of a
! pair Di,Dj, so just want the probability of any of the n_i walkers
! spawning at least once on Dj.
! P_successful_spawn(j | i)[n_i] = 1 - P_not_spawn(j | i)[n_i]
! P_not_spawn(j | i )[n_i] is the probability of none of the n_i walkers spawning on j from i.
! This requires either not generating j, or generating j and not succesfully spawning, n_i times.
! P_not_spawn(j | i )[n_i] = [(1 - P_gen(j | i)) + ( P_gen( j | i ) * (1 - P_spawn(j | i))]^n_i
p_notlist_rdmfac = (1.0_dp - p_gen) + (p_gen * (1.0_dp - p_spawn_rdmfac))
! The bias fac is now n_i / P_successful_spawn(j | i)[n_i].
if (abs(real(int(SignCurr), dp) - SignCurr) > 1.0e-12_dp) then
! There's a non-integer population on this determinant. We need to
! consider both possibilities - whether we attempted to spawn
! int(SignCurr) times or int(SignCurr)+1 times.
p_max_walktospawn = abs(SignCurr - real(int(SignCurr), dp))
p_not_spawn = (1.0_dp - p_max_walktospawn) * (p_notlist_rdmfac**abs(int(SignCurr))) + &
p_max_walktospawn * (p_notlist_rdmfac**(abs(int(SignCurr)) + 1))
else
p_not_spawn = p_notlist_rdmfac**(abs(SignCurr))
end if
p_spawn = abs(1.0_dp - p_not_spawn)
! Always use instantaneous signs for stochastically sampled off-diag
! elements (see CMO thesis).
RDMBiasFacCurr = SignCurr / p_spawn
end subroutine calc_rdmbiasfac