Commits

dan mackinlay committed b775793 Merge

merge

Comments (0)

Files changed (4)

     ds.calculate_entropies(**kwargs)
     return ds.I()
 
-def plugin_mi_dist_cont(x, y, x_nbins=None, y_nbins=None, **kwargs):
+def plugin_mi_dist_cont(x, y, x_bins=None, y_bins=None, **kwargs):
     """naive plugin estimation of MI of 2 unquantized vectors. """
-    if x_nbins is None: x_nbins = choose_n_bins(x.size)
-    if y_nbins is None: y_nbins = x_nbins
+    if x_bins is None: x_bins = choose_n_bins(x.size)
+    if y_bins is None: y_bins = x_bins
     return plugin_est_mi_of_histo(
-      counts2d(x.ravel(), y.ravel(), x_nbins, y_nbins)
+      counts2d(x.ravel(), y.ravel(), x_bins, y_bins)
     )
 
-def counts2d(x, y, x_nbins=None, y_nbins=None, **kwargs):
+def counts2d(x, y, x_bins=None, y_bins=None, **kwargs):
     """2d histogram of 2 unquantized vectors. The binning here involves a
     sort, so is slow for large vectors. Additionally, this does not
     work well when sample values are particularly degenerate."""
-    if y_nbins is None: y_nbins = x_nbins
-    x_b = closed_binning(x, x_nbins)
-    y_b = closed_binning(y, y_nbins)
+    if y_bins is None: y_bins = x_bins
+    x_b = closed_binning(x, x_bins)
+    y_b = closed_binning(y, y_bins)
     try:
         histo = sp.histogram2d(x, y, bins=(x_b, y_b))[0]
     except ValueError:
         # degenerate binning
-        x_size = max(x[-1] - x[0], 0.00001)
-        y_size = max(y[-1] - y[0], 0.00001)
-        x_b = x_b * (1-x_size)  + np.linspace(-x_size, x_size, x_b.size)
-        y_b = y_b * (1-y_size)  + np.linspace(-y_size, y_size, y_b.size)
+        x_span = max(x[-1] - x[0], 0.00001)
+        y_span = max(y[-1] - y[0], 0.00001)
+        x_b = x_b * (0.99)  + 0.01* np.linspace(-x_span, x_span, x_b.size)
+        y_b = y_b * (0.99)  + 0.01* np.linspace(-y_span, y_span, y_b.size)
+        histo = sp.histogram2d(x, y, bins=(x_b, y_b))[0]
     return histo
 
-def plugin_mi_dist_disc(x, y, x_nbins=None, y_nbins=None, **kwargs):
+def plugin_mi_dist_disc(x, y, x_bins=None, y_bins=None, **kwargs):
     """naive plugin estimation of MI of 2 quantized vectors"""
     return plugin_est_mi_of_histo(
-      qcounts2d(x.ravel(), y.ravel(), x_nbins=x_nbins, y_nbins=y_nbins, **kwargs)
+      qcounts2d(x.ravel(), y.ravel(), x_bins=x_bins, y_bins=y_bins, **kwargs)
     )
 
-def qcounts2d(x, y, x_nbins=None, y_nbins=None, **kwargs):
+def qcounts2d(x, y, x_bins=None, y_bins=None, **kwargs):
     """2d histogram of 2 quantized vectors."""
-    if x_nbins is None:
-        x_nbins = x.max() + 1
-    if y_nbins is None:
-        y_nbins = y.max() + 1
+    if x_bins is None:
+        x_bins = x.max() + 1
+    if y_bins is None:
+        y_bins = y.max() + 1
     counts = sp.histogram2d(x,y,
-      bins=(x_nbins, y_nbins),
-      range=[[0,x_nbins], [0,y_nbins]])[0]
+      bins=(x_bins, y_bins),
+      range=[[0,x_bins], [0,y_bins]])[0]
     return counts
 
 def plugin_est_mi_of_histo(counts):

trader_experiments.py

 """
 from utils import Bunch
 from sampling import Range
-from trader_stats_sets import gu_danesque_continuous_test, gu_danesque_2d, wicks_vs_me, wicks_vs_me_vs_ince, no_stats, order_mi_susc_ftw2, mi_slapdown_stats_set_2d, slim_mi_slapdown_stats_set_2d, slim_mi_slapdown_high_d_stats_set, stats_2d, order_mi_susc_test, order_mi_susc_ftw_trimmed, whitened_stats_2d, swept_stats, hi_d_test_stats, whitened_stats_hi_d, particlewise_angley, particlewise_angley_swept, particlewise_angley2_test, particlewise_distance_correlations
+from trader_stats_sets import gu_danesque_continuous_test, what_terry_wants, gu_danesque_2d, wicks_vs_me, wicks_vs_me_vs_ince, no_stats, order_mi_susc_ftw2, mi_slapdown_stats_set_2d, slim_mi_slapdown_stats_set_2d, slim_mi_slapdown_high_d_stats_set, stats_2d, order_mi_susc_test, order_mi_susc_ftw_trimmed, whitened_stats_2d, swept_stats, hi_d_test_stats, whitened_stats_hi_d, particlewise_angley, particlewise_angley_swept, particlewise_angley2_test, particlewise_distance_correlations
 from math import pi
 
 ### Stock experiments
+fuckfuckfuck_emergency_wicks_panic = Bunch(
+        trader_factory="WicksTraderSet",
+        limit=(65,), num_agents=1000,
+        keep_raw_data=True,
+        save_model_to_box=True,
+        keep_steps=5000,
+        discard_steps=10000,
+        repeat=1,
+        P1=Range(0,2*pi),
+        P2=0.3,
+        P3=0.94,
+        dimensions=2,
+        seed_offset=2345,
+        sampler='GridSampler',
+        stats_set=what_terry_wants,
+        norm_periodic=False,
+        desc="""Using Wick's parameters and his stats on the 2d case""")
 
 wicks_dim_sweep = Bunch(
         trader_factory="WicksTraderSet",
         P2=0.3,
         P3=0.94,
         dimensions=2,
+        keep_raw_data=True,
+        save_model_to_box=True,
         seed_offset=232345,
         sampler='GridSampler',
         stats_set=no_stats,
         limit=(129,), num_agents=3000,
         keep_steps=5000,
         discard_steps=10000,
-        repeat=8,
+        repeat=4,
         P1=Range(0,2*pi),
         P2=0.3,
         P3=0.94,
         dimensions=2,
+        keep_raw_data=True,
+        save_model_to_box=True,
         seed_offset=2345,
         sampler='GridSampler',
         stats_set=wicks_vs_me_vs_ince,
     use a binning based on known characteristics of the isotropic case.
     see _vel_loc_self_mi_adaptive for more info"""
     if n_bins == "wicks":
-        # bin using either double Wick's criterion (radius) or, if that has
+        # bin using either Wick's criterion (radius) or, if that has
         # too many bins, Cochrane's.
         n_bins = int(min(
             2.0/traderset.radius+0.5,
         loc_binner=bin_naive,
         vel_binner=bin_angle_naive,
         estimator="ince",
+        binning='disc',
         squish_bins=True,
         **mi_kwargs):
     """does loc determine angle? We work this out in the special 2D case using
     Wick's actual method, which involves the use of trignometric 2d angles,
     and idiosyncratic bin numbers and placement.
     """
-    #import pdb; pdb.set_trace()
     #from IPython.core.debugger import Tracer
     #Tracer()()
+    n_bins = int(min(
+        2.0/traderset.radius+0.5,
+        math.sqrt(choose_n_bins(vel_slice.shape[0]*vel_slice.shape[1], test=False))
+    ))
+    
     n_bins = int(np.floor(2./(traderset.radius)))
     n_loc_bins = n_bins
     n_vel_bins = n_bins
+
+    if binning =='disc':
+        locs = loc_binner(loc_slice[:,:,0:1], n_bins)
+        vels = vel_binner(vel_angle_slice, n_bins)
+        if squish_bins:
+            locs, n_loc_bins = squish(locs)
+            vels, n_vel_bins = squish(vels)
+        if estimator=="ince":
+            estimator = lambda x,y,**kwargs: ince_mi_dist_disc(x,y,n_loc_bins, n_vel_bins,**kwargs)
+        else:
+            estimator = lambda x,y,**kwargs: plugin_mi_dist_disc(x,y,n_loc_bins, n_vel_bins,**kwargs)
+    else:
+        locs = loc_slice[:,:,0:1]
+        vels = vel_angle_slice
+        if estimator=="ince":
+            estimator = lambda x,y,**kwargs: ince_mi_dist_cont(x,y, n_bins=n_bins, **kwargs)
+        else:
+            estimator = lambda x,y,**kwargs: plugin_mi_dist_cont(x,y, n_bins=n_bins, **kwargs)
     
-    locs = loc_binner(loc_slice[:,:,0:1], n_bins)
-    vels = vel_binner(vel_angle_slice, n_bins)
-    
-    if squish_bins:
-        locs, n_loc_bins = squish(locs)
-        vels, n_vel_bins = squish(vels)
-    
-    if estimator=="ince":
-        estimator = ince_mi_dist_disc
-    else:
-        estimator = plugin_mi_dist_disc
-            
     est = estimator(
         locs, vels,
-        n_loc_bins, n_vel_bins, **mi_kwargs
+        **mi_kwargs
     )
-    return d(est=est)
+    return d(est=est, n_bins=(n_loc_bins, n_vel_bins))
     
 def mi_wicks_hi_d(*args, **kwargs):
     return _do_slicewise_stat(_mi_wicks_hi_d,
     particle_len = loc_slice.shape[1]
     dim_len = loc_slice.shape[2]
     if n_bins == "wicks":
-        # bin using either double Wick's criterion (radius) or, if that has
-        # too many bins, Cochrane's.
+        # bin using either Wick's criterion (radius) 
         n_bins = int(np.floor(2./(traderset.radius)))
     elif n_bins == "cochrane":
         # bin using the cochrane criterion, which has as many bins as possible
-        n_bins = int(np.floor(np.sqrt(choose_n_bins(time_len*oversample*time_len*oversample))))
+        n_bins = int(np.floor(math.sqrt(choose_n_bins(time_len*oversample*time_len*oversample))))
     if binning =='disc':
         if estimator=="ince":
             estimator = ince_mi_dist_disc
     
     est = estimator(
         locs.ravel(), vels.ravel(),
-        n_loc_bins, n_vel_bins, **mi_kwargs
+        x_bins=n_loc_bins, y_bins=n_vel_bins, **mi_kwargs
     )
     return d(est=est)
 
   ("mi_wicks_2d_complete", "mi_wicks_2d", d(n_slices=1)),
 ]
 
+what_terry_wants = [
+  ("order_complete", "order", d(n_slices=1)),
+  ("order_continuous", "order", d(n_steps=1)),
+  ("mi_wicks_2d_piecewise_squished", "mi_wicks_2d", d(n_slices=10, squish=True, estimator='plugin')),
+  ("mi_wicks_2d_stepwise_squished", "mi_wicks_2d", d(n_steps=1, skip=50, squish=True, estimator='plugin')),
+  ("mi_wicks_2d_piecewise_adaptive", "mi_wicks_2d", d(n_slices=10, squish=True, binning='cont', estimator='plugin')),
+  ("mi_wicks_2d_stepwise_adaptive", "mi_wicks_2d", d(n_steps=1, skip=50, squish=True, binning='cont', estimator='plugin')),
+  ("mi_distance_angular_vel_particlewise_macerated_100_5_apriori", "mi_distance_angular_vel_particlewise_macerated", d(n_steps=100, estimator="naive", oversample=5, binning='disc', squish=True)),
+  ("mi_distance_angular_vel_particlewise_macerated_100_5_cont", "mi_distance_angular_vel_particlewise_macerated", d(n_steps=100, estimator="naive", oversample=5, binning='cont')),
+]
+
 gu_danesque_2d = [
   ("mi_distance_angular_vel_apriori_particlewise_alt_50", "mi_distance_angular_vel_apriori_particlewise", d(n_slices=1, estimator="naive", n_agents=2, oversample=50)),
   ("mi_distance_angular_vel_apriori_particlewise_alt_500", "mi_distance_angular_vel_apriori_particlewise", d(n_slices=1, estimator="naive", n_agents=2, oversample=500)),