{"id":2599,"date":"2020-04-25T00:11:07","date_gmt":"2020-04-24T15:11:07","guid":{"rendered":"https:\/\/julialang.kr\/?p=2599"},"modified":"2020-06-30T02:30:49","modified_gmt":"2020-06-29T17:30:49","slug":"pythonpandasewm-pandas-ewm-%ec%9d%98-%ec%9d%b4%ed%95%b4","status":"publish","type":"post","link":"https:\/\/julialang.kr\/?p=2599","title":{"rendered":"[Python,Pandas,EWM] pandas ewm \uc758 \uc774\ud574"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>ewm(\n    com=None,\n    span=None,\n    halflife=None,\n    alpha=None,\n    min_periods=0,\n    adjust=True,\n    ignore_na=False,\n    axis=0,\n)<\/code><\/pre>\n\n\n\n<p>Decay factor :  \ud835\udefc <\/p>\n\n\n\n<p>com : center of mass|<br>  \ud835\udefc=1\/(1+\ud835\udc50\ud835\udc5c\ud835\udc5a),&nbsp;for&nbsp;\ud835\udc50\ud835\udc5c\ud835\udc5a\u22650  <\/p>\n\n\n\n<p>span : window \ud06c\uae30 \ub610\ub294 \uae30\uac04<br>\ud835\udefc : \ud3c9\ud65c\uacc4\uc218(EP)<br>\uae08\uc77c\uc758 \uc9c0\uc218\uc774\ub3d9\ud3c9\uade0 = (\uae08\uc77c \uc885\uac00* \ud835\udefc ) + \uc804\uc77c\uc758 \uc9c0\uc218\uc774\ub3d9\ud3c9\uade0*(1- \ud835\udefc )<br> \ud835\udefc=2\/(\ud835\udc60\ud835\udc5d\ud835\udc4e\ud835\udc5b+1),&nbsp;for&nbsp;\ud835\udc60\ud835\udc5d\ud835\udc4e\ud835\udc5b\u22651 <\/p>\n\n\n\n<p>haflife : \ubc18\uac10\uae30<br> \ud835\udefc=1\u2212\ud835\udc52\ud835\udc65\ud835\udc5d(\ud835\udc59\ud835\udc5c\ud835\udc54(0.5)\/\u210e\ud835\udc4e\ud835\udc59\ud835\udc53\ud835\udc59\ud835\udc56\ud835\udc53\ud835\udc52),&nbsp;for&nbsp;\u210e\ud835\udc4e\ud835\udc59\ud835\udc53\ud835\udc59\ud835\udc56\ud835\udc53\ud835\udc52&gt;0 <\/p>\n\n\n\n<p>alpha : \ud3c9\ud65c \uacc4\uc218(smoothing factor)<br> \ud835\udefc  : \uc9c1\uc811 \uc785\ub825<br> 0&lt;\ud835\udefc\u22641 <\/p>\n\n\n\n<p>min_perods :  \uc8fc\uc5b4\uc9c4 window\ub0b4\uc5d0 \uc788\uc5b4\uc57c\ud560 \uad00\uce21\uce58\uc758 \ucd5c\uc18c \uac2f\uc218<\/p>\n\n\n\n<p>adjust : True or False<\/p>\n\n\n\n<p>Pandas\uc758 EWMA \uac1c\ub150\uc740 \uc544\ub798 \ub9c1\ud06c\ub97c \ucc38\uc870<\/p>\n\n\n\n<p><a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/user_guide\/computation.html\">https:\/\/pandas.pydata.org\/pandas-docs\/stable\/user_guide\/computation.html<\/a><\/p>\n\n\n\n<p>1 + (1-a) + (1-a)^2 + ,&#8230; \uc758 \ubb34\ud55c \uae09\uc218\ub294 1\/a \uc774\ub2e4.<br>\uc989 Adjust\uac00 False\uc778 \uacbd\uc6b0\ub294 \ubb34\ud55c\uae09\uc218\uc758 \uacbd\uc6b0 \uc774\uba70 \ub370\uc774\ud130\uac00 \ub9ce\uc740 \uacbd\uc6b0\uc5d0\ub294 False\ub85c \ud574\ub3c4 \ub41c\ub2e4.<br>\ub370\uc774\ud130\uac00 \uc801\uc740 \uacbd\uc6b0 Adjust\ub97c True \ud558\ub294 \uac83\uc774 \ub354 \uc815\ud655\ud568<br><a href=\"https:\/\/www.wolframalpha.com\/input\/?i=sum+%281+-+x%29%5Ei+i+from+0+to+infinite\">https:\/\/www.wolframalpha.com\/input\/?i=sum+%281+-+x%29%5Ei+i+from+0+to+infinite<\/a><\/p>\n\n\n\n<p>\ucf54\ub4dc \uad6c\ud604\uc740 \uc544\ub798 \ucc38\uc870<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def myEWMA(x, winsize, adjust=False):\n  # for loop count\n  t = len(x) - 1\n  alpha = 2.0 \/ (1.0 + winsize)\n  p_t = np.zeros(t + 1)\n  if adjust:\n    p = 0.0\n    adj = np.zeros(t + 1)\n    a = 0.0\n    for i in range(t+1):\n      p = (x[i] + (1.0-alpha)*p)\n      p_t[i] = p\n      p = p_t[i]\n      a += np.exp(i * np.log(1.0 - alpha))\n      p_t[i] \/=  a\n  else:\n    p_t[0] = x[0]\n    for i in range(1,t+1):\n      p_t[i] = alpha * x[i] + (1. - alpha)*p_t[i-1]\n\n  return p_t\n\na = np.array([10,20,30,40,50,60,70,80,90,100])\nprint(myEWMA(a,2,adjust=False))<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[10.         16.66666667 25.55555556 35.18518519 45.0617284  55.02057613\n 65.00685871 75.00228624 85.00076208 95.00025403]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>myEWMA(a,2,adjust=True)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>array([10.        , 17.5       , 26.15384615, 35.5       , 45.20661157,\n       55.08241758, 65.03202196, 75.01219512, 85.00457271, 95.00169354])<\/code><\/pre>\n\n\n\n<p>\uc704\uc758 \uacb0\uacfc\uc640 \uc544\ub798 pandas\uc640 \ube44\uad50 \ud558\uba70 \ub3d9\uc77c\ud568\uc744 \uc54c \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\ntt = pd.Series(range(tidx.shape[0]), index=tidx.index)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>tt.ewm(span=2,adjust=False).mean()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0    10.000000\n1    16.666667\n2    25.555556\n3    35.185185\n4    45.061728\n5    55.020576\n6    65.006859\n7    75.002286\n8    85.000762\n9    95.000254\ndtype: float64<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>tt.ewm(span=2,adjust=True).mean()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0    10.000000\n1    17.500000\n2    26.153846\n3    35.500000\n4    45.206612\n5    55.082418\n6    65.032022\n7    75.012195\n8    85.004573\n9    95.001694\ndtype: float64<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Decay factor : \ud835\udefc com : center of mass| \ud835\udefc=1\/(1+\ud835\udc50\ud835\udc5c\ud835\udc5a),&nbsp;for&nbsp;\ud835\udc50\ud835\udc5c\ud835\udc5a\u22650 span : window \ud06c\uae30 \ub610\ub294 \uae30\uac04\ud835\udefc : \ud3c9\ud65c\uacc4\uc218(EP)\uae08\uc77c\uc758 \uc9c0\uc218\uc774\ub3d9\ud3c9\uade0 = (\uae08\uc77c \uc885\uac00* \ud835\udefc ) + \uc804\uc77c\uc758 \uc9c0\uc218\uc774\ub3d9\ud3c9\uade0*(1- \ud835\udefc ) \ud835\udefc=2\/(\ud835\udc60\ud835\udc5d\ud835\udc4e\ud835\udc5b+1),&nbsp;for&nbsp;\ud835\udc60\ud835\udc5d\ud835\udc4e\ud835\udc5b\u22651 haflife : \ubc18\uac10\uae30 \ud835\udefc=1\u2212\ud835\udc52\ud835\udc65\ud835\udc5d(\ud835\udc59\ud835\udc5c\ud835\udc54(0.5)\/\u210e\ud835\udc4e\ud835\udc59\ud835\udc53\ud835\udc59\ud835\udc56\ud835\udc53\ud835\udc52),&nbsp;for&nbsp;\u210e\ud835\udc4e\ud835\udc59\ud835\udc53\ud835\udc59\ud835\udc56\ud835\udc53\ud835\udc52&gt;0 alpha : \ud3c9\ud65c \uacc4\uc218(smoothing factor) \ud835\udefc : \uc9c1\uc811 \uc785\ub825 0&lt;\ud835\udefc\u22641 min_perods : \uc8fc\uc5b4\uc9c4 window\ub0b4\uc5d0 \uc788\uc5b4\uc57c\ud560 \uad00\uce21\uce58\uc758 \ucd5c\uc18c \uac2f\uc218 adjust : True [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[26,1],"tags":[],"_links":{"self":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2599"}],"collection":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2599"}],"version-history":[{"count":9,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2599\/revisions"}],"predecessor-version":[{"id":2706,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2599\/revisions\/2706"}],"wp:attachment":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}