Danilo Lessa Bernardineli
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Script: Generating Stochastic Processes ###### tags: `cadCAD edu` ___ ## Links - Slides: https://docs.google.com/presentation/d/1CNYuuiRn_ckGczodvP4_E-E4RKA1LvCfV_BRzya8YC4/edit - Notebook: https://github.com/cadCAD-edu/cadCAD-Hacks-Private/blob/main/season-1/cadcad-hack-generating-stochastic-processes.ipynb - GDrive folder: https://drive.google.com/drive/u/0/folders/1dLm1z2erLPCER5HU9Lm1MuxOl8y-iAkE ___ ## Transcript ___ **Slide 1 (Logo)** - Welcome to cadCAD Hacks, the format in which we introduce you to the tips and tricks used by professional Web 3 cadCAD engineers. ___ **Slide 2 (How to)** - In this cadCAD Hack, we will demonstrate step by step how to generate stochastic processes as part of a cadCAD model. ___ **Slide 3 (Why Should You Care)** - Why should you care about this Hack as a cadCAD Engineer? - You'll find this very useful for simulating values that are representative of real data for modeling and counterfactuals! ___ **Slide 4 (What this video covers)** - Alright, here is what we'll cover in this video is - How to generate Gamma random numbers - How to generate Gaussian random numbers ___ **Slide 5 (Let's get started...)** - Now, let’s jump straight into the Jupyter Notebook, and get started! ___ **Notebook (Table of Contents)** - Alright, our Notebook is structured along with the cadCAD Edu Standard Notebook Layout and it is based on the **Live API Data hack**, which shows how to load the Ethereum per Bitcoin historical price data inside a cadCAD model; If the **Live API Data hack** is new to you, we highly recommend revisiting cadCAD Hack number 7 before continuing. - We’ll first import any required dependencies. - In the **Setup and Preparatory Steps** we will download historical prices from Coingecko API. - In the **modeling** and **simulation** sections we will use the same **Live API Data hack** model. - We'll spend most of our time here in the **Analysis** section (**NOTE: show with mouse**), where we're going to generate Normal and Gamma random distributions, and visually compare them agains historical data. - Alright, let's jump in. ___ **Notebook (Dependencies)** As in any cadCAD Hack, we have two dependency sections - **The first one** contains the **cadCAD standard dependencies** you are familiar with from the Complete Foundations Bootcamp or the cadCAD Edu Cheat Sheet. - As for the **hack-specific dependencies** we'll import: - **json** and **requests** libraries for pulling the price data from the API; - **math** for generating random numbers; - **Scipy Stats module** for generating stochastic processes. This is a scientific library that contains all kinds of statistical and numerical features that one should desire!; - **Plotly Express** and **pyplot** for visualization. - Given that our hack is based on the Hack number 7, Live Data, let's jump straight to the analysis section. ___ **Notebook (Analysis)** - Okay. So for a refresher, our output for the Live Data API hack are figures that show how the Ethereum price as denominated in Bitcoins evolves daily over time, as well as the percent change on each day. - Suppose now that we want to simulate data points for the price feed that resembles the real data. One way of doing that, is by taking random numbers that follows a similiar probability distribution, and one typical choice is to use the Gamma Distribution. - Long history short, the Gamma distribution generates random numbers that are between zero and infinity, and it contains several statistical properties that makes it a natural choice for price data. - In order to generate data with it, we define the `gamma_proc` function, which takes as a input a number of points to be generated and returns a list of random numbers. It works by invoking the scipy stats `gamma` class, and by using the `rvs` method of it. We are setting arbitrary numbers here for generating something that is close to the real data, although this can be done automatically by using Statistical Fitting procedures. - Then, we create a function called `generate_gamma` that takes a cadCAD results dataframe, and returns a new dataframe that contains two additional columns: one which is called `origin`, which will keep track of what data is generated or real, and another called `daily_price`, which contains the real numbers. By taking the old and the new dataframe together, we concatenate them so that we can compare them into a single visualization. - With that function ready, we perform our data preparation step: we get the simulation results, we drop the first row, we assign the `origin` column so that it indicates that the source is the real data, and we pipe that dataframe into our `generate_gamma` function. - The result is a dataframe that contains generated and real price data for each timestep, and we then plot it through the `px.histogram` method. - By passing `daily_price` into `x`, we can visualize the distribution of the prices, and by passing color equals `origin`, we can differentiate between the source. Also, we pass margin equals `violin` so that we have a second visualization of the distribution. - And that's it! Notice that the real data - indicated by blue - bars and margin is remarkebly similiar to the generated ones - indicated by red. This demonstrates that the generated data should look relatively similiar to the real one. - We can also use a different probability distribution for a different variable. For instance, this time we'll be using a Normal Distribution for modelling the relative daily price changes. The procedure is just like the previous one, but we use the `normal_proc` function instead of the `gamma_proc`, where we invoke `st.norm.rvs` for generating normal numbers rather than gamma ones. Also, on the `fig_df` definition, we set the variable of interest as being `normed`, which is equal to the difference on daily prices divided by the current price. - By doing that, we can visualize again the distribution when comparing real data vs simulated data. Notice that although they're generally similiar, the real data is long-tailed while the simulated one is not. This is a common phenomenae on complex systems, and is the basis of a lot of common knowledge and concepts in finance, like the notion of anti-fragility. **Notebook (OUTRO)** - And that's it for this Hack! By having a grasp of how to generate random numbers that follow different probability distributions and to compare them with real data, you're now equipped to perform stochastic simulations that are more realistic and nuanced. This is a key skill when dealing with real world complex systems! ___ **Slide 5 (Happy Hacking)** - We hope you enjoyed this Hack and that it will be useful on your journey as an cadCAD engineer. - As always, happy hacking, and see you next time!

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully