Skip to content

Instantly share code, notes, and snippets.

View etf-replay-rank-backtest.R
# Backtest of ETF Replay Ranking Algorithm
# clear the workspace
rm(list=ls())
# step-by-step example of ETF Replay Ranking Algorithm
##### etfReplayRank Function #####
# we can use the same step-by-step approach outlined above to write a function
# to rank instruments based on ETF Replay
@vishalbelsare
vishalbelsare / etfReplayRank.R
Created Sep 19, 2022 — forked from rbresearch/etfReplayRank.R
implementation of ranking algorithm similar to ETF Replay
View etfReplayRank.R
# Ranking Algorithm for ETF Replay
# Rank based on 3 factors
# 1. ReturnA (ret1)
# 2. ReturnB (ret2)
# 3. Volatility (vol)
# Returns are ranked from high to low
# Volatility is ranked from low to high
@vishalbelsare
vishalbelsare / orthogonalization.py
Created Sep 8, 2022 — forked from nicklatin/orthogonalization.py
Klein and Chow (2013) propose an optimal simultaneous orthogonal transformation of factors, following the so-called symmetric procedure of Schweinler and Wigner (1970) and Löwdin (1970): https://www.sciencedirect.com/science/article/abs/pii/S1062976913000185. The data transformation allows the identification of the underlying uncorrelated compon…
View orthogonalization.py
# orthogonalization of correlated factors in a multifactor model
def orthogonalize_factors(factors_df, output_format='df'):
"""
As described by Klein and Chow (2013) in Orthogonalized Factors and Systematic Risk Decompositions:
https://www.sciencedirect.com/science/article/abs/pii/S1062976913000185
They propose an optimal simultaneous orthogonal transformation of factors, following the so-called symmetric procedure
of Schweinler and Wigner (1970) and Löwdin (1970). The data transformation allows the identification of the underlying uncorrelated
components of common factors without changing their correlation with the original factors. It also facilitates the systematic risk
decomposition by disentangling the coefficient of determination (R²) based on factors' volatilities, which makes it easier to distinguish
@vishalbelsare
vishalbelsare / high_low_spread_estimator.py
Created Sep 8, 2022 — forked from nicklatin/high_low_spread_estimator.py
Computes the high-low spread estimator, an estimate of bid-offer spreads, a measure of liquidity risk. See Corwin & Schultz (2011) for details: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1106193
View high_low_spread_estimator.py
# high-low spread estimator (hlse)
def hlse(ohlc_df, frequency='daily'):
"""
Computes the high-low spread estimator, an estimate of bid-offer spreads, a measure of liquidity risk.
See Corwin & Schultz (2011) for details: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1106193
Parameters
----------
ohlc_df: DataFrame
DataFrame with DatetimeIndex and Open, High, Low and Close (OHLC) prices from which to compute the high-low spread estimates.
@vishalbelsare
vishalbelsare / macro_factor_mimicking_portfolios.py
Created Sep 8, 2022 — forked from nicklatin/macro_factor_mimicking_portfolios.py
Takes observable macro factors (surprises) as inputs and creates macro factor-mimicking portfolios (MFMPs) as outputs. It uses a novel machine-learning approach, the Principal Components Instrumental Variables FMP Estimator, described by Jurczenko and Teiletche (2020) in Macro Factor-Micking Portfolios: https://papers.ssrn.com/sol3/papers.cfm?ab…
View macro_factor_mimicking_portfolios.py
"""
This function takes observable macro factors (surprises) as inputs and creates macro factor mimicking portfolios (MFMPs) as outputs. It uses
a novel machine-learning approach, the Principal Components Instrumental Variables FMP Estimator,
described by Jurczenko and Teiletche (2020) in Macro Factor-Micking Portfolios:
https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3363598
The methodology addresses many of the common problems associated with macro factors and multifactor risk modeling and, as they show,
is superior to other common FMP approaches.
We describe the steps of the PCIV algorithm below, as well as in our Medium post:
For a more detailed explanation, see the link to the paper above.
Note that access to macroeconomic and base assets is required to estimate macro factor-mimicking portfolios. See the macro factors
@vishalbelsare
vishalbelsare / bookmarks_from_sql.py
Created Sep 5, 2021 — forked from iafisher/bookmarks_from_sql.py
Programmatically access your Firefox bookmarks
View bookmarks_from_sql.py
"""
A script to automatically export bookmarks from Firefox's SQLite database.
There does not seem to be a programmatic way to get Firefox to export its bookmarks in
the conventional HTML format. However, you can access the bookmark information directly
in Firefox's internal database, which is what this script does.
Always be careful when working with the internal database! If you delete data, you will
likely not be able to recover it.
View lazy.frame.R
# lazy data frame
#
# like idata.frame, but exploits lazy evaluation + macro code generation
# instead of active bindings
library(vadr)
lazy.frame <- function(df, enclos=parent.frame(), ...) UseMethod("lazy.frame")
lazy.frame.lazy.frame <- function(df, ...) df
@vishalbelsare
vishalbelsare / ERGM_from_scratch
Last active Aug 10, 2021 — forked from dmasad/ERGM_from_scratch
Implementing an ERGM from scratch
View ERGM_from_scratch
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@vishalbelsare
vishalbelsare / multi-face.ipynb
Created Jul 24, 2021 — forked from yang-zhang/multi-face.ipynb
Multi-task Deep Learning Experiment using fastai Pytorch
View multi-face.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View setup_part1.py
import numpy as np
import pandas as pd
from datetime import datetime
from fastparquet import write
def compute_vwap(df):
q = df['foreignNotional']
p = df['price']