Ch. 26 Review Exercise 1

Part I

The $P$-value of a test equals its observed significance level.


In [1]:
from question2 import *

In [2]:
q = MultiPartQuestion()

In [3]:
dir(q)


Out[3]:
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__metaclass__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_add_notifiers',
 '_notify_trait',
 '_parts_changed',
 '_remove_notifiers',
 '_seed_changed',
 '_trait_dyn_inits',
 '_trait_notifiers',
 '_trait_values',
 'active_parts',
 'answer',
 'check',
 'class_trait_names',
 'class_traits',
 'list_type',
 'lockout_time',
 'max_pts',
 'name',
 'num_answers',
 'on_trait_change',
 'parts',
 'seed',
 'solution',
 'submits_allowed',
 'text',
 'to_JSON',
 'trait_metadata',
 'trait_names',
 'traits',
 'type']

In [4]:
q.traits()


Out[4]:
{'active_parts': <IPython.utils.traitlets.List at 0x108289ed0>,
 'answer': <IPython.utils.traitlets.Unicode at 0x1082ec850>,
 'list_type': <IPython.utils.traitlets.Unicode at 0x1082ec6d0>,
 'lockout_time': <IPython.utils.traitlets.Int at 0x1082ec750>,
 'max_pts': <IPython.utils.traitlets.List at 0x1083c6ad0>,
 'name': <IPython.utils.traitlets.Unicode at 0x108289dd0>,
 'parts': <IPython.utils.traitlets.List at 0x1082ecad0>,
 'seed': <IPython.utils.traitlets.Int at 0x1082ec890>,
 'solution': <IPython.utils.traitlets.Unicode at 0x1082ecbd0>,
 'submits_allowed': <IPython.utils.traitlets.Int at 0x1082ecb90>,
 'text': <IPython.utils.traitlets.Unicode at 0x1082eccd0>,
 'type': <IPython.utils.traitlets.Unicode at 0x1082ec7d0>}

In [5]:
q.answer = 3


---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)
<ipython-input-5-cb85f3a1c21a> in <module>()
----> 1 q.answer = 3

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in __set__(self, obj, value)
    313 
    314     def __set__(self, obj, value):
--> 315         new_value = self._validate(obj, value)
    316         old_value = self.__get__(obj)
    317         obj._trait_values[self.name] = new_value

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in _validate(self, obj, value)
    321     def _validate(self, obj, value):
    322         if hasattr(self, 'validate'):
--> 323             return self.validate(obj, value)
    324         elif hasattr(self, 'is_valid_for'):
    325             valid = self.is_valid_for(value)

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in validate(self, obj, value)
   1027         if isinstance(value, bytes):
   1028             return unicode(value)
-> 1029         self.error(obj, value)
   1030 
   1031 

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in error(self, obj, value)
    344             e = "The '%s' trait must be %s, but a value of %r was specified." \
    345                 % (self.name, self.info(), repr_type(value))
--> 346         raise TraitError(e)
    347 
    348     def get_metadata(self, key):

TraitError: The 'answer' trait of a MultiPartQuestion instance must be a unicode string, but a value of 3 <type 'int'> was specified.

In [6]:
q.submits_allowed = '3'


---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)
<ipython-input-6-e047e074727a> in <module>()
----> 1 q.submits_allowed = '3'

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in __set__(self, obj, value)
    313 
    314     def __set__(self, obj, value):
--> 315         new_value = self._validate(obj, value)
    316         old_value = self.__get__(obj)
    317         obj._trait_values[self.name] = new_value

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in _validate(self, obj, value)
    321     def _validate(self, obj, value):
    322         if hasattr(self, 'validate'):
--> 323             return self.validate(obj, value)
    324         elif hasattr(self, 'is_valid_for'):
    325             valid = self.is_valid_for(value)

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in validate(self, obj, value)
    884         if isinstance(value, int):
    885             return value
--> 886         self.error(obj, value)
    887 
    888 class CInt(Int):

/Users/jonathantaylor/ipython/IPython/utils/traitlets.pyc in error(self, obj, value)
    344             e = "The '%s' trait must be %s, but a value of %r was specified." \
    345                 % (self.name, self.info(), repr_type(value))
--> 346         raise TraitError(e)
    347 
    348     def get_metadata(self, key):

TraitError: The 'submits_allowed' trait of a MultiPartQuestion instance must be an int, but a value of '3' <type 'str'> was specified.

In [7]:
q.submits_allowed = 3

In [8]:
q.seed = 3

In [9]:
q.parts


Out[9]:
[]

In [10]:
q.text = 'This is my question'
q.max_pts = [3,4,5]

In [11]:
q.to_JSON()


Out[11]:
{'list_type': u'a', 'parts': [], 'text': u'This is my question', 'type': u''}

In [12]:
q.comment_if_wrong = 'Hint: guess randomly!'

In [13]:
q.to_JSON()


Out[13]:
{'list_type': u'a', 'parts': [], 'text': u'This is my question', 'type': u''}

In [14]:
q2 = ShortAnswer()
q2.text = 'Please explain the central limit theorem.'

q3 = ShortAnswer()
q3.text = 'Please detail the assumptions you are making.'

In [15]:
q.parts = [q2,q3]
q.to_JSON()


Out[15]:
{'list_type': u'a',
 'parts': [{'max_pts': [1],
   'text': u'Please explain the central limit theorem.',
   'type': u''},
  {'max_pts': [1],
   'text': u'Please detail the assumptions you are making.',
   'type': u''}],
 'text': u'This is my question',
 'type': u''}

In [16]:
q.parts


Out[16]:
[<question2.ShortAnswer at 0x10af7ad50>,
 <question2.ShortAnswer at 0x10af7a7d0>]

In [ ]: