{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import panel as pn\n", "import pandas as pd\n", "import scipy.stats as stats\n", "from sqlalchemy import create_engine\n", "import hvplot.pandas\n", "pn.extension()\n", "db_url = 'sqlite:///instance/local.db'\n", "engine = create_engine(db_url)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Expirement to use z-score to find outlier of daily pct, not sure if this is helpful but here is it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b_stocks_df = None\n", "# load benchmark stock\n", "with engine.connect() as connection:\n", " b_stocks_df = pd.read_sql('calculated_b_stock', con=connection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### z = (x - μ) / σ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b_stocks_df['pct_mean'] = b_stocks_df.groupby('ticker')['pct']\\\n", " .transform(lambda x: x.rolling(7, min_periods=1).mean())\n", "b_stocks_df['pct_std'] = b_stocks_df.groupby('ticker')['pct']\\\n", " .transform(lambda x: x.rolling(7, min_periods=1).std())\n", "b_stocks_df['pct_z_score'] = (b_stocks_df['pct'] - b_stocks_df['pct_mean']) / b_stocks_df['pct_std']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "select = pn.widgets.Select(name='Select', options=b_stocks_df.display_name.unique().tolist())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ib_stocks_df = b_stocks_df.interactive()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "selected_stock_df = ib_stocks_df[ib_stocks_df.display_name == select]\n", "outliers = selected_stock_df[((ib_stocks_df['pct_z_score'] > 2) | (ib_stocks_df['pct_z_score'] < -2))]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outlier_plot = outliers.hvplot.scatter(x='date',y='pct',color='red')\n", "pct_plot = selected_stock_df.hvplot(x='date',y='pct')\n", "outlier_plot * pct_plot\n" ] } ], "metadata": { "kernelspec": { "display_name": "portfolio_risk_assesment", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }