{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "right-qatar",
   "metadata": {},
   "source": [
    "# How-To, First Steps\n",
    "\n",
    "## Load package and create basic options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "swiss-comedy",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.insert(0, os.path.abspath('../'))\n",
    "\n",
    "from robustfpm.finance import make_option"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "spread-hundred",
   "metadata": {},
   "source": [
    "Let's start with some basic options. Remember that 1D PutOnMax is just Put (the same goes for CallOnMax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "realistic-gates",
   "metadata": {},
   "outputs": [],
   "source": [
    "option1 = make_option(option_type='putonmax', strike=90)\n",
    "option2 = make_option(option_type='putonmax', strike=80, payoff_dates = 5)\n",
    "option3 = make_option(option_type='callonmax', strike=90, payoff_dates = [3,5])\n",
    "option4 = make_option(option_type='put2call1') # American Put2Call1 option"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amateur-reduction",
   "metadata": {},
   "source": [
    "Now let's see how these options got instantiated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "circular-circuit",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Option 1: {}'.format(option1))\n",
    "print('Option 2: {}'.format(option2))\n",
    "print('Option 3: {}'.format(option3))\n",
    "print('Option 4: {}'.format(option4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "apart-sunday",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Option 1 payoff at x = 80, 90, and 100, t = 3: {}'.format(option1.payoff([[80], [90], [100]], 3)))\n",
    "print('Option 2 payoff at x = 100, t = 3: {}'.format(option2.payoff(100, 3)))\n",
    "print('Option 3 payoff at x = 100, t = 3: {}'.format(option3.payoff(100, 3)))\n",
    "print('Option 3 payoff at x = 100, t = 4: {}'.format(option3.payoff(100, 4)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deluxe-service",
   "metadata": {},
   "source": [
    "## Creating solver and solving some problems"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hydraulic-marijuana",
   "metadata": {},
   "source": [
    "Now let's create some basic 1D Problem with Rectangular multiplicative dynamics and no trading constraints. For that, we need module `robustfpm.pricing`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ceramic-cloud",
   "metadata": {},
   "outputs": [],
   "source": [
    "from robustfpm.pricing import *\n",
    "import numpy as np\n",
    "pm1 = Problem(starting_price=np.array(100), \n",
    "            price_dynamics=ConstantDynamics(support=RectangularHandler([.9, 1.1]), type='mult'),\n",
    "            trading_constraints=NoConstraints, option=option1, \n",
    "            lattice=Lattice(delta=[1]), \n",
    "            time_horizon=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "moving-military",
   "metadata": {},
   "source": [
    "Now we create solver with some parameters."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "desirable-pixel",
   "metadata": {},
   "source": [
    "Most of the time, there is no point in tweaking *all* of these parameters, only some, namely `enable_timer` and `iter_tick`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accompanied-confidence",
   "metadata": {},
   "outputs": [],
   "source": [
    "solver = ConvhullSolver(enable_timer=True, ignore_warnings=True, iter_tick=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "casual-width",
   "metadata": {},
   "source": [
    "Now we solve it and see the result $V_0(x_0)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "random-target",
   "metadata": {},
   "outputs": [],
   "source": [
    "sol1 = solver.solve(pm1)\n",
    "# the solution is simply a dictionary\n",
    "print('Value: {0}'.format(sol1['Vf'][0][0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "congressional-natural",
   "metadata": {},
   "source": [
    "Let's play around and change Lattice step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "operating-medicaid",
   "metadata": {},
   "outputs": [],
   "source": [
    "pm1.lattice = Lattice(delta=[.1])\n",
    "sol2 = solver.solve(pm1)\n",
    "print('Value: {0}'.format(sol2['Vf'][0][0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "according-conversion",
   "metadata": {},
   "source": [
    "Let's try 2D Problem with another option and *additive* dynamics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pointed-adoption",
   "metadata": {},
   "outputs": [],
   "source": [
    "pm2 = Problem(starting_price=np.array([91,90]), price_dynamics=ConstantDynamics(support=RectangularHandler([[-1, 1],[-.75, 1]]), type='add'),\n",
    "             trading_constraints=IdenticalMap(RealSpaceHandler()),\n",
    "             option=option4,\n",
    "             lattice=Lattice(delta=[.1,.1]), time_horizon=5)\n",
    "solver.iter_tick = 200\n",
    "sol3 = solver.solve(pm2)\n",
    "print('Value: {0}'.format(sol3['Vf'][0][0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fifty-creature",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
