text stringlengths 0 828 |
|---|
bounds = ((lower_bound, upper_bound), ) * len(self.SUPPORTED_COINS) |
return minimize( |
fun=func, x0=weights, jac=func_deriv, bounds=bounds, |
constraints=constraints, method='SLSQP', options={'disp': False} |
)" |
729,"def allocate(self): |
"""""" |
Returns an efficient portfolio allocation for the given risk index. |
"""""" |
df = self.manager.get_historic_data()[self.SUPPORTED_COINS] |
#==== Calculate the daily changes ====# |
change_columns = [] |
for column in df: |
if column in self.SUPPORTED_COINS: |
change_column = '{}_change'.format(column) |
values = pd.Series( |
(df[column].shift(-1) - df[column]) / |
-df[column].shift(-1) |
).values |
df[change_column] = values |
change_columns.append(change_column) |
# print(df.head()) |
# print(df.tail()) |
#==== Variances and returns ====# |
columns = change_columns |
# NOTE: `risks` is not used, but may be used in the future |
risks = df[columns].apply(np.nanvar, axis=0) |
# print('\nVariance:\n{}\n'.format(risks)) |
returns = df[columns].apply(np.nanmean, axis=0) |
# print('\nExpected returns:\n{}\n'.format(returns)) |
#==== Calculate risk and expected return ====# |
cov_matrix = df[columns].cov() |
# NOTE: The diagonal variances weren't calculated correctly, so here is a fix. |
cov_matrix.values[[np.arange(len(self.SUPPORTED_COINS))] * 2] = df[columns].apply(np.nanvar, axis=0) |
weights = np.array([1/len(self.SUPPORTED_COINS)] * len(self.SUPPORTED_COINS)).reshape(len(self.SUPPORTED_COINS), 1) |
#==== Calculate portfolio with the minimum risk ====# |
min_risk = self.get_min_risk(weights, cov_matrix) |
min_return = np.dot(min_risk, returns.values) |
#==== Calculate portfolio with the maximum return ====# |
max_return = self.get_max_return(weights, returns) |
#==== Calculate efficient frontier ====# |
frontier = self.efficient_frontier( |
returns, cov_matrix, min_return, max_return, 6 |
) |
return frontier" |
730,"def handle_default_options(options): |
"""""" |
Pass in a Values instance from OptionParser. Handle settings and pythonpath |
options - Values from OptionParser |
"""""" |
if options.settings: |
#Set the percept_settings_module (picked up by settings in conf.base) |
os.environ['PERCEPT_SETTINGS_MODULE'] = options.settings |
if options.pythonpath: |
#Append the pythonpath and the directory one up from the pythonpath to sys.path for importing |
options.pythonpath = os.path.abspath(os.path.expanduser(options.pythonpath)) |
up_one_path = os.path.abspath(os.path.join(options.pythonpath, "".."")) |
sys.path.append(options.pythonpath) |
sys.path.append(up_one_path) |
return options" |
731,"def create_parser(self, prog_name, subcommand): |
"""""" |
Create an OptionParser |
prog_name - Name of a command |
subcommand - Name of a subcommand |
"""""" |
parser = OptionParser(prog=prog_name, |
usage=self.usage(subcommand), |
option_list=self.option_list) |
return parser" |
732,"def hook(name=None, *args, **kwargs): |
""""""Decorator to register the function as a hook |
"""""" |
def decorator(f): |
if not hasattr(f, ""hooks""): |
f.hooks = [] |
f.hooks.append((name or f.__name__, args, kwargs)) |
return f |
return decorator" |
733,"def register_hooks(func, hooks, obj): |
""""""Register func on obj via hooks. |
Hooks should be a tuple of (name, args, kwargs) where |
name is a method name of obj. If args or kwargs are not empty, |
the method will be called first and expect a new function as return. |
"""""" |
for name, args, kwargs in hooks: |
hook = getattr(obj, name) |
force_call = kwargs.pop(""_force_call"", False) |
if force_call or len(args) > 0 or len(kwargs) > 0: |
hook = hook(*args, **kwargs) |
hook(func)" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.