Example #1
0
 class Meta(_AbstractProbability.Meta):
     store_id = "R2G3Model"
     ind_properties = [
         PropIntel(name="W1", label="W1 (> 0.5)", math_label=r"$W_1$",
             stor_name="_W1", inh_name="inherit_W1", inh_from="parent.based_on.probabilities",
             is_independent=True, # flag for the view creation
             minimum=0.5, maximum=1.0, data_type=float, **PropIntel.REF_ST_WID),
         PropIntel(name="P111_or_P212", label="P111 (W1 < 2/3) or\nPx1x (W1 > 2/3)",
             stor_name="_P111_or_P212", inh_name="inherit_P111_or_P212",
             inh_from="parent.based_on.probabilities",
             is_independent=True, # flag for the view creation
             math_label=r"$P_{111} %s$ or $\newline P_{x1x} %s$" % (
             mt_range(0.5, "W_1", 2.0 / 3.0),
             mt_range(2.0 / 3.0, "W_1", 1.0)),
             minimum=0.0, maximum=1.0, data_type=float, **PropIntel.REF_ST_WID),
         PropIntel(name="G1", label="W2/(W2+W3)", math_label=r"$\large\frac{W_2}{W_3 + W_2}$",
             stor_name="_G1", inh_name="inherit_G1", inh_from="parent.based_on.probabilities",
             is_independent=True, # flag for the view creation
             minimum=0.0, maximum=1.0, data_type=float, **PropIntel.REF_ST_WID),
         PropIntel(name="G2", label="(W212+W213)/(W212+W213+W312+W313)",
             stor_name="_G2", inh_name="inherit_G2", inh_from="parent.based_on.probabilities",
             is_independent=True, # flag for the view creation
             math_label=r"$\large\frac{W_{212} + W_{213}}{W_{212} + W_{213} + W_{312} + W_{313}}$",
             minimum=0.0, maximum=1.0, data_type=float, **PropIntel.REF_ST_WID),
         PropIntel(name="G3", label="W212/(W212+W213)",
             stor_name="_G3", inh_name="inherit_G3", inh_from="parent.based_on.probabilities",
             is_independent=True, # flag for the view creation
             math_label=r"$\large\frac{W_{212}}{W_{212} + W_{213}}$",
             minimum=0.0, maximum=1.0, data_type=float, **PropIntel.REF_ST_WID),
         PropIntel(name="G4", label="W312/(W312+W313)",
             stor_name="_G4", inh_name="inherit_G4", inh_from="parent.based_on.probabilities",
             is_independent=True, # flag for the view creation
             math_label=r"$\large\frac{W_{312}}{W_{312} + W_{313}}$",
             minimum=0.0, maximum=1.0, data_type=float, **PropIntel.REF_ST_WID),
     ]
     inh_properties = [
         PropIntel(name="inherit_%s" % prop.name, label="Inherit flag for %s" % prop.name,
             data_type=bool, refinable=False, storable=True, has_widget=True,
             widget_type="toggle") \
             for prop in ind_properties
     ]
     properties = ind_properties + inh_properties
Example #2
0
    class Meta(_AbstractProbability.Meta):
        ind_properties = [
            PropIntel(
                name="W1",
                label="W1",
                math_label=r"$W_1$",
                stor_name="_W1",
                inh_name="inherit_W1",
                inh_from="parent.based_on.probabilities",
                is_independent=True,  # flag for the view creation
                minimum=0.0,
                maximum=1.0,
                data_type=float,
                **PropIntel.REF_ST_WID),
            PropIntel(
                name="P11_or_P22",
                label="P11_or_P22",
                stor_name="_P11_or_P22",
                inh_name="inherit_P11_or_P22",
                inh_from="parent.based_on.probabilities",
                is_independent=True,  # flag for the view creation
                math_label=r"$P_{11} %s$ or $\newline P_{22} %s$" %
                (mt_range(0.0, "W_1", 0.5), mt_range(0.5, "W_1", 1.0)),
                minimum=0.0,
                maximum=1.0,
                data_type=float,
                **PropIntel.REF_ST_WID),
        ]
        inh_properties = [
            PropIntel(name="inherit_%s" % prop.name, label="Inherit flag for %s" % prop.name,
                data_type=bool, refinable=False, storable=True, has_widget=True,
                widget_type="toggle") \
                for prop in ind_properties
        ]
        properties = ind_properties + inh_properties

        store_id = "R1G2Model"
Example #3
0
class R2G3Model(_AbstractProbability):
    r"""
    
    (Restricted) probability model for Reichweite 2 with 3 components.
    
    The (due to restrictions only) 6 independent variables are:
    
    .. math::
        :nowrap:

        \begin{align*}
            & W_{1}
            & P_{111} \text{(if $\nicefrac{1}{2} \leq W_1 < \nicefrac{2}{3}$) or} P_{x1x} \text{(if $\nicefrac{2}{3} \leq W_1 \leq 1)$ with $x \in \left\{ {2,3} \right\}$} \\
            & G_1 = \frac{W_2}{W_2 + W_3}
            & G_2 = \frac{W_{212} + W_{213}}{W_{212} + W_{213} + W_{312} + W_{313}} \\
            & G_3 = \frac{W_{212}}{W_{212} + W_{213}}
            & G_4 = \frac{W_{312}}{W_{312} + W_{313}} \\
        \end{align*}
        
    This model can not describe mixed layers in which the last two components
    occur right after each other in a stack. In other words there is always
    an alternation between (one or more) layers of the first component and a 
    single layer of the second or third component. Therefore, the weight 
    fraction of the first component (:math:`W_1`) needs to be > than 1/2.
    
    The restriction also translates in the following:
    
    .. math::
        :nowrap:
        
        \begin{align*}
            & P_{22} = P_{23} = P_{32} = P_{33} = 0 \\
            & P_{21} = P_{31} = 1 \\
            & \\
            & P_{122} = P_{123} = P_{132} = P_{133} = 0 \\
            & P_{121} = P_{131} = 1 \\
            & \\
            & P_{222} = P_{223} = P_{232} = P_{233} = 0 \\
            & P_{221} = P_{231} = 1 \\
            & \\
            & P_{322} = P_{323} = P_{332} = P_{333} = 0 \\
            & P_{321} = P_{331} = 1 \\
        \end{align*}
    
    Using the above, we can calculate a lot of the weight fractions of stacks:
    
    .. math::
        :nowrap:
    
        \begin{align*}
            & W_{22} = W_{23} = W_{32} = W_{33} 0 \\
            & W_{21} = W_{2} \\
            & W_{31} = W_{3} \\
            & \\
            & W_{122} = W_{123} = W_{132} = W_{133} = 0 \\
            & W_{121} = W_{12} = W_{21} = W_2 \\
            & W_{131} = W_{13} = W_{31} = W_3 \\
            & W_{11} = W_1 - W_{12} - W_{13} \\
            & \\
            & W_{221} = W_{231} = W_{222} = W_{223} = W_{232} = W_{233} = 0 \\
            & W_{331} = W_{331} = W_{322} = W_{323} = W_{332} = W_{333} = 0 \\             
        \end{align*}

    Then the remaining fractions and probablities can be calculated as follows:
    
    .. math::
        :nowrap:
    
        \begin{align*}
            & W_2 = G_1 * (1 - W_1) \\
            & W_3 = 1 - W_1 - W_2 \\
            & \\
            & W_x = W_2 + W_3 &
            & \text{if $W_1 < \nicefrac{2}{3}$:} \\
            & \quad \text{$P_{111}$ is given}\\
            & \quad P_{x1x} = 
            \begin{dcases}
                1 - \frac{W_1 - W_x}{W_x} \cdot (1 - P_{111}, & \text{if $W_x > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\ 
            & \\
            & \text{if $W_1 \geq \nicefrac{2}{3}$:} \\
            & \quad \text{$P_{x1x}$ is given}\\
            & \quad P_{111} = 
            \begin{dcases}
                1 - \frac{W_x}{W_1 - W_x} \cdot (1 - P_{x1x}, & \text{if $(W_1 - W_x) > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & \\
            & W_{x1x} = W_x \cdot P_{x1x} \\
            & W_{21x} = G_2 \cdot W_{x1x} \\
            & W_{31x} = W_{x1x} - W_{21x} \\
            & \\
            & W_{212} = G_3 \cdot W_{21x} \\
            & W_{213} = (1 - G_3) \cdot W_{21x} \\
            & W_{211} = W_{21} - W_{212} - W_{213} \\
            & \\
            & W_{312} = G_4 \cdot W_{31x} \\
            & W_{313} = (1 - G_4) \cdot W_{31x} \\
            & W_{311} = W_{31} - W_{312} - W_{313} \\
            & \\
            & W_{111} = W_{11} \cdot P_{111} \\
            & W_{112} = W_{12} - W_{212} - W_{312} \\
            & W_{112} = W_{13} - W_{213} - W_{313} \\
            & \\
            & \text{Calculate the remaining P using:} \\
            & P_{ijk} = 
            \begin{dcases}
                \frac{W_{ijk}}{W_{ij}}, & \text{if $W_{ij} > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\ 
        \end{align*}
        
    """

    # MODEL METADATA:
    class Meta(_AbstractProbability.Meta):
        store_id = "R2G3Model"

    # PROPERTIES:
    _G = 3
    twothirds = 2.0 / 3.0

    inherit_W1 = BoolProperty(default=False,
                              text="Inherit flag for W1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    W1 = FloatProperty(default=0.8,
                       text="W1 (> 0.5)",
                       math_text=r"$W_1 (> 0.5)$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.5,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_W1",
                       inherit_from="parent.based_on.probabilities.W1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_P111_or_P212 = BoolProperty(default=False,
                                        text="Inherit flag for P112_or_P211",
                                        persistent=True,
                                        visible=True,
                                        set_action_name="update",
                                        mix_with=(SetActionMixin, ))
    P111_or_P212 = FloatProperty(
        default=0.9,
        text="P111 (W1 < 2/3) or\nPx1x (W1 > 2/3)",
        math_text=r"$P_{111} %s$ or $\newline P_{x1x} %s$" %
        (mt_range(0.5, "W_1", 2.0 / 3.0), mt_range(2.0 / 3.0, "W_1", 1.0)),
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_P111_or_P212",
        inherit_from="parent.based_on.probabilities.P111_or_P212",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G1 = BoolProperty(default=False,
                              text="Inherit flag for G1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G1 = FloatProperty(default=0.9,
                       text="W2/(W2+W3)",
                       math_text=r"$\large\frac{W_2}{W_3 + W_2}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_G1",
                       inherit_from="parent.based_on.probabilities.G1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_G2 = BoolProperty(default=False,
                              text="Inherit flag for G2",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G2 = FloatProperty(
        default=0.9,
        text="(W212+W213)/(W212+W213+W312+W313)",
        math_text=
        r"$\large\frac{W_{212} + W_{213}}{W_{212} + W_{213} + W_{312} + W_{313}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G2",
        inherit_from="parent.based_on.probabilities.G2",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G3 = BoolProperty(default=False,
                              text="Inherit flag for G3",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G3 = FloatProperty(default=0.9,
                       text="W212/(W212+W213)",
                       math_text=r"$\large\frac{W_{212}}{W_{212} + W_{213}}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_G3",
                       inherit_from="parent.based_on.probabilities.G3",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_G4 = BoolProperty(default=False,
                              text="Inherit flag for G4",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G4 = FloatProperty(default=0.9,
                       text="W312/(W312+W313)",
                       math_text=r"$\large\frac{W_{312}}{W_{312} + W_{313}}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_G4",
                       inherit_from="parent.based_on.probabilities.G4",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    # ------------------------------------------------------------
    #      Initialization and other internals
    # ------------------------------------------------------------
    def __init__(self,
                 W1=0.8,
                 P111_or_P212=0.9,
                 G1=0.9,
                 G2=0.9,
                 G3=0.9,
                 G4=0.9,
                 inherit_W1=False,
                 inherit_P111_or_P212=False,
                 inherit_G1=False,
                 inherit_G2=False,
                 inherit_G3=False,
                 inherit_G4=False,
                 *args,
                 **kwargs):
        super(R2G3Model, self).__init__(R=2, *args, **kwargs)

        with self.data_changed.hold():
            self.W1 = not_none(W1, 0.8)
            self.inherit_W1 = inherit_W1
            self.P111_or_P212 = not_none(P111_or_P212, 0.9)
            self.inherit_P111_or_P212 = inherit_P111_or_P212
            self.G1 = not_none(G1, 0.9)
            self.inherit_G1 = inherit_G1
            self.G2 = not_none(G2, 0.9)
            self.inherit_G2 = inherit_G2
            self.G3 = not_none(G3, 0.9)
            self.inherit_G3 = inherit_G3
            self.G4 = not_none(G4, 0.9)
            self.inherit_G4 = inherit_G4

            self.update()

    # ------------------------------------------------------------
    #      Methods & Functions
    # ------------------------------------------------------------
    def update(self):
        with self.monitor_changes():

            # calculate Wx's:
            self.mW[0] = self.W1
            self.mW[1] = (1.0 - self.mW[0]) * self.G1
            self.mW[2] = 1.0 - self.mW[0] - self.mW[1]

            # consequences of restrictions:
            self.mW[1, 1] = 0
            self.mW[1, 2] = 0
            self.mW[2, 1] = 0
            self.mW[2, 2] = 0
            self.mW[0, 1, 0] = self.mW[0, 1] = self.mW[1, 0] = self.mW[1]
            self.mW[0, 2, 0] = self.mW[0, 2] = self.mW[2, 0] = self.mW[2]
            self.mW[0, 0] = self.mW[0] - self.mW[0, 1] - self.mW[0, 2]

            # continue calculations:
            Wx = self.mW[1] + self.mW[2]
            if self.mW[0] < self.twothirds:
                self.mP[0, 0, 0] = self.P111_or_P212
                Px0x = 1 - (self.mW[0] - Wx) / Wx * (
                    1 - self.mP[0, 0, 0]) if Wx != 0 else 0.0
            else:
                Px0x = self.P111_or_P212
                self.mP[0, 0, 0] = 1 - Wx / (self.mW[0] - Wx) * (1 - Px0x) if (
                    self.mW[0] - Wx) != 0 else 0.0

            Wx0x = Wx * Px0x
            W10x = self.G2 * Wx0x
            W20x = Wx0x - W10x

            self.mW[1, 0, 1] = self.G3 * W10x
            self.mW[1, 0, 2] = (1 - self.G3) * W10x
            self.mW[1, 0,
                    0] = self.mW[1, 0] - self.mW[1, 0, 1] - self.mW[1, 0, 2]

            self.mW[2, 0, 1] = self.G4 * W20x
            self.mW[2, 0, 2] = (1 - self.G4) * W20x
            self.mW[2, 0,
                    0] = self.mW[2, 0] - self.mW[2, 0, 1] - self.mW[2, 0, 2]

            self.mW[0, 0, 0] = self.mW[0, 0] * self.mP[0, 0, 0]
            self.mW[0, 0,
                    1] = self.mW[0, 1] - self.mW[1, 0, 1] - self.mW[2, 0, 1]
            self.mW[0, 0,
                    2] = self.mW[0, 2] - self.mW[1, 0, 2] - self.mW[2, 0, 2]

            # Calculate remaining P:
            for i in range(3):
                for j in range(3):
                    for k in range(3):
                        self.mP[i, j, k] = self.mW[i, j, k] / self.mW[
                            i, j] if self.mW[i, j] > 0 else 0.0

            self.solve()
            self.validate()

    pass  # end of class
Example #4
0
class R2G2Model(_AbstractProbability):
    r"""
    Probability model for Reichweite 2 with 2 components.
    
    The 4 (=g^2) independent variables are:
    
    .. math::
        :nowrap:
    
        \begin{align*}
            & W_1
            & P_{112} (W_1 leq \nicefrac{2}{3})
            \text{ or }P_{211} (W_1 > \nicefrac{2}{3}) \\
            & P_{21}
            & P_{122} (P_{21} leq \nicefrac{1}{2})
            \text{ or }P_{221} (P_{21} > \nicefrac{1}{2}) \\
        \end{align*}
            
    Calculation of the other variables happens as follows:
    
    .. math::
        :nowrap:

        \begin{align*}
            & W_2 = 1 - W_1 \\
            & P_{22} = 1 - P_{21} \\
            & \\
            & W_{21} = W_2 \cdot P_{21} \\
            & W_{21} = W_{12} \\
            & W_{11} = W_1 - W_{21} \\
            & W_{22} = W_{2} \cdot P_{22} \\
            & \\
            & \text{if $W_1 leq \nicefrac{2}{3}$:} \\
            & \quad \text{$P_{112}$ is given}\\
            & \quad P_{211} =
            \begin{dcases}
                \frac{W_{11}}{W_{21}} \cdot P_{112} , & \text{if $W_{21} > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & \\
            & \text{if $W_1 > \nicefrac{2}{3}$:} \\
            & \quad \text{$P_{211}$ is given}\\
            & \quad P_{112} =
            \begin{dcases}
                \frac{W_{21}}{W_{11}} \cdot P_{211} , & \text{if $W_{11} > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & \\
            & P_{212} = 1 - P_{211} \\
            & P_{111} = 1 - P_{112} \\
            & \\
            & \text{if $P_{21} leq \nicefrac{1}{2}$:} \\
            & \quad \text{$P_{122}$ is given}\\
            & \quad P_{221} =
            \begin{dcases}
                \frac{W_{12}}{W_{22}} \cdot P_{122} , & \text{if $W_{22} > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & \\
            & \text{if $P_{21} > \nicefrac{1}{2}$:} \\
            & \quad \text{$P_{221}$ is given}\\
            & \quad P_{122} =
            \begin{dcases}
                \frac{W_{22}}{W_{12}} \cdot P_{221} , & \text{if $W_{12} > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{121} = 1 - P_{122} \\
            & P_{222} = 1 - P_{221} \\
        \end{align*}
    
    """

    # MODEL METADATA:
    class Meta(_AbstractProbability.Meta):
        store_id = "R2G2Model"

    # PROPERTIES:
    _G = 2
    twothirds = 2.0 / 3.0

    inherit_W1 = BoolProperty(default=False,
                              text="Inherit flag for W1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    W1 = FloatProperty(default=0.75,
                       text="W1 (> 0.5)",
                       math_text=r"$W_1 (> 0.5)$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.5,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_W1",
                       inherit_from="parent.based_on.probabilities.W1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_P112_or_P211 = BoolProperty(default=False,
                                        text="Inherit flag for P112_or_P211",
                                        persistent=True,
                                        visible=True,
                                        set_action_name="update",
                                        mix_with=(SetActionMixin, ))
    P112_or_P211 = FloatProperty(
        default=0.75,
        text="P112 (W1 < 2/3) or\nP211 (W1 > 2/3)",
        math_text=r"$P_{112} %s$ or $\newlineP_{211} %s$" % (mt_range(
            1.0 / 2.0, "W_1", 2.0 / 3.0), mt_range(2.0 / 3.0, "W_1", 1.0)),
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_P112_or_P211",
        inherit_from="parent.based_on.probabilities.P112_or_P211",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_P21 = BoolProperty(default=False,
                               text="Inherit flag for P21",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    P21 = FloatProperty(default=0.75,
                        text="P21",
                        math_text=r"$P_{21}$",
                        persistent=True,
                        visible=True,
                        refinable=True,
                        store_private=True,
                        minimum=0.0,
                        maximum=1.0,
                        is_independent=True,
                        inheritable=True,
                        inherit_flag="inherit_P21",
                        inherit_from="parent.based_on.probabilities.P21",
                        set_action_name="update",
                        mix_with=(SetActionMixin, RefinableMixin,
                                  InheritableMixin))

    inherit_P122_or_P221 = BoolProperty(default=False,
                                        text="Inherit flag for P122_or_P221",
                                        persistent=True,
                                        visible=True,
                                        set_action_name="update",
                                        mix_with=(SetActionMixin, ))
    P122_or_P221 = FloatProperty(
        default=0.75,
        text="P112 (W1 < 1/2) or\nP221 (W1 > 1/2)",
        math_text=r"$P_{122} %s$ or $\newlineP_{221} %s$" %
        (mt_range(0.0, "W_1", 1.0 / 2.0), mt_range(1.0 / 2.0, "W_1", 1.0)),
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_P122_or_P221",
        inherit_from="parent.based_on.probabilities.P122_or_P221",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    # ------------------------------------------------------------
    #      Initialization and other internals
    # ------------------------------------------------------------
    def __init__(self,
                 W1=0.75,
                 P112_or_P211=0.75,
                 P21=0.75,
                 P122_or_P221=0.75,
                 inherit_W1=False,
                 inherit_P112_or_P211=False,
                 inherit_P21=False,
                 inherit_P122_or_P221=False,
                 *args,
                 **kwargs):
        super(R2G2Model, self).__init__(R=2, *args, **kwargs)

        with self.data_changed.hold():
            self.W1 = not_none(W1, 0.75)
            self.inherit_W1 = inherit_W1
            self.P112_or_P211 = not_none(P112_or_P211, 0.75)
            self.inherit_P112_or_P211 = inherit_P112_or_P211
            self.P21 = not_none(P21, 0.75)
            self.inherit_P21 = inherit_P21
            self.P122_or_P221 = not_none(P122_or_P221, 0.75)
            self.inherit_P122_or_P221 = inherit_P122_or_P221

            self.update()

    # ------------------------------------------------------------
    #      Methods & Functions
    # ------------------------------------------------------------
    def update(self):
        with self.monitor_changes():
            self.mW[0] = self.W1
            self.mW[1] = 1.0 - self.mW[0]

            self.mP[1, 0] = self.P21
            self.mP[1, 1] = 1.0 - self.mP[1, 0]

            self.mW[1, 0] = self.mW[1] * self.mP[1, 0]
            self.mW[1, 1] = self.mW[1] * self.mP[1, 1]
            self.mW[0, 1] = self.mW[1, 0]
            self.mW[0, 0] = self.mW[0] - self.mW[1, 0]

            if self.mW[0] <= self.twothirds:
                self.mP[0, 0, 1] = self.P112_or_P211
                if self.mW[1, 0] == 0.0:
                    self.mP[1, 0, 0] = 0.0
                else:
                    self.mP[1, 0,
                            0] = self.mP[0, 0, 1] * self.mW[0, 0] / self.mW[1,
                                                                            0]
            else:
                self.mP[1, 0, 0] = self.P112_or_P211
                if self.mW[0, 0] == 0.0:
                    self.mP[0, 0, 1] = 0.0
                else:
                    self.mP[0, 0,
                            1] = self.mP[1, 0, 0] * self.mW[1, 0] / self.mW[0,
                                                                            0]
            self.mP[1, 0, 1] = 1.0 - self.mP[1, 0, 0]
            self.mP[0, 0, 0] = 1.0 - self.mP[0, 0, 1]

            if self.mP[1, 0] <= 0.5:
                self.mP[0, 1, 1] = self.P122_or_P221
                self.mP[1, 1,
                        0] = self.mP[0, 1, 1] * self.mW[0, 1] / self.mW[1, 1]
            else:
                self.mP[1, 1, 0] = self.P122_or_P221
                self.mP[0, 1,
                        1] = self.mP[1, 1, 0] * self.mW[1, 1] / self.mW[0, 1]
            self.mP[0, 1, 0] = 1.0 - self.mP[0, 1, 1]
            self.mP[1, 1, 1] = 1.0 - self.mP[1, 1, 0]

            self.solve()
            self.validate()

    pass  # end of class
Example #5
0
class R1G4Model(_AbstractProbability):
    r"""
    Probability model for Reichweite 1 with 4 components.
    
    The independent variables (# = g*(g-1) = 12) are:
    
    .. math::
        :nowrap:
        
            \begin{align*}
                & W_1
                & P_{11} (W_1 < 0,5)\text{ or }P_{xx} (W_1 > 0,5) 
                with P_{xx} = \frac {W_{22} + W_{23} + W_{24} + W_{32} + W_{33} + W_{34} + W_{42} + W_{43} + W_{44}}{W_2 + W_3 + W_4} \\
                & R_2 = \frac{ W_2 }{W_2 + W_3 + W_4}
                & R_3 = \frac{ W_3 }{W_3 + W_4} \\
                & G_2 = \frac{W_{22} + W_{23} + W_{24}}{\sum_{i=2}^{4}\sum_{j=2}^4{W_{ij}}}
                & G_3 = \frac{W_{32} + W_{33} + W_{34}}{\sum_{i=3}^{4}\sum_{j=2}^4{W_{ij}}} \\
                & G_{22} = \frac{W_{22}}{W_{22} + W_{23} + W_{24}}
                & G_{23} = \frac{W_{23}}{W_{23} + W_{24}} \\
                & G_{32} = \frac{W_{32}}{W_{32} + W_{33} + W_{34}}
                & G_{33} = \frac{W_{33}}{W_{33} + W_{34}} \\
                & G_{42} = \frac{W_{42}}{W_{42} + W_{43} + W_{44}}
                & G_{44} = \frac{W_{43}}{W_{43} + W_{44}}
            \end{align*} 
    
    Calculation of the other variables happens as follows:
    
    .. math::
        :nowrap:
        
        \begin{align*}
            & \text{Calculate the base weight fractions of each component:} \\
            & W_2 = (1 - W_1) \cdot R_1 \\
            & W_3 = (1 - W_1 - W_2) \cdot R_2 \\
            & W_4 = (1 - W_1 - W_2 - W_3) \\
            & \\
            & \text{if $W_1 \leq 0.5$:} \\
            & \quad \text{$P_{11}$ is given}\\
            & \quad W_{xx} = W_{22} + W_{23} + W_{24} + W_{32} + W_{33} + W_{34} + W_{42} + W_{43} + W_{44} = W_1 \cdot (1 - P_{11}) + W_2 + W_3 + W_4 \\
            & \text{if $W_1 > 0.5$:} \\
            & \quad \text{$P_{xx}$ is given and $P_{11}$ is derived further down} \\
            & \quad W_{xx} = W_{22} + W_{23} + W_{24} + W_{32} + W_{33} + W_{34} + W_{42} + W_{43} + W_{44} = P_{xx} \cdot (W_2 + W_3 + W_4) \\  
            & \\
            & \text{Caclulate a partial sum of the $2^{nd}$ component's contributions: } \\
            & W_{2x} = W_{xx} \cdot G_2 \\
            & \text{Calculate a partial sum of the $3^{d}$ and $4^{th}$ component's contributions:} \\
            & W_{yx} = W_{xx} - W_{2x} \\
            & \text{Calculate a partial sum of the $3^{d}$ component's contributions:} \\
            & W_{3x} = W_{yx} \cdot G_3 \\
            & \text{Calculate a partial sum of the $4^{th}$ component's contributions:} \\
            & W_{4x} = W_{yx} - W_{3x} \\
            & \\
            & W_{22} = G_{22} \cdot W_{2x} \\
            & W_{23} = G_{23} \cdot (W_{2x} - W_{22}) \\
            & W_{24} = W{2x} - W_{22} - W_{23} \\
            & \\
            & W_{32} = G_{32} \cdot W_{3x} \\
            & W_{33} = G_{33} \cdot (W_{3x} - W_{32}) \\
            & W_{34} = W{3x} - W_{32} - W_{33} \\
            & \\
            & W_{42} = G_{42} \cdot W_{4x} \\
            & W_{43} = G_{43} \cdot (W_{4x} - W_{42}) \\
            & W_{44} = W{4x} - W_{42} - W_{43} \\
            & \\
            & \text{ From the above weight fractions the junction probabilities 
                for any combination of $2^{nd}$, $3^{d}$ and $4^{th}$ type
                components can be calculated. } \\
            & \text{ The remaining probabilities are: } \\
            & P_{12} = \begin{dcases}
                \frac{W_2 - W_{22} - W_{32} - W_{42}}{W_1}, & \text{if $W_1 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{13} = \begin{dcases}
                \frac{W_3 - W_{23} - W_{33} - W_{43}}{W_1}, & \text{if $W_1 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{14} = \begin{dcases}
                \frac{W_4 - W_{24} - W_{34} - W_{44}}{W_1}, & \text{if $W_1 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & \\
            & \text{if $W_1 \leq 0.5$}: \\
            & \quad P_{11} = 1 - P_{12} - P_{13} - P_{14} \\
            & \text{Remainder of weight fraction can now be calculated as follows:} \\
            & \quad W_{ij} = {W_{ii}} \cdot {P_{ij}} \quad \forall {i,j} \in \left[ {1, 4} \right] \\
        \end{align*}
    """

    # MODEL METADATA:
    class Meta(_AbstractProbability.Meta):
        store_id = "R1G4Model"

    # PROPERTIES
    _G = 4

    inherit_W1 = BoolProperty(default=False,
                              text="Inherit flag for W1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    W1 = FloatProperty(default=0.6,
                       text="W1",
                       math_text=r"$W_1$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_W1",
                       inherit_from="parent.based_on.probabilities.W1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_P11_or_P22 = BoolProperty(default=False,
                                      text="Inherit flag for P11_or_P22",
                                      persistent=True,
                                      visible=True,
                                      set_action_name="update",
                                      mix_with=(SetActionMixin, ))
    P11_or_P22 = FloatProperty(
        default=0.25,
        text="P11_or_P22",
        math_text=r"$P_{11} %s$ or $\newline P_{22} %s$" %
        (mt_range(0.0, "W_1", 0.5), mt_range(0.5, "W_1", 1.0)),
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_P11_or_P22",
        inherit_from="parent.based_on.probabilities.P11_or_P22",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_R1 = BoolProperty(default=False,
                              text="Inherit flag for R1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    R1 = FloatProperty(default=0.5,
                       text="W2/(W2+W3+W4)",
                       math_text=r"$\large\frac{W_2}{W_2 + W_3 + W_4}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_R1",
                       inherit_from="parent.based_on.probabilities.R1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_R2 = BoolProperty(default=False,
                              text="Inherit flag for R2",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    R2 = FloatProperty(default=0.5,
                       text="W3/(W3+W4)",
                       math_text=r"$\large\frac{W_3}{W_3 + W_4}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_R2",
                       inherit_from="parent.based_on.probabilities.R2",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_G1 = BoolProperty(default=False,
                              text="Inherit flag for G1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G1 = FloatProperty(
        default=0.5,
        text="(W22+W23+W24)/(W22+W23+W24+W32+W33+W34+W42+W43+W44)",
        math_text=
        r"$\large\frac{\sum_{j=2}^{4} W_{2j}}{\sum_{i=2}^{4} \sum_{j=2}^{4} W_{ij}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G1",
        inherit_from="parent.based_on.probabilities.G1",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G2 = BoolProperty(default=False,
                              text="Inherit flag for G2",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G2 = FloatProperty(
        default=0.4,
        text="(W32+W33+W34)/(W32+W33+W34+W42+W43+W44)",
        math_text=
        r"$\large\frac{\sum_{j=2}^{4} W_{3j}}{\sum_{i=3}^{4} \sum_{j=2}^{4} W_{ij}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G2",
        inherit_from="parent.based_on.probabilities.G2",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G11 = BoolProperty(default=False,
                               text="Inherit flag for G11",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    G11 = FloatProperty(
        default=0.5,
        text="W22/(W22+W23+W24)",
        math_text=r"$\large\frac{W_{22}}{\sum_{j=2}^{4} W_{2j}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G11",
        inherit_from="parent.based_on.probabilities.G11",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G12 = BoolProperty(default=False,
                               text="Inherit flag for G12",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    G12 = FloatProperty(
        default=0.5,
        text="W23/(W23+W24)",
        math_text=r"$\large\frac{W_{23}}{\sum_{j=3}^{4} W_{2j}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G12",
        inherit_from="parent.based_on.probabilities.G12",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G21 = BoolProperty(default=False,
                               text="Inherit flag for G21",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    G21 = FloatProperty(
        default=0.8,
        text="W32/(W32+W33+W34)",
        math_text=r"$\large\frac{W_{32}}{\sum_{j=2}^{4} W_{3j}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G21",
        inherit_from="parent.based_on.probabilities.G21",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G22 = BoolProperty(default=False,
                               text="Inherit flag for G22",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    G22 = FloatProperty(
        default=0.8,
        text="W33/(W32+W34)",
        math_text=r"$\large\frac{W_{33}}{\sum_{j=3}^{4} W_{3j}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G22",
        inherit_from="parent.based_on.probabilities.G22",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G31 = BoolProperty(default=False,
                               text="Inherit flag for G31",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    G31 = FloatProperty(
        default=0.7,
        text="W42/(W42+W43+W44)",
        math_text=r"$\large\frac{W_{42}}{\sum_{j=2}^{4} W_{4j}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G31",
        inherit_from="parent.based_on.probabilities.G31",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G32 = BoolProperty(default=False,
                               text="Inherit flag for G32",
                               persistent=True,
                               visible=True,
                               set_action_name="update",
                               mix_with=(SetActionMixin, ))
    G32 = FloatProperty(
        default=0.5,
        text="W43/(W43+W44)",
        math_text=r"$\large\frac{W_{43}}{\sum_{j=3}^{4} W_{4j}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G32",
        inherit_from="parent.based_on.probabilities.G32",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    # ------------------------------------------------------------
    #      Initialization and other internals
    # ------------------------------------------------------------

    def __init__(self,
                 W1=0.6,
                 P11_or_P22=0.25,
                 R1=0.5,
                 R2=0.5,
                 G1=0.5,
                 G2=0.4,
                 G11=0.5,
                 G12=0.5,
                 G21=0.8,
                 G22=0.75,
                 G31=0.7,
                 G32=0.5,
                 inherit_W1=False,
                 inherit_P11_or_P22=False,
                 inherit_R1=False,
                 inherit_R2=False,
                 inherit_G1=False,
                 inherit_G2=False,
                 inherit_G11=False,
                 inherit_G12=False,
                 inherit_G21=False,
                 inherit_G22=False,
                 inherit_G31=False,
                 inherit_G32=False,
                 *args,
                 **kwargs):
        super(R1G4Model, self).__init__(R=1, *args, **kwargs)

        with self.data_changed.hold():
            self.W1 = not_none(W1, 0.6)
            self.inherit_W1 = inherit_W1
            self.P11_or_P22 = not_none(P11_or_P22, 0.25)
            self.inherit_P11_or_P22 = inherit_P11_or_P22
            self.R1 = not_none(R1, 0.5)
            self.inherit_R1 = inherit_R1
            self.R2 = not_none(R2, 0.5)
            self.inherit_R2 = inherit_R2
            self.G1 = not_none(G1, 0.5)
            self.inherit_G1 = inherit_G1
            self.G2 = not_none(G2, 0.4)
            self.inherit_G2 = inherit_G2
            self.G11 = not_none(G11, 0.5)
            self.inherit_G11 = inherit_G11
            self.G12 = not_none(G12, 0.5)
            self.inherit_G12 = inherit_G12
            self.G21 = not_none(G21, 0.8)
            self.inherit_G21 = inherit_G21
            self.G22 = not_none(G22, 0.75)
            self.inherit_G22 = inherit_G22
            self.G31 = not_none(G31, 0.7)
            self.inherit_G31 = inherit_G31
            self.G32 = not_none(G32, 0.5)
            self.inherit_G32 = inherit_G32

            self.update()

    # ------------------------------------------------------------
    #      Methods & Functions
    # ------------------------------------------------------------
    def update(self):
        with self.monitor_changes():
            self.mW[0] = self.W1
            self.mW[1] = (1.0 - self.mW[0]) * self.R1
            self.mW[2] = (1.0 - self.mW[0] - self.mW[1]) * self.R2
            self.mW[3] = 1.0 - self.mW[0] - self.mW[1] - self.mW[2]

            W0inv = 1.0 / self.mW[0] if self.mW[0] > 0.0 else 0.0

            if self.mW[0] < 0.5:  # P11 is given
                self.mP[0, 0] = self.P11_or_P22
                Wxx = self.mW[0] * (self.mP[0, 0] -
                                    1) + self.mW[1] + self.mW[2] + self.mW[3]
            else:  # P22 is given
                Wxx = self.P11_or_P22 * (self.mW[1] + self.mW[2] + self.mW[3])

            W1x = Wxx * self.G1  # = W11 + W12 + W13
            Wyx = (Wxx - W1x)  # = W21 + W22 + W23 + W31 + W32 + W33
            W2x = Wyx * self.G2  # = W21 + W22 + W23
            W3x = Wyx - W2x  # = W31 + W32 + W33

            self.mW[1, 1] = self.G11 * W1x
            self.mW[1, 2] = self.G12 * (W1x - self.mW[1, 1])
            self.mW[1, 3] = W1x - self.mW[1, 1] - self.mW[1, 2]

            self.mW[2, 1] = self.G21 * W2x
            self.mW[2, 2] = self.G22 * (W2x - self.mW[2, 1])
            self.mW[2, 3] = W2x - self.mW[2, 1] - self.mW[2, 2]

            self.mW[3, 1] = self.G31 * W3x
            self.mW[3, 2] = self.G32 * (W3x - self.mW[3, 1])
            self.mW[3, 3] = W3x - self.mW[3, 1] - self.mW[3, 2]

            for i in range(1, 4):
                self.mP[i, 0] = 1
                for j in range(1, 4):
                    self.mP[i, j] = self.mW[
                        i, j] / self.mW[i] if self.mW[i] > 0 else 0
                    self.mP[i, 0] -= self.mP[i, j]
                self.mW[i, 0] = self.mW[i] * self.mP[i, 0]

            self.mP[0, 1] = (self.mW[1] - self.mW[1, 1] - self.mW[2, 1] -
                             self.mW[3, 1]) * W0inv
            self.mP[0, 2] = (self.mW[2] - self.mW[1, 2] - self.mW[2, 2] -
                             self.mW[3, 2]) * W0inv
            self.mP[0, 3] = (self.mW[3] - self.mW[1, 3] - self.mW[2, 3] -
                             self.mW[3, 3]) * W0inv

            if self.mW[0] >= 0.5:
                self.mP[0,
                        0] = 1 - self.mP[0, 1] - self.mP[0, 2] - self.mP[0, 3]

            for i in range(4):
                for j in range(4):
                    self.mW[i, j] = self.mW[i] * self.mP[i, j]

            self.solve()
            self.validate()

    pass  # end of class
Example #6
0
class R1G2Model(_AbstractProbability):
    r"""
    Probability model for Reichweite 1 with 2 components.
    
    The 2(=g*(g-1)) independent variables are:
    
    .. math::
        :nowrap:
        
        \begin{flalign*}
            & W_1 \\
            & \text{$P_{11} (W_1 < 0.5)$ or $P_{22} (W_1 > 0.5)$}
        \end{flalign*}
    
    Calculation of the other variables happens as follows:
    
    .. math::
        :nowrap:
        
        \begin{align*}
            & W_2 = 1 – W_1 \\
            & \begin{aligned}
                & \text{$P_{11}$ is given:}  \\
                & \quad P_{12} = 1 - P_{11} \\
                & \quad P_{21} = \frac{W_1 \cdot P_{12}}{W2} \\
                & \quad P_{22} = 1 - P_{21} \\
            \end{aligned}
            \quad \quad
            \begin{aligned}
                & \text{$P_{22}$ is given:} \\
                & \quad P_{21} = 1 - P_{22} \\
                & \quad P_{12} = \frac{W_2 \cdot P_{21}}{W1} \\
                & \quad P_{11} = 1 - P_{12} \\            
            \end{aligned} \\
        \end{align*}
        
    """

    # MODEL METADATA:
    class Meta(_AbstractProbability.Meta):
        store_id = "R1G2Model"

    # PROPERTIES:
    _G = 2

    inherit_W1 = BoolProperty(default=False,
                              text="Inherit flag for W1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))

    W1 = FloatProperty(default=0.0,
                       text="W1",
                       math_text=r"$W_1$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_W1",
                       inherit_from="parent.based_on.probabilities.W1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_P11_or_P22 = BoolProperty(default=False,
                                      text="Inherit flag for P11_or_P22",
                                      persistent=True,
                                      visible=True,
                                      set_action_name="update",
                                      mix_with=(SetActionMixin, ))

    P11_or_P22 = FloatProperty(
        default=0.0,
        text="P11_or_P22",
        math_text=r"$P_{11} %s$ or $\newline P_{22} %s$" %
        (mt_range(0.0, "W_1", 0.5), mt_range(0.5, "W_1", 1.0)),
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_P11_or_P22",
        inherit_from="parent.based_on.probabilities.P11_or_P22",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    # ------------------------------------------------------------
    #      Initialization and other internals
    # ------------------------------------------------------------
    def __init__(self,
                 W1=0.75,
                 P11_or_P22=0.5,
                 inherit_W1=False,
                 inherit_P11_or_P22=False,
                 *args,
                 **kwargs):
        super(R1G2Model, self).__init__(R=1, *args, **kwargs)

        with self.data_changed.hold():
            self.W1 = not_none(W1, 0.75)
            self.inherit_W1 = inherit_W1
            self.P11_or_P22 = not_none(P11_or_P22, 0.5)
            self.inherit_P11_or_P22 = inherit_P11_or_P22

            self.update()

    # ------------------------------------------------------------
    #      Methods & Functions
    # ------------------------------------------------------------
    def update(self):
        with self.monitor_changes():
            self.mW[0] = self.W1
            self.mW[1] = 1.0 - self.mW[0]
            if self.mW[0] <= 0.5:
                self.mP[0, 0] = self.P11_or_P22
                self.mP[0, 1] = 1.0 - self.mP[0, 0]
                self.mP[1, 0] = self.mW[0] * self.mP[0, 1] / self.mW[1]
                self.mP[1, 1] = 1.0 - self.mP[1, 0]
            else:
                self.mP[1, 1] = self.P11_or_P22
                self.mP[1, 0] = 1.0 - self.mP[1, 1]
                self.mP[0, 1] = self.mW[1] * self.mP[1, 0] / self.mW[0]
                self.mP[0, 0] = 1.0 - self.mP[0, 1]

            self.solve()
            self.validate()

    pass  # end of class
Example #7
0
class R1G3Model(_AbstractProbability):
    r"""
    Probability model for Reichweite 1 with 3 components.
    
    The 6 (=g*(g-1)) independent variables are:
    
    .. math::
        :nowrap:
        
        \begin{align*}
                & W_1
                & \text{$P_{11} (W_1 < 0.5)$ or $P_{xx} (W_1 > 0.5)$}
                with P_{xx} = \frac {W_{22} + W_{23} + W_{32} + W_{33} + W_{42} + W_{43}}{W_2 + W_3} \\
                & G_1 = \frac{W_2}{W_2 + W_3}
                & G_2 = \frac{W_{22} + W_{23}}{W_{22} + W_{23} + W_{32} + W_{33}} \\
                & G_3 = \frac{W_{22}}{W_{22} + W_{23}}
                & G_4 = \frac{W_{32}}{W_{32} + W_{33}} \\
        \end{align*}
            
    Calculation of the other variables happens as follows:
    
    
    
    .. math::
        :nowrap:
        
        \begin{align*}
            & \text{Calculate the 'inverted' ratios of $G_2$, $G_3$ and $G_4$ as follows:} \\
            & \quad G_i^{\text{-1}} =
            \begin{cases}
                G_i^{-1} - 1.0, & \text{if } G_i > 0 \\
                0,              & \text{otherwise}
            \end{cases} \quad \forall i \in \left\{ {2, 3, 4}\right\} \\
            & \\
            & \text{Calculate the base weight fractions of each component:} \\
            & \quad W_2 = (1 - W_1) \cdot G_1\\
            & \quad W_3 = 1.0 - W_1 - W_2 \\
            & \\
            & \text{if $W_1 \leq 0.5$:} \\
            & \quad \text{$P_{11}$ is given and W_xx is derived as} \\
            & \quad W_{xx} = W_{22} + W_{23} + W_{32} + W_{23} = W_1 \cdot (1 - P_{11}) + W_2 + W_3 \\ 
            & \\
            & \text{if $W_1 > 0.5$:} \\
            & \quad \text{$P_{xx}$ is given and $P_{11}$ is derived further down} \\
            & \quad W_{xx} = W_{22} + W_{23} + W_{32} + W_{23} = P_{xx} \cdot (W_2 + W_3) \\
            & \\
            & W_{22} = W_{xx} \cdot G_2 \cdot G_3 \\
            & W_{23} = W_{22} \cdot G_3^{-1} \\
            & W_{32} = W_{xx} \cdot (1 - G_2) \\
            & W_{33} = G_4^{-1} \cdot W_{32} \\
            & \\
            & P_{23} = 
            \begin{dcases}
                \dfrac{W_{23}}{W_2}, & \text{if $W_2 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{12} = 1 - P_{22} - P_{23} \\
            & \\
            & P_{32} =
            \begin{dcases}
                \frac{W_{32}}{W_3}, & \text{if $W_3 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{33} =
            \begin{dcases}
                \frac{W_{33}}{W_3}, & \text{if $W_3 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{31} = 1 - P_{32} - P_{33} \\
            & \\
            & P_{12} =
            \begin{dcases}
                \frac{W_2 - W_{22} - W_{32}}{W_1}, & \text{if $W_1 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & P_{13} =
            \begin{dcases}
                \frac{W_3 - W_{23} - W_{33}}{W_1}, & \text{if $W_1 > 0$} \\
                0, & \text{otherwise}
            \end{dcases} \\
            & \\
            & \text{if $W_1 > 0.5$}: \\
            & \quad P_{11} = 1 - P_{12} - P_{13} \\
            & \\
            & \text{Remainder of weight fraction can be calculated as follows:} \\
            & \quad W_{ij} = {W_{ii}} \cdot {P_{ij}} \quad \forall {i,j} \in \left[ {1, 3} \right] \\
        \end{align*}
        
    """

    # MODEL METADATA:
    class Meta(_AbstractProbability.Meta):
        store_id = "R1G3Model"

    # PROPERTIES
    _G = 3

    inherit_W1 = BoolProperty(default=False,
                              text="Inherit flag for W1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    W1 = FloatProperty(default=0.8,
                       text="W1",
                       math_text=r"$W_1$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_W1",
                       inherit_from="parent.based_on.probabilities.W1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_P11_or_P22 = BoolProperty(default=False,
                                      text="Inherit flag for P11_or_P22",
                                      persistent=True,
                                      visible=True,
                                      set_action_name="update",
                                      mix_with=(SetActionMixin, ))
    P11_or_P22 = FloatProperty(
        default=0.7,
        text="P11_or_P22",
        math_text=r"$P_{11} %s$ or $\newline P_{22} %s$" %
        (mt_range(0.0, "W_1", 0.5), mt_range(0.5, "W_1", 1.0)),
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_P11_or_P22",
        inherit_from="parent.based_on.probabilities.P11_or_P22",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G1 = BoolProperty(default=False,
                              text="Inherit flag for G1",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G1 = FloatProperty(default=0.7,
                       text="W2/(W2+W3)",
                       math_text=r"$\large\frac{W_2}{W_3 + W_2}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_G1",
                       inherit_from="parent.based_on.probabilities.G1",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_G2 = BoolProperty(default=False,
                              text="Inherit flag for G2",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G2 = FloatProperty(
        default=0.7,
        text="(W22+W23)/(W22+W23+W32+W33)",
        math_text=
        r"$\large\frac{W_{22} + W_{23}}{W_{22} + W_{23} + W_{32} + W_{33}}$",
        persistent=True,
        visible=True,
        refinable=True,
        store_private=True,
        minimum=0.0,
        maximum=1.0,
        is_independent=True,
        inheritable=True,
        inherit_flag="inherit_G2",
        inherit_from="parent.based_on.probabilities.G2",
        set_action_name="update",
        mix_with=(SetActionMixin, RefinableMixin, InheritableMixin))

    inherit_G3 = BoolProperty(default=False,
                              text="Inherit flag for G3",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G3 = FloatProperty(default=0.7,
                       text="W22/(W22+W23)",
                       math_text=r"$\large\frac{W_{22}}{W_{22} + W_{23}}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_G3",
                       inherit_from="parent.based_on.probabilities.G3",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    inherit_G4 = BoolProperty(default=False,
                              text="Inherit flag for G4",
                              persistent=True,
                              visible=True,
                              set_action_name="update",
                              mix_with=(SetActionMixin, ))
    G4 = FloatProperty(default=0.7,
                       text="W23/(W32+W33)",
                       math_text=r"$\large\frac{W_{22}}{W_{22} + W_{23}}$",
                       persistent=True,
                       visible=True,
                       refinable=True,
                       store_private=True,
                       minimum=0.0,
                       maximum=1.0,
                       is_independent=True,
                       inheritable=True,
                       inherit_flag="inherit_G4",
                       inherit_from="parent.based_on.probabilities.G4",
                       set_action_name="update",
                       mix_with=(SetActionMixin, RefinableMixin,
                                 InheritableMixin))

    # ------------------------------------------------------------
    #      Initialization and other internals
    # ------------------------------------------------------------
    def __init__(self,
                 W1=0.8,
                 P11_or_P22=0.7,
                 G1=0.7,
                 G2=0.7,
                 G3=0.7,
                 G4=0.7,
                 inherit_W1=False,
                 inherit_P11_or_P22=False,
                 inherit_G1=False,
                 inherit_G2=False,
                 inherit_G3=False,
                 inherit_G4=False,
                 *args,
                 **kwargs):
        super(R1G3Model, self).__init__(R=1, *args, **kwargs)

        with self.data_changed.hold():
            self.W1 = not_none(W1, 0.8)
            self.inherit_W1 = bool(inherit_W1)
            self.P11_or_P22 = not_none(P11_or_P22, 0.7)
            self.inherit_P11_or_P22 = bool(inherit_P11_or_P22)
            self.G1 = not_none(G1, 0.7)
            self.inherit_G1 = bool(inherit_G1)
            self.G2 = not_none(G2, 0.7)
            self.inherit_G2 = bool(inherit_G2)
            self.G3 = not_none(G3, 0.7)
            self.inherit_G3 = bool(inherit_G3)
            self.G4 = not_none(G4, 0.7)
            self.inherit_G4 = bool(inherit_G4)

            self.update()

    # ------------------------------------------------------------
    #      Methods & Functions
    # ------------------------------------------------------------
    def update(self):
        with self.monitor_changes():
            self.mW[0] = self.W1
            self.mW[1] = (1 - self.mW[0]) * self.G1
            self.mW[2] = 1.0 - self.mW[0] - self.mW[1]

            W0inv = 1.0 / self.mW[0] if self.mW[0] > 0.0 else 0.0

            Wxx = 0
            if self.mW[0] <= 0.5:  # P00 given
                self.mP[0, 0] = self.P11_or_P22
                # Wxx = W11 + W12 + W21 + W22
                Wxx = self.mW[0] * (self.mP[0, 0] -
                                    1) + self.mW[1] + self.mW[2]
            else:  # Pxx given
                # Wxx = W11 + W12 + W21 + W22
                Wxx = (1.0 - self.mW[0]) * self.P11_or_P22

            # W11 + W12 = Wxx * G2:
            self.mW[1, 1] = Wxx * self.G2 * self.G3
            self.mW[1, 2] = Wxx * self.G2 * (1 - self.G3)
            self.mP[1,
                    1] = self.mW[1,
                                 1] / self.mW[1] if self.mW[1] > 0.0 else 0.0

            self.mW[2, 1] = Wxx * (1 - self.G2) * self.G4
            self.mW[2, 2] = Wxx * (1 - self.G2) * (1 - self.G4)

            self.mP[1, 2] = (self.mW[1, 2] /
                             self.mW[1]) if self.mW[1] > 0.0 else 0.0
            self.mP[1, 0] = 1 - self.mP[1, 1] - self.mP[1, 2]

            self.mP[2, 1] = (self.mW[2, 1] /
                             self.mW[2]) if self.mW[2] > 0.0 else 0.0
            self.mP[2, 2] = (self.mW[2, 2] /
                             self.mW[2]) if self.mW[2] > 0.0 else 0.0
            self.mP[2, 0] = 1 - self.mP[2, 1] - self.mP[2, 2]

            self.mP[0,
                    1] = (self.mW[1] - self.mW[1, 1] - self.mW[2, 1]) * W0inv
            self.mP[0,
                    2] = (self.mW[2] - self.mW[1, 2] - self.mW[2, 2]) * W0inv

            if self.mW[0] > 0.5:
                self.mP[0, 0] = 1 - self.mP[0, 1] - self.mP[0, 2]

            for i in range(3):
                for j in range(3):
                    self.mW[i, j] = self.mW[i, i] * self.mP[i, j]

            self.solve()
            self.validate()

    pass  # end of class
Example #8
0
 class Meta(_AbstractProbability.Meta):
     ind_properties = [
         PropIntel(
             name="W1",
             label="W1",
             math_label=r"$W_1$",
             stor_name="_W1",
             inh_name="inherit_W1",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="P11_or_P22",
             label="P11_or_P22",
             stor_name="_P11_or_P22",
             inh_name="inherit_P11_or_P22",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$P_{11} %s$ or $\newline P_{22} %s$" %
             (mt_range(0.0, "W_1", 0.5), mt_range(0.5, "W_1", 1.0)),
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="R1",
             label="W2/(W2+W3+W4)",
             math_label=r"$\large\frac{W_2}{W_2 + W_3 + W_4}$",
             stor_name="_R1",
             inh_name="inherit_R1",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="R2",
             label="W3)/(W3+W4)",
             math_label=r"$\large\frac{W_3}{W_3 + W_4}$",
             stor_name="_R2",
             inh_name="inherit_R2",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G1",
             label="(W22+W23+W24)/(W22+W23+W24+W32+W33+W34+W42+W43+W44)",
             stor_name="_G1",
             inh_name="inherit_G1",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=
             r"$\large\frac{\sum_{j=2}^{4} W_{2j}}{\sum_{i=2}^{4} \sum_{j=2}^{4} W_{ij}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G2",
             label="(W32+W33+W34)/(W32+W33+W34+W42+W43+W44)",
             stor_name="_G2",
             inh_name="inherit_G2",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=
             r"$\large\frac{\sum_{j=2}^{4} W_{3j}}{\sum_{i=3}^{4} \sum_{j=2}^{4} W_{ij}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G11",
             label="W22/(W22+W23+W24)",
             stor_name="_G11",
             inh_name="inherit_G11",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$\large\frac{W_{22}}{\sum_{j=2}^{4} W_{2j}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G12",
             label="W23/(W23+W24)",
             stor_name="_G12",
             inh_name="inherit_G12",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$\large\frac{W_{23}}{\sum_{j=3}^{4} W_{2j}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G21",
             label="W32/(W32+W33+W34)",
             stor_name="_G21",
             inh_name="inherit_G21",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$\large\frac{W_{32}}{\sum_{j=2}^{4} W_{3j}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G22",
             label="W33/(W32+W34)",
             stor_name="_G22",
             inh_name="inherit_G22",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$\large\frac{W_{33}}{\sum_{j=3}^{4} W_{3j}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G31",
             label="W42/(W42+W43+W44)",
             stor_name="_G31",
             inh_name="inherit_G31",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$\large\frac{W_{42}}{\sum_{j=2}^{4} W_{4j}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID),
         PropIntel(
             name="G32",
             label="W43/(W43+W44)",
             stor_name="_G32",
             inh_name="inherit_G32",
             inh_from="parent.based_on.probabilities",
             is_independent=True,  # flag for the view creation
             math_label=r"$\large\frac{W_{43}}{\sum_{j=3}^{4} W_{4j}}$",
             minimum=0.0,
             maximum=1.0,
             data_type=float,
             **PropIntel.REF_ST_WID)
     ]
     inh_properties = [
         PropIntel(name="inherit_%s" % prop.name, label="Inherit flag for %s" % prop.name,
             data_type=bool, refinable=False, storable=True, has_widget=True,
             widget_type="toggle") \
             for prop in ind_properties
     ]
     properties = ind_properties + inh_properties
     store_id = "R1G4Model"