-1
$\begingroup$
Table[{n, FindDistribution[RandomSample[
    Join @@ Table[ConstantArray[k, Binomial[n, k]], {k, 0, n}]
], TargetFunctions -> "Discrete"]}, {n, 15}] // TableForm

returns this table:

1   BinomialDistribution[1,0.5]
2   BinomialDistribution[2,0.5]
3   BinomialDistribution[4,0.375]
4   BinomialDistribution[5,0.4]
5   BinomialDistribution[6,0.416667]
6   BinomialDistribution[7,0.428571]
7   BinomialDistribution[8,0.4375]
8   BinomialDistribution[8,0.5]
9   BinomialDistribution[10,0.450662]
10  BinomialDistribution[11,0.441601]
11  BinomialDistribution[12,0.461921]
12  BinomialDistribution[12,0.500552]
13  BinomialDistribution[13,0.512226]
14  BinomialDistribution[18,0.384842]
15  BinomialDistribution[16,0.473303]

But in fact the data provided should give in the $n$th row BinomialDistribution[n,0.5] since the frequencies literally coincide with those corresponding to the probability formula $\binom nk(0.5)^n$. In some cases (for $n=1,2,8,12$) the answer is very close to it, but in many others quite far. Why?

Can one do something to achieve better results?

$\endgroup$
7
  • 1
    $\begingroup$ You have not described what you are trying to accomplish. Just given the code, we have no way of knowing why it isn't satisfactory. $\endgroup$
    – Bob Hanlon
    Commented Apr 13, 2020 at 14:05
  • $\begingroup$ @BobHanlon You are right, sorry - will add that $\endgroup$ Commented Apr 13, 2020 at 15:40
  • $\begingroup$ The main issue is that you are sampling without replacement and just getting a reordering of the list of numbers. Using RandomChoice rather than RandomSample will get you sampling with replacement. (But that's not the only issue here.) $\endgroup$
    – JimB
    Commented Apr 13, 2020 at 15:57
  • $\begingroup$ @JimB With RandomSample frequencies are as in the "ideal case" - predicted by the PDF: $\binom nk$ copies of $k$, with size of the sample $2^n$. $\endgroup$ Commented Apr 13, 2020 at 15:59
  • $\begingroup$ Then why do you even have RandomSample if it doesn't matter? Also, note that you are attempting to estimate two parameters: $n$ and $p$. $\endgroup$
    – JimB
    Commented Apr 13, 2020 at 16:01

1 Answer 1

4
$\begingroup$

I think you have some misunderstandings about the functions you are using.

Suppose n=3. Then

n=3;
Join @@ Table[ConstantArray[k, Binomial[n, k]], {k, 0, n}]

gets you

{0, 1, 1, 1, 2, 2, 2, 3}

If you apply RandomSample to this list, you'll get a random ordering of the elements: the same number of 1's, the same number of 2's, etc.

FindDistribution assumes independence of the observations so among other things order doesn't matter.

So you'll always get

BinomialDistribution[4, 0.375]

Nothing is wrong with FindDistribution.

Addition:

Maybe I overstated "Nothing is wrong with FindDistribution."

It appears that the documentation could use more details. Using TargetFunctions->{BinomialDistribution} rather than TargetFunctions->"Discrete" gets the reasonable results (although I question that almost zero real datasets have the frequencies exactly matching those with p=1/2).

nn = 14;
data = Join @@ Table[ConstantArray[k, Binomial[nn, k]], {k, 0, nn}];
sol1 = FindDistribution[data, 1, "LogLikelihood", TargetFunctions -> {BinomialDistribution}]
(* {{BinomialDistribution[14, 0.5], -2.04481}} *)
sol2 = FindDistribution[data, 1, "LogLikelihood", TargetFunctions -> "Discrete"]
(* {{BinomialDistribution[17, 0.41605], -2.05258}} *)

Also note that the LogLikelihood option gives the mean of the individual log likelihood contributions rather than the log of the likelihood:

LogLikelihood[BinomialDistribution[sol1[[1, 1, 1]], sol1[[1, 1, 2]]],  data]/Length[data]
(* -2.04481 *)
LogLikelihood[BinomialDistribution[sol2[[1, 1, 1]], sol2[[1, 1, 2]]],   data]/Length[data]
(* -2.05258 *)
$\endgroup$
6
  • $\begingroup$ Sorry for misunderstanding - I've provided additional explanations in the question. I believe in this case BinomialDistribution[3,0.5] would be a better guess, no? $\endgroup$ Commented Apr 13, 2020 at 15:45
  • $\begingroup$ Thanks, the additional explanation is helpful. But you probably want RandomChoice rather than RandomSample as the former samples with replacement and the latter samples without replacement (hence you just get a rearrangement of the numbers). But in any event you're only getting a single sample with a small sample size. There is going to be considerable variability that is inherent and not the fault of FindDistribution. $\endgroup$
    – JimB
    Commented Apr 13, 2020 at 15:55
  • $\begingroup$ Well the sample size increases exponentially, so one would express increasing accuracy - which is not the case. I chose RandomSample to have frequencies exactly equal to those derived from probabilities $\endgroup$ Commented Apr 13, 2020 at 15:57
  • $\begingroup$ Sorry, I cannot understand what do you mean by "almost zero real datasets", could you please explain this a little bit more? $\endgroup$ Commented Apr 14, 2020 at 9:21
  • 1
    $\begingroup$ I mean "unrealistic", "contrived", "lacking any practical use", and "likely useless". But I certainly could be wrong. However, there's no indication in the question as to what real-world problem is to be solved with the code you present. FindDistribution does find distributions and parameters but without a method to estimate measures of precision it's not doing a complete and desirable job. $\endgroup$
    – JimB
    Commented Apr 14, 2020 at 16:45

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