LRydin / MFDFA

Multifractal Detrended Fluctuation Analysis in Python
MIT License
128 stars 27 forks source link

Use our own data #20

Closed Rabia-Raja closed 2 years ago

Rabia-Raja commented 3 years ago

How to run this code on our .csv file or .txt file?

LRydin commented 3 years ago

Hey @Rabia-Raja, I would suggest opening the .csv file with pandas, try something like this:

Reading a csv file

import pandas as pd
data = pd.read_csv('data.csv')

now data.csv might have a lot of columns, so you need to choose the column of your data. Then you need to just transform it to a numpy array with

import numpy as np
data = np.array(data)

and now data is a numpy array

(pandas can be a bit difficult to star with, but do tell me if it doesn't work)

Reading a txt file

If you have a .txt file, just use

import numpy as np
data = np.loadtxt('data.txt')
Rabia-Raja commented 3 years ago

Thankyou so much for quick reply.

import numpy as np data = np.read_txt('data.csv')

After this, where to use this variable 'data' in code and continue the process.

On Tue, Feb 9, 2021 at 6:32 PM Rydin notifications@github.com wrote:

Hey @Rabia-Raja https://github.com/Rabia-Raja, I would suggest opening the .csv file with pandas, try something like this: Reading a csv file

import pandas as pddata = pd.read_csv('data.csv')

now data.csv might have a lot of columns, so you need to choose the column of your data. Then you need to just transform it to a numpy array with

import numpy as npdata = np.array(data)

and now data is a numpy array

(pandas can be a bit difficult to star with, but do tell me if it doesn't work) Reading a txt file

If you have a txt file, just use

import numpy as npdata = np.read_txt('data.csv')

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/LRydin/MFDFA/issues/20#issuecomment-775937306, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJK6FSJCQAI52K6GVXNUCZDS6E2NBANCNFSM4XK7RLDA .

LRydin commented 3 years ago

Alright, so if you now have the data as an numpy array, all you need to do is define a lag. This is the segments needed to use (MF)DFA. The simplest case is something like this:

# The lag
lag = np.unique(np.logspace(0.5, data.size/100, 100).astype(int))

# use MFDFA
lag, dfa = MFDFA(data, lag = lag)

This creates a lag with 100 points, logaritmically spaced, from 3 to data.size / 100, i.e., until 1 / 100 over the total length of the data. The results is saved into the variable dfa.

Rabia-Raja commented 3 years ago

Thanks a lot. I run the code accordingly. Now I got the value of dfa and one graph as well on my data. I have a text file of 3000 rows (stock index). I am attaching a figure. I want to obtain these four graphs along with generalized Hurst exponents Q order(-10 to +10). So, can I get this with your code?

Thanks again for your help.

On Tue, Feb 9, 2021 at 6:43 PM Rydin notifications@github.com wrote:

Alright, so if you now have the data as an numpy array, all you need to do is define a lag. This is the segments needed to use (MF)DFA. The simplest case is something like this:

The laglag = np.unique(np.logspace(0.5, data.size/100, 100).astype(int))

use MFDFAlag, dfa = MFDFA(data, lag = lag)

This creates a lag with 100 points, logaritmically spaced, from 3 to data.size / 100, i.e., until 1 / 100 over the total length of the data. The results is saved into the variable dfa.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/LRydin/MFDFA/issues/20#issuecomment-775945432, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJK6FSOVYTOFZDOPHH7PVR3S6E3X5ANCNFSM4XK7RLDA .

LRydin commented 3 years ago

Yes. If you are looking for different q values (from -10 to 10) all you need to do is the following

# Select a list of powers q
q_list = np.linspace(-10,10,41)
q_list = q_list[q_list!=0.0]

# Obtain the (MF)DFA as
lag, dfa = MFDFA(data, lag = lag, q = q_list, order = 1)

Here you have a code with 40 different points of q from -10 to 10. You can also change the order of the polynomial fitting by changing the order = 1 to the degree of the polynomial you want. The dfa variable now has 2 dimensions: the first dimesion dfa[0:40,:] is the q parameter, the second dfa[:,0:100] is the MFDFA over the lag. This means that for example q=2 is in

# how is the dfa organised? e.g. q=2 is in
print(q_list)
... [-10.   -9.5  -9.   -8.5  -8.   -7.5  -7.   -6.5  -6.   -5.5  -5.   -4.5
     -4.   -3.5  -3.   -2.5  -2.   -1.5  -1.   -0.5   0.5   1.    1.5   2.
     2.5   3.    3.5   4.    4.5   5.    5.5   6.    6.5   7.    7.5   8.
     8.5   9.    9.5  10. ]

# thus
print(q_list[23])
... 2.0

# this means the MFDFA of q=2 is in
dfa[23,:]
LRydin commented 3 years ago

P.S. Did you attach a figure? I can't see it...

I have a text file of 3000 rows (stock index). I am attaching a figure. I want to obtain these four graphs along with generalized

Rabia-Raja commented 3 years ago

Thankyou so much. I will run code according to your instructions.

I am attaching figure again. you can check it.

Thankyou :)

On Tue, Feb 9, 2021 at 7:50 PM Rydin notifications@github.com wrote:

P.S. Did you attach a figure? I can't see it...

I have a text file of 3000 rows (stock index). I am attaching a figure. I want to obtain these four graphs along with generalized

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/LRydin/MFDFA/issues/20#issuecomment-775995724, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJK6FSIYKLU3F27WWMUQSL3S6FDTRANCNFSM4XK7RLDA .

LRydin commented 3 years ago

Dear @Rabia-Raja, do tell me if this worked out.

I suspect you are also interested in getting the singularity spectrum plots, as in (q,H(q)), (q, tau(q)), and (α,D(α)) functions. I have these automatic plotting fuctions ready and written but I they are still in the developer state (in the dev branch), since I still need to write the documentation.

If you are interested in having them as well, I hope to soon enough have the latest MFDFA version out. Then I will tell you how to get these easily with MFDFA.

LRydin commented 2 years ago

I'm closing this, it seems we did what we could here :)