Пример #1
0
 def runQuotesTest(self, writer_constructor, reader_constructor):
     startdate = '19861105'
     enddate = '19870929'
     symbols = ['ax1','ax2','ax3']
     data = np.random.rand(len(symbols), 5, 200)
     data[:,4,:] = np.int64(np.floor(data[:,4,:] * 100000))
     quotes = data.tolist()
     
     fn = 'quote_csv_test'
     
     writer = writer_constructor(fn, startdate, enddate)
     
     for s,q in zip(symbols,quotes):
         writer.writeQuote(s,q)
     
     writer.close()
     
     reader = reader_constructor(fn)
     
     assert reader.startdate == startdate, 'Start Date not read correctly'
     assert reader.enddate == enddate, 'End Date not read correctly'
     assert all([s in symbols for s in reader.symbols()]), 'Symbols not read correctly: [' + ','.join(reader.symbols()) + '] != [' + ','.join(symbols) + ']'
     assert all([close_enough(reader[s],data[i]) for i,s in enumerate(symbols)]), 'Data not read correctly'
     assert all([any([close_enough(rd,wd) for wd in data]) for rd in reader.quotes()]), \
             'Data not read correctly through quotes()'
Пример #2
0
 def testNormalizeDataWithVolume(self):
     """Test the normalize data function with a selected RateOfReturn divisor row"""
     data = np.random.rand(3, 100)
     data = np.array(data.tolist() + np.random.randint(100, 10000, (1, 100)).tolist())
     ndata = normalizeData(data, volume_row=3)
     assert np.shape(ndata) == (4, 99), "Normalize Data gave incorrect shape"
     for nd, d in izip(ndata[0:3], data[0:3]):
         assert close_enough(nd, rateOfReturn(d)), "Normalize Data did not use Rate of Return correctly"
     assert close_enough(
         ndata[3], normalizeVolume(data[3][1:])
     ), "Normalize Data did not use Normalize Volume correctly"
Пример #3
0
 def testOptimizeWithFullRiskTolerance(self):
     '''Test MVPO with large risk tolerance'''
     cc = np.eye(2)
     mvpo = MeanVariancePortfolioOptimizer(cc, risk_tolerance=1000)
     
     current_alloc = np.array([.5,.5])
     predicted_stats = np.array([[.1,1],[.5,10]])
     
     new_alloc, var, ret = mvpo.optimize(current_alloc, predicted_stats)
     
     assert close_enough(new_alloc,[0,1]), 'Allocations in full risk tolerance case did not choose the higher return: new_alloc=' + str(new_alloc)
     assert close_enough(var,1000), 'Variance was not calculated correctly'
     assert close_enough(ret,.5), 'Returns were not calculated correctly'
Пример #4
0
 def testOptimizeWithEqualChoices(self):
     '''Test MVPO with equal choices'''
     cc = np.eye(2)
     mvpo = MeanVariancePortfolioOptimizer(cc)
     
     current_alloc = np.array([1,0])
     predicted_stats = np.array([[.1,1],[.1,1]])
     
     new_alloc, var, ret = mvpo.optimize(current_alloc, predicted_stats)
     
     assert close_enough(new_alloc,[.5,.5]), 'Allocations in equal investment case did not diversify properly'
     assert close_enough(var,2), 'Variance was not calculated correctly'
     assert close_enough(ret,.1), 'Returns were not calculated correctly'
Пример #5
0
 def testOptimizeWithSmallRiskTolerance(self):
     '''Test MVPO with small risk tolerance'''
     cc = np.eye(2)
     mvpo = MeanVariancePortfolioOptimizer(cc, risk_tolerance=0)
     
     current_alloc = np.array([1,0])
     predicted_stats = np.array([[.5,20],[.01,0]])
     
     new_alloc, var, ret = mvpo.optimize(current_alloc, predicted_stats)
     
     assert close_enough(new_alloc,[0,1]), 'Allocations in no risk tolerance case did not take lower risk'
     assert close_enough(var,0), 'Variance was not calculated correctly'
     assert close_enough(ret,0.01), 'Returns were not calculated correctly'
Пример #6
0
 def testNormalizeDataBasic(self):
     """Test the normalize data function with basic inputs"""
     data = np.random.rand(3, 100)
     ndata = normalizeData(data)
     assert np.shape(ndata) == (3, 99), "Normalize Data gave incorrect shape"
     for nd, d in izip(ndata, data):
         assert close_enough(nd, rateOfReturn(d)), "Normalize Data did not use Rate of Return function correctly"
Пример #7
0
 def testNormalizeDataWithSelectedDivisor(self):
     """Test the normalize data function with a selected RateOfReturn divisor row"""
     data = np.random.rand(3, 100)
     data[2] += 100
     ndata = normalizeData(data, ror_divisor_row=2)
     assert np.shape(ndata) == (3, 99), "Normalize Data gave incorrect shape"
     for nd, d in izip(ndata, data):
         assert close_enough(
             nd, rateOfReturn(d, data[2][0:-1])
         ), "Normalize Data did not use Rate of Return with divisor function correctly"
Пример #8
0
    def testMakeNormalizeDataGenerator(self):
        """Test the normalize data generator function"""
        data = np.random.rand(3, 100)
        simple_norm = DataNormalizer()
        ndata = simple_norm(data)
        assert np.shape(ndata) == (3, 99), "Normalize Data gave incorrect shape"
        for nd, d in izip(ndata, data):
            assert close_enough(nd, rateOfReturn(d)), "Normalize Data did not use Rate of Return function correctly"

        data = np.array(data.tolist() + np.random.randint(100, 10000, (1, 100)).tolist())
        complex_norm = DataNormalizer(1, 3)
        ndata = complex_norm(data)
        assert np.shape(ndata) == (4, 99), "Normalize Data gave incorrect shape"
        for nd, d in izip(ndata[0:3], data[0:3]):
            assert close_enough(
                nd, rateOfReturn(d, data[1][0:-1])
            ), "Normalize Data did not use Rate of Return correctly"
        assert close_enough(
            ndata[3], normalizeVolume(data[3][1:])
        ), "Normalize Data did not use Normalize Volume correctly"
Пример #9
0
 def testOptimizeExecution(self):
     '''Test MVPO execution'''
     x = np.matrix(np.random.randn(self.ninvest) / 3)
     corr_coeffs = x.T * x;
     mvpo = MeanVariancePortfolioOptimizer(corr_coeffs)
      
     current_alloc = np.arange(self.ninvest,dtype=np.float64) / np.arange(self.ninvest).sum()
     predicted_stats = np.random.randn(self.ninvest,2)
     predicted_stats[:,0] = predicted_stats[:,0] / 3
     predicted_stats[:,1] = abs(predicted_stats[:,1]) * 3
     new_alloc, unused_var, unused_ret = mvpo.optimize(current_alloc, predicted_stats)
     assert close_enough(sum(new_alloc),1), "New allocations are not equal to 1"
Пример #10
0
 def testOptimizeWithShorting(self):
     '''Test MVPO with shorting allowed'''
     cc = np.eye(self.ninvest)
     mvpo = MeanVariancePortfolioOptimizer(cc, allow_shorting=True, risk_tolerance=1)
     
     current_alloc = np.random.rand(self.ninvest)
     current_alloc /= sum(current_alloc)
     predicted_stats = np.random.rand(self.ninvest,2)
     predicted_stats[:,0] = predicted_stats[:,0] / 2 - .25
     predicted_stats[:,1] = predicted_stats[:,1] * 10
     predicted_stats[0,0] = .01
     predicted_stats[0,1] = 0
     
     new_alloc, unused_var, unused_ret = mvpo.optimize(current_alloc, predicted_stats)
     
     assert close_enough(sum(new_alloc),1), 'New allocations with shorting are not equal to 1'
     assert (np.array(new_alloc) < 0).any(), 'At least one of these allocations should be negative'
Пример #11
0
    def testRateOfReturnWithDivisor(self):
        """Test the rate of return function with a specified divisor"""
        data = [1, 2, 2.5, 2]
        div = [2, 2, 2]
        bad_div = [2, 2, 2, 2]
        ror = rateOfReturn(data, divisor=div)
        assert np.shape(ror) == (3,), "Rate of return has incorrect size"
        assert (np.array(ror) == np.array([0.5, 0.25, -0.25])).all(), "Rate of return is incorrect"

        try:
            ror = rateOfReturn(data, divisor=bad_div)
            assert False, "Rate of return did not assert error for incorrect sized divisor"
        except AssertionError:
            pass

        data = np.random.rand(100)
        div = np.random.rand(99) + 100
        ror = rateOfReturn(data, div)
        assert np.shape(ror) == (99,), "Rate of return has incorrect size"
        assert close_enough(ror, np.ones((99,))), "Rate of return is wrong!"
Пример #12
0
 def testNormalizeVolume(self):
     """Test the normalize volume function"""
     data = range(1, 10)
     norm_data = normalizeVolume(data)
     assert len(norm_data) == 9, "NormalizeVolume changed length of vector"
     assert close_enough(norm_data, np.arange(0.2, 2, 0.2)), "NormalizeVolume gave incorrect output"