Mathematica contingency table

Trying to create what I consider a contingency table, consider the following:

dist = Parallelize[Table[RandomVariate[NormalDistribution[]], {100000}]]; dist2 = Rest@FoldList [0.95 # + #2 &, 0, dist]; dist3 = Rest@FoldList [0.95 # + Abs[#2] &, 0, dist]; dist4 = {dist2, dist3}\[Transpose] q1 = Flatten[{Quantile[dist2, {1/3, 2/3}], Quantile[dist3, {1/3, 2/3}]}] {-1.39001, 1.33851, 15.0327, 16.6757} 

enter image description here

What I need to do: for each dist4 element, I need to see a window with the image 9 below, which it belongs to:

 for example : {1.55191, 15.7189} belongs to 2 1.55 belongs to 1 and 15.71 belongs to 8 So the intersection is 2. 

I tried If, or Switch, but it should take a long time to write. Is there an automatic way to do this?

+4
source share
4 answers

If I understand the question, I think he is doing this:

 {a, b, c, d} = q1; tbl = Range@9 ~Partition~ 3; f[{x_, y_}] := tbl[[ Which[x > b, 1, x > a, 2, x <= a, 3], Which[y < c, 1, y < d, 2, y >= d, 3] ]] f /@ dist4 // Short 
+3
source
 y[t_] := Piecewise[{{7, t < q1[[1]]}, {4, t <= q1[[2]]}}, 1]; x[t_] := Piecewise[{{0, t < q1[[3]]}, {1, t <= q1[[4]]}}, 2]; {{##}, x[#1] + y[#2]} & @@@ dist4 

Or perhaps using BinLists !:

 k = BinLists[dist4, {Join[{Min[dist4[[All, 1]]]}, q1[[1 ;; 2]], {Max[dist4[[All, 1]]]}]}, {Join[{Min[dist4[[All, 2]]]}, q1[[3 ;; 4]], {Max[dist4[[All, 2]]]}]} ]; Flatten[Replace[ Flatten[MapIndexed[{#1, #2} &, k, {2}], 1], {{x__}, t_} :> (Join[{#}, {9 - 3 First@t + Last@t }] & /@ {x}), {1}], 1] 
+2
source

Have you considered using the step function? Depending on whether you want the output to be {3,2} in the case of slot 8 or actually number 8, the implementation may differ.

 g1[x_] := Piecewise[{{1, x > 1.33851}, {2, 1.33851 >= x > -1.39001}, 3}] g2[x_] := Piecewise[{{1, x < 15.0327}, {2, 15.0327 <= x < 16.6757}}, 3] slotfn[{a_, b_}] := {g2[b], g1[a]} slotnumber[{a_, b_}] := 3 g2[b] + g1[a] 

If the belisarius implementation is actually the same, I will remove it. I note that my non-functional version of no- # ensures that you only pass two arguments, either slotfn or slotnumber .

+1
source

With a separate specification of restrictions:

 limits = { {.1, .15, .5, \[Infinity]}, {1, 2, 3, \[Infinity]} }; cell[l_List] := Table[ Position[ limits[[i]], _?(# > l[[i]] &), 1, 1 ][[1, 1]], {i, 1, Length@l }] 

cell[{.4, 1.5}] then gives {3, 2} . You can convert using:

 (cell[{.4, 1.5}] - {0, 1})*{1, Length[limits[[1]]]} // Plus @@ # & 

which gives 7 .

+1
source

All Articles