How would I calculate a continuous PDF from discrete data using Python? - python

I have some discrete data of the beta spectrum of Bismuth-210, which I would like to try to fit a continuous PDF to so I can improve the accuracy of a simulation I'm building. Is there anything in Python that allows me to input discrete PDF data and output a function for a continuous distribution? I'm looking to be able to derive a mathematical function, i.e. f(x)= ax+b (obviously not actually of this form, but you get the point). I haven't been able to try too much because I'm so lost as to where to start, but I tried creating a piecewise function of the data, which is not sufficiently smooth (nor succinct) for my purposes.

Related

How to find the function's equation using Neural Network (Jupyter notebook)

I'm looking for some guidance as apparently there isn't too much information about it.
I would like to know, if there's any documentation available to extract an function's equation from a given a dataset, or if someone can guide me how to obtain the equation to get all the points from a given data.
I know there are 2 methods:
Universal Approximation Universal Approximation.
Regression using Neural Networks
In both cases, it seems that doesn't fits what I need do it. Basically I have this function:
In which I would like to obtain an equation that satisfies this function F(x) = ?
Reducing the noise, I got the following graph:
My question is: is it possible to obtain a function through the graph only?
The input data is just a velocity vector in kilometers per hour.

use FFT spectrum to define a lambda function

my problem is the following: I have N different measurements of a quantity which depends on two other quantities that I also know. I would like to use find a function two variable function that approximates the data, and I thought that using Fourier transforms was a nice idea.
Does anybody has a suggestion on how should I proceed? I think as a first step I want to do a FFT of my data, but then how can I implement the inverse FT not only for the points where I measured but for any pair (x,y) as input?
Thanks a lot.
(I am using python).

How can I statistically compare a lightcurve data set with the simulated lightcurve?

With python I want to compare a simulated light curve with the real light curve. It should be mentioned that the measured data contain gaps and outliers and the time steps are not constant. The model, however, contains constant time steps.
In a first step I would like to compare with a statistical method how similar the two light curves are. Which method is best suited for this?
In a second step I would like to fit the model to my measurement data. However, the model data is not calculated in Python but in an independent software. Basically, the model data depends on four parameters, all of which are limited to a certain range, which I am currently feeding mannualy to the software (planned is automatic).
What is the best method to create a suitable fit?
A "Brute-Force-Fit" is currently an option that comes to my mind.
This link "https://imgur.com/a/zZ5xoqB" provides three different plots. The simulated lightcurve, the actual measurement and lastly both together. The simulation is not good, but by playing with the parameters one can get an acceptable result. Which means the phase and period are the same, magnitude is in the same order and even the specular flashes should occur at the same period.
If I understand this correctly, you're asking a more foundational question that could be better answered in https://datascience.stackexchange.com/, rather than something specific to Python.
That said, as a data science layperson, this may be a problem suited for gradient descent with a mean-square-error cost function. You initialize the parameters of the curve (possibly randomly), then calculate the square error at your known points.
Then you make tiny changes to each parameter in turn, and calculate how the cost function is affected. Then you change all the parameters (by a tiny amount) in the direction that decreases the cost function. Repeat this until the parameters stop changing.
(Note that this might trap you in a local minimum and not work.)
More information: https://towardsdatascience.com/implement-gradient-descent-in-python-9b93ed7108d1
Edit: I overlooked this part
The simulation is not good, but by playing with the parameters one can get an acceptable result. Which means the phase and period are the same, magnitude is in the same order and even the specular flashes should occur at the same period.
Is the simulated curve just a sum of sine waves, and are the parameters just phase/period/amplitude of each? In this case what you're looking for is the Fourier transform of your signal, which is very easy to calculate with numpy: https://docs.scipy.org/doc/scipy/reference/tutorial/fftpack.html

Curve Fitting in Python for extrapolation, Regression analysis

This question is regarding curve fitting in python.
First, I would say that I do not know the curve fit function to insert into "curve_fit" function in the scipy library; therefore, I am trying to use a polyfit which is OK if I am interested in interpolation but my goal is to predict values at future points, in other words extrapolation.
I have attached a screenshot of a raw signal, smoothed and its polyfit result. It has the correct poly order but still fails at extrapolation. My conclusion is that poly fit is not the right approach here, but I can not estimate the curve function. What are you thoughts?
Please note that this is not a distribution since the y values may keep slowly decreasing infinitely, even below 0.
I'd say the function looks like an exponential Gaussian but again it's not a distribution so dont want to do that.
My last thought was to split the plot into two, the first model can certainly be modeled as a polynomial and the second as an exponential. (values are different than first png cuz it's of a different signal).
Then, maybe combine the two. What do you think about this?
Attached is a screenshot of this too.
Since many curves can fit the data and extrapolate differently, you need to choose the right basis functions to get the behaviour you want.
So far you have tried polynomials for instance, these however tend to +- infinite, which is perhaps not what you want.
I would try and use curve_fit on a sum of Hermite polynomials or Laguerre polynomials. For instance, for Laguerre polynomials, you could try
a + b*exp(-k x) + c*(1-x)*exp(-k x) + d*(x^2 - 4*x + 2)*exp(-k x) + ...
Python has a lot of convenience functions built in for this, see e.g. https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.polynomials.laguerre.html
Note however that you should also fit k to your data, which you could use curve_fit for.

Matlab sine curve fitting with linear increase

I have multiple sets of data, each of which have a roughly sinusoidal shape. I have attached a photo of one such data Roughly sinusoidal data
I want to fit an equation of the form y = Asin(kx+b)+mx+c. Basically the data is a sine wave with a linear increase. Is there any simple way of doing this, preferably without toolboxes as I have about 20 of these data to sort through?
Thank you.
I am using matlab, however I am open to python too :).
What you want to do is to use regression in your function to fit the data.
One way to do this is to apply Non-linear least squares to find the parameters that minimize the square error for your function.
Take a look at the matlab function lsqcurvefit (http://es.mathworks.com/help/optim/ug/lsqcurvefit.html)

Categories

Resources