SQL描述(4)

接前一篇用一句SQL計算出進口價格定基價格指數,這一篇將介紹一句SQL搞定 2個指標在不同時差範圍內的相關性係數。

公式如下:Y ,X 分別表示2個指標,這2個指標必須有可比性,最齊碼變化頻率一樣。比如都是一月一次的數據。

L表示時差,即X,Y差L個月。

代碼的樣子:

-----L  以參數的形式傳入,從0 開始到 L的正向序列 
with   lv   as (select LEVEL-1 lv from dual CONNECT BY LEVEL <=11+1), 
       ---C0A0101 cpi 基準指標
       xy   as (select indi_date,max(case when indi_id='C0A0101' then indi_value else null end) as y,
                       ----C0A010D rpi 可選參數  【工業生產者出廠價格指數(PPI同比) C0A0201】【M1同比增速  C070104】
                               max(case when indi_id='C0A010D' then indi_value else null end) as x
              from indi_value where indi_id in ('C0A0101','C0A010D') and indi_date between '20130101' and '20130731'
              group by indi_date),
       --獲取均值   cpi 均值 & rpi 均值
      xyavg as ( select avg(y) as ya,avg(x) as xa from xy where y is not null and x is not null), 
      fxy  as (select  indi_date, y,x,
                       --根據lv用lag函數進行錯位
                       lag(y,lv) OVER(partition  by lv order by  indi_date ) as yy,
                       lag(x,lv) OVER(partition  by lv order by  indi_date ) as xx,
                      (select ya from xyavg ) as ya,(select xa from xyavg) as xa,lv
               from xy join lv on 1=1 where y is not null and x is not null)  
      --L>0 時 y 從第一個元素開始 x 從第 1+L個元素開始
      select lv as L, sum((xx-xa)*(y-ya))/sqrt(sum((xx-xa)*(xx-xa))*sum((y-ya)*(y-ya))) as r
      from fxy where yy is not null and xx is not null
      group by lv 
      union
      --L<0 時,x 從一個元素開始,y 從第1-L個元素開始      
      select -lv as L, sum((x-xa)*(yy-ya))/sqrt(sum((x-xa)*(x-xa))*sum((yy-ya)*(yy-ya))) as r
      from fxy where yy is not null and xx is not null
      group by lv 

上面的公式最主要的東西是變量n,n又和L有關係。n隱含在將指標按數據日期排序後的先後順序中。


這一系列經濟指標分析所用的公式都是一位高人提供。我只是用sql重寫了一遍。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章