예제 #1
0
 def set_format(self, fmt):
     '''Set the selected output option.'''
     if not fmt in self.outputs:
         stderr('Unsupported output format specified; using defaults',
                'WARNING')
     if not isinstance(fmt, str):
         stderr('Output format is not string; using defaults', 'WARNING')
     self.output_format = fmt
예제 #2
0
def write_file_table(
        account_values,
        account_types,
        fraction_times_margin_strategy_went_bankrupt,
        outfile,
        fraction_times_margin_ended_with_emergency_savings_gap=None,
        avg_percent_diff_from_simple_calc=None,
        avg_simple_calc_value=None):
    REGULAR_INDEX = 0
    LEVERAGE_INDEX = 1
    assert account_types[
        REGULAR_INDEX] == "regular", "Regular account has to go in the 0th index"
    assert account_types[LEVERAGE_INDEX] in [
        "margin", "lev"
    ], "Margin / leveraged-ETF account has to go in the 1st index"
    outfile.write("<table>\n")
    outfile.write(
        """<tr><td><i>Type</i></td> <td><i>Mean &plusmn; stderr</i></td> <td><i>Median</i></td> <td><i>Min</i></td> <td><i>Max</i></td> <td><i>E[&radic;<span style="text-decoration: overline">wealth</span>] &plusmn; stderr</i></td> <td><i>&sigma;<sub>ln(wealth)</sub></i></td> </tr>\n"""
    )
    for type in account_types:
        numpy_values = numpy.array(account_values[type])
        log_values = map(
            math.log, numpy_values +
            1)  # The +1 here is so that the log() value will be at least 0
        sqrt_values = map(math.sqrt, numpy_values)
        outfile.write(
            "<tr><td><i>{}</i></td> <td>${:,} &plusmn; ${:,}</td> <td>${:,}</td> <td>${:,}</td> <td>${:,}</td> <td>{:,} &plusmn; {:,}</td> <td>{:.2f}</td></tr>\n"
            .format(return_pretty_name_for_type(type),
                    int(round(numpy.mean(numpy_values), 0)),
                    int(round(util.stderr(numpy_values), 0)),
                    int(round(numpy.median(numpy_values), 0)),
                    int(round(util.percentile(account_values[type], 0), 0)),
                    int(round(util.percentile(account_values[type], 1), 0)),
                    int(round(numpy.mean(sqrt_values), 0)),
                    int(round(util.stderr(sqrt_values), 0)),
                    numpy.std(log_values)))
    outfile.write("</table>")
    emergency_savings_gap = ""
    if fraction_times_margin_ended_with_emergency_savings_gap is not None:
        emergency_savings_gap = " Margin investor ended with no assets and a deficit in emergency savings {}% of the time.".format(
            round(100 * fraction_times_margin_ended_with_emergency_savings_gap,
                  1))
    avg_simple_calc_string = ""
    if avg_simple_calc_value is not None:
        avg_simple_calc_string = " Average simple-calculation margin ending balance was {}.".format(
            util.format_as_dollar_string(avg_simple_calc_value))
    outfile.write(
        "\nLeverage is better than regular {}% of the time.{} Leveraged account went fully bankrupt {}% of the time.{}"
        .format(
            int(
                round(
                    100 * util.probability_x_better_than_y(
                        account_values[account_types[LEVERAGE_INDEX]],
                        account_values[account_types[REGULAR_INDEX]]),
                    1)), emergency_savings_gap,
            round(100 * fraction_times_margin_strategy_went_bankrupt, 1),
            avg_simple_calc_string))
예제 #3
0
    def __init__(self, outputs=None):
        '''Class initialization'''
        self.groups = dict()

        if not isinstance(outputs, list):
            stderr('Outputs passed to D42Opts.__init__ was not type list.',
                   'WARNING')
            outputs = []

        self.parser = argparse.ArgumentParser(
            usage='d42-cli [-h] <optional arguments> [operation] <options>',
            formatter_class=lambda prog: argparse.HelpFormatter(
                prog, max_help_position=30))
        self._add_base(outputs)
예제 #4
0
파일: d42.py 프로젝트: MTecknology/d42-cli
    def load_modules(self):
        '''Attempts to load all modules in mods_list'''
        com = None
        if not self.mods_list:
            stderr('No modules loaded', 'NOTICE')
            return None

        for nam in self.mods_list:
            try:
                mod = __import__(nam)
                components = nam.split('.')
                for com in components[1:]:
                    mod = getattr(mod, com)
                setattr(self, com, mod)
                self.modules_loaded.append(com)
            except:
                stderr('Unable to load module {}'.format(nam), 'NOTICE')
예제 #5
0
    def load_libraries(self):
        '''Load and register libraries used for outputters.
        This allows outputters to be available despite one or two breaking
        or being otherwise unloadable.'''
        # Required Outputters ; libs already loaded
        # d42-cli will not run without json
        # pprint is the easiest to test/debug/skim and is the default
        outputs = ['json', 'pprint', 'devnull', 'raw']

        # Attempt to load additional outputters
        for lib in ['yaml']:
            try:
                library = __import__(lib)
                globals()[lib] = library
                outputs.append(lib)
            except:
                # No reason to stop loading, but drop something on stderr
                # This should only be reached if there is a bug in this file
                stderr('Unable to load library: {}'.format(lib), 'NOTICE')

        self.outputs = outputs
예제 #6
0
파일: d42.py 프로젝트: MTecknology/d42-cli
    def update_attributes(self):
        '''Updates object attributes from passed options'''
        opts = self.opts
        if hasattr(opts, 'd42_url'):
            if opts.d42_url is not None:
                self.api_url = opts.d42_url
        if hasattr(opts, 'd42_user'):
            if opts.d42_user is not None:
                self.api_user = opts.d42_user
        if hasattr(opts, 'd42_pass'):
            if opts.d42_pass is not None:
                self.api_pass = opts.d42_pass

        if hasattr(opts, 'd42_params'):
            if opts.d42_params is not None:
                try:
                    self.params = json.loads(opts.d42_params)
                except:
                    stderr(
                        'Unable to parse parameters; is it valid serialized json?',
                        exit_status=11)
        if hasattr(opts, 'd42_prm'):
            if opts.d42_prm is not None:
                for param in opts.d42_prm:
                    opt = param.split('=')
                    self.params[opt[0]] = opt[1]

        if hasattr(opts, 'output'):
            if opts.output is not None:
                if opts.output not in self.outputter.outputs:
                    # This /should/ be impossible to get to, but handling it anyway
                    stderr('Selected output format is not available.',
                           exit_status=11)
                self.outputter.set_format(opts.output)

        if self.api_url and not self.api_url.endswith('/'):
            self.api_url = self.api_url + '/'
def write_file_table(account_values, account_types, 
                     fraction_times_margin_strategy_went_bankrupt, outfile,
                     fraction_times_margin_ended_with_emergency_savings_gap=None,
                     avg_percent_diff_from_simple_calc=None,
                     avg_simple_calc_value=None):
    REGULAR_INDEX = 0
    LEVERAGE_INDEX = 1
    assert account_types[REGULAR_INDEX] == "regular", "Regular account has to go in the 0th index"
    assert account_types[LEVERAGE_INDEX] in ["margin","lev"], "Margin / leveraged-ETF account has to go in the 1st index"
    outfile.write("<table>\n")
    outfile.write("""<tr><td><i>Type</i></td> <td><i>Mean &plusmn; stderr</i></td> <td><i>Median</i></td> <td><i>Min</i></td> <td><i>Max</i></td> <td><i>E[&radic;<span style="text-decoration: overline">wealth</span>] &plusmn; stderr</i></td> <td><i>&sigma;<sub>ln(wealth)</sub></i></td> </tr>\n""");
    for type in account_types:
        numpy_values = numpy.array(account_values[type])
        log_values = map(math.log, numpy_values+1) # The +1 here is so that the log() value will be at least 0
        sqrt_values = map(math.sqrt, numpy_values)
        outfile.write("<tr><td><i>{}</i></td> <td>${:,} &plusmn; ${:,}</td> <td>${:,}</td> <td>${:,}</td> <td>${:,}</td> <td>{:,} &plusmn; {:,}</td> <td>{:.2f}</td></tr>\n".format( 
            return_pretty_name_for_type(type), 
            int(round(numpy.mean(numpy_values),0)) , int(round(util.stderr(numpy_values),0)), 
            int(round(numpy.median(numpy_values),0)) , 
            int(round(util.percentile(account_values[type], 0),0)) , 
            int(round(util.percentile(account_values[type], 1),0)) ,
            int(round(numpy.mean(sqrt_values),0)), int(round(util.stderr(sqrt_values),0)), 
            numpy.std(log_values) ))
    outfile.write("</table>")
    emergency_savings_gap = ""
    if fraction_times_margin_ended_with_emergency_savings_gap is not None:
        emergency_savings_gap = " Margin investor ended with no assets and a deficit in emergency savings {}% of the time.".format(round(100 * fraction_times_margin_ended_with_emergency_savings_gap,1))
    avg_simple_calc_string = ""
    if avg_simple_calc_value is not None:
        avg_simple_calc_string = " Average simple-calculation margin ending balance was {}.".format(util.format_as_dollar_string(avg_simple_calc_value))
    outfile.write("\nLeverage is better than regular {}% of the time.{} Leveraged account went fully bankrupt {}% of the time.{}".format(
        int(round(100 * util.probability_x_better_than_y(
            account_values[account_types[LEVERAGE_INDEX]],
            account_values[account_types[REGULAR_INDEX]]),1)), 
        emergency_savings_gap,
        round(100 * fraction_times_margin_strategy_went_bankrupt,1),
        avg_simple_calc_string))
예제 #8
0
    def _print_secret(self, data):
        '''Render plain output and then clear the screen'''
        try:
            import curses
        except:
            stderr('Unable to load curses library for outputter.',
                   exit_status=32)

        if not isinstance(data, str):
            stderr('Data passed to renderer was invalid.', exit_status=32)
        if '\n' in data:
            stderr('This renderer does not support newlines.', exit_status=32)

        try:
            # Initialize Curses
            stdscr = curses.initscr()
            curses.cbreak()
            curses.noecho()
            stdscr.keypad(1)

            # Render display
            stdscr.addstr(1, 2, "Secret:")
            stdscr.addstr(3, 5, data)
            stdscr.refresh()
            i = 7
            while True:
                stdscr.addstr(5, 1, 'Timeout: %s' % str(i))
                stdscr.refresh()
                time.sleep(1)
                i -= 1
                if i <= 0:
                    break
        except:
            stderr('Unable to render terminal.')
        finally:
            curses.nocbreak()
            stdscr.keypad(0)
            curses.echo()
            curses.endwin()
예제 #9
0
    def render(self, data, fmt=None):
        '''Render the reousted output using the requested format'''
        if not fmt:
            fmt = self.output_format

        if not isinstance(fmt, str):
            stderr('Output format was externally broked; forcing defaults',
                   'WARNING')
            fmt = 'pprint'

        if not hasattr(self, '_print_{}'.format(fmt)):
            # If a valid outputter was requested but is unavailable, it should
            # be assumed something is expecting output in that format. Rather
            # than revert to a default to get data out, we should die here.
            stderr('Requested outputter unavailable', exit_status=31)

        try:
            rndr = getattr(self, '_print_{}'.format(fmt))
            rndr(data)
        except:
            # Same as above; an error here should result in death.
            stderr('Unable to render data with requested outputter.',
                   exit_status=31)
예제 #10
0
파일: rbmtrain.py 프로젝트: yskst/chDNN
    
    outf    = args['--of']
    rbmtype = args['--rt']
    gpu  = not args['--cpu']
    mbsize = int(args['--mb'])
    epoch  = int(args['--epoch'])
    lr = float(args['--lr'])
    mm = float(args['--mm'])
    re = float(args['--re'])
    actf = args['--af']
    seed = int(args['--seed'])
    visnum = int(args['<visnum>'])
    hidnum = int(args['<hidnum>'])

    if rbmtype!="gb" and rbmtype!="bb":
        util.stderr("Unknown RBM type: %s" % rbmtype)
    elif rbmtype == "gb":
        rbm = gbRBM(visnum, hidnum, seed=seed)
    else:
        rbm = bbRBM(visnum, hidnum)
    
    af = dataio.str2actf(actf)

    data = dataio.dataio(args['<file>'], args['--df'], visnum).astype(np.float32)
    ndata = data.shape[0]
    
    if not gpu:
        trainer = rbm.train_cpu
        xp = np
    else:
        trainer = rbm.train_gpu