1
$\begingroup$

I have asked a question here. Yet I am trying to do the same procedure with AssociationThread Suppose I have the following function which is a normal distribution:

σ = 4.75;
μ = 3.96;
f[x_] := 1/(σ*Sqrt[2 π]) Exp[-(1/2) ((x - μ)/σ)^2]

and I have the following list:

cases={2, 8, 9, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 28, 30, 34, 37, 43, \
55, 59, 90, 129, 180, 229, 276, 349, 396, 446, 571, 794, 1152, 1572, \
1965, 2282, 2679, 3225, 3905, 4814, 5745, 6826, 7860, 9068, 11084, \
13106, 15368, 17975, 20633, 23005, 25443, 28956, 32719, 36802, 40814, \
44865, 48188, 51221, 55551, 60024, 64256, 68271, 71776, 74771, 77456, \
80762, 84179, 87782, 91219, 94644, 97217, 99301, 102374, 105368, \
108129, 110897, 113666, 115476, 116880, 119100, 121384, 123731, \
125852, 127804, 129042, 130001, 131623, 133233, 134671, 136117, \
137141, 138034, 138759, 140004, 141102, 142177, 143070, 143979, \
144612, 145249, 146191, 147209, 148030, 148778, 149483, 149937, \
150322, 150832, 151375, 151917, 152371, 152806, 153147, 153452, \
153900, 154379, 154821, 155135, 155457, 155666, 155872, 156171, \
156481, 156846, 157120, 157391, 157652, 157860, 158139, 158447, \
158735, 158982, 159217, 159367, 159486, 159687, 159891, 160059, \
160209, 160325, 160382, 160391, 160391}
Prob = Table[NIntegrate[f[x], {x, i, i + 1}], {i, 1, 141}];
α = 
  AssociationThread[Table[Subscript[t, i], {i, 1, 141}] -> cases];
β = 
  AssociationThread[Table[Subscript[c, i], {i, 1, 141}] -> cases];
γ = 
  AssociationThread[Table[Subscript[p, i], {i, 1, 141}] -> Prob];

I want to replace the values from alpha, beta and gamma accordingly into the following table:

Table[1/(Subscript[c, j] - (1 - θ) Subscript[t, j])
   Sum[Subscript[p, 
    n] (Subscript[c, n] - (1 - θ) Subscript[t, n]), {n, 2, 
    141}], {j, 1, 140}]

I wonder how one can achieve this?

$\endgroup$

2 Answers 2

1
$\begingroup$

Take a smaller example with the same structure as the one in OP:

k = 4;
cc = Array[Subscript[c, #] &, k];
pp = Array[Subscript[p, #] &, k];
tt = Array[Subscript[t, #] &, k];

You can get your table using:

tbl = (1/Most[cc - (1 - θ) tt]) Rest[pp].Rest[cc - (1 - θ) tt];

TeXForm @ tbl

$\left\{\frac{p_2 \left(c_2-(1-\theta ) t_2\right)+p_3 \left(c_3-(1-\theta ) t_3\right)+p_4 \left(c_4-(1-\theta ) t_4\right)}{c_1-(1-\theta ) t_1},\\\frac{p_2 \left(c_2-(1-\theta ) t_2\right)+p_3 \left(c_3-(1-\theta ) t_3\right)+p_4 \left(c_4-(1-\theta ) t_4\right)}{c_2-(1-\theta ) t_2},\\\frac{p_2 \left(c_2-(1-\theta ) t_2\right)+p_3 \left(c_3-(1-\theta ) t_3\right)+p_4 \left(c_4-(1-\theta ) t_4\right)}{c_3-(1-\theta ) t_3}\right\}$

First, consider associations with symbolic values to see that replacements work as intended:

assoccc = AssociationThread[cc, Array[Subscript[Γ, #] &, k]];
assocpp = AssociationThread[pp, Array[Subscript[ρ, #] &, k]];
assoctt = AssociationThread[tt, Array[Subscript[τ, #] &, k]];

Join the three associations into a single association and use ReplaceAll:

assocjoin = Join[assoccc, assocpp, assoctt];

tbl /. assocjoin

$\left\{\frac{\rho _2 \left(\Gamma _2-(1-\theta ) \tau _2\right)+\rho _3 \left(\Gamma _3-(1-\theta ) \tau _3\right)+\rho _4 \left(\Gamma _4-(1-\theta ) \tau _4\right)}{\Gamma _1-(1-\theta ) \tau _1},\\\frac{\rho _2 \left(\Gamma _2-(1-\theta ) \tau _2\right)+\rho _3 \left(\Gamma _3-(1-\theta ) \tau _3\right)+\rho _4 \left(\Gamma _4-(1-\theta ) \tau _4\right)}{\Gamma _2-(1-\theta ) \tau _2},\\\frac{\rho _2 \left(\Gamma _2-(1-\theta ) \tau _2\right)+\rho _3 \left(\Gamma _3-(1-\theta ) \tau _3\right)+\rho _4 \left(\Gamma _4-(1-\theta ) \tau _4\right)}{\Gamma _3-(1-\theta ) \tau _3}\right\}$

For your example, use k = 141 and replace Array[Subscript[Γ, #] &, k] and Array[Subscript[τ, #] &, k] with cases and replace Array[Subscript[ρ, #] &, k] with Prob:

k = Length @ cases;
cc = Array[Subscript[c, #] &, k];
pp = Array[Subscript[p, #] &, k];
tt = Array[Subscript[t, #] &, k];
tbl = (1/Most[cc - (1 - θ) tt]) Rest[pp].Rest[cc - (1 - θ) tt];

α = AssociationThread[tt, cases];
β = AssociationThread[cc, cases];
γ = AssociationThread[pp, Prob];

tbl2 = tbl /. Join[α, β, γ];
Short[tbl2, 3]

enter image description here

$\endgroup$
-2
$\begingroup$

From your code, it seems that $c=t=$cases, $p=$Prob. If so, $\theta$ is useless, since

$$ \frac{c_i-(1-\theta)t_i}{c_j-(1-\theta)t_j}=\frac{c_i-(1-\theta)c_i}{c_j-(1-\theta)c_j}=\frac{\theta c_i}{\theta c_j}=\frac{c_i}{c_j} $$

Then the code for your table is simply

n = Length[cases]; (* 141 *)

Rest[Inner[Times, Prob, cases, List]].ConstantArray[1/Most[cases], 
  n - 1]

or just

Rest[Prob].Rest[cases]/Most[cases]

If $c\neq t$, then similarly,

Rest[p].Rest[c - (1 - \[Theta]) t]/Most[c - (1 - \[Theta]) t]

Remember you don't need to create an Association in this case, just keep them in the vector (List).

$\endgroup$
1
  • 1
    $\begingroup$ Theta is there exactly for that reason and is not useless. It is a numerical constant between 0 and 1. But for the rest you’re correct. $\endgroup$
    – Wiliam
    Commented Jun 30, 2020 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.