Пример #1
0
def test_cfitz_pandas():
    # 1d
    dta = macrodata.load_pandas().data
    index = date_range(start='1959-01-01', end='2009-10-01', freq='Q')
    dta.index = index
    cycle, trend = cffilter(dta["infl"])
    ndcycle, ndtrend = cffilter(dta['infl'].values)
    assert_allclose(cycle.values, ndcycle, rtol=1e-14)
    assert_equal(cycle.index[0], datetime(1959, 3, 31))
    assert_equal(cycle.index[-1], datetime(2009, 9, 30))
    assert_equal(cycle.name, "infl_cycle")

    # 2d
    cycle, trend = cffilter(dta[["infl", "unemp"]])
    ndcycle, ndtrend = cffilter(dta[['infl', 'unemp']].values)
    assert_allclose(cycle.values, ndcycle, rtol=1e-14)
    assert_equal(cycle.index[0], datetime(1959, 3, 31))
    assert_equal(cycle.index[-1], datetime(2009, 9, 30))
    assert_equal(cycle.columns.values, ["infl_cycle", "unemp_cycle"])
Пример #2
0
 def run(self, data):
     index, col, low, high, drift = (
         self.cfg.get(p) for p in ["index", "col", "low", "high", "drift"]
     )
     df = build_data(data, self.cfg)
     cycle, trend = cffilter(df, low, high, drift)
     df = pd.concat([df, cycle, trend], axis=1, keys=[col, "cycle", "trend"])
     df = df.reset_index()
     return_data, _code = build_base_chart(
         df.fillna(0), index, [col, "cycle", "trend"], agg="raw"
     )
     return return_data
Пример #3
0
def test_cfitz_filter():
    # Test Christiano-Fitzgerald Filter. Results taken from R.
    # NOTE: The Stata mata code and the matlab code it's based on are wrong.
    cfilt_res = array([
        [0.712599537179426, 0.439563468233128],
        [1.06824041304411, 0.352886666575907],
        [1.19422467791128, 0.257297004260607],
        [0.970845473140327, 0.114504692143872],
        [0.467026976628563, -0.070734782329146],
        [-0.089153511514031, -0.238609685132605],
        [-0.452339254128573, -0.32376584042956],
        [-0.513231214461187, -0.314288554228112],
        [-0.352372578720063, -0.258815055101336],
        [-0.160282602521333, -0.215076844089567],
        [-0.0918782593827686, -0.194120745417214],
        [-0.168083823205437, -0.158327420072693],
        [-0.291595204965808, -0.0742727139742986],
        [-0.348638756841307, 0.037008291163602],
        [-0.304328040874631, 0.108196527328748],
        [-0.215933150969686, 0.0869231107437175],
        [-0.165632621390694, -0.0130556619786275],
        [-0.182326839507151, -0.126570926191824],
        [-0.223737786804725, -0.205535321806185],
        [-0.228939291453403, -0.269110078201836],
        [-0.185518327227038, -0.375976507132174],
        [-0.143900152461529, -0.53760115656157],
        [-0.162749541550174, -0.660065018626038],
        [-0.236263634756884, -0.588542352053736],
        [-0.275785854309211, -0.236867929421996],
        [-0.173666515108109, 0.303436335579219],
        [0.0963135720251639, 0.779772338801993],
        [0.427070069032285, 0.929108075350647],
        [0.629034743259998, 0.658330841002647],
        [0.557941248993624, 0.118500049361018],
        [0.227866624051603, -0.385048321099911],
        [-0.179878859883227, -0.582223992561493],
        [-0.428263000051965, -0.394053702908091],
        [-0.381640684645912, 0.0445437406977307],
        [-0.0942745548364887, 0.493997792757968],
        [0.238132391504895, 0.764519811304315],
        [0.431293754256291, 0.814755206427316],
        [0.455010435813661, 0.745567043101108],
        [0.452800768971269, 0.709401694610443],
        [0.615754619329312, 0.798293251119636],
        [1.00256335412457, 0.975856845059388],
        [1.44841039351691, 1.09097252730799],
        [1.64651971120370, 0.967823457118036],
        [1.35534532901802, 0.522397724737059],
        [0.580492790312048, -0.16941343361609],
        [-0.410746188031773, -0.90760401289056],
        [-1.26148406066881, -1.49592867122591],
        [-1.75784179124566, -1.87404167409849],
        [-1.94478553960064, -2.14586210891112],
        [-2.03751202708559, -2.465855239868],
        [-2.20376059354166, -2.86294187189049],
        [-2.39722338315852, -3.15004697654831],
        [-2.38032366161537, -3.01390466643222],
        [-1.91798022532025, -2.23395210271226],
        [-0.982318490353716, -0.861346053067472],
        [0.199047030343412, 0.790266582335616],
        [1.28582776574786, 2.33731327460104],
        [2.03565905376430, 3.54085486821911],
        [2.41201557412526, 4.36519456268955],
        [2.52011070482927, 4.84810517685452],
        [2.45618479815452, 4.92906708807477],
        [2.22272146945388, 4.42591058990048],
        [1.78307567169034, 3.20962906108388],
        [1.18234431860844, 1.42568060336985],
        [0.590069172333348, -0.461896808688991],
        [0.19662302949837, -1.89020992539465],
        [0.048307034171166, -2.53490571941987],
        [-0.0141956981899000, -2.50020338531674],
        [-0.230505187108187, -2.20625973569823],
        [-0.700947410386801, -2.06643697511048],
        [-1.27085123163060, -2.21536883679783],
        [-1.64082547897928, -2.49016921117735],
        [-1.62286182971254, -2.63948740221362],
        [-1.31609762181362, -2.54685250637904],
        [-1.03085567704873, -2.27157435428923],
        [-1.01100120380112, -1.90404507430561],
        [-1.19823958399826, -1.4123209792214],
        [-1.26398933608383, -0.654000086153317],
        [-0.904710628949692, 0.447960016248203],
        [-0.151340093679588, 1.73970411237156],
        [0.592926881165989, 2.85741581650685],
        [0.851660587507523, 3.4410446351716],
        [0.480324393352127, 3.36870271362297],
        [-0.165153230782417, 2.82003806696544],
        [-0.459235919375844, 2.12858991660866],
        [0.0271158842479935, 1.55840980891556],
        [1.18759188180671, 1.17980298478623],
        [2.43238266962309, 0.904011534980672],
        [3.08277213720132, 0.595286911949837],
        [2.79953663720953, 0.148014782859571],
        [1.73694442845833, -0.496297332023011],
        [0.357638079951977, -1.33108149877570],
        [-0.891418825216945, -2.22650083183366],
        [-1.77646467793627, -2.89359299718574],
        [-2.24614790863088, -2.97921619243347],
        [-2.29048879096607, -2.30003092779280],
        [-1.87929656465888, -1.05298381273274],
        [-1.04510101454788, 0.215837488618531],
        [0.00413338508394524, 0.937866257924888],
        [0.906870625251025, 0.92664365343019],
        [1.33869057593416, 0.518564571494679],
        [1.22659678454440, 0.288096869652890],
        [0.79380139656044, 0.541053084632774],
        [0.38029431865832, 1.01905199983437],
        [0.183929413600038, 1.10529586616777],
        [0.140045425897033, 0.393618564826736],
        [0.0337313182352219, -0.86431819007665],
        [-0.269208622829813, -1.85638085246792],
        [-0.687276639992166, -1.82275359004533],
        [-1.00161592325614, -0.692695765071617],
        [-1.06320089194036, 0.803577361347341],
        [-0.927152307196776, 1.67366338751788],
        [-0.786802101366614, 1.42564362251793],
        [-0.772970884572502, 0.426446388877964],
        [-0.81275662801789, -0.437721213831647],
        [-0.686831250382476, -0.504255468075149],
        [-0.237936463020255, 0.148656301898438],
        [0.459631879129522, 0.832925905720478],
        [1.12717379822508, 0.889455302576383],
        [1.48640453200855, 0.268042676202216],
        [1.46515245776211, -0.446505038539178],
        [1.22993484959115, -0.563868578181134],
        [1.0272100765927, 0.0996849952196907],
        [0.979191212438404, 1.05053652824665],
        [1.00733490030391, 1.51658415000556],
        [0.932192535457706, 1.06262774912638],
        [0.643374300839414, -0.0865180803476065],
        [0.186885168954461, -1.24799408923277],
        [-0.290842337365465, -1.80035611156538],
        [-0.669446735516495, -1.58847333561510],
        [-0.928915624595538, -0.932116966867929],
        [-1.11758635926997, -0.307879396807850],
        [-1.26832454569756, -0.00856199983957032],
        [-1.35755577149251, -0.0303537516690989],
        [-1.34244112665546, -0.196807620887435],
        [-1.22227976023299, -0.342062643495923],
        [-1.04601473486818, -0.390474392372016],
        [-0.85158508717846, -0.322164402093596],
        [-0.605033439160543, -0.126930141915954],
        [-0.218304303942818, 0.179551077808122],
        [0.352173017779006, 0.512327303000081],
        [1.01389600097229, 0.733397490572755],
        [1.55149778750607, 0.748740387440165],
        [1.75499674757591, 0.601759717901009],
        [1.56636057468633, 0.457705308377562],
        [1.12239792537274, 0.470849913286519],
        [0.655802600286141, 0.646142040378738],
        [0.335285115340180, 0.824103600255079],
        [0.173454596506888, 0.808068498175582],
        [0.0666753011315252, 0.521488214487996],
        [-0.0842367474816212, 0.0583493276173476],
        [-0.285604762631464, -0.405958418332253],
        [-0.465735422869919, -0.747800086512926],
        [-0.563586691231348, -0.94982272350799],
        [-0.598110322024572, -1.04736894794361],
        [-0.65216025756061, -1.04858365218822],
        [-0.789663117801624, -0.924145633093637],
        [-0.984704045337959, -0.670740724179446],
        [-1.12449565589348, -0.359476803003931],
        [-1.07878318723543, -0.092290938944355],
        [-0.775555435407062, 0.102132527529259],
        [-0.231610677329856, 0.314409560305622],
        [0.463192794235131, 0.663523546243286],
        [1.17416973448423, 1.13156902460931],
        [1.74112278814906, 1.48967153067024],
        [2.00320855757084, 1.42571085941843],
        [1.8529912317336, 0.802460519079555],
        [1.30747261947211, -0.169219078629572],
        [0.540237070403222, -1.01621539672694],
        [-0.177136817092375, -1.3130784867977],
        [-0.611981468823591, -0.982477824460773],
        [-0.700240028737747, -0.344919609255406],
        [-0.572396497740112, 0.125083535035390],
        [-0.450934466600975, 0.142553112732280],
        [-0.494020014254326, -0.211429053871656],
        [-0.701707589094918, -0.599602868825992],
        [-0.94721339346157, -0.710669870591623],
        [-1.09297139748946, -0.47846194092245],
        [-1.08850658866583, -0.082258450179988],
        [-0.976082880696692, 0.235758921309309],
        [-0.81885695346771, 0.365298185204303],
        [-0.63165529525553, 0.384725179378064],
        [-0.37983149226421, 0.460240196164378],
        [-0.0375551354277652, 0.68580913832794],
        [0.361996927427804, 0.984470835955107],
        [0.739920615366072, 1.13195975020298],
        [1.03583478061534, 0.88812510421667],
        [1.25614938962160, 0.172561520611839],
        [1.45295030231799, -0.804979390544485],
        [1.64887158748426, -1.55662011197859],
        [1.78022721495313, -1.52921975346218],
        [1.71945683859668, -0.462240366424548],
        [1.36728880239190, 1.31213774341268],
        [0.740173894315912, 2.88362740582926],
        [-0.0205364331835904, 3.20319080963167],
        [-0.725643970956428, 1.75222466531151],
        [-1.23900506689782, -0.998432917440275],
        [-1.52651897508678, -3.72752870885448],
        [-1.62857516631435, -5.00551707196292],
        [-1.59657420180451, -4.18499132634584],
        [-1.45489013276495, -1.81759097305637],
        [-1.21309542313047, 0.722029457352468]])
    dta = macrodata.load_pandas().data[['tbilrate', 'infl']].values[1:]
    cyc, trend = cffilter(dta)
    assert_almost_equal(cyc, cfilt_res, 8)
    # do 1d
    cyc, trend = cffilter(dta[:, 1])
    assert_almost_equal(cyc, cfilt_res[:, 1], 8)
Пример #4
0
def random_walk_filter(X, low=50, high=300, drift=True):
    # Filter : Centers around zero

    cycle, trend = cffilter(X, low=low, high=high, drift=drift)
    return cycle