1
$\begingroup$

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.

How would one go about this?

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?

$\endgroup$
  • $\begingroup$ Please show what you've tried so far so we can help you get past where you're currently stuck. $\endgroup$ – JimB Feb 6 at 13:36
  • $\begingroup$ I edited the original post with what I've done so far. $\endgroup$ – indiffer Feb 6 at 14:09
  • $\begingroup$ 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. $\endgroup$ – John Doty Feb 6 at 14:30
  • $\begingroup$ You have some repeat "days passed between each invoice date" values which you should address. $\endgroup$ – MikeY Feb 6 at 14:31
  • 1
    $\begingroup$ Generally, avoid polynomial fits unless you understand why they are treacherous. Fitting Fourier series directly makes more sense, and Fourier fits are tamer beasts. $\endgroup$ – John Doty Feb 6 at 14:36
1
$\begingroup$

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]]

enter image description here

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – indiffer Feb 6 at 14:55
0
$\begingroup$

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]]]

Mathematica graphics

Mathematica graphics

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – indiffer Feb 6 at 15:05
  • $\begingroup$ 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. $\endgroup$ – Hugh Feb 6 at 16:18
  • $\begingroup$ I only have the dates not the hour/min info if that's what you're asking. $\endgroup$ – indiffer Feb 6 at 16:54
  • $\begingroup$ The dates, together with invoice amounts, would be best for a frequency analysis. $\endgroup$ – Hugh Feb 6 at 16:57
  • $\begingroup$ So do you suggest to get rid of fourier transform and go with say just a DATELINEPLOT? $\endgroup$ – indiffer Feb 6 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.