3
$\begingroup$
RandomFunction[QueueingProcess[3, 5], {0, 15}]
Histogram[data, Automatic, "PDF"]

gives a very nice way to see the histogram based on the specified process. However, my process data comes from the observed values.

I tried to assign

data={{0.0, 0}, {2.0199, 1}, {3.3544, 0}, {6.2484, 1}, {7.0204, 
  0}, {16.6974, 1}, {17.4653, 0}, {33.1508, 1}, {33.5897, 
  2}, {36.3656, 1}, {48.2725, 2}, {57.1227, 1}, {67.6013, 
  0}, {69.2908, 1}, {72.8626, 0}, {86.6029, 1}, {87.6669, 
  0}, {120.7927, 1}, {122.8568, 0}, {125.4026, 1}, {131.7756, 
  0}, {132.8221, 1}, {135.0257, 0}, {140.9808, 1}, {147.2401, 
  0}, {160.6539, 1}, {162.4473, 0}, {170.9659, 1}, {177.8401, 
  2}, {181.3527, 1}, {195.5088, 0}, {205.4589, 1}, {211.7528, 2}}

Where the first item corresponds to the time and the second to the actual value. However, mathematica does not seem to recognize it, taking the first item as the value.

This is a continuous process and I only have records when the value changes, i.e. from 0 to 1, to 2 to 1, etc.

How can I create a Histogram based on such time data?

$\endgroup$
3
  • 1
    $\begingroup$ please note that you can also Upvote (click the up triangle) an answer -- especially when you Accept it:) $\endgroup$
    – kglr
    Commented Nov 2, 2014 at 2:57
  • 1
    $\begingroup$ I know, but it requires registration. I'll try it. Thank you for pointing out! $\endgroup$
    – dark blue
    Commented Nov 2, 2014 at 2:58
  • $\begingroup$ Tangentially related: "Partitioning time series." $\endgroup$ Commented Nov 2, 2014 at 4:31

2 Answers 2

1
$\begingroup$

For instance, if I have a process and it first spends 2 minutes in state 1, then 200 minutes in state 2 and 10 minutes in state 0: {{0,0},{2,1}, {202, 2}, {212,0}} I would want the time be included in the calculation of the relative frequency as opposed to the number of occurences.

Based on this description, I can suggest the following approach:

data = {{0, 0}, {2, 1}, {202, 2}, {212, 0}, {213, 0}, {214, 1}, {230, 
    2}, {252, 0}};

cd = {Total@#[[;; , 1]], #[[1, 2]]} & /@ 
   GatherBy[Transpose[{Differences[data[[;; , 1]]], data[[2 ;;, 2]]}],
     Last];

Plotting total times in minutes:

BarChart[{cd[[;; , 1]]}, ChartLegends -> cd[[;; , 2]], AxesLabel -> "min"]

hist

Plotting relative frequencies of states:

BarChart[{cd[[;; , 1]]/Total[cd[[;; , 1]]]}, 
 ChartLegends -> cd[[;; , 2]], AxesLabel -> "probability"]

hist2

Computing mean from the relative frequencies:

mean = cd[[;; , 2]].cd[[;; , 1]]/Total[cd[[;; , 1]]]
145/84

And an approach using WeightedData with weights being the time counts:

wd = WeightedData @@ Reverse[Transpose[cd]];

BarChart[{wd["EmpiricalPDF"][[2]]}, 
 ChartLegends -> wd["EmpiricalPDF"][[1]], AxesLabel -> "probability"]
Mean[wd]
Variance[wd]

bc

145/84

3251/1750
$\endgroup$
6
  • $\begingroup$ Alexey, thank you for your reply. I really appreciate it. It seems like I need to adjust this to account for many different values (beyond 0,1,2). Would you please let me know how to allow for more values? $\endgroup$
    – dark blue
    Commented Nov 2, 2014 at 13:20
  • 1
    $\begingroup$ The described approach is very general and does not depend on the number of values and/or their types, in accordance with the spirit of Mathematica the language. You can use arbitrary number of states and describe them not necessarily as Integers: Strings will work the same way, no changes in the code are needed. $\endgroup$ Commented Nov 2, 2014 at 13:26
  • $\begingroup$ Alexey, is it possible to convert the value (i.e. the y axis) to reflect probability (i.e. relative frequency)? $\endgroup$
    – dark blue
    Commented Nov 2, 2014 at 13:34
  • 1
    $\begingroup$ I updated the answer with the code for plotting relative frequencies. As to the mean and variance, it is not completely clear to me how should we understand them in this context. It seems that some model of the underlying random process is needed in order to produce the definitions of these quantities. If you have such definitions, please include them in the question. $\endgroup$ Commented Nov 2, 2014 at 17:17
  • 1
    $\begingroup$ I added WeightedData-based approach. I am not sure however what assumptions this approach implies. $\endgroup$ Commented Nov 2, 2014 at 17:57
3
$\begingroup$

RandomFunction produces a TemporalData object. The action of Histogram on TemporalData is to just do a histogram of the values (ignoring time stamps).

td = RandomFunction[QueueingProcess[3, 5], {0, 15}];
GraphicsRow[{Histogram[td], Histogram[td["Values"]]}]

enter image description here

Thus, you can either wrap your data in TemporalData or just make a histogram of the values without the time stamps.

GraphicsRow[{Histogram[TemporalData@data], Histogram[data[[All, 2]]]}]

enter image description here

EDIT

To accomplish more what I think you are after you can weight the observations by the amount of time spent at each state. This will ignore the last state because we don't know how long you stopped there.

Histogram[WeightedData[data[[1 ;; -2, 2]], Differences@data[[All, 1]]]]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Andy, Thank you very much for your help. Do I understand it correctly that both approaches won't give true histogram? For instance, if I have a process and it first spends 2 minutes in state 1, then 200 minutes in state 2 and 10 minutes in state 0: {{0,0},{2,1}, {202, 2}, {212,0}} I would want the time be included in the calculation of the relative frequency as opposed to the number of occurences. $\endgroup$
    – dark blue
    Commented Nov 2, 2014 at 2:57
  • $\begingroup$ You do understand that correctly. $\endgroup$
    – Andy Ross
    Commented Nov 2, 2014 at 16:29

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.