<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from pandas import * <EOL> import numpy as np <EOL> import os <EOL> class Future ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , series , code = None ) : <EOL> """<STR_LIT>""" <EOL> self . series = series . dropna ( ) <EOL> self . settleDate = self . series . index [ - <NUM_LIT:1> ] <EOL> self . dt = len ( self . series ) <EOL> self . code = code <EOL> def monthNr ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . code . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] ) <EOL> def dr ( self , date ) : <EOL> """<STR_LIT>""" <EOL> return ( sum ( self . series . index > date ) ) <EOL> def price ( self , date ) : <EOL> """<STR_LIT>""" <EOL> return self . series . get_value ( date ) <EOL> def returns ( df ) : <EOL> """<STR_LIT>""" <EOL> return ( df / df . shift ( <NUM_LIT:1> ) - <NUM_LIT:1> ) <EOL> def recounstructVXX ( ) : <EOL> """<STR_LIT>""" <EOL> dataDir = os . path . expanduser ( '<STR_LIT>' ) + '<STR_LIT>' <EOL> X = DataFrame . from_csv ( dataDir + '<STR_LIT>' ) <EOL> futures = [ ] <EOL> codes = X . columns <EOL> endDates = [ ] <EOL> for code in codes : <EOL> f = Future ( X [ code ] , code = code ) <EOL> print code , '<STR_LIT::>' , f . settleDate <EOL> endDates . append ( f . settleDate ) <EOL> futures . append ( f ) <EOL> endDates = np . array ( endDates ) <EOL> for i in range ( <NUM_LIT:1> , len ( futures ) ) : <EOL> futures [ i ] . dt = futures [ i ] . dr ( futures [ i - <NUM_LIT:1> ] . settleDate ) <EOL> idx = X . index <EOL> Y = DataFrame ( index = idx , columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> W = DataFrame ( data = np . zeros ( X . values . shape ) , index = idx , columns = X . columns ) <EOL> for date in idx : <EOL> i = np . nonzero ( endDates >= date ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> first = futures [ i ] <EOL> second = futures [ i + <NUM_LIT:1> ] <EOL> dr = first . dr ( date ) <EOL> dt = first . dt <EOL> W . set_value ( date , codes [ i ] , <NUM_LIT:100> * dr / dt ) <EOL> W . set_value ( date , codes [ i + <NUM_LIT:1> ] , <NUM_LIT:100> * ( dt - dr ) / dt ) <EOL> p1 = first . price ( date ) <EOL> p2 = second . price ( date ) <EOL> w1 = <NUM_LIT:100> * dr / dt <EOL> w2 = <NUM_LIT:100> * ( dt - dr ) / dt <EOL> Y . set_value ( date , '<STR_LIT>' , p1 ) <EOL> Y . set_value ( date , '<STR_LIT>' , p2 ) <EOL> Y . set_value ( date , '<STR_LIT>' , first . dr ( date ) ) <EOL> Y . set_value ( date , '<STR_LIT>' , w1 ) <EOL> Y . set_value ( date , '<STR_LIT>' , w2 ) <EOL> Y . set_value ( date , '<STR_LIT>' , ( p1 * w1 + p2 * w2 ) / <NUM_LIT:100> ) <EOL> valCurr = ( X * W . shift ( <NUM_LIT:1> ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> valYest = ( X . shift ( <NUM_LIT:1> ) * W . shift ( <NUM_LIT:1> ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> Y [ '<STR_LIT>' ] = valCurr / valYest - <NUM_LIT:1> <EOL> return Y <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> Y = recounstructVXX ( ) <EOL> print Y . head ( <NUM_LIT:30> ) <EOL> Y . to_csv ( '<STR_LIT>' ) </s>
