4

Trying to build what I believe to be a contingency table, please 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 element of dist4 I need to see with of the 9 box below 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 have tried If, or Switch but it is to long to write. Is there an automatic way to do this ?

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
500
  • 6,509
  • 8
  • 46
  • 80
  • By the way, if you adopt [my `Fold` and `FoldList` modification](http://stackoverflow.com/questions/4198961/what-is-in-your-mathematica-tool-bag/5433867#5433867), you can write: `dist2 = FoldList[0.95 # + #2 &, dist];` – Mr.Wizard Oct 22 '11 at 23:44

4 Answers4

3

If I understand the question, I think this does it:

{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
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
2
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]
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • PS: do I get a time adjustment for doing this on a sleepy Sunday morning? I'm still drinking my coffee. – Verbeia Oct 22 '11 at 23:51
  • 1
    @Verbeia No, you don't get any adjustment since it's still late Saturday evening here, and I won't even see the question for 12 more hours. ;-) – Brett Champion Oct 23 '11 at 01:15
  • belisarius, why do you have `SlotSequence` in your first code block? – Mr.Wizard Oct 24 '11 at 20:42
  • I think you know I know that. I was trying to hint that you'd mixed methods. Anyway, whether you change `/@` to `@@@` or `##` to `#` I think the forms should agree. – Mr.Wizard Oct 28 '11 at 04:23
  • @Mr. I think you know I think you know that. I don't know why you feel it's wrong, but be my guest and correct it if it is so. – Dr. belisarius Oct 28 '11 at 04:35
  • he he he... I think.. Ah nevermind. ;-) I just find the construct `(*stuff*) ## (*stuff*) & /@ (*stuff*)` confusing. The presence of `##` suggests that there is the possibility of more than one argument, and yet `Map` doesn't provide for that. Honestly, what do you think? – Mr.Wizard Oct 28 '11 at 04:40
  • @Mr. Probably I don't get confused because I usually read prefix expressions from right to left, so I know what the arguments are in advance. – Dr. belisarius Oct 28 '11 at 04:58
  • Interesting. So you read most lines of Mathematica code in reverse; is that correct? Still, in this case is there any reason not to change your answer? If you don't have any objection, I will. – Mr.Wizard Oct 28 '11 at 05:04
  • @Mr. No only I read the lines in reverse, I also write (and think) them right to left when I design code bottom-up. I go the other way only with more difficult tasks that deserve top down design. And yes, please edit the answer if you think it is confusing. – Dr. belisarius Oct 28 '11 at 05:18
1

Have you considered using a step function? Depending on whether you want the output to be {3,2} in the case of slot 8, or actually the number 8, the implementation might 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 belisarius's implementation really is the same, I'll delete. I'll note that my non-functional no-# version ensures that you're only passing two arguments to either slotfn or slotnumber.

Verbeia
  • 4,400
  • 2
  • 23
  • 44
1

With a separate limits specification:

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}] will then yield {3, 2}. You can convert with:

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

which yields 7.

Artefacto
  • 96,375
  • 17
  • 202
  • 225