Skip to content

Integration

Functions for integrating population frequency spectra.

five_pops(phi, xx, T, nu1=1, nu2=1, nu3=1, nu4=1, nu5=1, m12=0, m13=0, m14=0, m15=0, m21=0, m23=0, m24=0, m25=0, m31=0, m32=0, m34=0, m35=0, m41=0, m42=0, m43=0, m45=0, m51=0, m52=0, m53=0, m54=0, gamma1=0, gamma2=0, gamma3=0, gamma4=0, gamma5=0, h1=0.5, h2=0.5, h3=0.5, h4=0.5, h5=0.5, theta0=1, initial_t=0, frozen1=False, frozen2=False, frozen3=False, frozen4=False, frozen5=False, deme_ids=None)

Integrate a 5-dimensional phi foward.

Note
  • nu's, gamma's, m's, and theta0 may be functions of time.

  • Generalizing to different grids in different phi directions is straightforward. The tricky part will be later doing the extrapolation correctly.

Parameters:

Name Type Description Default
phi array - like

Initial 5-dimensional phi

required
xx array - like

1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed that this grid is used in all dimensions.

required
T float

Time at which to halt integration

required
nu1 float

Population sizes

1
nu2 float

Population sizes

1
nu3 float

Population sizes

1
nu4 float

Population sizes

1
nu5 float

Population sizes

1
m12 float

Migration rates. Note that m12 is the rate into 1 from 2.

0
m13 float

Migration rates. Note that m13 is the rate into 1 from 3.

0
m14 float

Migration rates. Note that m14 is the rate into 1 from 4.

0
m15 float

Migration rates. Note that m15 is the rate into 1 from 5.

0
m21 float

Migration rates. Note that m21 is the rate into 2 from 1.

0
m23 float

Migration rates. Note that m23 is the rate into 2 from 3.

0
m24 float

Migration rates. Note that m24 is the rate into 2 from 4.

0
m25 float

Migration rates. Note that m25 is the rate into 2 from 5.

0
m31 float

Migration rates. Note that m31 is the rate into 3 from 1.

0
m32 float

Migration rates. Note that m32 is the rate into 3 from 2.

0
m34 float

Migration rates. Note that m34 is the rate into 3 from 4.

0
m35 float

Migration rates. Note that m35 is the rate into 3 from 5.

0
m41 float

Migration rates. Note that m41 is the rate into 4 from 1.

0
m42 float

Migration rates. Note that m42 is the rate into 4 from 2.

0
m43 float

Migration rates. Note that m43 is the rate into 4 from 3.

0
m45 float

Migration rates. Note that m45 is the rate into 4 from 5.

0
m51 float

Migration rates. Note that m51 is the rate into 5 from 1.

0
m52 float

Migration rates. Note that m52 is the rate into 5 from 2.

0
m53 float

Migration rates. Note that m53 is the rate into 5 from 3.

0
m54 float

Migration rates. Note that m54 is the rate into 5 from 4.

0
gamma1 float

Selection coefficients on all segregating alleles

0
gamma2 float

Selection coefficients on all segregating alleles

0
gamma3 float

Selection coefficients on all segregating alleles

0
gamma4 float

Selection coefficients on all segregating alleles

0
gamma5 float

Selection coefficients on all segregating alleles

0
h1 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h2 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h3 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h4 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h5 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
theta0 float

Propotional to ancestral size. Typically constant.

1
initial_t float

Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.)

0
frozen1 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen2 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen3 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen4 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen5 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
deme_ids list[str])

sequence of strings representing the names of demes

None
Source code in dadi/Integration.py
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
def five_pops(phi, xx, T, nu1=1, nu2=1, nu3=1, nu4=1, nu5=1,
              m12=0, m13=0, m14=0, m15=0, m21=0, m23=0, m24=0, m25=0,
              m31=0, m32=0, m34=0, m35=0, m41=0, m42=0, m43=0, m45=0,
              m51=0, m52=0, m53=0, m54=0, 
              gamma1=0, gamma2=0, gamma3=0, gamma4=0, gamma5=0,
              h1=0.5, h2=0.5, h3=0.5, h4=0.5, h5=0.5,
              theta0=1, initial_t=0, frozen1=False, frozen2=False,
              frozen3=False, frozen4=False, frozen5=False, deme_ids=None):
    """
    Integrate a 5-dimensional phi foward.

    Note:
        - nu's, gamma's, m's, and theta0 may be functions of time.

        - Generalizing to different grids in different phi directions is
            straightforward. The tricky part will be later doing the extrapolation
            correctly.

    Args:
        phi (array-like): Initial 5-dimensional phi
        xx (array-like): 1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed
            that this grid is used in all dimensions.
        T (float): Time at which to halt integration
        nu1 (float): Population sizes
        nu2 (float): Population sizes
        nu3 (float): Population sizes
        nu4 (float): Population sizes
        nu5 (float): Population sizes
        m12 (float): Migration rates. Note that m12 is the rate 
             *into 1 from 2*.
        m13 (float): Migration rates. Note that m13 is the rate 
             *into 1 from 3*.
        m14 (float): Migration rates. Note that m14 is the rate
             *into 1 from 4*.
        m15 (float): Migration rates. Note that m15 is the rate
             *into 1 from 5*.
        m21 (float): Migration rates. Note that m21 is the rate 
             *into 2 from 1*.
        m23 (float): Migration rates. Note that m23 is the rate 
             *into 2 from 3*.
        m24 (float): Migration rates. Note that m24 is the rate
             *into 2 from 4*.
        m25 (float): Migration rates. Note that m25 is the rate
             *into 2 from 5*.
        m31 (float): Migration rates. Note that m31 is the rate 
             *into 3 from 1*.
        m32 (float): Migration rates. Note that m32 is the rate 
             *into 3 from 2*.
        m34 (float): Migration rates. Note that m34 is the rate
             *into 3 from 4*.
        m35 (float): Migration rates. Note that m35 is the rate
             *into 3 from 5*.
        m41 (float): Migration rates. Note that m41 is the rate
             *into 4 from 1*.
        m42 (float): Migration rates. Note that m42 is the rate
             *into 4 from 2*.
        m43 (float): Migration rates. Note that m43 is the rate
             *into 4 from 3*.
        m45 (float): Migration rates. Note that m45 is the rate
             *into 4 from 5*.
        m51 (float): Migration rates. Note that m51 is the rate
             *into 5 from 1*.
        m52 (float): Migration rates. Note that m52 is the rate
             *into 5 from 2*.
        m53 (float): Migration rates. Note that m53 is the rate
             *into 5 from 3*.
        m54 (float): Migration rates. Note that m54 is the rate
             *into 5 from 4*.
        gamma1 (float): Selection coefficients on *all* segregating alleles
        gamma2 (float): Selection coefficients on *all* segregating alleles
        gamma3 (float): Selection coefficients on *all* segregating alleles
        gamma4 (float): Selection coefficients on *all* segregating alleles
        gamma5 (float): Selection coefficients on *all* segregating alleles
        h1 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h2 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h3 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h4 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h5 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        theta0 (float): Propotional to ancestral size. Typically constant.
        initial_t (float): Time at which to start integration. (Note that this only matters
                if one of the demographic parameters is a function of time.)
        frozen1 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen2 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen3 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen4 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen5 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        deme_ids (list[str])): sequence of strings representing the names of demes
    """
    if T - initial_t == 0:
        return phi
    elif T - initial_t < 0:
        raise ValueError('Final integration time T (%f) is less than '
                         'intial_time (%f). Integration cannot be run '
                         'backwards.' % (T, initial_t))

    if (frozen1 and (m12 != 0 or m21 != 0 or m13 !=0 or m31 != 0 or m41 != 0 or m14 != 0 or m15 != 0 or m51 != 0))\
       or (frozen2 and (m12 != 0 or m21 != 0 or m23 != 0 or m32 != 0 or m24 != 0 or m42 != 0 or m25 != 0 or m52 != 0))\
       or (frozen3 and (m13 != 0 or m31 != 0 or m23 != 0 or m32 != 0 or m34 != 0 or m43 != 0 or m35 != 0 or m53 !=0))\
       or (frozen4 and (m14 != 0 or m41 != 0 or m24 != 0 or m42 != 0 or m34 != 0 or m43 != 0 or m45 != 0 or m54 != 0))\
       or (frozen5 and (m15 != 0 or m51 != 0 or m25 != 0 or m52 != 0 or m35 != 0 or m53 != 0 or m45 != 0 or m54 != 0)):
        raise ValueError('Population cannot be frozen and have non-zero '
                         'migration to or from it.')
    bb = aa = zz = yy = xx

    nu1_f, nu2_f = Misc.ensure_1arg_func(nu1), Misc.ensure_1arg_func(nu2)
    nu3_f, nu4_f = Misc.ensure_1arg_func(nu3), Misc.ensure_1arg_func(nu4)
    nu5_f = Misc.ensure_1arg_func(nu5)
    gamma1_f, gamma2_f = Misc.ensure_1arg_func(gamma1), Misc.ensure_1arg_func(gamma2)
    gamma3_f, gamma4_f = Misc.ensure_1arg_func(gamma3), Misc.ensure_1arg_func(gamma4)
    gamma5_f = Misc.ensure_1arg_func(gamma5)
    h1_f, h2_f = Misc.ensure_1arg_func(h1), Misc.ensure_1arg_func(h2)
    h3_f, h4_f = Misc.ensure_1arg_func(h3), Misc.ensure_1arg_func(h4)
    h5_f = Misc.ensure_1arg_func(h5)
    m12_f, m13_f, m14_f, m15_f = Misc.ensure_1arg_func(m12), Misc.ensure_1arg_func(m13), Misc.ensure_1arg_func(m14), Misc.ensure_1arg_func(m15)
    m21_f, m23_f, m24_f, m25_f = Misc.ensure_1arg_func(m21), Misc.ensure_1arg_func(m23), Misc.ensure_1arg_func(m24), Misc.ensure_1arg_func(m25)
    m31_f, m32_f, m34_f, m35_f = Misc.ensure_1arg_func(m31), Misc.ensure_1arg_func(m32), Misc.ensure_1arg_func(m34), Misc.ensure_1arg_func(m35)
    m41_f, m42_f, m43_f, m45_f = Misc.ensure_1arg_func(m41), Misc.ensure_1arg_func(m42), Misc.ensure_1arg_func(m43), Misc.ensure_1arg_func(m45)
    m51_f, m52_f, m53_f, m54_f = Misc.ensure_1arg_func(m51), Misc.ensure_1arg_func(m52), Misc.ensure_1arg_func(m53), Misc.ensure_1arg_func(m54)
    theta0_f = Misc.ensure_1arg_func(theta0)

    if cuda_enabled:
        import dadi.cuda
        phi = dadi.cuda.Integration._five_pops_temporal_params(phi, xx, T, initial_t, 
            nu1_f, nu2_f, nu3_f, nu4_f, nu5_f,
            m12_f, m13_f, m14_f, m15_f, m21_f, m23_f, m24_f, m25_f, m31_f, m32_f, m34_f, m35_f,
            m41_f, m42_f, m43_f, m45_f, m51_f, m52_f, m53_f, m54_f, 
            gamma1_f, gamma2_f, gamma3_f, gamma4_f, gamma5_f,
            h1_f, h2_f, h3_f, h4_f, h5_f, theta0_f, frozen1, frozen2, frozen3, frozen4, frozen5, deme_ids)
        return phi

    current_t = initial_t
    nu1, nu2, nu3, nu4, nu5 = nu1_f(current_t), nu2_f(current_t), nu3_f(current_t), nu4_f(current_t), nu5_f(current_t)
    gamma1, gamma2, gamma3, gamma4, gamma5 = gamma1_f(current_t), gamma2_f(current_t), gamma3_f(current_t), gamma4_f(current_t), gamma5_f(current_t)
    h1, h2, h3, h4, h5 = h1_f(current_t), h2_f(current_t), h3_f(current_t), h4_f(current_t), h5_f(current_t)
    m12, m13, m14, m15 = m12_f(current_t), m13_f(current_t), m14_f(current_t), m15_f(current_t)
    m21, m23, m24, m25 = m21_f(current_t), m23_f(current_t), m24_f(current_t), m25_f(current_t)
    m31, m32, m34, m35 = m31_f(current_t), m32_f(current_t), m34_f(current_t), m35_f(current_t)
    m41, m42, m43, m45 = m41_f(current_t), m42_f(current_t), m43_f(current_t), m45_f(current_t)
    m51, m52, m53, m54 = m51_f(current_t), m52_f(current_t), m53_f(current_t), m54_f(current_t)

    dx,dy,dz,da,db = numpy.diff(xx),numpy.diff(yy),numpy.diff(zz),numpy.diff(aa),numpy.diff(bb)
    demes_hist = [[0, [nu1,nu2,nu3,nu4,nu5], [m12,m13,m14,m15,m21,m23,m24,m25,m31,m32,m34,m35,m41,m42,m43,m45,m51,m52,m53,m54]]]
    while current_t < T:
        dt = min(_compute_dt(dx,nu1,[m12,m13,m14,m15],gamma1,h1),
                 _compute_dt(dy,nu2,[m21,m23,m24,m25],gamma2,h2),
                 _compute_dt(dz,nu3,[m31,m32,m34,m35],gamma3,h3),
                 _compute_dt(da,nu4,[m41,m42,m43,m45],gamma4,h4),
                 _compute_dt(db,nu5,[m51,m52,m53,m54],gamma5,h5))
        this_dt = min(dt, T - current_t)

        next_t = current_t + this_dt

        nu1, nu2, nu3, nu4, nu5 = nu1_f(next_t), nu2_f(next_t), nu3_f(next_t), nu4_f(next_t), nu5_f(next_t)
        gamma1, gamma2, gamma3, gamma4, gamma5 = gamma1_f(next_t), gamma2_f(next_t), gamma3_f(next_t), gamma4_f(next_t), gamma5_f(next_t)
        h1, h2, h3, h4, h5 = h1_f(next_t), h2_f(next_t), h3_f(next_t), h4_f(next_t), h5_f(next_t)
        m12, m13, m14, m15 = m12_f(next_t), m13_f(next_t), m14_f(next_t), m15_f(next_t)
        m21, m23, m24, m25 = m21_f(next_t), m23_f(next_t), m24_f(next_t), m25_f(next_t)
        m31, m32, m34, m35 = m31_f(next_t), m32_f(next_t), m34_f(next_t), m35_f(next_t)
        m41, m42, m43, m45 = m41_f(next_t), m42_f(next_t), m43_f(next_t), m45_f(next_t)
        m51, m52, m53, m54 = m51_f(next_t), m52_f(next_t), m53_f(next_t), m54_f(next_t)
        theta0 = theta0_f(next_t)

        demes_hist.append([next_t, [nu1,nu2,nu3,nu4,nu5], [m12,m13,m14,m15,m21,m23,m24,m25,m31,m32,m34,m35,m41,m42,m43,m45,m51,m52,m53,m54]])
        if numpy.any(numpy.less([T,nu1,nu2,nu3,nu4,nu5,m12,m13,m14,m15,m21,
                                 m23,m24,m25, m31,m32,m34,m35, m41,m42,m43,m45,
                                 m51,m52,m53,m54, theta0],
                                0)):
            raise ValueError('A time, population size, migration rate, or '
                             'theta0 is < 0. Has the model been mis-specified?')
        if numpy.any(numpy.equal([nu1,nu2,nu3,nu4,nu5], 0)):
            raise ValueError('A population size is 0. Has the model been '
                             'mis-specified?')

        _inject_mutations_5D(phi, this_dt, xx, yy, zz, aa, bb, theta0,
                             frozen1, frozen2, frozen3, frozen4, frozen5)
        if not frozen1:
            phi = int_c.implicit_5Dx(phi, xx, yy, zz, aa, bb, nu1, m12, m13, m14, m15,
                                     gamma1, h1, this_dt, use_delj_trick)
        if not frozen2:
            phi = int_c.implicit_5Dy(phi, xx, yy, zz, aa, bb, nu2, m21, m23, m24, m25,
                                     gamma2, h2, this_dt, use_delj_trick)
        if not frozen3:
            phi = int_c.implicit_5Dz(phi, xx, yy, zz, aa, bb, nu3, m31, m32, m34, m35,
                                     gamma3, h3, this_dt, use_delj_trick)
        if not frozen4:
            phi = int_c.implicit_5Da(phi, xx, yy, zz, aa, bb, nu4, m41, m42, m43, m45,
                                     gamma4, h4, this_dt, use_delj_trick)
        if not frozen5:
            phi = int_c.implicit_5Db(phi, xx, yy, zz, aa, bb, nu5, m51, m52, m53, m54,
                                     gamma5, h5, this_dt, use_delj_trick)

        current_t = next_t
    Demes.cache.append(Demes.IntegrationNonConst(history = demes_hist, deme_ids=deme_ids))
    return phi

four_pops(phi, xx, T, nu1=1, nu2=1, nu3=1, nu4=1, m12=0, m13=0, m14=0, m21=0, m23=0, m24=0, m31=0, m32=0, m34=0, m41=0, m42=0, m43=0, gamma1=0, gamma2=0, gamma3=0, gamma4=0, h1=0.5, h2=0.5, h3=0.5, h4=0.5, theta0=1, initial_t=0, frozen1=False, frozen2=False, frozen3=False, frozen4=False, deme_ids=None)

Integrate a 4-dimensional phi foward.

Note
  • nu's, gamma's, m's, and theta0 may be functions of time.

  • Generalizing to different grids in different phi directions is straightforward. The tricky part will be later doing the extrapolation correctly.

Parameters:

Name Type Description Default
phi array - like

Initial 4-dimensional phi

required
xx array - like

1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed that this grid is used in all dimensions.

required
T float

Time at which to halt integration

required
nu1 float

Population sizes

1
nu2 float

Population sizes

1
nu3 float

Population sizes

1
nu4 float

Population sizes

1
m12 float

Migration rates. Note that m12 is the rate into 1 from 2.

0
m13 float

Migration rates. Note that m13 is the rate into 1 from 3.

0
m14 float

Migration rates. Note that m14 is the rate into 1 from 4.

0
m21 float

Migration rates. Note that m21 is the rate into 2 from 1.

0
m23 float

Migration rates. Note that m23 is the rate into 2 from 3.

0
m24 float

Migration rates. Note that m24 is the rate into 2 from 4.

0
m31 float

Migration rates. Note that m31 is the rate into 3 from 1.

0
m32 float

Migration rates. Note that m32 is the rate into 3 from 2.

0
m34 float

Migration rates. Note that m34 is the rate into 3 from 4.

0
m41 float

Migration rates. Note that m41 is the rate into 4 from 1.

0
m42 float

Migration rates. Note that m42 is the rate into 4 from 2.

0
m43 float

Migration rates. Note that m43 is the rate into 4 from 3.

0
gamma1 float

Selection coefficients on all segregating alleles

0
gamma2 float

Selection coefficients on all segregating alleles

0
gamma3 float

Selection coefficients on all segregating alleles

0
gamma4 float

Selection coefficients on all segregating alleles

0
h1 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h2 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h3 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h4 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
theta0 float

Propotional to ancestral size. Typically constant.

1
initial_t float

Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.)

0
frozen1 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen2 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen3 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen4 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
deme_ids list[str]

sequence of strings representing the names of demes

None
Source code in dadi/Integration.py
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
def four_pops(phi, xx, T, nu1=1, nu2=1, nu3=1, nu4=1,
              m12=0, m13=0, m14=0, m21=0, m23=0, m24=0, 
              m31=0, m32=0, m34=0, m41=0, m42=0, m43=0,
              gamma1=0, gamma2=0, gamma3=0, gamma4=0, 
              h1=0.5, h2=0.5, h3=0.5, h4=0.5,
              theta0=1, initial_t=0, frozen1=False, frozen2=False,
              frozen3=False, frozen4=False, deme_ids=None):
    """
    Integrate a 4-dimensional phi foward.

    Note:
        - nu's, gamma's, m's, and theta0 may be functions of time.

        - Generalizing to different grids in different phi directions is
            straightforward. The tricky part will be later doing the extrapolation
            correctly.

    Args:
        phi (array-like): Initial 4-dimensional phi
        xx (array-like): 1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed
            that this grid is used in all dimensions.
        T (float): Time at which to halt integration
        nu1 (float): Population sizes
        nu2 (float): Population sizes
        nu3 (float): Population sizes
        nu4 (float): Population sizes
        m12 (float): Migration rates. Note that m12 is the rate 
             *into 1 from 2*.
        m13 (float): Migration rates. Note that m13 is the rate 
             *into 1 from 3*.
        m14 (float): Migration rates. Note that m14 is the rate
             *into 1 from 4*.
        m21 (float): Migration rates. Note that m21 is the rate 
             *into 2 from 1*.
        m23 (float): Migration rates. Note that m23 is the rate 
             *into 2 from 3*.
        m24 (float): Migration rates. Note that m24 is the rate
             *into 2 from 4*.
        m31 (float): Migration rates. Note that m31 is the rate 
             *into 3 from 1*.
        m32 (float): Migration rates. Note that m32 is the rate 
             *into 3 from 2*.
        m34 (float): Migration rates. Note that m34 is the rate
             *into 3 from 4*.
        m41 (float): Migration rates. Note that m41 is the rate
             *into 4 from 1*.
        m42 (float): Migration rates. Note that m42 is the rate
             *into 4 from 2*.
        m43 (float): Migration rates. Note that m43 is the rate
             *into 4 from 3*.
        gamma1 (float): Selection coefficients on *all* segregating alleles
        gamma2 (float): Selection coefficients on *all* segregating alleles
        gamma3 (float): Selection coefficients on *all* segregating alleles
        gamma4 (float): Selection coefficients on *all* segregating alleles
        h1 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h2 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h3 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h4 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        theta0 (float): Propotional to ancestral size. Typically constant.
        initial_t (float): Time at which to start integration. (Note that this only matters
                if one of the demographic parameters is a function of time.)
        frozen1 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen2 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen3 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen4 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        deme_ids (list[str]): sequence of strings representing the names of demes
    """
    if T - initial_t == 0:
        return phi
    elif T - initial_t < 0:
        raise ValueError('Final integration time T (%f) is less than '
                         'intial_time (%f). Integration cannot be run '
                         'backwards.' % (T, initial_t))


    if (frozen1 and (m12 != 0 or m21 != 0 or m13 !=0 or m31 != 0 or m41 != 0 or m14 != 0))\
       or (frozen2 and (m12 != 0 or m21 != 0 or m23 != 0 or m32 != 0 or m24 != 0 or m42 != 0))\
       or (frozen3 and (m13 != 0 or m31 != 0 or m23 !=0 or m32 != 0 or m34 != 0 or m43 != 0))\
       or (frozen4 and (m14 != 0 or m41 != 0 or m24 !=0 or m42 != 0 or m34 != 0 or m43 != 0)):
        raise ValueError('Population cannot be frozen and have non-zero '
                         'migration to or from it.')
    aa = zz = yy = xx

    nu1_f, nu2_f = Misc.ensure_1arg_func(nu1), Misc.ensure_1arg_func(nu2)
    nu3_f, nu4_f = Misc.ensure_1arg_func(nu3), Misc.ensure_1arg_func(nu4)
    gamma1_f, gamma2_f = Misc.ensure_1arg_func(gamma1), Misc.ensure_1arg_func(gamma2)
    gamma3_f, gamma4_f = Misc.ensure_1arg_func(gamma3), Misc.ensure_1arg_func(gamma4)
    h1_f, h2_f = Misc.ensure_1arg_func(h1), Misc.ensure_1arg_func(h2)
    h3_f, h4_f = Misc.ensure_1arg_func(h3), Misc.ensure_1arg_func(h4)
    m12_f, m13_f, m14_f = Misc.ensure_1arg_func(m12), Misc.ensure_1arg_func(m13), Misc.ensure_1arg_func(m14)
    m21_f, m23_f, m24_f = Misc.ensure_1arg_func(m21), Misc.ensure_1arg_func(m23), Misc.ensure_1arg_func(m24)
    m31_f, m32_f, m34_f = Misc.ensure_1arg_func(m31), Misc.ensure_1arg_func(m32), Misc.ensure_1arg_func(m34)
    m41_f, m42_f, m43_f = Misc.ensure_1arg_func(m41), Misc.ensure_1arg_func(m42), Misc.ensure_1arg_func(m43)
    theta0_f = Misc.ensure_1arg_func(theta0)

    if cuda_enabled:
        import dadi.cuda
        phi = dadi.cuda.Integration._four_pops_temporal_params(phi, xx, T, initial_t,
                nu1_f, nu2_f, nu3_f, nu4_f, m12_f, m13_f, m14_f, m21_f, m23_f, m24_f, m31_f, m32_f, m34_f,
                m41_f, m42_f, m43_f, gamma1_f, gamma2_f, gamma3_f, gamma4_f, h1_f, h2_f, h3_f, h4_f,
                theta0_f, frozen1, frozen2, frozen3, frozen4, deme_ids)
        return phi

    current_t = initial_t
    nu1, nu2, nu3, nu4 = nu1_f(current_t), nu2_f(current_t), nu3_f(current_t), nu4_f(current_t)
    gamma1, gamma2, gamma3, gamma4 = gamma1_f(current_t), gamma2_f(current_t), gamma3_f(current_t), gamma4_f(current_t)
    h1, h2, h3, h4 = h1_f(current_t), h2_f(current_t), h3_f(current_t), h4_f(current_t)
    m12, m13, m14 = m12_f(current_t), m13_f(current_t), m14_f(current_t)
    m21, m23, m24 = m21_f(current_t), m23_f(current_t), m24_f(current_t)
    m31, m32, m34 = m31_f(current_t), m32_f(current_t), m34_f(current_t)
    m41, m42, m43 = m41_f(current_t), m42_f(current_t), m43_f(current_t)

    dx,dy,dz,da = numpy.diff(xx),numpy.diff(yy),numpy.diff(zz),numpy.diff(aa)
    demes_hist = [[0, [nu1,nu2,nu3,nu4], [m12,m13,m14,m21,m23,m24,m31,m32,m34,m41,m42,m43]]]
    while current_t < T:
        dt = min(_compute_dt(dx,nu1,[m12,m13,m14],gamma1,h1),
                 _compute_dt(dy,nu2,[m21,m23,m24],gamma2,h2),
                 _compute_dt(dz,nu3,[m31,m32,m34],gamma3,h3),
                 _compute_dt(da,nu4,[m41,m42,m43],gamma4,h4))
        this_dt = min(dt, T - current_t)

        next_t = current_t + this_dt

        nu1, nu2, nu3, nu4 = nu1_f(next_t), nu2_f(next_t), nu3_f(next_t), nu4_f(next_t)
        gamma1, gamma2, gamma3, gamma4 = gamma1_f(next_t), gamma2_f(next_t), gamma3_f(next_t), gamma4_f(next_t)
        h1, h2, h3, h4 = h1_f(next_t), h2_f(next_t), h3_f(next_t), h4_f(next_t)
        m12, m13, m14 = m12_f(next_t), m13_f(next_t), m14_f(next_t)
        m21, m23, m24 = m21_f(next_t), m23_f(next_t), m24_f(next_t)
        m31, m32, m34 = m31_f(next_t), m32_f(next_t), m34_f(next_t)
        m41, m42, m43 = m41_f(next_t), m42_f(next_t), m43_f(next_t)
        theta0 = theta0_f(next_t)

        demes_hist.append([next_t, [nu1,nu2,nu3,nu4], [m12,m13,m14,m21,m23,m24,m31,m32,m34,m41,m42,m43]])
        if numpy.any(numpy.less([T,nu1,nu2,nu3,nu4,m12,m13,m14,m21,
                                 m23, m24, m31, m32, m34, m41, m42, m43, theta0],
                                0)):
            raise ValueError('A time, population size, migration rate, or '
                             'theta0 is < 0. Has the model been mis-specified?')
        if numpy.any(numpy.equal([nu1,nu2,nu3,nu4], 0)):
            raise ValueError('A population size is 0. Has the model been '
                             'mis-specified?')

        _inject_mutations_4D(phi, this_dt, xx, yy, zz, aa, theta0,
                             frozen1, frozen2, frozen3, frozen4)
        if not frozen1:
            phi = int_c.implicit_4Dx(phi, xx, yy, zz, aa, nu1, m12, m13, m14,
                                     gamma1, h1, this_dt, use_delj_trick)
        if not frozen2:
            phi = int_c.implicit_4Dy(phi, xx, yy, zz, aa, nu2, m21, m23, m24,
                                     gamma2, h2, this_dt, use_delj_trick)
        if not frozen3:
            phi = int_c.implicit_4Dz(phi, xx, yy, zz, aa, nu3, m31, m32, m34,
                                     gamma3, h3, this_dt, use_delj_trick)
        if not frozen4:
            phi = int_c.implicit_4Da(phi, xx, yy, zz, aa, nu4, m41, m42, m43,
                                     gamma4, h4, this_dt, use_delj_trick)

        current_t = next_t
    Demes.cache.append(Demes.IntegrationNonConst(history = demes_hist, deme_ids=deme_ids))
    return phi

one_pop(phi, xx, T, nu=1, gamma=0, h=0.5, theta0=1.0, initial_t=0, frozen=False, beta=1, deme_ids=None)

Integrate a 1-dimensional phi foward.

Parameters:

Name Type Description Default
phi array - like

Initial 1-dimensional phi

required
xx array - like

Grid upon (0,1) overwhich phi is defined. nu, gamma, and theta0 may be functions of time.

required
T float

Time at which to halt integration

required
nu float

Population size

1
gamma float

Selection coefficient on all segregating alleles

0
h float

Dominance coefficient. h = 0.5 corresponds to genic selection. Heterozygotes have fitness 1+2sh and homozygotes have fitness 1+2s.

0.5
theta0 float

Propotional to ancestral size. Typically constant.

1.0
beta float

Breeding ratio, beta=Nf/Nm.

1
initial_t float

Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.)

0
frozen bool

If True, population is 'frozen' so that it does not change. In the one_pop case, this is equivalent to not running the integration at all.

False
deme_ids list[str]

sequence of strings representing the names of demes

None
Source code in dadi/Integration.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def one_pop(phi, xx, T, nu=1, gamma=0, h=0.5, theta0=1.0, initial_t=0, 
            frozen=False, beta=1, deme_ids=None):
    """
    Integrate a 1-dimensional phi foward.

    Args:
        phi (array-like): Initial 1-dimensional phi
        xx (array-like): Grid upon (0,1) overwhich phi is defined.
            nu, gamma, and theta0 may be functions of time.
        T (float): Time at which to halt integration
        nu (float): Population size
        gamma (float): Selection coefficient on *all* segregating alleles
        h (float): Dominance coefficient. h = 0.5 corresponds to genic selection. 
                Heterozygotes have fitness 1+2sh and homozygotes have fitness 1+2s.
        theta0 (float): Propotional to ancestral size. Typically constant.
        beta (float): Breeding ratio, beta=Nf/Nm.
        initial_t (float): Time at which to start integration. (Note that this only matters
                if one of the demographic parameters is a function of time.)
        frozen (bool): If True, population is 'frozen' so that it does not change.
                In the one_pop case, this is equivalent to not running the
                integration at all.
        deme_ids (list[str]): sequence of strings representing the names of demes
    """
    phi = phi.copy()

    # For a one population integration, freezing means just not integrating.
    if frozen:
        return phi

    if T - initial_t == 0:
        return phi
    elif T - initial_t < 0:
        raise ValueError('Final integration time T (%f) is less than '
                         'intial_time (%f). Integration cannot be run '
                         'backwards.' % (T, initial_t))

    vars_to_check = (nu, gamma, h, theta0, beta)
    if numpy.all([numpy.isscalar(var) for var in vars_to_check]):
        Demes.cache.append(Demes.IntegrationConst(duration = T-initial_t, start_sizes = [nu], deme_ids=deme_ids))
        return _one_pop_const_params(phi, xx, T, nu, gamma, h, theta0, 
                                     initial_t, beta)

    nu_f = Misc.ensure_1arg_func(nu)
    gamma_f = Misc.ensure_1arg_func(gamma)
    h_f = Misc.ensure_1arg_func(h)
    theta0_f = Misc.ensure_1arg_func(theta0)
    beta_f = Misc.ensure_1arg_func(beta)

    current_t = initial_t
    nu, gamma, h = nu_f(current_t), gamma_f(current_t), h_f(current_t)
    beta = beta_f(current_t)
    dx = numpy.diff(xx)
    demes_hist = [[0, [nu], []]]
    while current_t < T:
        dt = _compute_dt(dx,nu,[0],gamma,h)
        this_dt = min(dt, T - current_t)

        # Because this is an implicit method, I need the *next* time's params.
        # So there's a little inconsistency here, in that I'm estimating dt
        # using the last timepoints nu,gamma,h.
        next_t = current_t + this_dt
        nu, gamma, h = nu_f(next_t), gamma_f(next_t), h_f(next_t)
        beta = beta_f(next_t)
        theta0 = theta0_f(next_t)
        demes_hist.append([next_t, [nu], []])

        if numpy.any(numpy.less([T,nu,theta0], 0)):
            raise ValueError('A time, population size, migration rate, or '
                             'theta0 is < 0. Has the model been mis-specified?')
        if numpy.any(numpy.equal([nu], 0)):
            raise ValueError('A population size is 0. Has the model been '
                             'mis-specified?')

        _inject_mutations_1D(phi, this_dt, xx, theta0)
        # Do each step in C, since it will be faster to compute the a,b,c
        # matrices there.
        phi = int_c.implicit_1Dx(phi, xx, nu, gamma, h, beta, this_dt, 
                                 use_delj_trick=use_delj_trick)
        current_t = next_t
    Demes.cache.append(Demes.IntegrationNonConst(history = demes_hist, deme_ids=deme_ids))
    return phi

one_pop_X(phi, xx, T, nu=1, gamma=0, h=0.5, beta=1, alpha=1, theta0=1.0, initial_t=0, frozen=False)

Integrate a 1-dimensional phi foward.

nu, gamma, and theta0 may be functions of time.

Parameters:

Name Type Description Default
phi array - like

Initial 1-dimensional phi

required
xx array - like

Grid upon (0,1) overwhich phi is defined.

required
T float

Time at which to halt integration

required
nu float

Population size

1
gamma float

Scaled selection coefficient on all segregating alleles

0
h float

Dominance coefficient. h = 0.5 corresponds to genic selection. Heterozygous females have fitness 1+2sh and homozygous females have fitness 1+2s. Male carriers have fitness 1+2s.

0.5
theta0 float

Propotional to ancestral size. Typically constant.

1.0
beta float

Breeding ratio, beta=Nf/Nm.

1
alpha float

Male to female mutation rate ratio, beta = mu_m / mu_f.

1
initial_t float

Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.)

0
frozen bool

If True, population is 'frozen' so that it does not change. In the one_pop case, this is equivalent to not running the integration at all.

False
Source code in dadi/Integration.py
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
def one_pop_X(phi, xx, T, nu=1, gamma=0, h=0.5, beta=1, alpha=1, theta0=1.0, 
              initial_t=0, frozen=False):
    """
    Integrate a 1-dimensional phi foward.

    nu, gamma, and theta0 may be functions of time.

    Args:
        phi (array-like): Initial 1-dimensional phi
        xx (array-like): Grid upon (0,1) overwhich phi is defined.
        T (float): Time at which to halt integration
        nu (float): Population size
        gamma (float): Scaled selection coefficient on *all* segregating alleles
        h (float): Dominance coefficient. h = 0.5 corresponds to genic selection. 
                   Heterozygous females have fitness 1+2sh and homozygous females have
                   fitness 1+2s. Male carriers have fitness 1+2s.
        theta0 (float): Propotional to ancestral size. Typically constant.
        beta (float): Breeding ratio, beta=Nf/Nm.
        alpha (float): Male to female mutation rate ratio, beta = mu_m / mu_f.
        initial_t (float): Time at which to start integration. (Note that this only matters
                if one of the demographic parameters is a function of time.)
        frozen (bool): If True, population is 'frozen' so that it does not change.
                In the one_pop case, this is equivalent to not running the
                integration at all.
    """
    phi = phi.copy()

    # For a one population integration, freezing means just not integrating.
    if frozen:
        return phi

    if T - initial_t == 0:
        return phi
    elif T - initial_t < 0:
        raise ValueError('Final integration time T (%f) is less than '
                         'intial_time (%f). Integration cannot be run '
                         'backwards.' % (T, initial_t))

    vars_to_check = (nu, gamma, h, theta0, beta, alpha)
    if numpy.all([numpy.isscalar(var) for var in vars_to_check]):
        return _one_pop_const_params_X(phi, xx, T, nu, gamma, h, beta, alpha, 
                                       theta0, initial_t)
    else:
        raise NotImplementedError('X chromosome integration currently only '
                                  'implemented for constant parameters.')

set_timescale_factor(pts, factor=10)

Controls the fineness of timesteps during integration.

The timestep will be proportional to Numerics.default_grid(pts)[1]/factor. Typically, pts should be set to the largest number of grid points used in extrapolation.

An adjustment factor of 10 typically results in acceptable accuracy. It may be desirable to increase this factor, particularly when population sizes are changing continously and rapidly.

Source code in dadi/Integration.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def set_timescale_factor(pts, factor=10):
    """
    Controls the fineness of timesteps during integration.

    The timestep will be proportional to Numerics.default_grid(pts)[1]/factor.
    Typically, pts should be set to the *largest* number of grid points used in
    extrapolation. 

    An adjustment factor of 10 typically results in acceptable accuracy. It may
    be desirable to increase this factor, particularly when population sizes
    are changing continously and rapidly.
    """
    # Implementation note: This cannot be easily be set automatically, because
    # the integration doesn't know whether its results will be used in an
    # extrapolation.
    global timescale_factor
    logger.warning('set_timescale_factor has been deprecated, as it may be too '
                'conservative (and thus slow) in choosing timesteps. If you '
                'wish to take smaller timesteps for accuracy (particularly for '
                'a very quickly growing population), manually set '
                'dadi.Integration.timescale_factor to a smaller value. '
                '(Current value is %g.)' % timescale_factor)
    timescale_factor = Numerics.default_grid(pts)[1]/factor

three_pops(phi, xx, T, nu1=1, nu2=1, nu3=1, m12=0, m13=0, m21=0, m23=0, m31=0, m32=0, gamma1=0, gamma2=0, gamma3=0, h1=0.5, h2=0.5, h3=0.5, theta0=1, initial_t=0, frozen1=False, frozen2=False, frozen3=False, enable_cuda_cached=False, deme_ids=None)

Integrate a 3-dimensional phi foward.

Note
  • nu's, gamma's, m's, and theta0 may be functions of time.

  • Generalizing to different grids in different phi directions is straightforward. The tricky part will be later doing the extrapolation correctly.

Parameters:

Name Type Description Default
phi array - like

Initial 3-dimensional phi

required
xx array - like

1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed that this grid is used in all dimensions.

required
T float

Time at which to halt integration

required
nu1 float

Population sizes

1
nu2 float

Population sizes

1
nu3 float

Population sizes

1
m12 float

Migration rates. Note that m12 is the rate into 1 from 2.

0
m13 float

Migration rates. Note that m13 is the rate into 1 from 3.

0
m21 float

Migration rates. Note that m21 is the rate into 2 from 1.

0
m23 float

Migration rates. Note that m23 is the rate into 2 from 3.

0
m31 float

Migration rates. Note that m31 is the rate into 3 from 1.

0
m32 float

Migration rates. Note that m32 is the rate into 3 from 2.

0
gamma1 float

Selection coefficients on all segregating alleles

0
gamma2 float

Selection coefficients on all segregating alleles

0
gamma3 float

Selection coefficients on all segregating alleles

0
h1 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h2 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h3 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
theta0 float

Propotional to ancestral size. Typically constant.

1
initial_t float

Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.)

0
frozen1 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen2 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen3 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
enable_cuda_cached bool

If True, enable CUDA integration with slower constant parameter method. Likely useful only for benchmarking.

False
deme_ids list[str]

sequence of strings representing the names of demes

None
Source code in dadi/Integration.py
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
def three_pops(phi, xx, T, nu1=1, nu2=1, nu3=1,
               m12=0, m13=0, m21=0, m23=0, m31=0, m32=0,
               gamma1=0, gamma2=0, gamma3=0, h1=0.5, h2=0.5, h3=0.5,
               theta0=1, initial_t=0, frozen1=False, frozen2=False,
               frozen3=False, enable_cuda_cached=False, deme_ids=None):
    """
    Integrate a 3-dimensional phi foward.

    Note:
        - nu's, gamma's, m's, and theta0 may be functions of time.

        - Generalizing to different grids in different phi directions is
          straightforward. The tricky part will be later doing the extrapolation
          correctly.

    Args:
        phi (array-like): Initial 3-dimensional phi
        xx (array-like): 1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed
            that this grid is used in all dimensions.
        T (float): Time at which to halt integration
        nu1 (float): Population sizes
        nu2 (float): Population sizes
        nu3 (float): Population sizes
        m12 (float): Migration rates. Note that m12 is the rate 
             *into 1 from 2*.
        m13 (float): Migration rates. Note that m13 is the rate 
             *into 1 from 3*.
        m21 (float): Migration rates. Note that m21 is the rate 
             *into 2 from 1*.
        m23 (float): Migration rates. Note that m23 is the rate 
             *into 2 from 3*.
        m31 (float): Migration rates. Note that m31 is the rate 
             *into 3 from 1*.
        m32 (float): Migration rates. Note that m32 is the rate 
             *into 3 from 2*.
        gamma1 (float): Selection coefficients on *all* segregating alleles
        gamma2 (float): Selection coefficients on *all* segregating alleles
        gamma3 (float): Selection coefficients on *all* segregating alleles
        h1 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h2 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h3 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        theta0 (float): Propotional to ancestral size. Typically constant.
        initial_t (float): Time at which to start integration. (Note that this only matters
                if one of the demographic parameters is a function of time.)
        frozen1 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen2 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen3 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        enable_cuda_cached (bool): If True, enable CUDA integration with slower constant
                        parameter method. Likely useful only for benchmarking.
        deme_ids (list[str]): sequence of strings representing the names of demes
    """
    phi = phi.copy()

    if T - initial_t == 0:
        return phi
    elif T - initial_t < 0:
        raise ValueError('Final integration time T (%f) is less than '
                         'intial_time (%f). Integration cannot be run '
                         'backwards.' % (T, initial_t))


    if (frozen1 and (m12 != 0 or m21 != 0 or m13 !=0 or m31 != 0))\
       or (frozen2 and (m12 != 0 or m21 != 0 or m23 !=0 or m32 != 0))\
       or (frozen3 and (m13 != 0 or m31 != 0 or m23 !=0 or m32 != 0)):
        raise ValueError('Population cannot be frozen and have non-zero '
                         'migration to or from it.')

    vars_to_check = [nu1,nu2,nu3,m12,m13,m21,m23,m31,m32,gamma1,gamma2,
                     gamma3,h1,h2,h3,theta0]
    if numpy.all([numpy.isscalar(var) for var in vars_to_check]):
        if not cuda_enabled or (cuda_enabled and enable_cuda_cached):
            Demes.cache.append(Demes.IntegrationConst(duration = T-initial_t, 
                               start_sizes = [nu1, nu2, nu3],
                               mig = [m12, m13, m21, m23, m31, m32], deme_ids=deme_ids))
            return _three_pops_const_params(phi, xx, T, nu1, nu2, nu3,
                                            m12, m13, m21, m23, m31, m32,
                                            gamma1, gamma2, gamma3, h1, h2, h3,
                                            theta0, initial_t,
                                            frozen1, frozen2, frozen3)
    zz = yy = xx

    nu1_f = Misc.ensure_1arg_func(nu1)
    nu2_f = Misc.ensure_1arg_func(nu2)
    nu3_f = Misc.ensure_1arg_func(nu3)
    m12_f = Misc.ensure_1arg_func(m12)
    m13_f = Misc.ensure_1arg_func(m13)
    m21_f = Misc.ensure_1arg_func(m21)
    m23_f = Misc.ensure_1arg_func(m23)
    m31_f = Misc.ensure_1arg_func(m31)
    m32_f = Misc.ensure_1arg_func(m32)
    gamma1_f = Misc.ensure_1arg_func(gamma1)
    gamma2_f = Misc.ensure_1arg_func(gamma2)
    gamma3_f = Misc.ensure_1arg_func(gamma3)
    h1_f = Misc.ensure_1arg_func(h1)
    h2_f = Misc.ensure_1arg_func(h2)
    h3_f = Misc.ensure_1arg_func(h3)
    theta0_f = Misc.ensure_1arg_func(theta0)

    if cuda_enabled:
        import dadi.cuda
        phi = dadi.cuda.Integration._three_pops_temporal_params(phi, xx, T, initial_t,
                nu1_f, nu2_f, nu3_f, m12_f, m13_f, m21_f, m23_f, m31_f, m32_f, 
                gamma1_f, gamma2_f, gamma3_f, h1_f, h2_f, h3_f, 
                theta0_f, frozen1, frozen2, frozen3, deme_ids)
        return phi

    current_t = initial_t
    nu1,nu2,nu3 = nu1_f(current_t), nu2_f(current_t), nu3_f(current_t)
    m12,m13 = m12_f(current_t), m13_f(current_t)
    m21,m23 = m21_f(current_t), m23_f(current_t)
    m31,m32 = m31_f(current_t), m32_f(current_t)
    gamma1,gamma2 = gamma1_f(current_t), gamma2_f(current_t)
    gamma3 = gamma3_f(current_t)
    h1,h2,h3 = h1_f(current_t), h2_f(current_t), h3_f(current_t)
    dx,dy,dz = numpy.diff(xx),numpy.diff(yy),numpy.diff(zz)
    demes_hist = [[0, [nu1,nu2,nu3], [m12,m13,m21,m23,m31,m32]]]
    while current_t < T:
        dt = min(_compute_dt(dx,nu1,[m12,m13],gamma1,h1),
                 _compute_dt(dy,nu2,[m21,m23],gamma2,h2),
                 _compute_dt(dz,nu3,[m31,m32],gamma3,h3))
        this_dt = min(dt, T - current_t)

        next_t = current_t + this_dt

        nu1,nu2,nu3 = nu1_f(next_t), nu2_f(next_t), nu3_f(next_t)
        m12,m13 = m12_f(next_t), m13_f(next_t)
        m21,m23 = m21_f(next_t), m23_f(next_t)
        m31,m32 = m31_f(next_t), m32_f(next_t)
        gamma1,gamma2 = gamma1_f(next_t), gamma2_f(next_t)
        gamma3 = gamma3_f(next_t)
        h1,h2,h3 = h1_f(next_t), h2_f(next_t), h3_f(next_t)
        theta0 = theta0_f(next_t)
        demes_hist.append([next_t, [nu1,nu2,nu3], [m12,m13,m21,m23,m31,m32]])

        if numpy.any(numpy.less([T,nu1,nu2,nu3,m12,m13,m21,m23,m31,m32,theta0],
                                0)):
            raise ValueError('A time, population size, migration rate, or '
                             'theta0 is < 0. Has the model been mis-specified?')
        if numpy.any(numpy.equal([nu1,nu2,nu3], 0)):
            raise ValueError('A population size is 0. Has the model been '
                             'mis-specified?')

        _inject_mutations_3D(phi, this_dt, xx, yy, zz, theta0,
                             frozen1, frozen2, frozen3)
        if not frozen1:
            phi = int_c.implicit_3Dx(phi, xx, yy, zz, nu1, m12, m13, 
                                     gamma1, h1, this_dt, use_delj_trick)
        if not frozen2:
            phi = int_c.implicit_3Dy(phi, xx, yy, zz, nu2, m21, m23, 
                                     gamma2, h2, this_dt, use_delj_trick)
        if not frozen3:
            phi = int_c.implicit_3Dz(phi, xx, yy, zz, nu3, m31, m32, 
                                     gamma3, h3, this_dt, use_delj_trick)

        current_t = next_t
    Demes.cache.append(Demes.IntegrationNonConst(history = demes_hist, deme_ids=deme_ids))
    return phi

two_pops(phi, xx, T, nu1=1, nu2=1, m12=0, m21=0, gamma1=0, gamma2=0, h1=0.5, h2=0.5, theta0=1, initial_t=0, frozen1=False, frozen2=False, nomut1=False, nomut2=False, enable_cuda_cached=False, deme_ids=None)

Integrate a 2-dimensional phi foward.

Note
  • nu's, gamma's, m's, and theta0 may be functions of time.

  • Generalizing to different grids in different phi directions is straightforward. The tricky part will be later doing the extrapolation correctly.

Parameters:

Name Type Description Default
phi array - like

Initial 2-dimensional phi

required
xx array - like

1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed that this grid is used in all dimensions.

required
T float

Time at which to halt integration

required
nu1 float

Population sizes

1
nu2 float

Population sizes

1
gamma1 float

Selection coefficients on all segregating alleles

0
gamma2 float

Selection coefficients on all segregating alleles

0
h1 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
h2 float

Dominance coefficients. h = 0.5 corresponds to genic selection.

0.5
m12 float

Migration rates. Note that m12 is the rate into 1 from 2.

0
m21 float

Migration rates. Note that m12 is the rate into 1 from 2.

0
theta0 float

Propotional to ancestral size. Typically constant.

1
initial_t float

Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.)

0
frozen1 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
frozen2 bool

If True, the corresponding population is "frozen" in time (no new mutations and no drift), so the resulting spectrum will correspond to an ancient DNA sample from that population.

False
nomut1 bool

If True, no new mutations will be introduced into the given population.

False
nomut2 bool

If True, no new mutations will be introduced into the given population.

False
enable_cuda_cached bool

If True, enable CUDA integration with slower constant parameter method. Likely useful only for benchmarking.

False
deme_ids list[str]

sequence of strings representing the names of demes

None
Source code in dadi/Integration.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
def two_pops(phi, xx, T, nu1=1, nu2=1, m12=0, m21=0, gamma1=0, gamma2=0,
             h1=0.5, h2=0.5, theta0=1, initial_t=0, frozen1=False,
             frozen2=False, nomut1=False, nomut2=False, enable_cuda_cached=False, deme_ids=None):
    """
    Integrate a 2-dimensional phi foward.

    Note:
        - nu's, gamma's, m's, and theta0 may be functions of time.

        - Generalizing to different grids in different phi directions is
          straightforward. The tricky part will be later doing the extrapolation
          correctly.

    Args:
        phi (array-like): Initial 2-dimensional phi
        xx (array-like): 1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed
            that this grid is used in all dimensions.
        T (float): Time at which to halt integration
        nu1 (float): Population sizes
        nu2 (float): Population sizes
        gamma1 (float): Selection coefficients on *all* segregating alleles
        gamma2 (float): Selection coefficients on *all* segregating alleles
        h1 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        h2 (float): Dominance coefficients. h = 0.5 corresponds to genic selection.
        m12 (float): Migration rates. Note that m12 is the rate *into 1 from 2*.
        m21 (float): Migration rates. Note that m12 is the rate *into 1 from 2*.
        theta0 (float): Propotional to ancestral size. Typically constant.
        initial_t (float): Time at which to start integration. (Note that this only matters
                if one of the demographic parameters is a function of time.)
        frozen1 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        frozen2 (bool): If True, the corresponding population is "frozen" in time
                        (no new mutations and no drift), so the resulting spectrum
                        will correspond to an ancient DNA sample from that
                        population.
        nomut1 (bool): If True, no new mutations will be introduced into the
                    given population.
        nomut2 (bool): If True, no new mutations will be introduced into the
                    given population.
        enable_cuda_cached (bool): If True, enable CUDA integration with slower constant
                        parameter method. Likely useful only for benchmarking.
        deme_ids (list[str]): sequence of strings representing the names of demes
    """
    phi = phi.copy()

    if T - initial_t == 0:
        return phi
    elif T - initial_t < 0:
        raise ValueError('Final integration time T (%f) is less than '
                         'intial_time (%f). Integration cannot be run '
                         'backwards.' % (T, initial_t))

    if (frozen1 or frozen2) and (m12 != 0 or m21 != 0):
        raise ValueError('Population cannot be frozen and have non-zero '
                         'migration to or from it.')

    vars_to_check = [nu1,nu2,m12,m21,gamma1,gamma2,h1,h2,theta0]
    if numpy.all([numpy.isscalar(var) for var in vars_to_check]):
        # Constant integration with CUDA turns out to be slower,
        # so we only use it in specific circumsances.
        Demes.cache.append(Demes.IntegrationConst(duration = T-initial_t, 
                           start_sizes = [nu1, nu2], mig = [m12,m21], deme_ids=deme_ids))
        if not cuda_enabled or (cuda_enabled and enable_cuda_cached):
            return _two_pops_const_params(phi, xx, T, nu1, nu2, m12, m21,
                    gamma1, gamma2, h1, h2, theta0, initial_t,
                    frozen1, frozen2, nomut1, nomut2)
    yy = xx

    nu1_f = Misc.ensure_1arg_func(nu1)
    nu2_f = Misc.ensure_1arg_func(nu2)
    m12_f = Misc.ensure_1arg_func(m12)
    m21_f = Misc.ensure_1arg_func(m21)
    gamma1_f = Misc.ensure_1arg_func(gamma1)
    gamma2_f = Misc.ensure_1arg_func(gamma2)
    h1_f = Misc.ensure_1arg_func(h1)
    h2_f = Misc.ensure_1arg_func(h2)
    theta0_f = Misc.ensure_1arg_func(theta0)

    if cuda_enabled:
        import dadi.cuda
        phi = dadi.cuda.Integration._two_pops_temporal_params(phi, xx, T, initial_t,
                nu1_f, nu2_f, m12_f, m21_f, gamma1_f, gamma2_f, h1_f, h2_f, theta0_f, 
                frozen1, frozen2, nomut1, nomut2, deme_ids)
        return phi

    current_t = initial_t
    nu1,nu2 = nu1_f(current_t), nu2_f(current_t)
    m12,m21 = m12_f(current_t), m21_f(current_t)
    gamma1,gamma2 = gamma1_f(current_t), gamma2_f(current_t)
    h1,h2 = h1_f(current_t), h2_f(current_t)
    dx,dy = numpy.diff(xx),numpy.diff(yy)

    demes_hist = [[0, [nu1,nu2], [m12,m21]]]
    while current_t < T:
        dt = min(_compute_dt(dx,nu1,[m12],gamma1,h1),
                 _compute_dt(dy,nu2,[m21],gamma2,h2))
        this_dt = min(dt, T - current_t)

        next_t = current_t + this_dt

        nu1,nu2 = nu1_f(next_t), nu2_f(next_t)
        m12,m21 = m12_f(next_t), m21_f(next_t)
        gamma1,gamma2 = gamma1_f(next_t), gamma2_f(next_t)
        h1,h2 = h1_f(next_t), h2_f(next_t)
        theta0 = theta0_f(next_t)
        demes_hist.append([next_t, [nu1,nu2], [m12,m21]])

        if numpy.any(numpy.less([T,nu1,nu2,m12,m21,theta0], 0)):
            raise ValueError('A time, population size, migration rate, or '
                             'theta0 is < 0. Has the model been mis-specified?')
        if numpy.any(numpy.equal([nu1,nu2], 0)):
            raise ValueError('A population size is 0. Has the model been '
                             'mis-specified?')

        _inject_mutations_2D(phi, this_dt, xx, yy, theta0, frozen1, frozen2,
                             nomut1, nomut2)
        if not frozen1:
            phi = int_c.implicit_2Dx(phi, xx, yy, nu1, m12, gamma1, h1,
                                     this_dt, use_delj_trick)
        if not frozen2:
            phi = int_c.implicit_2Dy(phi, xx, yy, nu2, m21, gamma2, h2,
                                     this_dt, use_delj_trick)

        current_t = next_t
    Demes.cache.append(Demes.IntegrationNonConst(history = demes_hist, deme_ids=deme_ids))
    return phi