Quantopian corrupts simple list. Why? How to work around?

Hello QWorld,

I'm trying to extract a simple list from history() and I cannot.

I'd welcome any clues on how to do this.

Please see the attached algo for my code intentions.

Dan

2
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
# bad_yclass2.py

# Subject:
# Quantopian corrupts simple list.
# Why?
# How to work-around?
# Reproduce the problem at line 45.

import numpy as np
from sklearn import linear_model

def initialize(context):
context.spy = symbol('SPY')

def handle_data(context, data):
# Hard coded prices:
x0  = [186.24, 188.54, 190.32, 194.05, 192.71, 194.95, 196.38, 196.11, 198.37, 198.14, 199.45]
x0  = np.array(x0)
x1 = x0 - 1
x0m = np.mean(x0)
yclass0  = x0 > x0m
bigx = np.zeros( (len(x0),2) )
bigx[:,0] = x0
bigx[:,1] = x1
clf = linear_model.LogisticRegression()
# A nice simple list:
yclass0 = [aclass for aclass in yclass0]
# I should see:
# [False, False, False, False, False, True, True, True, True, True, True]
print(yclass0)
clf.fit(bigx, yclass0)

# Now get yclass from history() rather than my hardcoded list.
prices  = history(bar_count=11, frequency='1d', field='price')
x2      = np.array([aprice[0] for aprice in prices.values])
x2m     = np.mean(x2)
yclass1 = x2 > x2m
# A nice simple list:
yclass2 = [aclass for aclass in yclass1]
# I should see:
# [False, False, False, False, False, True, True, True, True, True, True]
print(yclass2)

# uncomment next line and it fails due to yclass2, why?:
#clf.fit(bigx, yclass2)
# Error due to yclass2:
# Runtime exception: ValueError: The number of classes has to be greater than one.

# yclass2 looks the same as yclass0 and yclass3:
yclass3 = [False, False, False, False, False, True, True, True, True, True, True]
clf.fit(bigx, yclass3)

# Q1: What makes yclass2 different?
# ans: yclass2 was derived from prices from history()
# Also,
# yclass2 must be different than yclass0 and yclass3,
# but print() is unable to tell me what the difference is.
# Q2: What can I do to discern the difference between yclass2,0,3?
# Q3: How can I operate on yclass2 so it behaves like yclass0,3?

# Clue to Q1:
# When I attempt to see yclass2 in debugger, I get error:
# TypeError: iteration over a 0-d array
#

There was a runtime error.
2 responses

Hi Dan,

I see where your confusing is coming from. The error is being thrown before the algorithm starts running so when you put the whole clause into a try/except block to go past the first iteration of error checking, you'll see that it actually doesn't fail in the actual algorithm run:

  try:
clf.fit(bigx, yclass2)
print "success"
except ValueError:
print "failure"


Only "success" is printed out not "failure with this block of code.

Try it out and let me know what you think!

Seong

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Seong,
Your example works well for me.
It does exactly what I need.
Thanks!
Dan