what to do to fit gaussian model in scherrer equation in python? - python

i'm new to python and i'm trying to fit the gaussian function into scherrer equation using python and the problem is that i don't know how to do it . similarly with the laurentzian model . can some one explains me how to do it . Thanks
More explanation : for x and y values i want them to be read from a text file and then use them in the process.

If you want a more specific solution you should probably provide an example.
In general, scipy.curve_fit is a great solution for the most fitting problems.
You can find a tutorial about it here. In particular, there is also an example of how to fit a Gaussian function: https://scipy-cookbook.readthedocs.io/items/FittingData.html#Fitting-gaussian-shaped-data.

You might want to take a look here:
Gaussian fit for Python
I have no idea how you get your data, but if you have just the function, try generating values using the function to get something you can actually fit the gauss curve.

Related

Can matplotlib library tell me the function of the data that I have passed?

I am successfully able to create a scatter plot using matplot library in python. I want to know the function that will exist from the data that I have passed (created by program). Basically a function from graph kind of like getting function from data. As per my scatterplot, it is more of an exponential function, not a straight regression line (y = mx+b). Basically looking for function (NOT line) of best fit. Suggestions??
You could be looking into the curve-fitting problem. There are multiple numerical methods to extract the BEST FIT function. SciPy has a curve_fit method to find the best fit using the least squared method.

curve fitting by parts - lmfit Python

I would like to know if in Python, and more precisely, in lmfit library, there is an option for fitting data by parts ? I would like to fit data defined in different ranges and then obtain a unique fit.
Thank you
Without a more concrete example, it is hard to give a concrete answer. But, if I understand your question correctly, you are looking to do a fit to one specific region of your data, then a fit (probably with a different functional form) to another region of your data, and then perhaps combine the multiple regions to get a final fit.
If that is correct, then yes, this can be done with lmfit (and probably with other libraries as well). Let's say you want to fit data that is sort of peak like with an exponential decaying background. First, isolate a region around that peak (it doesn't have to be perfect) and fit a peak (say, Gaussian to that). Then fit an exponential decay to all the data except the peak area. (Aside: numpy.where can be very useful in identifying the regions). Finally, combine the two and fit the whole curve to peak + background.
If that is too vague and doesn't point you in the right direction, please make the question more specific.

online linear regression with forgetting

I need a way to run a linear regression during a simulation in python. New X and y values come in, should be fitted and new coefficient estimates should be made. However, older values should get a lower weight.
Is there a package that can do this?
Short answer here, perhaps more an idea than a solution.
Have you tried scipy.optimize.curve_fit ?
It would do the fitting, but you would still have to code yourself the lower-weightening of the old values before passing it through the absolute_sigma parameter.

How to use statsmodels to fit data

I have a dataset which I need to fit to a GEV distribution. The data is one dimensional, and is stored in a numpy array. Currently, I am using scipy.stats.genextreme.fit(data), which works ok, but gives totally inaccurate results (obvious by plotting the pdf). After some investigation it turns out that my data does not fit well in log space, which scipy uses in its MLE fitting algorithm, so I need to try something like GMM instead which is only available in statsmodels. The problem is that I can't find anything which looks like scipy's fit function. All the examples I've found seem to deal with far more complicated data than I have. Also, statsmodels requires endog and exog parameters for eveything, and I have no idea what these are.
This should be really simple, so I'm sure I'm missing something obvious. Has anyone used statsmodels in this way, and if so, any pointers as to how to do it?
I'm guessing you want Gaussian Mixture Model (GMM) and not Generalized Method of Moments (GMM). The former GMM is available in scikit-learn here. The latter has code in statsmodels, but it's a work in progress.
EDIT Actually it's not clear to me that you want GMM. Maybe you just want a kernel density estimator (KDE). This is available in statsmodels here with an example
Hmm, if you do want to use (Generalized) Method of Moments to fit some kind of probability weighted GEV, then you need to specify the moment conditions, but I don't have a ready example for (G)MM in statsmodels for how you specify the moment conditions. You might be better off asking on the mailing list.

Using PyMC to perform double integration

I need to perform double integration using MCMC method. I have already done it using romberg and doublequad integrations with correct results. I need to also use MCMC integration to compare the results. I found it difficult to understand PyMC.
The outline is this: I have some timeseries data and I need to find out which distribution fits it. I have a set of equations that tells me what to do that involves Double Integration.
Hoping for some guidance.
I'd suggest you start with a simple direct sampling MC and do a trivial 2D integral for which you can obtain the answer by paper and pencil. Then move on to a MCMC for the same integral.

Categories