1

I am trying to obtain an equation for a function fitted to some histogram data, I was thinking of trying to do this by fitting a rational function as the data doesn't resemble any distribution recognisable by myself.

The data is experimental, and I want to be able to generate a random number according to its distribution. Hence I am hoping to be able to fit it to some sort of PDF from which I can obtain a CDF, which can be rearranged to a function into which a uniformly distributed random number between 0 and 1 can be substituted in order to obtain the desired result.

I have attempted to use the histfit function, which has worked but I couldn't figure out how to obtain an equation for the curve it fitted. Is there something stupid I have missed?

Update: I have discovered the function rationalfit, however I am struggling to figure out what the inputs need to be.

Further Update: Upon exploring the histfit command further I have discovered the option to fit it to a kernal, the figure for which looks promising, however I am only able to obtain a set of x and y values for the curve, not its equation as a I wanted.

  • 1
    Noticed that `histfit` supose that your data follow a normal distribution. So you still need to know where your data come from and what is the best distribution for those data. – obchardon Aug 19 '19 at 15:36
  • You can specify a distribution other than normal, e.g. `histfit(b,10,'beta')` for a beta distribution. – am304 Aug 19 '19 at 15:53
  • 1
    Depending on your data, it may be appropriate to estimate the empirical distribution using the methods in [this answer](https://stackoverflow.com/a/56759220/8239061) if you don't want to assume a theoretical distribution. Of course, this depends on the data and the application context. – SecretAgentMan Aug 19 '19 at 16:12
  • What are you going to do with the equation? Why are you fitting? I can provide some approaches but my response depends on these things. – SecretAgentMan Aug 19 '19 at 16:15
  • I have a set of experimental data, which can vary greatly depending on certain parameters of the experiment. I am looking for a method to generically find a PDF so I can find a CDF that I can rearrange to get a function that can be used to generate a function according to that distribution. – I suck at Maths Aug 19 '19 at 16:26

1 Answers1

2

From the documentation on histfit:

Algorithms

histfit uses fitdist to fit a distribution to data. Use fitdist to obtain parameters used in fitting.

So the answer to your question is to use fitdist to get the parameters you're after. Here's the example from the documentation:

rng default; % For reproducibility
r = normrnd(10,1,100,1);
histfit(r)

enter image description here

pd = fitdist(r,'Normal')

pd = 
  NormalDistribution

  Normal distribution
       mu = 10.1231   [9.89244, 10.3537]
    sigma =  1.1624   [1.02059, 1.35033]
am304
  • 13,758
  • 2
  • 22
  • 40
  • Hello, my data is slightly horrible looking and doesn't resemble any distribution that I can recognise, which is why I was hopping that I could try to fit it to a rational function. Is there a way to do this? – I suck at Maths Aug 19 '19 at 15:59