Skip to content
KevinYuann edited this page Nov 19, 2022 · 12 revisions

How to run tradebooks

  1. set up environement as in readme
  2. click run
  3. see plots
  4. play with variables

Multi-dimensional Visualization

If you want to draw plots in higher dimensions than 2D, here's how. The hypercast() function is built exactly for this purpose.

Code snippet:

import pandas as pd
import plot
from architect.libs.utillib import hypercast

# region parameters
x = np.arange(start=0, stop=10, step=1)
a = np.arange(start=0, stop=20, step=1)

# region broadcasting
x_hyper, a_hyper = hypercast(x, a)

# region evaluation
y_hyper = some_function(x_hyper, a_hyper)

# region plot
dfd = {"x": x_hyper.flatten(), "y": y_hyper.flatten(), "a": a_hyper.flatten()}
df = pd.DataFrame(data=dfd)

What's happening under the hood is that the function is converting the input of multiple 1D arrays into multiple hypercubes that can be described by an n-dimensional array.

Image of 4-D Hypercube (Image from Wikimedia Commons)

The most important feature to note that is that all of these hypercubes will be of the shape size-wise, allowing us to easily use them in functions. Another feature that is important to mention is that the orientation of the array will depend on the order in which the 1D arrays are passed into the function.

Using hypercast and the plot dependency, we can create four different types of multi-dimensional visualizations: a line plot, a scatter plot, a 3D scatter plot, and a surface plot.

After generating the plots, you can use the style_to_matlab(fig) function to format it as a matlab figure. This requires a plot as its only argument. Additionally, you use the save(fig, name: str, path: Path) function to save it as a file.

For the line and scatter plots, the axis and legend labels are taken from the column names. Furthermore, you can use LaTeX directives for rendering mathematical notation by encapsulating them in dollar signs: "$...$". Though, it is recommended to avoid using LaTeX when using facets because the value of the variable at the facets will not be rendered.

Implmentation of Multi-dimensional plots

The line plot supports up to 4D data.

fig = plot.line(
    df: pd.DataFrame,       # dataframe containing data.
    x: str,                 # column name treated as x coordinates of trace(s).
    y: str | list[str],     # column name(s) treated as y coordinates of trace(s).
    fc: str = None,         # column name treated as facet column of trace(s).
    fr: str = None,         # column name treated as facet row of trace(s).
    x_error: str = None,    # column name treated as x error of trace(s) for plotting horizontal error bars.
    y_error: str = None,    # column name treated as x error of trace(s) for plotting vertical error bars.
    markers: bool = False,  # whether to plot markers on data points.
    title: str = None,      # title of plot.
    show: bool = False,     # whether to show plot.
    dark: bool = False,     # plot in dark mode.
)

The scatter plot supports up to 7D data.

fig = plot.scatter(
    df: pd.DataFrame,       # dataframe containing data.
    x: str,                 # column name treated as x coordinate of trace(s).
    y: str | list[str],     # column name(s) treated as y coordinates of trace(s).
    c: str = None,          # column name treated as color of trace(s).
    s: str = None,          # column name treated as size of trace(s). Does not map negative values.
    m: str = None,          # column name treated as marker symbol of trace(s).
    fc: str = None,         # column name treated as facet column of trace(s).
    fr: str = None,         # column name treated as facet row of trace(s).
    x_error: str = None,    # column name treated as x error of trace(s) for plotting horizontal error bars.
    y_error: str = None,    # column name treated as y error of trace(s) for plotting vertical error bars.
    title: str = None,      # title of plot.
    show: bool = False,     # whether to slow plot.
)

The 3D scatter plot supports up to 6D data.

fig = plot.scatter(
    df: pd.DataFrame,       # dataframe containing data.
    x: str,                 # column name treated as x coordinate of trace(s).
    y: str,                 # column name treated as y coordinates of trace(s).
    z: str | list[str]      # column name(s) treated as z coordinates of trace(s).
    c: str = None,          # column name treated as color of trace(s).
    s: str = None,          # column name treated as size of trace(s). Does not map negative values.
    m: str = None,          # column name treated as marker symbol of trace(s).
    fc: str = None,         # column name treated as facet column of trace(s).
    fr: str = None,         # column name treated as facet row of trace(s).
    x_error: str = None,    # column name treated as x error of trace(s).
    y_error: str = None,    # column name treated as y error of trace(s).
    z_error: str = None,    # column name treated as z error of trace(s).
    title: str = None,      # title of plot.
    show: bool = False,     # whether to slow plot.
)

The surface plot support up to 3D data.

fig = plot.surface(
    x: Iterable,                 # x-coordinate data.
    y: Iterable,                 # y-coordinate data.
    z: Iterable[float, float],   # z-coordinate data array.
    title: str = None,           # title pf plot.
    title_x: str = None,         # title of x-axis.
    title_y: str = None,         # title of y-axis.
    title_z: str = None,         # title of z-axis.
    show: bool = False,          # whether to show plot.
)

Usage of Multi-dimensional plots

The usage of multi-dimensional plots is essentially exactly the same as tradebooks.

  1. Set up environment in README
  2. Click run
  3. See plots
  4. Play with variables