# How to find Chi Square and p value?

I want to fit derived distribution on real data using chi square goodness of fit and obtain the values of chi square statistic degrees of freedom and p value for the following data.

 realdata = {1.901, 2.132, 2.203, 2.228, 2.257, 2.35, 2.361, 2.396, 2.397, 2.445,
2.454, 2.474, 2.518, 2.522, 2.525, 2.532, 2.575, 2.614, 2.616, 2.618,
2.624, 2.659, 2.675, 2.738, 2.74, 2.856, 2.917, 2.928, 2.937, 2.937,
2.977, 2.996, 3.03, 3.125, 3.139, 3.145, 3.22, 3.223, 3.235, 3.243,
3.264, 3.272, 3.294, 3.332, 3.346, 3.377, 3.408, 3.435, 3.493, 3.501,
3.537, 3.554, 3.562, 3.628, 3.852, 3.871, 3.886, 3.971, 4.024, 4.027,
4.225, 4.395, 5.02}


The estimated values from the distribution are

B3={1.423, 1.60531, 1.91936, 2.05091, 2.47455, 2.69969, 2.76714,
2.90064, 2.91656, 2.94114, 3.58762, 3.59938, 3.68031, 3.71198,
3.82609, 3.82718, 3.84886, 3.86427, 3.87892, 4.01329, 4.07798,
4.18344, 4.3254, 4.61485, 4.67868, 5.06294, 5.14497, 5.36878,
5.41941, 5.50392, 5.54326, 5.66175, 5.84535, 5.9574, 6.06544,
6.30447, 6.35543, 6.4426, 6.78468, 6.86134, 6.86655, 7.01095,
7.01412, 7.17624, 7.22524, 7.23386, 7.30574, 7.47471, 7.92554,
7.96138, 8.18417, 8.25138, 8.44125, 8.66353, 8.67256, 9.0121,
9.34577, 10.4512, 10.9002, 10.9205, 11.3457, 11.4269, 12.3981}


I tried the following code for obtaining chi square statistic value, degrees of freedom and p value.

pearsonTest[obs_List, exp_List] /;
Dimensions[obs] == Dimensions[exp] :=
Block[{t},
t = Total[(Flatten@obs - Flatten@exp)^2/Flatten@exp] // N; {Rule[
"chisqr", t],
Rule["p-val",
SurvivalFunction[
ChiSquareDistribution[
Times @@
Table[Dimensions[exp][[i]] - 1, {i, Length@Dimensions@exp}]],
t]]}]
pearsonTest[realdata,B3]


I want to know whether the above code is correct in my problem or not.

PearsonChiSquareTest[realdata, B3, "TestDataTable"]

$$\left( \begin{array}{ccc} \text{} & \text{Statistic} & \text{P-Value} \\ \text{Pearson }\chi ^2 & 65.6585 & 3.032157194843353 \,10^{-10} \\ \end{array} \right)$$ clearly indicates that realdata and B3 are poured from different barrels.