How to implement EWMA plots using Python? | Pd EWMA

EWMA stands for Exponentially Weighted Moving Average. It processes data in ... df = pd.read_csv('sales.csv',index_col='Month') df.head().ArchiveWriteforusHowtoimplementEWMAplotsusingPython?JayashreedomalaFollowFeb3·4minreadThisarticlewillhelpyouunderstandEWMAandtheimplementationusingPython.PhotobyLukeChesser(Unsplash)WhatisEWMA?EWMAstandsforExponentiallyWeightedMovingAverage.Itprocessesdatainsuchawaythatitgiveslessimportanceandweightagetothedatawhichisfurtherpastintime.Soitgivesmoreweightagetothedatathatisrecent.WhyisEWMAneeded?ItisabetterversionoftheSMA(Simplemovingaverage)sinceSMAusessmallwindowsforcalculationsleadingtomorenoise.Thereisalaginthestartoftheplotanditneverreachesthepeak.Italsofailstogiveanyinformationaboutfuturebehaviorandonlyshowsthetrends.ThisisthereasonEWMAishelpful.WhatisthemathbehindEWMA?TheformulaforEWMAisasfollows:x(t)=inputvaluew(i)=appliedweightwhichchangesfromi=0toi=ty(t)=outputvalueSotheweighttermw(i)isdefinedaccordingtotheadjustparameter(α).Iftalkingintermsofspan(s)—PeriodorN-dayEWmovingaverage,ifs≥1thenα=2/(s+1).Iftalkingintermsofcenterofmass(c),ifc≥0thenα=1/(1+c).Iftalkingintermsofhalf-life(h)—timetakenfortheweighttoreducetohalf,ifh>0thenα=1—exp(log0.5/h)Iftalkingintermsofsmoothingfactor,αcanbesetdirectly.Iteasytoworkwithspanandsotheplotswillbecreatedusingit.HowtoimplementEWMAusingPython?→ImportingpackagesThebasicpackageslikenumpyandpandasareimportedfordealingwithdata.Tohelpwithplotting,thematplotlibpackageisimported.importnumpyasnpimportpandasaspdimportmatplotlib.pyplotasplt%matplotlibinline→AnalyzedataThedatasetusedwillbesalesdata.Itisaunivariatedatasetthathasthemonthandsalescolumn.Thedataisreadandthe‘Month’columnismentionedastheindexcolumn.df=pd.read_csv('sales.csv',index_col='Month')df.head()Nowthe‘Month’columnischangedtoaDateTimeindexsoastohelpwiththefurtherprocess.df.index=pd.to_datetime(df.index)df.head()→CreatingSMATheSMAcanbecreatedbyaddinganextracolumnandusingthe‘rolling’functionandmentionthewindowas6for6monthsand12for12months


常見投資理財問答


延伸文章資訊