1
$\begingroup$

So I have generated a set of random data points as shown below

sample1 = RandomVariate[BinormalDistribution[{0, 0.4}, {1.2, 1.2}, 0], 250]; 

From the sample, I need to create a list of data points from the data above to fit into the NonlinearModelFit function. And because the NonlinearModelFit function requires the data to be structured in the form of a list of coordinate triples: e.g. {x, y, H[x,y]} where x and y are the pixel locations, and H[x,y] is the histogram value at that x-y coordinate, I need to generate data points.

The function I need to fit is:

A Exp[-((x - μ1)^2 + (y - μ2)^2)/(2 σ^2)] 

Here is what I tried (so I first generated a list from the sample data points)

h1 = HistogramList[sample1, {-4, 4, 0.35}, "PDF"] 

And then I tried to extract the x-y data points (but it wasn't very successful and I also do not know how to get the H[x,y] or function of x,y pairs as well)

h1xy = Table[{h1[[1, j]], h1[[2, j]]}, {j, 1, Length[h1[[1]]] - 1}] 

Would anyone know how to do this that could help me out?

$\endgroup$
1
  • $\begingroup$ If your actual data is from a random sample from some distribution, then you definitely don't want to use NonlinearModelFit and you don't want to extract "data" from the construction of a HistogramList. You want to do what @BobHanlon suggests. $\endgroup$ Commented Mar 6, 2022 at 3:50

2 Answers 2

4
$\begingroup$

You can estimate the distribution from the data using either FindDistributionParameters or EstimatedDistribution.

Clear["Global`*"] dist1 = BinormalDistribution[{0, 0.4}, {1.2, 1.2}, 0]; SeedRandom[1234]; sample1 = RandomVariate[dist1, 250]; 

Your target distribution is

dist2 = BinormalDistribution[{μ1, μ2}, {σ, σ}, 0]; 

The estimate of the parameters of the target distribution is

param = FindDistributionParameters[ sample1, dist2] (* {μ1 -> 0.0574925, σ -> 1.10886, μ2 -> 0.272623} *) 

Alternatively, you get the same distribution using EstimatedDistribution

EstimatedDistribution[sample1, dist2] === (dist2 /. param) (* True *) 
$\endgroup$
3
$\begingroup$
sample1 = RandomVariate[BinormalDistribution[{0, 0.4}, {1.2, 1.2}, 0], 250]; {{bins1, bins2}, hts} = HistogramList[sample1, {-4, 4, 0.35}, "PDF"] f = {b1, b2} |-> Outer[List, Rest@b1, Rest@b2]; (* replace with any choice *) xys = f[bins1, bins2]; Catenate@MapThread[Append, {xys, hts}, 2] (* list of pts *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.