# Input of Frequency distribution

**URL:** <https://community.wolfram.com/t/input-of-frequency-distribution/16872>\
**Category:** Questions\
**Tags:** statistics-and-probability\
**Created:** [October 14, 2020, 5:52am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872 "2020-10-14T05:52:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lesoch](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lesoch/32/19645_2.png) [@lesoch](https://community.wolfram.com/u/lesoch)\
**Post date:** [October 14, 2020, 5:52am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/1 "2020-10-14T05:52:01Z")

</div>

How do we represent frequency distribution below in Mathematica?

![dist.jpg](https://global.discourse-cdn.com/wolfram/original/2X/1/1c6ab240754ab5e664d1c6d8ab51f0e830fe1990.jpeg)

I can find the mean, median and mode manually. Just curious to know how can I represent the frequency distribution using Mathematica and from it determine the mean, median and mode.

The values that I determined manually using the respective formulas are mean = 35.8867, median = 33.9444, mode = 28.1364

I tried to approach it in Mathematica 12.1 by using probability distribution:

```
pdf[x_] := Piecewise[{{20, 19.5 <= x < 24.5},
   				{24, 24.5 <= x < 29.5},
   				{45, 29.5 <= x < 39.5},
   				{30, 39.5 <= x < 54.5},
   				{5, 54.5 <= x < 59.5},
   				{4, 59.5 <= x < 69.5}}, 0]

dist = ProbabilityDistribution[pdf[x], {x, 19.5, 69.5}, Method -> "Normalize"];

Mean[dist]

Median[dist]

```

The results that I obtained is mean = 38.9198, median = 37.7738, which are incorrect, and so is my approach.

Anyone can help?

---

<div class="post-metadata">

**Author:** ![claudemante1](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@claudemante1](https://community.wolfram.com/u/claudemante1)\
**Post date:** [October 14, 2020, 3:31pm UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/2 "2020-10-14T15:31:39Z")

</div>

Hello,  
your mean is correct, since

```
Integrate[x PDF[dist, x], {x, 19.5, 69.5}]=38.9198

```

---

<div class="post-metadata">

**Author:** ![lesoch](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lesoch/32/19645_2.png) [@lesoch](https://community.wolfram.com/u/lesoch)\
**Post date:** [October 14, 2020, 10:17pm UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/3 "2020-10-14T22:17:44Z")

</div>

From elementary statistics text, the mean is computed as:

```
sumFreq = Total[{20, 24, 45, 30, 5, 4 }]

mean = Total[{22 x 20, 27 x 24, 34.5 x 45, 47 x 30, 57 x 5, 64.5 x 4}] / sumFreq

```

which gives mean = 35.8867

The median class is 30-39, and the median is computed as

```
median = 29.5 + 10 ((sumFreq /2 - (20 + 24))/45)

```

which gives median = 33.9444

The mode is estimated by

```
mode = 24.5 + (24/5 - 20/5)/((24/5 - 20/5) + (24/5 - 45/10)) 5 = 28.1364

```

My doubt is whether my representation of the uneven frequency distribution using Piecewise and ProbabilityDistribution appropriate to estimate the mean and median? If it is, then

```
Integrate[x PDF[dist, x], {x, 19.5, 69.5}]=38.9198 is also correct.

```

But why the discrepancy from the manual estimation of the mean using the class mid-points?

Thanks for your kind response.

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [October 14, 2020, 11:01pm UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/4 "2020-10-14T23:01:27Z")

</div>

The above mean computation is not properly weighting for interval lengths. So it would actually be:

```
vals = {20, 24, 45, 45, 30, 30, 30, 5, 4, 4};
midpts = {22, 27, 32, 37, 42, 47, 52, 57, 62, 67};
midpts.vals/Total[vals] // N

(* Out[169]= 38.9198 *)

```

---

<div class="post-metadata">

**Author:** ![lesoch](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lesoch/32/19645_2.png) [@lesoch](https://community.wolfram.com/u/lesoch)\
**Post date:** [October 15, 2020, 8:18am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/5 "2020-10-15T08:18:37Z")

</div>

Since my Mean[dist] is correct, so is my Median[dist]?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![claudemante1](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@claudemante1](https://community.wolfram.com/u/claudemante1)\
**Post date:** [October 15, 2020, 8:20am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/6 "2020-10-15T08:20:14Z")

</div>

“My doubt is whether my representation of the uneven frequency distribution using Piecewise and ProbabilityDistribution appropriate to estimate the mean and median?”  
Yes, but other representations are probably better. It’s an issue of Density Estimation.

Your **dist** estimates the density by a piecewise constant function, while the solution proposed by D.L. is the midpoint density estimation. More sophisticated methods consist in fitting by some F the associated distribution function in a convenient basis (polynomials (basic, trigonometrical, Berstein,..), splines, wavelets). Then f:=F’ is some density estimation, Integrate[x f[x]] is the estimation of the mean, x0 such that F[x0]==0.5 is an estimation of the median, etc…  
There is a large literature about this topic.

---

<div class="post-metadata">

**Author:** ![lesoch](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lesoch/32/19645_2.png) [@lesoch](https://community.wolfram.com/u/lesoch)\
**Post date:** [October 15, 2020, 12:57pm UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/7 "2020-10-15T12:57:28Z")

</div>

Thanks for your explanation. Very much appreciated. I will find out more about kernel density estimation. The representation of frequency distribution is in the realm of elementary statistics but the theory behind it, as happens most of the time, is not elementary. Thanks again.

---

<div class="post-metadata">

**Author:** ![shokutaku](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@shokutaku](https://community.wolfram.com/u/shokutaku)\
**Post date:** [October 17, 2020, 1:04am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/8 "2020-10-17T01:04:26Z")

</div>

In an effort to contribute something (useful, I hope) to the community after all these years, I offer this schoolboy approach with yet another result for the weighted average… 34.

But, I have no idea how to add the code from my cloud directory, so I attach the nb file in hopes someone can see it.

[aveMaria.nb](https://community.wolfram.com/uploads/short-url/eegNBPUXZ2rBqUumN8HeEOT4RFD.nb) (10.8 KB)

---

<div class="post-metadata">

**Author:** ![anon21378091](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon21378091](https://community.wolfram.com/u/anon21378091)\
**Post date:** [October 17, 2020, 5:00am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/9 "2020-10-17T05:00:50Z")

</div>

Thanks David. I’m trying to understand your use of ConstantArray in detailing the frequencies. I’m lost.

---

<div class="post-metadata">

**Author:** ![shokutaku](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@shokutaku](https://community.wolfram.com/u/shokutaku)\
**Post date:** [October 17, 2020, 7:44pm UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/10 "2020-10-17T19:44:46Z")

</div>

You are not alone, I am lost somewhere between Wolfram Language, math and physics 97% of the time. I used ConstantArray as a easy way to generate repeated frequency values the length of the mass span. ConstantArray[x,4] → {x,x,x,x} so I just repeated the frequencies for the length of the mass span. That way, all data is weighted properly. I would not recommend trying this with large data sets but for lists of under several hundred elements it is a straightforward method.

Note that I erred in the data table so the proper result is 39. Technically, you can not get more significant digits than the least of your data, so you can only resolve to 39. Here is a corrected code block…

```
Block[{x = {{{20, 24}, 20}, {{25, 29}, 24}, {{30, 39}, 45}, {{40, 54},
      30}, {{55, 59}, 5}, {{60, 69}, 4}}, m, f, a},
 m = Table[Range[x[[i, 1, 1]], x[[i, 1, 2]]], {i, 1, Length@x}];
 f = Table[ConstantArray[x[[i, 2]], Length[m[[i]]]], {i, 1, Length@x}];
 a = Flatten[m].Flatten[f]/Total[Flatten[f]];
 Column[{
   StringForm["Given a mass-frequency table: ``", 
    MatrixForm[x\[Transpose]]],
   StringForm["The table range of masses m \[Rule] ``", 
    m // MatrixForm],
   StringForm["Corresponding frequencies f \[Rule] ``", 
    f // MatrixForm],
   StringForm[
    "Thus m*f/(Total@f) evaluates average a \[TildeTilde] ``", 
    Round[N@a, 1]]
   }]
 ]

```

---

<div class="post-metadata">

**Author:** ![lesoch](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lesoch/32/19645_2.png) [@lesoch](https://community.wolfram.com/u/lesoch)\
**Post date:** [October 18, 2020, 9:26am UTC](https://community.wolfram.com/t/input-of-frequency-distribution/16872/11 "2020-10-18T09:26:49Z")

</div>

Thanks for your time and effort to help explain the codes, David.

What if I change the codes to the following?

```
midpts = Range[20, 69];    
freq = Join[Table[20, {5}], Table[24, {5}], Table[45, {10}], 
   Table[30, {15}], Table[5, {5}], Table[4, {10}]];    
mean = midpts . freq /Total[freq] // N

```

The answer 38.9198 (39 to 2 sig fig) is the same as yours. Please comment.
