Problem with fitting Moving average into curve

Viewed 88

I have a problem with the Moving average function in Python. I tried using a couple of methods but they didn't work. Moving average that I used before work well on my light curve, but problem occurs with phased diagram: Phase diagram with fitted moving average

I don't know why it's not working properly. Here is a code I use:

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('phase.txt', dtype='float,float', names=["Time","Flux"])
t = data["Time"]
flux = data["Flux"]

def movingaverage(interval, window_size):
   window = np.ones(int(window_size))/float(window_size)
   return np.convolve(interval, window,'same')

av = movingaverage(flux, 10)

plt.plot(t,flux,"C0.", ms=2)
plt.plot(t,av,"r")
plt.show

I would expect something like this: fitted moving average

Here is my file with values in two columns: https://drive.google.com/file/d/1IQScAo8iduv90wuaor8o9eJT85qBpKY4/view?usp=sharing

2 Answers

I noticed that your time values and the corresponding flux values are not properly sorted. Time values should be ascending.

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('phase.txt', dtype='float,float', names=["Time","Flux"])
t = data["Time"]
flux = data["Flux"]

# sort time values in ascending order
sort_idx = t.argsort()
t = t[sort_idx]

# sort flux values accordingly
flux = flux[sort_idx]


def movingaverage(interval, window_size):
   window = np.ones(int(window_size))/float(window_size)
   return np.convolve(interval, window,'same')

av = movingaverage(flux, 10)

plt.plot(t,flux,"C0.", ms=2)
plt.plot(t,av,"r")
plt.ylim([0.95, 1.05]) # zoom in a little
plt.show()

Does this yield the result you were expecting?


I have tried both, only sorting time values as well as sorting time and flux values.

Only sorting time values: ![enter image description here

Sorting time values and flux values: ![enter image description here

Which one looks more plausible to you?

Your data is quite scattered. Consider averaging over a larger window.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('phase.txt', sep=' ', names=["Time","Flux"]).sort_values(by='Time')
df['MA_10'] = df.Flux.rolling(window=10).mean()
df['MA_100'] = df.Flux.rolling(window=100).mean()

plt.style.use('ggplot')
plt.figure(figsize=(16,9))
plt.scatter(df.Time, df.Flux, c='blue')
plt.plot(df.Time, df.MA_10, c='red')
plt.plot(df.Time, df.MA_100, c='black')
plt.ylim([0.95, 1.05])
plt.show()

enter image description here

Related