# Polynomial fit and Fourier transform plot

I'm pretty new to Mathematica so excuse me for obvious questions. I have a dataset, second column contains invoice amount and the first column contains days passed between each invoice date starting at 0 (time data but not uniform spaced).

I'm trying to fit a polynomial function that best represents this data, apply a Fourier transform, and then plot the Fourier transform. I'm mainly aiming to catch weird invoice frequencies using the Fourier transform.

Any help would be appreciated.

Here is the full data: Full Data

I used this code from one of the users here to fit a simple sin curve

len = Subtract @@ data[[{-1, 1}, 1]];
funcfunc = Fit[data, Table[Sin[(π n)/len x], {n, 1, 50}], x];
Show[
ListPlot[data],
Plot[funcfunc, {x, 0, len}, PlotStyle -> {{Thick, Red}}]]


Would an $$n$$th degree polynomial fit be better?

Assuming I go ahead with this code and the sin series fit how would I plot the Fourier transform of the fit?

• Please show what you've tried so far so we can help you get past where you're currently stuck. – JimB Feb 6 '19 at 13:36
• I edited the original post with what I've done so far. – indiffer Feb 6 '19 at 14:09
• I don't see a polynomial fit in your code. You seem to be fitting a Fourier sine series. The coefficients of a Fourier series approximate the Fourier transform of a sampled function. – John Doty Feb 6 '19 at 14:30
• You have some repeat "days passed between each invoice date" values which you should address. – MikeY Feb 6 '19 at 14:31
• Generally, avoid polynomial fits unless you understand why they are treacherous. Fitting Fourier series directly makes more sense, and Fourier fits are tamer beasts. – John Doty Feb 6 '19 at 14:36

Here is something for you to experiment with

data = Import["C:\\Users\\zhk\\Desktop\\data.txt", "Data"];

NF = NonlinearModelFit[data,
a*x^7 + b*x^4 + c*x^3 + d*x^2 + e*x + f, {a, b, c, d, e, f, g}, x];

Show[ListPlot[data],
Plot[NF[x], {x, data[[1, 1]], data[[-1, 1]]}, PlotStyle -> Red]]


FT = FourierTransform[NF[x], x, \[Omega]]

Plot[Evaluate@Abs@FT, {\[Omega], -10, 10}, PlotRange -> All]


The FourierTransform plot doesn't make any sense to me but this is a sample exactly doing what you asked for.

• This is great for a starting point but as you said FourierTransform does not make much sense. I'm guessing the fit needs a bit more peaks like a sine series as @JohnDoty mentioned above. BTW whenever I try to change the NF in your last two lines of code with funcfunc (fit variable from my code) I get an NIntegrate error. What might be the reason for that? – indiffer Feb 6 '19 at 14:55

Here are some thoughts on your data.

data = Import["semerkand.xlsx"][[1]];
data[[1]]


{"DAYS BETWEEN", "AMOUNT"}

Now let's plot the DAYS BETWEEN and the AMOUNT against point number

ListLinePlot[data[[2 ;; -1, 1]]]
ListLinePlot[data[[2 ;; -1, 2]]]
`

So I guess you have sorted your data so that small DAYS BETWEEN are at the start of the data. What appears to happen is that you get a small value invoice followed by a large value invoice followed by a small value invoice...

What are you actually trying to do? There is no point in trying to look at Fourier because you only have 2 points in a cycle. Do you wish to put a mean line through the data to even out the small, large, small fluctuation?

• I haven't arranged the data actually. These are the invoices of a customer starting from 2012. Some small value invoices might be for a different product and that is why there are regular gaps like 28-30 days between invoices and some very unusual amount like 0 days or 1 days. Overall, I'm just trying to do some analysis for accounts receivable of different customers and to identify these kinds of anomalies. My boss suggested this Polynomial Fit and Fourier Transform approach as he is an electrical engineer and thus my questions regarding these. – indiffer Feb 6 '19 at 15:05
• Do you also have data for the actual time the invoice came in? If so this would give a time history which is more straightforward to analyse for periodicy. – Hugh Feb 6 '19 at 16:18
• I only have the dates not the hour/min info if that's what you're asking. – indiffer Feb 6 '19 at 16:54
• The dates, together with invoice amounts, would be best for a frequency analysis. – Hugh Feb 6 '19 at 16:57
• So do you suggest to get rid of fourier transform and go with say just a DATELINEPLOT? – indiffer Feb 6 '19 at 19:49