예제 #1
0
def get_xlwings_exercise() -> LabExercise:
    xlwings_mono = pl.Monospace('xlwings')
    bullet_contents = [
        [[
            'For all of the', xlwings_mono,
            'lab exercises, work with "xlwings Lab.xlsx".'
        ],
         [
             'Use', xlwings_mono,
             'to read the values in the column A and then write them beside',
             'the initial values in column B'
         ]], [
             'Get the value in C9 and multiply it by 2.5 in Python',
         ],
        [
            'Read the table which starts in E4 into Python. Multiply the prices by 2.5, and then output '
            'back into Excel starting in cell H5.',
            'Ensure that the outputted table appears in the same format as the original (pay attention to '
            'index and header)'
        ],
        [
            'In column L, write 5, 10, 15 ... 100 spaced two cells apart, so L1 would have 5, L4 would have 10, '
            'and so on.'
        ]
    ]
    return LabExercise(bullet_contents,
                       'Dynamically Reading and Writing to Excel',
                       f"Reading and Writing to Excel with {xlwings_mono}",
                       label='lab:xlwings')
def get_monte_carlo_python_exercise() -> LabExercise:
    retirement_model = pl.Monospace('RetirementModel')

    bullet_contents = [
        [
            pl.TextSize(-1),
            'Work off of your existing Project 1 Python model',
            'You are concerned the NPV could be heavily affected by changes in the interest rate. '
            'Instead of fixing it, draw it from a normal distribution with mean of 7% and standard deviation of 2%.',
            'Run the model 10,000 times and collect the years to retirement results. Visualize the results. Create a '
            'table of probabilities and the minimum NPV we could expect with that probability. Output '
            'the chance that the NPV will be more than \\$400,000,000.'
        ],
        [
            pl.TextSize(-1),
            "Continue from the first lab exercise. Now you are also concerned that your assembly line will not be "
            "as efficient amd so the number of phones per machine will be lower. So draw that from a normal "
            "distribution with mean 100,000 and standard deviation of 20,000. ",
            "As you run the model, also store what were the interest and number of phones corresponding "
            "to the NPV. You want to see which has a greater impact on the NPV: "
            "interest or number of phones. Visualize the relationship between interest and NPV, and "
            "the relationship between beginning salary and NPV. Also run a regression "
            "to quantitatively determine which has a greater effect."
        ]
    ]

    return LabExercise(bullet_contents,
                       'Monte Carlo Python',
                       f"Monte Carlo Simulation of Python Models",
                       label='lab:monte-carlo-python')
예제 #3
0
def get_lab_exercise() -> LabExercise:
    pd_mono = pl.Monospace('pandas')
    to_excel_mono = pl.Monospace('to_excel')
    bullet_contents = [
        [
            'Download "MSFT Financials.xls" from Canvas in Lab Exercises -> Read Write Excel Pandas',
            'Read the sheet "Income Statement" into a DataFrame',
            'Write the DataFrame to a new workbook, "My Data.xlsx", with the sheet '
            'name "Income Statement"'
        ],
        [[
            'Use the same "MSFT Financials.xls" from the first exercise on Slide',
            pl.Ref('lab:read-write-excel-pandas-1')
        ],
         'Output to five separate workbooks, named "My Data1.xlsx", "My Data2.xlsx", and so on.',
         [
             'Do this',
             pl.Underline('without'), 'writing the', to_excel_mono,
             'command multiple times'
         ]],
        [
            'Note: this exercise uses the Advanced material covered in Examples -> Read Write Excel Pandas',
            [
                'Use the same "MSFT Financials.xls" from the first exercise on Slide',
                pl.Ref('lab:read-write-excel-pandas-1')
            ],
            'Output to five separate sheets in the same workbook "My Data.xlsx". The sheets should '
            'be named "Income Statement 1", "Income Statement 2", and so on.',
            [
                'Do this',
                pl.Underline('without'), 'writing the', to_excel_mono,
                'command multiple times'
            ]
        ]
    ]
    return LabExercise(bullet_contents,
                       'Reading and Writing to Excel',
                       f"Reading and Writing to Excel with {pd_mono}",
                       label='lab:read-write-excel-pandas')
def get_content():
    lecture = get_python_basics_lecture()
    conditionals_lab = get_python_basics_conditionals_lab_lecture().to_pyexlatex()
    lists_lab = get_python_basics_lists_lab_lecture().to_pyexlatex()
    functions_lab = get_python_basics_functions_lab_lecture().to_pyexlatex()
    data_types_lab = get_python_basics_data_types_lab_lecture().to_pyexlatex()
    classes_lab = get_python_basics_classes_lab_lecture().to_pyexlatex()
    appendix_frames = [
        *conditionals_lab.appendix_frames(),
        *lists_lab.appendix_frames(),
        *functions_lab.appendix_frames(),
        *data_types_lab.appendix_frames(),
        *classes_lab.appendix_frames()
    ]


    next_slide = lp.Overlay([lp.NextWithIncrement()])
    function_example = pl.Python(
"""
def my_func(a, b, c=10):
    return a + b + c

>>> my_func(5, 6)
21
""")

    use_class_example = pl.Python(
"""
from car_example import Car

>>> my_car = Car('Honda', 'Civic')
>>> print(my_car)
Car(make='Honda', model='Civic')
>>> type(my_car)
car_example.Car
>>> my_car.make
'Honda'
>>> my_car.drive()
'The Honda Civic is driving away!'
""")

    dataclass_example = pl.Python(
"""
from dataclasses import dataclass

@dataclass
class ModelInputs:
    interest_rates: tuple = (0.05, 0.06, 0.07)
    pmt: float = 1000

>>> inputs = ModelInputs(pmt=2000)
>>> print(inputs)
ModelInputs(interest_rates=(0.05, 0.06, 0.07), pmt=2000)
>>> type(inputs)
__main__.ModelInputs
>>> inputs.interest_rates
(0.05, 0.06, 0.07)
>>> inputs.pmt
2000
""")

    if_example = [pl.Python(
"""
>>> if 5 == 6:
>>>     print('not true')
>>> else:
>>>     print('else clause')
>>> 
>>> this = 'woo'
>>> that = 'woo'
>>> 
>>> if this == that:
>>>     print('yes, print me')
>>> if this == 5:
>>>     print('should not print')
"""
    ), pl.Monospace('else clause'), OutputLineBreak(), pl.Monospace('yes, print me')]

    build_list_example = pl.Python(
"""
>>> inputs = [1, 2, 3]
>>> outputs = []
>>> for inp in inputs:
>>>     outputs.append(
>>>         inp + 10
>>>     )
>>> outputs.insert(0, 'a')
>>> print(outputs)
['a', 11, 12, 13]
"""
    )

    enumerate_example = [pl.Python(
"""
>>> inputs = ['a', 'b', 'c']
>>> for i, inp in enumerate(inputs):
>>>     print(f'input number {i}: {inp}')
"""
    ),
        pl.Monospace('input number 0: a'),
        OutputLineBreak(),
        pl.Monospace('input number 1: b'),
        OutputLineBreak(),
        pl.Monospace('input number 2: c')
    ]

    list_indexing_example = pl.Python(
"""
>>> my_list = ['a', 'b', 'c', 'd']
>>> my_list[0]  # first item
'a'
>>> my_list[1]  # second item
'b'
>>> my_list[-1]  # last item
'd'
>>> my_list[:-1]  # up until last item
['a', 'b', 'c']
>>> my_list[1:]  # after the first item
['b', 'c', 'd']
>>> my_list[1:3]  # from the second to the third item
['b', 'c']
"""
    )
    f_string_example = pl.Python(
"""
>>> my_num = 5 / 6
>>> print(my_num)
0.8333333333333334
>>> print(f'My number is {my_num:.2f}')
'My number is 0.83'
"""
    )
    f_string = pl.Monospace("f''")
    f_mono = pl.Monospace('f')
    try_except_example = pl.Python(
"""
>>> my_list = ['a', 'b']
>>> try:
>>>     my_value = my_list[10]
>>> except IndexError:
>>>     print('caught the error')
caught the error
"""
    )
    list_100_5 = pl.Monospace('[100] * 5')
    next_slide = lp.Overlay([lp.NextWithIncrement()])
    annuity_example = pl.Python(
"""
>>> annuity = [100] * 5
>>> annuities = [
>>>     annuity,
>>>     [0, 0, 0] + annuity
>>> ]
>>> n_years = 10
>>> output = [0] * n_years
>>> for i in range(n_years):
>>>     for ann in annuities:
>>>         try:
>>>             output[i] += ann[i]
>>>         except IndexError:
>>>             pass
>>> print(output)   
[100, 100, 100, 200, 200, 100, 100, 100, 0, 0]
"""
    )

    site_link = Hyperlink(SITE_URL, 'the course site')

    return [
        pl.Section(
            [
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        "Now we are going to build our first complex Python model",
                        "We will also learn a bit more Python before we can get there",
                        "Just as we did in Excel, we need to add structure to make the model navigatable",
                        "Logic should be organized in functions and be documented",
                    ],
                    graphics=[
                        images_path('python-logo.png')
                    ],
                    title='An Organized Structure of an Advanced Python Model'
                ),
                lp.GraphicFrame(
                    [
                        get_model_structure_graphic()
                    ],
                    title='The Structure of a Complex Model'
                ),
            ],
            title='Introduction',
            short_title='Intro'
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        lp.Block(
                            [
                                pl.TextSize(-1),
                                if_example,
                            ],
                            title='If Statements in Python'
                        ),
                    ],
                    title='Python Conditionals - If Statement'
                ),
                lp.DimRevealListFrame(
                    [
                        'Use two equals signs to compare things (single to assign things)',
                        'Else is equivalent to value if false behavior in Excel',
                        'We can do a lot more than just set a single value, anything can be done in an if or else statement',
                        [pl.Monospace('elif'),
                         ' is a shorthand for else if, e.g. not the last condition, but this condition']
                    ],
                    title='Explaining the If-Else Statements'
                ),
                InClassExampleFrame(
                    [
                        f"On {site_link}, there is a Jupyter notebook called Python Basics containing all "
                        f"of the examples for today's lecture",
                        'Now I will go through the example material under "Conditionals"'
                    ],
                    title='Conditionals Example',
                    block_title='Trying out Conditionals'
                ),
                conditionals_lab.presentation_frames(),
            ],
            title='Conditionals'
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        lp.Block(
                            build_list_example,
                            title='List Building'
                        ),
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                ['Use ', pl.Monospace('.append'), ' to add an item to the end of a list'],
                                ['Use ', pl.Monospace('.insert'), ' to add an item at a certain position'],
                            ])
                        ])
                    ],
                    title='Python Patterns - Building a List'
                ),
                lp.Frame(
                    [
                        pl.UnorderedList([
                            'Index is base zero (0 means first item, 1 means second item)',
                        ]),
                        pl.VFill(),
                        list_indexing_example,
                    ],
                    title='List Indexing and Slicing'
                ),
                InClassExampleFrame(
                    [
                        "We will keep working off of Python Basics.ipynb",
                        'Now I will go through the example material under "Working more with Lists"'
                    ],
                    title='Lists Example',
                    block_title='Doing More with Lists'
                ),
                lists_lab.presentation_frames(),
            ],
            title='More with Lists',
            short_title='Lists'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        "In Python, we can group logic into functions",
                        "Functions have a name, inputs, and outputs",
                        "Functions are objects like everything else in Python",
                        function_example,
                    ],
                    title='Functions - Grouping Reusable Logic'
                ),
                InClassExampleFrame(
                    [
                        "We will keep working off of Python Basics.ipynb",
                        'Now I will go through the example material under "Functions"'
                    ],
                    title='Functions Example',
                    block_title='Structuring Code using Functions'
                ),
                functions_lab.presentation_frames(),
            ],
            title='Functions'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        'In Python, everything is an object except for variable names, which are references to objects',
                        'Every object has a type. We have learned about strings, numbers, lists, and booleans (True, False)',
                        'In the next section on classes, we will learn more about the relationship between the type '
                        'and the object'
                    ],
                    title='What are Types?'
                ),
                # TODO [#12]: add f-strings to Jupyter example
                lp.Frame(
                    [
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                'You may have noticed that we can end up with a lot of decimals in Python output',
                                'Further, you may want to include your results as part of a larger output, such as a sentence.',
                                f'For these operations, we have {f_mono} strings: {f_string}'
                            ],
                                vertical_fill=True)
                        ]),
                        lp.Block(
                            f_string_example,
                            title='Example',
                            overlay=next_slide
                        )
                    ],
                    title='Formatting Python Strings'
                ),
                lp.DimRevealListFrame(
                    [
                        'So far I have just said that numbers are a type in Python, but this is a simplification',
                        ['There are two main types of numbers in python:', pl.Monospace('float'), 'and',
                         pl.Monospace('int'), 'corresponding to a floating point number and an integer, respectively'],
                        ['An', pl.Monospace('int'), 'is a number without decimals, while a', pl.Monospace('float'),
                         'has decimals, regardless of whether they are zero'],
                        ['For example,', pl.Monospace('3.5'), 'and', pl.Monospace('3.0'), 'are floats, while',
                         pl.Monospace('3'), 'is an int, even though', pl.Monospace('3.0 == 3 is True')],
                        ["Usually, this doesn't matter. But to loop a number of times, you must pass an",
                         pl.Monospace('int')]
                    ],
                    title='Numeric Types'
                ),
                lp.DimRevealListFrame(
                    [
                        ['A', pl.Monospace('tuple'), 'is like a', pl.Monospace('list'), "but you can't change it after "
                         "it has been created (it is immutable)"],
                        ['Tuples are in parentheses instead of brackets, e.g.', pl.Monospace('("a", "b")')],
                        ['A', pl.Monospace('dict'), 'short for dictionary, stores a mapping. Use them if you want '
                         'to store values associated to other values'],
                        ['We will come back to', pl.Monospace('dicts'), 'later in the course, but I wanted to',
                         'introduce them now as they are a very fundamental data type']
                    ],
                    title='Additional Built-In Types'
                ),
                InClassExampleFrame(
                    [
                        "We will keep working off of Python Basics.ipynb",
                        'Now I will go through the example material under "Exploring Data Types"'
                    ],
                    title='Data Types Example',
                    block_title='Understanding the Different Data Types'
                ),
                data_types_lab.presentation_frames(),
            ],
            title='More about Data Types',
            short_title='Data Types'
        ),
        pl.Section(
            [
                lp.GraphicFrame(
                    images_path('class-object.pdf'),
                    title='Overview of Classes and Objects'
                ),
                lp.DimRevealListFrame(
                    [
                        'In Python, everything is an object except for variable names, which are references to objects',
                        'Strings, floats, ints, lists, and tuples are types of objects. There are many more types of '
                        'objects and users can define their own types of objects',
                        'A class is a definition for a type of object. It defines how it is created, the data '
                        'stored in it, and the functions attached to it',
                        'We can write our own classes to create new types of objects to work with'
                    ],
                    title='Everything is an Object. Every Object has a Class'
                ),
                lp.DimRevealListFrame(
                    [
                        'From a single class definition, an unlimited number of objects can be created',
                        'Typically the class definition says it should accept some data to create the object',
                        'Then when you have multiple objects of the same type (created from the same class), '
                        'they will have the same functions (methods) attached to them, but different data stored within',
                        ['For example, we can create two different lists. They will have different contents, but we can',
                        'do', pl.Monospace('.append'), 'on either of the lists']
                    ],
                    title='Many Objects to One Class'
                ),
                lp.MultiGraphicFrame(
                    [
                        images_path('class-object.pdf'),
                        images_path('list-object.pdf')
                    ],
                    title='Lists are Objects',
                    vertical=False
                ),
                lp.MultiGraphicFrame(
                    [
                        images_path('class-object.pdf'),
                        images_path('car-object.pdf')
                    ],
                    title='We can Make Custom Objects Too',
                    vertical=False
                ),
                lp.Frame(
                    [
                        pl.UnorderedList(['Constructing an object from a class looks like calling a function:']),
                        lp.Block(
                            [
                                pl.TextSize(-1),
                                use_class_example,
                            ],
                            title='Using Custom Classes in Python'
                        ),
                    ],
                    title='Creating and Using Objects'
                ),
                lp.DimRevealListFrame(
                    [
                        'I will not be teaching you about creating general classes in this course. It is very useful '
                        'but is generally more advanced. I encourage you to learn them outside the course.',
                        "We covered this material for two reasons:",
                        ['To give a better understanding of how Python works in general, and why sometimes we '
                        'call functions as', pl.Monospace('something.my_func()'), 'rather than',
                         pl.Monospace('my_func()')],
                        ['We are going to use', pl.Monospace('dataclasses'), 'to store our model data. They are',
                         'a simplified version of classes used mainly for storing data.']
                    ],
                    title='Where we Will Focus in This Course'
                ),
                lp.Frame(
                    [
                        pl.UnorderedList(['An organized way to store our model input data:']),
                        lp.Block(
                            [
                                pl.TextSize(-3),
                                dataclass_example,
                            ],
                            title='Using Dataclasses in Python'
                        ),
                    ],
                    title='Dataclass Intro'
                ),
                lp.DimRevealListFrame(
                    [
                        ['A', pl.Monospace('dataclass'), 'is just a class which is more convenient to create, and',
                         'is typically used to group data together'],
                        ['If you need to pass around multiple variables together, they make sense. For our models, we '
                        'will want to pass around all the inputs, so one', pl.Monospace('dataclass'), 'for all the',
                         'inputs to the model makes sense'],
                        'This way instead of having to pass around every input individually to every function, just '
                        'pass all the input data as one argument',
                        ['Also enables easy tab-completion. What were the names of my inputs? Just hit tab after',
                         pl.Monospace('data.')]
                    ],
                    title='What, When and Why Dataclasses?'
                ),
                InClassExampleFrame(
                    [
                        "We will keep working off of Python Basics.ipynb",
                        'For this example, also go and download car_example.py and put it in the same folder',
                        'Now I will go through the example material under "Working with Classes"'
                    ],
                    title='Classes Example',
                    block_title='Working with Classes and Creating Dataclasses'
                ),
                classes_lab.presentation_frames(),
            ],
            title='Classes and Dataclasses',
            short_title='Classes'
        ),
        pl.Section(
            [
                # TODO [#13]: add error handling to Jupyter example
                lp.Frame(
                    [
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                "You have certainly already seen errors coming from your Python code. When they have come up, the code doesn't run.",
                                'Sometimes you actually expect to get an error, and want to handle it in some way, rather than having your program fail.'
                            ],
                                vertical_fill=True)
                        ]),
                        lp.Block(
                            try_except_example,
                            title='Example',
                            overlay=next_slide
                        )
                    ],
                    title='Python Error Handling'
                ),
                lp.DimRevealListFrame(
                    [
                        "Let's say you're receiving annuities. There is a single annuity which produces \$100 for 5 years. You receive this annuity in year 0 and in year 3.",
                        f"You might define the annuity cash flows as a list of 100, 5 times ({list_100_5})",
                        'Then you want to come up with your overall cash flows, going out to 15 years'
                    ],
                    title='An Example where Error Handling is Useful'
                ),
                lp.Frame(
                    [
                        lp.Block(
                            [
                                pl.TextSize(-1),
                                annuity_example,
                            ],
                            title='Calculating the Sum of Unaligned Annuity Cash-Flows'
                        )
                    ],
                    title='Applying Error Handling'
                ),
            ],
            title='Error Handling',
            short_title='Errors'
        ),
        pl.PresentationAppendix(
            [
                lecture.pyexlatex_resources_frame,
                *appendix_frames
            ]
        )
    ]
예제 #5
0
def get_lab_exercise() -> LabExercise:
    pd_mono = pl.Monospace('pandas')
    dfs_mono = pl.Monospace('DataFrames')
    next_slide = lp.Overlay([lp.UntilEnd(lp.NextWithIncrement())])
    df_to_excel_example = pl.Python(
        "df.to_excel('data.xlsx', sheet_name='My Data', index=False)")
    df_from_excel_example = pl.Python(
        "df = pd.read_excel('data.xlsx', sheet_name='My Data')")
    index_false_mono = pl.Monospace('index=False')
    addin_install_mono = pl.Monospace('xlwings addin install')
    addin_install_success = pl.Monospace(
        'Successfuly installed the xlwings add-in! Please restart Excel.')
    random_seed_py = pl.Monospace('random_seed.py')
    random_seed_excel = pl.Monospace('random_seed.xlsm')
    quickstart_mono = pl.Monospace('xlwings quickstart')
    quickstart_project_mono = pl.Monospace(
        'xlwings quickstart my_project_name')
    cd_mono = pl.Monospace('cd')
    xw_func_decorator = pl.Monospace('@xw.func')
    random_choice_mono = pl.Monospace('random_choice')
    random_choices_mono = pl.Monospace('random_choices')
    random_choice_py = pl.Monospace('random.choices')
    xlwings_mono = pl.Monospace('xlwings')
    df_returns_example = pl.Monospace("df['Stock Price'].pct_change()")

    bullet_contents = [
        [[
            'In your', xlwings_mono,
            'Python file, add a function which will generate 1, 2, ... N in a column for',
            'whatever N is passed.'
        ],
         [
             'Add a cell in your wookbook containing the number of rows to be generated, and call your UDF in',
             'another cell.'
         ],
         [
             'Change the number of rows to be generated and see the number of rows in the output change.'
         ]],
        [[
            'In your', xlwings_mono,
            'project workbook, add a column Stock Price which has five prices below it,',
            '100, 110, 115, 108, and 105.'
        ],
         [
             'In the Python file, write a UDF, which accepts the top-left cell of this table (the one',
             'with Stock Price), and returns the stock returns.'
         ],
         [
             'Call this UDF next to the stock price column so that you have the return from last period to this one',
             'next to the stock prices.'
         ], ['You may find', df_returns_example, 'useful for this purpose.'],
         ['Answers should be 10%, 4.5%, -6%, -2.8%']]
    ]

    return LabExercise(bullet_contents,
                       'Advanced UDFs',
                       f"Dynamic and Array UDFs with {xlwings_mono}",
                       label='lab:dynamic-array-udf')
예제 #6
0
def get_content():
    random.seed(1000)
    site_link = Hyperlink(SITE_URL, 'the course site')
    lecture = get_getting_started_with_python_and_excel_lecture()

    full_fix = Monospace('\$A\$2')
    col_fix = Monospace('\$A2')
    row_fix = Monospace('A\$2')
    notebook = Monospace('jupyter notebook')
    lab = Monospace('jupyter lab')
    list_ = Monospace('list')
    for_ = Monospace('for')
    append = Monospace('append')
    numpy = Monospace('numpy')
    cmd = Monospace('cmd')
    terminal = Monospace('terminal')
    in_block = Monospace('In [ ]:')
    numpy_financial = pl.Monospace('numpy_financial')

    example_python_iteration = Block(
        Python("""
inputs = [5, 10, 15]
for item in inputs:
    new_value = item + 2
    print(new_value)

7
12
17
        """),
        title='Python Iteration'
    )
    return [
        pl.Section(
            [
                TwoColumnGraphicDimRevealFrame(
                    [
                        'The focus today is to get familiar working in both Excel and Python',
                        'We will approach this by building a simple model with both tools',
                        'In later lectures, we will move to combining the tools'
                    ],
                    graphics=[
                        images_path('equations-chalkboard.jpg')
                    ],
                    title='Approaching a Problem with Two Tools'
                ),
                DimRevealListFrame(
                    [
                        "Let's take what is perhaps the simplest finance problem, which everyone should understand",
                        "While you may have approached such a problem with a calculator before, we will build models for it instead",
                        "Martha is saving for retirement. She earns \$60,000 per year and is able to save 25% of that. If she invests "
                        "her savings, earning 5% per year, and she needs \$1,500,000 to retire, how soon can she retire?"
                    ],
                    title='A Simple Retirement Problem'
                ),
                ModelFlowchartFrame(
                    [
                        [
                            'Wages, Savings',
                            'Investment',
                            Node('Cash in the bank, person retires',
                                 options=real_world_style + ['text width=2.5cm'])
                        ],
                        [
                            Node('Cash Flows, Savings Rate, Interest Rates',
                                 options=model_style + ['text width=3.5cm']),
                            'Model',
                            Node('FV of CF, time until retirement', options=model_style + ['text width=2.5cm'])
                        ]
                    ],
                    title='Breaking Down the Retirement Problem'
                ),
            ],
            title='An Introductory Model',
            short_title='Basic Problem'
        ),
        pl.Section(
            [
                TwoColumnGraphicDimRevealFrame(
                    [
                        "It is easy to use Excel as a calculator and just type the math in directly. But we want to build a model.",
                        "Changing inputs should result in a change to outputs. The way to do this in Excel is cell references",
                        rf"Fixed references become important when trying to drag formulas, e.g. {full_fix} (fully fixed), {col_fix} (fixed on "
                        rf"column), or {row_fix} (fixed on row)."
                    ],
                    graphics=[
                        images_path('excel-logo.png')
                    ],
                    title='Solving the Problem in Excel'
                ),
                InClassExampleFrame(
                    [
                        ['Go to', site_link, 'and download Simple Retirement Model Excel'],
                        'Follow along as I recreate the simple model.'
                    ],
                    title='Simple Retirement Problem in Excel',
                    block_title='Intro Excel Exercise'
                )
            ],
            title='Excel Solution'
        ),
        pl.Section(
            [
                TwoColumnGraphicDimRevealFrame(
                    [
                        pl.TextSize(-1),
                        "Using Python in the terminal is kind of a pain. And so, tools were born.",
                        "Jupyter is a graphical interface we can use for Python. It also supports over 40 other "
                        "languages such as R, SAS, Julia, and Scala",
                        f"You can use {notebook} or {lab}. The latter has a lot more features outside of the notebook. "
                        f"We will focus on using {lab} in this class as it is the future of Jupyter."
                    ],
                    graphics=[
                        images_path('jupyter-notebook.png')
                    ],
                    title="How We'll Work in Python"
                ),
                Frame(
                    [
                        LabBlock(
                            [
                                OrderedList([
                                    "Launch Anaconda Navigator",
                                    "Find Jupyter Notebook on the main screen, and click launch",
                                    "You should see a list of folders and files. Click New and then Python 3",
                                    f"Now you should see a code cell with {in_block} next to it"
                                ])
                            ],
                            title='Launch Jupyter Notebook'
                        ),
                        AlertBlock(
                            f"If you don't have Anaconda Navigator, just open a terminal (search {cmd} on Windows, {terminal} on Mac). Then in the "
                            f"terminal, type {lab} and enter. Then continue with the third step."
                        )
                    ],
                    title="Let's Get Set up with Jupyter"

                ),
                DimRevealListFrame(
                    [
                        "In Excel, the basic unit is a cell. In Python, the basic unit is an object.",
                        "In Excel, content in a cell is either a number (123) or a string (ABC)",
                        "In Python, all objects have types. They might also be a number or a string, or something else.",
                        f"Rather than using a cell reference like {full_fix}, we assign names to objects in Python",
                        Python("""
                        my_number = 6
                        my_string = 'ABC'
                        """)
                    ],
                    title='Some Python Basics'
                ),
                TwoColumnFrame(
                    [
                        pl.Graphic(images_path('numpy-logo.png')),
                        pl.Block(
                            [
                                pl.TextSize(-2),
                                'In the future, these numpy financial functions are being moved '
                                'to a separate package', f'{numpy_financial}.', 'For',
                                "the purposes of this class, this won't matter, but in the future you may have "
                                "to install", numpy_financial, 'to use these functions.',
                                'In the meantime, you will see a warning come up when calling the functions.'
                            ],
                            title='Note: Deprecation warning'
                        ),
                    ],
                    [
                        UnorderedList([
                            DimAndRevealListItems(
                                [
                                    "Basic operations in Python are straightforward",
                                    Python('2 + 5 = 7'),
                                    Python('6 - 2 = 4'),
                                    Python('2 * 3 = 6'),
                                    Python('5 / 2 = 2.5'),
                                    f'A lot more is available using the {numpy} package',
                                    Python("np.pv, np.nper,\nnp.fv, np.pmt"),
                                    Hyperlink(
                                        'https://numpy.org/doc/stable/reference/routines.financial.html',
                                        f'All numpy financial functions')
                                ],
                                dim_earlier_items=False
                            )
                        ])
                    ],
                    title='Doing Some Math in Python'
                ),
                InClassExampleFrame(
                    [
                        ['Go to', site_link, 'and download Simple Retirement Model Python'],
                        'In Jupyter, then navigate to your Downloads folder (or wherever you saved it)',
                        'You should then see Simple Retirement Model.ipynb come up in the list of files in Jupyter. '
                        'Click it to open it and follow along.'
                    ],
                    title='Simple Retirement Problem in Python',
                    block_title='Intro Python Exercise'
                )
            ],
            title='Python Solution'
        ),
        pl.Section(
            [
                DimRevealListFrame(
                    [
                        "Now we've got basic models to determine how long it will take Martha to retire.",
                        "We've got a few assumptions built into the model. One is that Martha will earn 5% on her investments",
                        "Rates of return are volatile, so we want to see how long it would take her to retire if her return was different"
                    ],
                    title='Extending the Model - Multiple Interest Rates'
                ),
                DimRevealListFrame(
                    [
                        "In programming, for model building or otherwise, you often need to repeat the same process for multiple different things",
                        "In Excel, you would do this by dragging formulas.",
                        f"In Python, as in most other programming languages, we would use a {for_} loop",
                        "This says, do something, for each value I pass into the loop"
                    ],
                    title='Programming Fundamentals - Iteration'
                ),
                BasicTwoColumnFrame(
                    [
                        example_python_iteration
                    ],
                    [
                        Block(
                            Graphic(
                                images_path('excel-iteration.png')
                            ),
                            title='Excel Iteration'
                        )
                    ],
                    title='Iteration - Python vs. Excel'
                ),
                TwoColumnFrame(
                    [
                        UnorderedList([
                            DimAndRevealListItems([
                                "There's a few things to unpack here",
                                f"Here's another type of object: not a number or a string, but a {list_}",
                                f"A {list_} holds multiple objects, and you can add or remove items from {list_}s",
                            ])
                        ])
                    ],
                    [
                        example_python_iteration
                    ],
                    title='Explaining Python Iteration'
                ),
                TwoColumnFrame(
                    [
                        UnorderedList([
                            DimAndRevealListItems([
                                "Here we define a list of three numbers as inputs",
                                f"Then we use a {for_} loop to get each input out of the list, and add 2 to it to create the new value",
                                "Finally we print each value as it is generated"
                            ])
                        ])
                    ],
                    [
                        example_python_iteration
                    ],
                    title='Explaining Python Iteration (pt. 2)'
                ),
                InClassExampleFrame(
                    [
                        'I will now expand the existing Excel and Python models to examine multiple interest rates',
                        'Continue viewing the same previously downloaded files.'
                    ],
                    title='Iterating the Existing Model',
                    block_title='Expanding on Python and Excel'
                ),
                get_simple_retirement_lab_lecture().to_pyexlatex().presentation_frames(),
            ],
            title='Extending the Model and Iteration',
            short_title='Extend & Iterate'
        ),
        pl.PresentationAppendix(
            [
                lecture.pyexlatex_resources_frame,
                pl.TextSize(-1),
                get_simple_retirement_lab_lecture().to_pyexlatex().appendix_frames(),
                pl.TextSize(0),
            ]
        )
    ]
def get_content():
    random.seed(1000)

    lecture = get_probability_lecture()
    scenario_excel_lab = get_scenario_analysis_excel_lab_lecture().to_pyexlatex()
    scenario_python_lab = get_scenario_analysis_python_lab_lecture().to_pyexlatex()
    randomness_excel_lab = get_randomness_excel_lab_lecture().to_pyexlatex()
    randomness_python_lab = get_randomness_python_lab_lecture().to_pyexlatex()
    random_stock_lab = get_random_stock_model_lab_lecture().to_pyexlatex()
    full_model_internal_randomness_lab = get_extend_model_internal_randomness_lab_lecture().to_pyexlatex()
    appendix_frames = [
        lecture.pyexlatex_resources_frame,
        scenario_excel_lab.appendix_frames(),
        scenario_python_lab.appendix_frames(),
        randomness_excel_lab.appendix_frames(),
        randomness_python_lab.appendix_frames(),
        random_stock_lab.appendix_frames(),
        full_model_internal_randomness_lab.appendix_frames(),
    ]

    df_mono = pl.Monospace('DataFrame')
    next_slide = lp.Overlay([lp.NextWithIncrement()])
    with_previous = lp.Overlay([lp.NextWithoutIncrement()])
    rand_mono = pl.Monospace('=RAND')
    rand_between_mono = pl.Monospace('=RANDBETWEEN')
    norm_inv_mono = pl.Monospace('=NORM.INV')
    excel_random_normal_example = pl.Monospace('=NORM.INV(RAND(), 10, 1)')
    random_module_mono = pl.Monospace('random')
    py_rand_mono = pl.Monospace('random.random')
    py_rand_uniform_mono = pl.Monospace('random.uniform')
    py_rand_norm_mono = pl.Monospace('random.normalvariate')
    py_random_link = Hyperlink('https://docs.python.org/3.7/library/random.html#real-valued-distributions',
                               '(and other distributions)')
    py_random_normal_example = pl.Monospace('random.normalvariate(10, 1)')
    random_seed_example = pl.Monospace('random.seed(0)')
    next_slide = lp.Overlay([lp.NextWithIncrement()])
    n_iter = pl.Equation(str_eq='n_{iter}')
    df_mono = pl.Monospace('DataFrame')
    df_std = pl.Monospace('df.std()')
    df_mean = pl.Monospace('df.mean()')
    random_choices_mono = pl.Monospace('random.choices')
    random_choices_example = pl.Monospace("random.choices(['Recession', 'Normal', 'Expansion'], [0.3, 0.5, 0.2])")

    return [
        pl.Section(
            [
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        'So far everything in our models has been deterministic',
                        'Further, we have not explored any scenarios in our models, we have taken the base case as '
                        'the only case',
                        'Unfortunately, the real world is very random. Many possible scenarios could occur.'
                    ],
                    [
                        images_path('dice.jpg'),
                    ],
                    title='Why Model Probability'
                ),
                lp.DimRevealListFrame(
                    [
                        'There are a few ways we can gain a richer understanding of the modeled situation by '
                        'incorporating probability',
                        f'The simplest is {pl.Bold("scenario modeling")}, in which different situations are defined with probabilities, '
                        'and the result of the model is the expected value across the cases.',
                        ['Another is', pl.Bold('internal randomness'),
                         'where randomness is incorporated directly within '
                         'the model logic'],
                        ['Finally,', pl.Bold("Monte Carlo simulation"),
                         'treats the model as deterministic but externally varies the '
                         'inputs to get a distribution of outputs.']
                    ],
                    title='How to Bring Probability In'
                ),
            ],
            title='Motivation for Probability Modeling',
            short_title='Intro',
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                ['When something is measured numerically, it can be either a', pl.Bold('discrete'),
                                 'variable, or a', pl.Bold('continuous'), 'variable.'],

                            ])
                        ]),
                        lp.Block(
                            [
                                pl.Equation(str_eq=r'x \in \{x_1, x_2, ... x_n\}', inline=False),
                                pl.VSpace(-0.4),
                                pl.UnorderedList([
                                    [pl.Equation(str_eq=r'\{x_1, x_2, ... x_n\}:'), 'A specific set of values'],
                                ])
                            ],
                            title='Discrete Variables'
                        ),
                        lp.Block(
                            [
                                pl.Equation(str_eq=r'x \in \mathbb{R} \text{ or } [a, b]', inline=False),
                                pl.VSpace(-0.4),
                                pl.UnorderedList([
                                    [pl.Equation(str_eq='\mathbb{R}:'), 'All real numbers'],
                                    [pl.Equation(str_eq='[a, b]:'), 'Some interval between two values or infinity'],

                                ])
                            ],
                            title='Continuous Variables'
                        ),
                    ],
                    title='Math Review: Discrete and Continuous Variables'
                ),
                lp.Frame(
                    [
                        pl.TextSize(-3),
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                [pl.Bold('Expected value'), 'is the average outcome over repeated trials'],
                                "It is generally useful to get a single output from multiple possible cases",
                            ], dim_earlier_items=False)
                        ]),
                        lp.Block(
                            [
                                pl.Equation(str_eq=r'E[x] = \sum_{i=1}^{N} p_i x_i', inline=False),
                                pl.VSpace(-0.4),
                                pl.UnorderedList([
                                    [pl.Equation(str_eq=r'E[x]:'), 'Expected value for', pl.Equation(str_eq='x')],
                                    [pl.Equation(str_eq=r'x_i:'), 'A specific value for', pl.Equation(str_eq='x')],
                                    [pl.Equation(str_eq=r'p_i:'), 'The probability associated with value',
                                     pl.Equation(str_eq='x_i')],
                                    [pl.Equation(str_eq=r'N:'), 'The total number of possible values of',
                                     pl.Equation(str_eq='x')],
                                ])
                            ],
                            title='Discrete Variables'
                        ),
                        lp.Block(
                            [
                                pl.Equation(str_eq=r'E[x] = \frac{1}{N} \sum_{i=1}^{N} x_i', inline=False),
                                pl.VSpace(-0.4),
                                pl.UnorderedList([
                                    [pl.Equation(str_eq=r'N:'), 'The number of samples collected for',
                                     pl.Equation(str_eq='x')],
                                ])
                            ],
                            title='Continuous Variables'
                        ),
                    ],
                    title='Math Review: Expected Value'
                ),
                lp.GraphicFrame(
                    images_path('different-variance-plot.pdf'),
                    title='Math Review: Variance in One Picture'
                ),
                lp.Frame(
                    [
                        pl.TextSize(-2),
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                [pl.Bold('Variance'), 'and', pl.Bold('standard deviation'),
                                 'are measures of the dispersion '
                                 'of values of a random variable.'],
                                'Variance is the real quantity of interest, but standard deviation is easier to understand '
                                'because it has the same units as the variable, while variance has units squared'
                            ], dim_earlier_items=False),
                        ]),
                        lp.Block(
                            [
                                EquationWithVariableDefinitions(
                                    r'Var[x] = \sigma^2 = \frac{1}{N - 1} \sum_{i=1}^{N} (x_i - \mu)^2',
                                    [
                                        [pl.Equation(str_eq=r'N:'), 'Number of samples of', pl.Equation(str_eq=r'x')],
                                        [pl.Equation(str_eq=r'\mu:'), 'Sample mean'],
                                    ]
                                ),
                            ],
                            title='Variance of a Continuous Variable'
                        ),
                        lp.Block(
                            [
                                EquationWithVariableDefinitions(
                                    r'\sigma = \sqrt{Var[x]}',
                                    [
                                        [pl.Equation(str_eq=r'\sigma:'), 'Standard deviation'],
                                    ],
                                    space_adjustment=-0.5
                                ),
                            ],
                            title='Standard Deviation'
                        ),
                    ],
                    title='Math Review: Variance and Standard Deviation'
                ),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        ['A', pl.Bold('probability distribution'),
                         'represents the probabilities of different values of '
                         'a variable'],
                        'For discrete variables, this is simply a mapping of possible values to probabilities, e.g. for a coin '
                        'toss, heads = 50% and tails = 50%',
                        'For continuous variables, a continuous distribution is needed, such as the normal distribution',
                    ],
                    graphics=[
                        images_path('normal-distribution.png'),
                    ],
                    title='Math Review: Probability Distributions'
                ),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        pl.TextSize(-2),
                        ["You've probably heard of the", pl.Bold('normal distribution'),
                         'as it is very commonly used because it occurs a lot in nature'],
                        ['It is so common because of the', pl.Bold('central limit theorem'), 'which says that '
                                                                                             'averages of variables will follow a normal distribution, regardless of the distribution of the '
                                                                                             'variable itself'],
                        'This has many applications. For example, we can view the investment rate as an average across '
                        'individual investment returns, and so it will be normally distributed.',
                    ],
                    graphics=[
                        images_path('normal-distribution-percentages.png'),
                    ],
                    title='Math Review: Normal Distribution'
                ),
                lp.Frame(
                    [
                        pl.TextSize(-3),
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                'We want to extend our retirement model to say that the investment return is not constant.',
                                'We can treat the interest rate as either a discrete (specific values) or a continuous '
                                '(range of values, more realistic) variable'
                            ], dim_earlier_items=False),

                        ]),
                        lp.Block(
                            [
                                pl.Center(
                                    [
                                        lt.Tabular(
                                            [
                                                lt.ValuesTable.from_list_of_lists([
                                                    ['Interest Rate', 'Probability']
                                                ]),
                                                lt.MidRule(),
                                                lt.ValuesTable.from_list_of_lists([
                                                    ['2%', '30%'],
                                                    ['5%', '50%'],
                                                    ['7%', '20%'],
                                                ]),
                                            ],
                                            align='cc'
                                        )
                                    ]
                                )
                            ],
                            title='As a Discrete Variable'
                        ),
                        lp.Block(
                            [
                                pl.Equation(str_eq=r'r_i \sim N(\mu, \sigma^2)', inline=False),
                                pl.VSpace(-0.5),
                                pl.UnorderedList([
                                    [pl.Equation(str_eq=r'N:'), 'Normal distribution'],
                                    [pl.Equation(str_eq=r'\mu:'), 'Interest rate mean'],
                                    [pl.Equation(str_eq=r'\sigma:'), 'Interest rate standard deviation'],
                                ])
                            ],
                            title='As a Continuous Variable'
                        ),
                    ],
                    title='A Non-Constant Interest Rate'
                ),
            ],
            title='Mathematical Tools for Probability Modeling',
            short_title='Math Review'
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        pl.TextSize(-1),
                        lp.Block(
                            [
                                pl.Center(
                                    [
                                        lt.Tabular(
                                            [
                                                lt.ValuesTable.from_list_of_lists([
                                                    ['State of Economy', 'Interest Rate', 'Savings Rate', 'Probability']
                                                ]),
                                                lt.MidRule(),
                                                lt.ValuesTable.from_list_of_lists([
                                                    ['Recession', '2%', '35%', '30%'],
                                                    ['Normal', '5%', '30%', '50%'],
                                                    ['Expansion', '7%', '25%', '20%'],
                                                ]),
                                            ],
                                            align='l|ccc'
                                        )
                                    ]
                                )
                            ],
                            title='Interest Rate Scenarios'
                        ),
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                ['In scenario modeling, different cases for model parameters are chosen. Several '
                                 'parameters may be altered at once in a given case.'],
                                "Here we are making the different cases the state of the economy. When the economy is doing "
                                "poorly, the individual earns a lower return, but also saves more because they don't want to "
                                "overspend at a bad time",
                                "When the economy does well, the individual earns a higher return, but also spends more"
                            ])
                        ]),
                    ],
                    title='Scenario Modeling'
                ),
                lp.DimRevealListFrame(
                    [
                        ['We can implement scenario modeling', pl.Bold('internal'), 'or', pl.Bold('external'),
                         'to our model'],
                        ['With an internal implementation, the cases are built', pl.Underline('into the model logic'),
                         'itself, '
                         'and model logic also takes the expected value of the case outputs. The inputs of the model',
                         'are now the cases and probabilities.'],
                        ['With an external implementation, the', pl.Underline('model logic is left unchanged,'),
                         'instead the '
                         'model is run separately with each case, then the expected value is calculated across the outputs '
                         'from the multiple model runs.'],
                    ],
                    title='Implementing Scenario Modeling'
                ),
                lp.Frame(
                    [
                        pl.Center(
                            [
                                lt.Tabular(
                                    [
                                        lt.ValuesTable.from_list_of_lists([
                                            [pl.Bold('Internal'), pl.Bold('External')]
                                        ]),
                                        lt.MidRule(),
                                        lt.MidRule(),
                                        lt.ValuesTable.from_list_of_lists([
                                            ['Original model is now an old version',
                                             'Original model can still be used normally'],
                                        ]),
                                        # TODO [#14]: each row should come one per slide, but need to allow overlays in lt items
                                        lt.MidRule(),
                                        lt.ValuesTable.from_list_of_lists([
                                            ['Model runs exactly as before',
                                             'Getting full results of model requires running the model multiple times and '
                                             'aggregating output']
                                        ]),
                                        lt.MidRule(),
                                        lt.ValuesTable.from_list_of_lists([
                                            ['Model complexity has increased', 'Model complexity unchanged']
                                        ]),
                                        lt.MidRule(),
                                        lt.ValuesTable.from_list_of_lists([
                                            ['Complexity to run model is unchanged',
                                             'Complexity to run model has increased']
                                        ]),
                                    ],
                                    align='L{5cm}|R{5cm}'
                                )
                            ]
                        )
                    ],
                    title='Internal or External Scenario Analysis?'
                ),
                lp.DimRevealListFrame(
                    [
                        'For internal scenario analysis, set up a table of the cases and probabilities. Then calculate the '
                        'expected value of these cases for each model parameter. Then use the expected value as the new '
                        'model parameter.',
                        'For external scenario analysis, a data table is useful. Create the data table of outputs for each case '
                        'and another table of case probabilities, then combine them to produce the expected value of '
                        'the output.',
                        'If you are trying to change more than two inputs at once in external scenario '
                        'analysis, this becomes more '
                        'challenging but you can assign a number to each set of inputs and have the model look up the '
                        'inputs based on the case number, using the case number as the data table input.'

                    ],
                    title='Scenario Analysis in Excel'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through adding external scenario analysis to the Dynamic Salary Retirement Model '
                        'in Excel',
                        'The completed exercise on the course site as "Dynamic Salary Retirement Model Sensitivity.xlsx"',
                    ],
                    title='Scenario Analysis in Excel',
                    block_title='Adding Scenario Analysis to the Dynamic Retirement Excel Model'
                ),
                scenario_excel_lab.presentation_frames(),
                lp.DimRevealListFrame(
                    [
                        ['For internal scenario analysis, set up a', df_mono,
                         'or dictionary of the cases and probabilities. Then calculate '
                         'the expected value of these cases for each model parameter. Then use the expected value as the new '
                         'model parameter.'],
                        'For external scenario analysis, just call your model function with each input case, collect the '
                        'results, and combine them to produce the expected value of the output.'
                    ],
                    title='Scenario Analysis in Python'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through adding external scenario analysis to the Dynamic Salary Retirement Model '
                        'in Python',
                        'he completed exercise on the course site as "Dynamic Salary Retirement Model Scenario.ipynb"',
                    ],
                    title='Scenario Analysis in Python',
                    block_title='Adding Scenario Analysis to the Dynamic Retirement Python Model'
                ),
                scenario_python_lab.presentation_frames(),
            ],
            title='Scenario Modeling'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        ["Using the technique of", pl.Bold('internal randomness,'),
                         'something random is added internally to the model'],
                        'Instead of taking a fixed input, random values for that variable are drawn',
                        'This technique can be used with both discrete and continuous variables'
                    ],
                    title='What is Internal Randomness?'
                ),
                lp.GraphicFrame(
                    internal_randomness_graphic(),
                    title='Internal Randomness in One Picture'
                ),
                lp.DimRevealListFrame(
                    [
                        'Internal randomness makes sense when the random behavior is integral to your model',
                        'If you are just trying to see how changing inputs affects outputs, or trying to get confidence intervals for outputs, '
                        'an external method such as sensitivity analysis or Monte Carlo simulation would make more sense.',
                        'For example, if we want to allow investment returns to vary in our retirement model, an external method fits well because '
                        'the core model itself is deterministic',
                        'If instead we were modeling a portfolio, we might use internal randomness to get the returns for each asset.'
                    ],
                    title='Should I Use Internal Randomness?'
                ),
                lp.DimRevealListFrame(
                    [
                        'Similarly to our discussion of internal vs. external sensitivity analysis, internal randomness keeps '
                        'operational complexity (how to run the model) low, but increases model complexity.',
                        'The main drawback of internal randomness is that the same set of inputs will give different outputs each time the model is run',
                        'While this is the desired behavior, it can make it difficult to determine whether everything is working.'
                    ],
                    title='Internal Randomness Advantages and Pitfalls'
                ),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        'Instead of taking the input as fixed, draw it from a distribution',
                        'We need to define a distribution for each input we want to randomize. This will typically be a normal distribution, and then '
                        'we just need to give it a reasonable mean and standard deviation',
                        'Put the most reasonable or usual value as the mean. Then think about the probabilities of the normal distribution relative '
                        'to standard deviation to set it'
                    ],
                    graphics=[
                        images_path('normal-distribution-percentages.png'),
                    ],
                    title='Internal Randomness with Continuous Variables'
                ),
                lp.DimRevealListFrame(
                    [
                        ['The main functions for randomness in Excel are', rand_mono, 'and', rand_between_mono],
                        'The latter gives a random number between two numbers, while the former gives a random number '
                        'between 0 and 1. Both of these draw from a uniform distribution (every number equally likely)',
                        ['Meanwhile, the', norm_inv_mono,
                         'function gives the value for a certain normal distribution at a certain probability (it is not random)'],
                        'We can combine these two functions to draw random numbers from a normal distribution',
                        [excel_random_normal_example,
                         'would draw a number from a normal distribution with mean 10 and standard deviation 1'],
                    ],
                    title='Internal Randomness with Continuous Variables in Excel'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through generating random continuous variables '
                        'in Excel',
                        'The completed exercise on the course site is called "Generating Random Numbers.xlsx"',
                        'We will focus only on the "Continuous" sheet for now',
                    ],
                    title='Example for Continuous Random Variables in Excel',
                    block_title='Generating Random Numbers from Normal Distributions in Excel'
                ),
                randomness_excel_lab.presentation_frames(),
                lp.DimRevealListFrame(
                    [
                        ['In Python, we have the built-in', random_module_mono, 'module'],
                        ['It has functions analagous to those in Excel:', py_rand_mono, 'works like', rand_mono,
                         'and', py_rand_uniform_mono, 'works like', rand_between_mono],
                        ['Drawing numbers from a normal distribution', py_random_link, 'is easier: just one function',
                         py_rand_norm_mono],
                        [py_random_normal_example,
                         'would draw a number from a normal distribution with mean 10 and standard deviation 1']
                    ],
                    title='Internal Randomness with Continuous Variables in Python'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through generating random continuous variables '
                        'in Python',
                        'The completed exercise on the course site is called "Generating Random Numbers.ipynb"',
                        'We will focus only on the "Continuous" section for now',
                    ],
                    title='Example for Continuous Random Variables in Python',
                    block_title='Generating Random Numbers from Normal Distributions in Python'
                ),
                randomness_python_lab.presentation_frames(),
                lp.DimRevealListFrame(
                    [
                        'We can also build randomness into the model for discrete variables',
                        "With discrete variables, our distribution is just a table of probabilities for the different values",
                        'To pick a random value for a discrete variable, first add another column to your table which has the '
                        'cumulative sum of the prior probabilties, and then another column which is that column plus the '
                        'current probability',
                        'Then generate a random number between 0 and 1 from a uniform distribution',
                        'If the generated number is between the probability and the cumulative sum of prior probabilities, choose that case'
                    ],
                    title='Internal Randomness with Discrete Variables'
                ),
                lp.Frame(
                    [
                        pl.TextSize(-1),
                        lp.Block(
                            [
                                pl.Center(
                                    [
                                        lt.Tabular(
                                            [
                                                lt.ValuesTable.from_list_of_lists([
                                                    ['State of Economy', 'Interest Rate', 'Probability', 'Begin Range',
                                                     'End Range']
                                                ]),
                                                lt.MidRule(),
                                                lt.ValuesTable.from_list_of_lists([
                                                    ['Recession', '2%', '30%', '0%', '30%'],
                                                    ['Normal', '5%', '50%', '30%', '80%'],
                                                    ['Expansion', '7%', '20%', '80%', '100%'],
                                                ]),
                                            ],
                                            align='L{2cm}|cccc'
                                        )
                                    ]
                                )
                            ],
                            title='Interest Rate Scenarios'
                        ),
                        pl.UnorderedList([
                            pl.TextSize(-2),
                            lp.DimAndRevealListItems([
                                'The Begin Range column is calculated as the cumulative sum of prior probabilities',
                                'The End Range column is calculated as Begin Range + Probability',
                                "Generate a random number between 0 and 1. If it is between the begin and end range, "
                                "that is the selected value",
                                "If it's 0.15, it's a recession. If it's 0.45, it's a normal period. If it's 0.94, it's "
                                "an expansion period."
                            ], vertical_fill=True)
                        ]),
                    ],
                    title='An Example of Internal Randomness with Discrete Variables'
                ),
                lp.DimRevealListFrame(
                    [
                        'The steps in the preceeding slides need to be carried out manually in Excel',
                        ['In Python, there is a built-in function which is doing all of this in the background,',
                         random_choices_mono],
                        ['Simply do', random_choices_example, 'to yield the exact same result for the prior example']
                    ],
                    title='Random Discrete Variables in Python'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through generating random discrete variables '
                        'in both Excel and Python',
                        'We will be continuing with the same Excel workbook and Jupyter notebook from before, '
                        '"Generating Random Numbers.xlsx" and "Generating Random Numbers.ipynb"',
                        'We will focus only on the "Discrete" sheet/section now',
                    ],
                    title='Example for Discrete Random Variables in Excel and Python',
                    block_title='Generating Random Numbers from Discrete Distributions in Excel and Python'
                ),
                random_stock_lab.presentation_frames(),
                InClassExampleFrame(
                    [
                        'I will now add internal randomness with discrete variables to '
                        'both the Excel and Python Dynamic Salary Retirement models to simulate economic conditions '
                        'changing year by year',
                        'The completed models on the course site are called '
                        '"Dynamic Salary Retirement Model Internal Randomness.xlsx" and '
                        '"Dynamic Salary Retirement Model Internal Randomness.ipynb"',
                    ],
                    title='Adding Internal Randomness to Excel and Python Models',
                    block_title='Extending the Dynamic Salary Retirement Model with Internal Randomness'
                ),
                full_model_internal_randomness_lab.presentation_frames(),
            ],
            title='Internal Randomness'
        ),
        pl.PresentationAppendix(appendix_frames),
    ]
예제 #8
0
def get_content():
    lecture = get_advanced_modeling_lecture()

    pd_mono = pl.Monospace('pandas')
    selenium_mono = pl.Monospace('selenium')
    requests_mono = pl.Monospace('requests')
    matplotlib_mono = pl.Monospace('matplotlib')
    holoviews_mono = pl.Monospace('holoviews')
    open_mono = pl.Monospace('open')
    os_mono = pl.Monospace('os')
    shutil_mono = pl.Monospace('shutil')
    pathlib_mono = pl.Monospace('pathlib')
    git_mono = pl.Monospace('git')
    sqlalchemy_mono = pl.Monospace('SQLAlchemy')
    fin_model_types = {
        'Portfolio valuation and optimization': 'Find the returns, value, and risk of a portfolio and select the best '
                                                'asset allocation for the portfolio',
        'Additional Funds Needed (AFN)': 'Budgeting model which uses forecasted financial statements to determine '
                                         'how much capital should be raised',
        'Lease or Own': 'Guides the decision of whether to rent or buy an asset',
        'Event Studies': 'Tries to determine the impact of an event',
        'Merger and Aquisition (M&A)': 'A DCF valuation of a target company with operations being combined '
                                       'with the parent at a merger date. Detetermines M&A price.',
        'Leveraged Buyout (LBO)': 'A specialized M&A model used for when large amounts of debt are being used '
                                  'to purchase the target firm.',
        'Derivatives Valuation': 'Value options, swaps, forwards, etc.',
        'Debt models': 'Immunization models are about having the right amount of cash in the future, term structure '
                       'models estimate the rates for different maturity bonds, and default-adjusted return models '
                       'factor in default in determining debt returns',
        'Value at Risk (VaR)': 'Determine how much an investment might lose with a certain probability in a certain '
                               'time span',
    }
    fin_model_items = [f'{pl.Underline(key)}: {value}' for key, value in fin_model_types.items()]

    return [
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        'Throughout the Financial Modeling with Python and Excel course, '
                        'we have covered Python and Excel basics',
                        'We have also covered financial modeling specifics, such as how to structure a '
                        'financial model in both Python and Excel, cash flow and probability modeling, '
                        'sensitivity analysis, scenario analysis, and Monte Carlo simulations',
                        'As far as types of financial models, we covered a retirement model, a '
                        'capital budgeting model, a lender profitability model, and the discounted '
                        'cash flow (DCF) valuation of a stock',
                        "There is a lot I didn't cover in that course. Let's "
                        "do a quick overview of it today.",
                        "This will serve as a roadmap to learn additional topics after completing the "
                        "first course. This is also the beginning of a new "
                        "Advanced Financial Modeling with Python course"
                    ],
                    title='What we Covered and What is Left'
                )
            ],
            title='Introduction'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    fin_model_items[:5],
                    title='Financial Models 1'
                ),
                lp.DimRevealListFrame(
                    fin_model_items[5:9],
                    title='Financial Models 2'
                ),
                lp.DimRevealListFrame(
                    [
                        'Here are some links with free resources to learn more about types of models',
                        'The following examples will be using Excel only',
                        Hyperlink('http://macabacus.com/learn', 'Macabacus'),
                        Hyperlink(
                            'https://corporatefinanceinstitute.com/resources/knowledge/modeling/',
                            'Corporate Finance Institute'
                        ),
                        Hyperlink('https://fminstitute.com/learning/', 'Financial Modeling Institute'),
                        'And I found a couple Python resources, though the coding standards are not great:',
                        Hyperlink('http://www.financeandpython.com/Finance.html', 'Finance and Python'),
                        Hyperlink(
                            'https://www.datacamp.com/community/tutorials/finance-python-trading',
                            'Build a Trading Algorithm in Python',
                        )
                    ],
                    title='Financial Model Resources'
                )
            ],
            title='Types of Financial Models'
        ),
        pl.Section(
            [
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        'Data pipelines are about getting the input data into your model in a '
                        'standardized way',
                        'Within data pipelines, there are two main steps: data collection and '
                        'data wrangling (cleaning + reformatting).',
                        'Either step can be automated, ideally both would be, but it always comes down '
                        'to a tradeoff with modeler time'
                    ],
                    graphics=[
                        images_path('data-pipeline.png')
                    ]
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        'Data can come from many sources, but most typically it originates from the Internet',
                        'If you can manually download the data, you can automate it via web scraping',
                        'You can also extract different types of data with web scraping, even if it is not structured '
                        'as a dataset',
                        'Some data comes from an API, where you need to send requests to download the data',
                        [selenium_mono, 'uses Python code to drive a browser such as Chrome while', requests_mono,
                         'is a more lightweight, text based way to make web requests (good for APIs).'],
                        ['Another common source of data is SQL databases. While you will still need to have a basic '
                         'understanding of SQL to query them, Python can help with', sqlalchemy_mono, 'which allows',
                         'you to write the queries in Python.', pd_mono, 'also has functionality to work with SQL']
                    ],
                    title='Data Collection'
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        ['We have already covered the best general-purpose tool for wrangling data:', pd_mono],
                        'We did not cover it in enough detail to cover all data cleaning cases',
                        'The main material left is selecting, merging, grouping, and reshaping',
                        'Even if your data is without issues, you may still need to change the format, combine '
                        'sources, aggregate, etc.',
                        'Regular expressions are a way of matching any possible string and extracting parts of '
                        'it, which useful for messy data',
                    ],
                    title='Data Wrangling'
                ),
                lp.DimRevealListFrame(
                    [
                        Hyperlink(
                            'https://stackabuse.com/getting-started-with-selenium-and-python/',
                            'Get Started Browser-Based Web Scraping with Selenium'
                        ),
                        Hyperlink(
                            'https://github.com/mherrmann/selenium-python-helium',
                            'Helium - A Higher-Level API to Selenium'
                        ),
                        Hyperlink(
                            'https://realpython.com/python-requests/',
                            'Get Started Text-Based Web Scraping with Requests'
                        ),
                        Hyperlink(
                            'https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html',
                            'Pandas Intro, Covers Basics of Needed Topics'
                        ),
                        Hyperlink(
                            'https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html#cookbook',
                            'Advanced Pandas'
                        ),
                        Hyperlink(
                            'https://scotch.io/tutorials/an-introduction-to-regex-in-python',
                            'Intro to Regular Expressions'
                        ),
                        Hyperlink(
                            'https://docs.python.org/3/library/re.html',
                            'Regular Expression Reference'
                        ),
                        Hyperlink(
                            'https://auth0.com/blog/sqlalchemy-orm-tutorial-for-python-developers/',
                            'SQLAlchemy Overview Tutorial'
                        ),
                        Hyperlink(
                            'https://towardsdatascience.com/sqlalchemy-python-tutorial-79a577141a91',
                            'SQLAlchemy Simple Tutorial and Examples'
                        ),
                        Hyperlink(
                            'https://docs.sqlalchemy.org/en/13/intro.html',
                            'SQLAlchemy Documentation'
                        ),
                    ],
                    title='Data Pipelines Resources',

                )
            ],
            title='Data Pipelines'
        ),
        pl.Section(
            [
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        'We covered basic mathematical tools including basic probability theory, '
                        'algebra, variance/standard deviation, averages, and basic regressions',
                        'Most financial modeling does not take very advanced math, with the exception '
                        'of some derivatives models',
                        "But there are a few more useful tools we didn't have time to cover"
                    ],
                    graphics=[
                        images_path('equations-chalkboard-2.jpg')
                    ],
                    title='What Math we Covered'
                ),
                lp.DimRevealListFrame(
                    [
                        ['Often we want to maximize or minimize something, e.g. maximize returns or NPV, '
                         'minimize risk. We can do this in general with a mathematical technique called',
                         pl.Underline('optimization')],
                        ['If you have a complicated custom model, such that the algebra is getting too difficult '
                        'to do by hand, you can use a', pl.Underline('computer algebra system')],
                        [pl.Underline('Levenshtein (edit) distance'), 'can be used to say how similar two strings are,',
                         'which is useful for data cleaning and more.']
                    ],
                    title='General Math Tools'
                ),
                lp.DimRevealListFrame(
                    [
                        'We covered Ordinary Least Squares (OLS) regressions, which is what anyone means if they '
                        'just say regression',
                        ["There are many more kinds of regressions, we can't even mention them all here. "
                         "But most likely to be useful include", pl.Underline('logistic regression'), 'for when',
                         'probabilities are dependent variables and', pl.Underline('panel regression + fixed effects'),
                         'for when you are dealing',
                         'with multiple instruments over time'],
                        'There are many time-series models to cover, as was mentioned in the forecasting lecture',
                        [pl.Underline('Machine learning/AI'), 'can be used to make classifications or predictions']
                    ],
                    title='Statistics Tools'
                ),
                lp.DimRevealListFrame(
                    [
                        Hyperlink(
                            'https://towardsdatascience.com/optimization-with-scipy-and-application-ideas-to-machine-learning-81d39c7938b8',
                            'Optimization with SciPy'
                        ),
                        Hyperlink(
                            'https://docs.sympy.org/latest/tutorial/preliminaries.html',
                            'Computer Algebra with SymPy'
                        ),
                        Hyperlink(
                            'https://www.geeksforgeeks.org/fuzzywuzzy-python-library/',
                            'Levenshtein Distance using fuzzywuzzy'
                        ),
                        Hyperlink(
                            'https://towardsdatascience.com/logistic-regression-python-7c451928efee',
                            'Intro to Logistic Regression in Python'
                        ),
                        Hyperlink(
                            'https://medium.com/pew-research-center-decoded/using-fixed-and-random-effects-models-for-panel-data-in-python-a795865736ab',
                            'Intro to Panel Regression in Python'
                        ),
                        Hyperlink(
                            'https://www.statsmodels.org/stable/py-modindex.html',
                            'Statistical Models in statsmodels'
                        ),
                        Hyperlink(
                            'https://bashtage.github.io/linearmodels/doc/index.html',
                            'More Statistical Models in linearmodels'
                        ),
                        Hyperlink(
                            'https://docs.scipy.org/doc/scipy/reference/stats.html',
                            'More Statistical Tools in Scipy'
                        ),
                        Hyperlink(
                            'https://machinelearningmastery.com/machine-learning-in-python-step-by-step/',
                            'Getting Started with Machine Learning in Python'
                        ),
                        Hyperlink(
                            'https://machinelearningmastery.com/start-here/',
                            'General Introduction to Machine Learning'
                        ),
                        Hyperlink(
                            'https://datacamp.com/community/tutorials/deep-learning-python',
                            'Deep Learning (AI) in Python using Keras'
                        ),
                        Hyperlink(
                            'https://github.com/automl/auto-sklearn/',
                            'Automated Machine Learning with auto-sklearn'
                        ),
                    ],
                    title='Mathematical Tools Resources'
                )
            ],
            title='Mathematical Tools'
        ),
        pl.Section(
            [
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        "We didn't have very much time to cover presenting the model in Python, other than formatting text",
                        "For Excel, the model is already presented so just structure the workbook well",
                        "A well structured Jupyter notebook or Python script is important for another modeler to "
                        "pick up where you left off. But it still is not an ideal format for non-technical consumers "
                        "of your model"
                    ],
                    graphics=[
                        images_path('example-report.png')
                    ],
                    title='Presenting Model Results'
                ),
                lp.DimRevealListFrame(
                    [
                        "One easy way that doesn't require anything we haven't learned is separating model logic "
                        "and presentation",
                        "Develop as you will while building the model. But when it's time to share the results, "
                        "separate the main model logic (classes and functions) into separate Python file(s)",
                        "Then the Jupyter notebook will just show high-level calls to your model and the results"
                    ],
                    title='Present with Basic Jupyter'
                ),
                lp.DimRevealListFrame(
                    [
                        'Less technical consumers of the model may just want the model conclusions and not '
                        'to actually use the model themselves',
                        'In this case, it is useful to generate reports containing the model results',
                        'There are three major ways to make reports: HTML, LaTeX, and direct PDF solutions',
                        'If all you need is PDF output, a direct solution is fine, but HTML and LaTeX (which '
                        'can be converted to HTML) can both be converted to PDF and have the added advantage of '
                        'being viewable as a web page',
                        'Templating is also easier with HTML and LaTeX'
                    ],
                    title='Create Reports'
                ),
                lp.DimRevealListFrame(
                    [
                        'There is another way for non-technical consumers of your model to interact with it: '
                        'via an app',
                        'It is possible to build apps right in a Jupyter notebook using widgets',
                        'A web app could also be created with a web framework',
                        'With an app, a non-technical consumer of your model can adjust the inputs and see '
                        'the outputs, without having any knowledge of Python or instructions'
                    ],
                    title='Publish Reports and Models'
                ),
                lp.DimRevealListFrame(
                    [
                        ['We covered very basic plots using', pd_mono],
                        [matplotlib_mono, 'provides all the plotting functionality to', pd_mono, 'and any', pd_mono,
                         'plots can be adjusted using', matplotlib_mono, 'options'],
                        'Several libraries exist for interactive plots, which allow for dropdowns, sliders, '
                        'selecting points, zooming, tooltips, and more',
                        ['Other plotting styles are becoming more popular, such as', holoviews_mono + ',', 'in which',
                         'you just describe the data and it can generate interactive plots for you']
                    ],
                    title='Advanced Plotting'
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        Hyperlink(
                            'https://dev.to/goyder/automatic-reporting-in-python---part-1-from-planning-to-hello-world-32n1',
                            'Intro to Creating HTML Reports in Python',
                        ),
                        Hyperlink(
                            'https://towardsdatascience.com/creating-pdf-reports-with-python-pdfkit-and-jinja2-templates-64a89158fa2d',
                            'Create HTML Reports and Output to PDF',
                        ),
                        Hyperlink(
                            'https://realpython.com/primer-on-jinja-templating/',
                            'Templating HTML and LaTeX Using Jinja'
                        ),
                        Hyperlink(
                            'https://www.reportlab.com/docs/reportlab-userguide.pdf',
                            'Direct PDF Output with Reportlab',
                        ),
                        Hyperlink(
                            'https://nickderobertis.github.io/py-ex-latex/',
                            'Direct to LaTeX Using pyexlatex'
                        ),
                        Hyperlink(
                            'https://www.datacamp.com/community/tutorials/matplotlib-tutorial-python',
                            'Matplotlib Plotting Tutorial'
                        ),
                        Hyperlink(
                            'https://holoviews.org/getting_started/index.html',
                            'Get started with Holoviews'
                        ),
                        Hyperlink(
                            'https://www.youtube.com/watch?v=L91rd1D6XTA&ab_channel=Enthought',
                            'Introduction to Panel for Building Apps in Jupyter'
                        ),
                        Hyperlink(
                            'https://panel.holoviz.org/getting_started/index.html',
                            'Tutorial for Panel'
                        ),
                        Hyperlink(
                            'https://voila.readthedocs.io/en/stable/',
                            'Convert a Jupyter Notebook to a Web App using Voila'
                        ),
                        Hyperlink(
                            'https://voila-gallery.org/services/gallery/',
                            'Examples of Voila Web Apps'
                        ),
                        Hyperlink(
                            'https://scotch.io/tutorials/getting-started-with-flask-a-python-microframework',
                            'Get Started Building Web Apps with Flask'
                        ),
                        Hyperlink(
                            'https://anvil.works/learn/tutorials/jupyter-notebook-to-web-app',
                            'Convert a Jupyter Notebook to a Web App using Anvil'
                        ),
                        Hyperlink(
                            'https://www.fullstackpython.com/',
                            'Full Tutorials for Python Web Development'
                        ),
                    ],
                    title='Presentation Resources'
                )
            ],
            title='Present Results'
        ),
        pl.Section(
            [
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        'We were only able to cover basic Python, and certainly not a lot of practices that would '
                        'typically be used in programming',
                        'Python is very flexible in that you can be productive in it with very little understanding '
                        'or mastery of it',
                        'But if you do gain a greater knowledge of it, it can unlock new potential.',
                        'Further, there is more we can learn from the software engineering world in how to improve '
                        'our programming'
                    ],
                    graphics=[
                        images_path('python-code-blur.jpeg')
                    ],
                    title='Your Programming Future'
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        'We can learn a few things from the software engineers that are programming all day every day',
                        [pl.Underline('Version control'), 'or', pl.Underline('source control'), 'is a way of tracking',
                         'changes to code over time. You can get the full history of the project, and multiple',
                         'people can work on the same project at the same time and later merge the changes together.',
                         git_mono, 'is the gold standard for this.'],
                        ['Share', git_mono, 'repositories with the world via Github (or others), allowing anyone to '
                         'use and improve your project'],
                        "Automated testing of your code allows you to make changes ensuring it won't break anything",
                        "Input validation is useful for when others interact with your model directly",
                        'Use an integrated development environment (IDE) such as PyCharm or VS Code for better code '
                        'completion and linking',
                        'Automatically run tests, style your code, deploy Python packages or web apps, and more with '
                        'continuous integration/continuous deployment (CI/CD)'
                    ],
                    title='General Programming'
                ),
                lp.DimRevealListFrame(
                    [
                        ['Python can work with the files on your computer through the', open_mono, 'command and the',
                         os_mono, pathlib_mono,
                         'and', shutil_mono, 'modules'],
                        'Get unique items, unions, intersections using sets',
                        'Override how objects work by overriding double-underscore (dunder) class methods',
                        'Modify existing functions using decorators',
                        'Store Python objects by pickling them',
                        'Check yourself, make your code easier to understand, and power up your IDE using type annotations',
                        'Structure projects using packages and modules',
                        'Isolate project requirements using virtual environments',
                    ],
                    title='Python Programming'
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        Hyperlink(
                            'https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners',
                            'Get Started with Git and Github'
                        ),
                        Hyperlink(
                            'https://desktop.github.com/',
                            'Github Desktop is an App that Makes Git Easy'
                        ),
                        Hyperlink(
                            'https://realpython.com/python-testing/',
                            'Get Started with Automated Testing in Python'
                        ),
                        Hyperlink(
                            'https://www.youtube.com/watch?v=IL3eZYiV70g',
                            'Intro to Input Validation in Python'
                        ),
                        Hyperlink(
                            'https://www.jetbrains.com/pycharm/',
                            'PyCharm IDE'
                        ),
                        Hyperlink(
                            'https://code.visualstudio.com/',
                            'Visual Studio Code (VS Code) IDE'
                        ),
                        Hyperlink(
                            'https://semaphoreci.com/blog/cicd-pipeline',
                            'Intro to CI/CD'
                        ),
                        Hyperlink(
                            'https://github.com/features/actions',
                            'CI/CD Using Github Actions'
                        ),
                        Hyperlink(
                            'https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python',
                            'Intro to Reading and Writing Files with Python'
                        ),
                        Hyperlink(
                            'https://realpython.com/python-pathlib/',
                            'Beyond the Basics of Working with Files in Python'
                        ),
                    ],
                    title='Programming Resources 1'
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        Hyperlink(
                            'https://realpython.com/python-sets/',
                            'Work with Sets in Python'
                        ),
                        Hyperlink(
                            'https://dbader.org/blog/python-dunder-methods',
                            'Get Started with Dunder Methods in Python'
                        ),
                        Hyperlink(
                            'https://realpython.com/python-data-classes/',
                            'Advanced Dataclasses'
                        ),
                        Hyperlink(
                            'https://ipython.readthedocs.io/en/stable/config/integrating.html',
                            'Rich Object Display in Jupyter'
                        ),
                        Hyperlink(
                            'https://realpython.com/primer-on-python-decorators/',
                            'Get Started with Python Decorators'
                        ),
                        Hyperlink(
                            'https://www.datacamp.com/community/tutorials/pickle-python-tutorial',
                            'Store Python Objects with pickle and dill'
                        ),
                        Hyperlink(
                            'https://dev.to/dstarner/using-pythons-type-annotations-4cfe',
                            'Intro to Type Annotations in Python'
                        ),
                        Hyperlink(
                            'https://docs.python-guide.org/writing/structure/',
                            'Intro to Project Structure in Python'
                        ),
                        Hyperlink(
                            'https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/',
                            'Introduction to Virtual Environments in Python'
                        ),
                        Hyperlink(
                            'https://realpython.com/pipenv-guide/',
                            'Virtual Environments Made Easy with pipenv'
                        )
                    ],
                    title='Programming Resources 2'
                )

            ],
            title='Programming'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        Hyperlink(
                            'https://docs.python-guide.org/',
                            "The Hitchhiker's Guide to Python"
                        ),
                        Hyperlink(
                            'https://realpython.com/',
                            'Real Python'
                        ),
                        Hyperlink(
                            'https://automatetheboringstuff.com/',
                            'Automate the Boring Stuff with Python'
                        ),
                        Hyperlink(
                            'https://pbpython.com/',
                            'Practical Business Python'
                        )

                    ],
                    title='General Resources'
                )
            ],
            title='Extras'
        ),
        pl.PresentationAppendix([lecture.pyexlatex_resources_frame]),
    ]
예제 #9
0
def get_series_example_frames():
    pd_mono = pl.Monospace('pandas')
    series_mono = pl.Monospace('Series')
    df_mono = pl.Monospace('DataFrame')

    series_ex_1 = pl.Python("""
>>> df = pd.DataFrame()
>>> df['Numbers'] = [1, 2, 3]
>>> df['Categories'] = ['apple', 'apple', 'orange']
>>> df
""")
    series_ex_2 = pl.Python("""
>>> df['Categories']
0     apple
1     apple
2    orange
Name: Categories, dtype: object
>>> type(df['Categories'])
pandas.core.series.Series
""")
    series_ex_3 = pl.Python("""
>>> cats = df['Categories']
>>> cats
0     apple
1     apple
2    orange
Name: Categories, dtype: object
>>> cats[2]
'orange'
>>> cats.index = ['a', 'b', 'c']
>>> cats
a     apple
b     apple
c    orange
Name: Categories, dtype: object
>>> cats['b']
'apple'
""")
    return [
        lp.DimRevealListFrame([[
            'A', series_mono, 'is the other main', pd_mono,
            'data type besides a', df_mono
        ], [
            'A single column or row of a', df_mono, 'is a', series_mono
        ], ['A', series_mono, 'has a name, an index, and one value per index']
                               ],
                              title=f'What is a {series_mono}?'),
        lp.Frame([
            lp.Block([
                series_ex_1,
                pl.Graphic(images_path('series-example-df.png'), width=0.2),
                series_ex_2
            ],
                     title=f'{series_mono} Example')
        ],
                 title=f"{df_mono}s are Made up of {series_mono}'"),
        lp.Frame(
            [
                lp.Block([
                    pl.TextSize(-1),
                    series_ex_3,
                ],
                         title=f'Working with {series_mono} Example')
            ],
            title=f"Access {series_mono} Values Just Like {df_mono} Columns")
    ]
예제 #10
0
from lectures.lab_exercises.notes import get_read_write_excel_pandas_lab_lecture, get_read_write_xlwings_lab_lecture
from plbuild.paths import images_path
from pltemplates.exercises.xlwings import get_xlwings_exercise
from pltemplates.frames.in_class_example import InClassExampleFrame
from pltemplates.frames.model_flowchart import (ModelFlowchartFrame,
                                                real_world_style, model_style,
                                                in_out_style)
from pltemplates.blocks import LabBlock, InClassExampleBlock
from pltemplates.eq_with_variable_defs import EquationWithVariableDefinitions
from pltemplates.hyperlink import Hyperlink
from pltemplates.exercises.read_write_excel_pandas import (get_lab_exercise)
from pltemplates.exercises.intro_udf import get_lab_exercise as get_intro_udf_exercise
from pltemplates.exercises.advanced_udf import get_lab_exercise as get_advanced_udf_exercise
from schedule.main import LECTURE_9_NAME

xlwings_mono = pl.Monospace('xlwings')

AUTHORS = ['Nick DeRobertis']
SHORT_TITLE = 'Combining Tools'
SUBTITLE = f'Using {xlwings_mono} to Run Excel from Python'

SHORT_AUTHOR = 'DeRobertis'
INSTITUTIONS = [
    [
        'University of Florida',
        'Department of Finance, Insurance, and Real Estate'
    ],
]
SHORT_INSTITUTION = 'UF'

DOCUMENT_CLASS = lp.Presentation
예제 #11
0
def get_content():
    pd_mono = pl.Monospace('pandas')
    dfs_mono = pl.Monospace('DataFrames')
    df_mono = pl.Monospace('DataFrame')
    next_slide = lp.Overlay([lp.UntilEnd(lp.NextWithIncrement())])
    df_to_excel_example = pl.Python(
        "df.to_excel('data.xlsx', sheet_name='My Data', index=False)")
    df_from_excel_example = pl.Python(
        "df = pd.read_excel('data.xlsx', sheet_name='My Data')")
    index_false_mono = pl.Monospace('index=False')
    addin_install_mono = pl.Monospace('xlwings addin install')
    addin_install_success = pl.Monospace(
        'Successfuly installed the xlwings add-in! Please restart Excel.')
    random_seed_py = pl.Monospace('random_seed.py')
    random_seed_excel = pl.Monospace('random_seed.xlsm')
    quickstart_mono = pl.Monospace('xlwings quickstart')
    quickstart_project_mono = pl.Monospace(
        'xlwings quickstart my_project_name')
    cd_mono = pl.Monospace('cd')
    xw_func_decorator = pl.Monospace('@xw.func')
    xw_arg_decorator = pl.Monospace('@xw.arg')
    xw_ret_decorator = pl.Monospace('@xw.ret')
    x_mono = pl.Monospace('x')
    expand_table_mono = pl.Monospace("expand='table'")
    random_choice_mono = pl.Monospace('random_choice')
    random_choice_py = pl.Monospace('random.choices')

    lecture = get_combining_excel_python_lecture()
    pd_read_write_exercise = get_read_write_excel_pandas_lab_lecture(
    ).to_pyexlatex()
    xlwings_exercise = get_read_write_xlwings_lab_lecture().to_pyexlatex()

    read_from_excel_example = pl.Python("""
my_value = sht.range("G11").value  # single value
# all values in cell range
my_value = sht.range("G11:F13").value  
# expands cell range down and right getting all values
my_values = sht.range("G11").expand().value  
""")

    write_to_excel_example = pl.Python("""
sht.range("G11").value = 10
sht.range("G11").value = [10, 11]  # horizontal
sht.range("G11:G12").value = [10, 11]  # vertical
# table, DataFrame from elsewhere
sht.range("G11").value = df  
""")

    return [
        pl.Section([
            lp.DimRevealListFrame([
                "We have learned how to use both Excel and Python to solve problems. Throughout this process, there "
                "were advantages and disadvantages of each tool for each problem.",
                "I wanted you to know both tools so you could pick whichever is best to tackle your problem",
                "For larger problems, you'll likely find some parts are better with Excel and some with Python",
                "After this lecture, you won't need to choose one anymore, you can use both at once."
            ],
                                  title='Leveraging the Power of Both Tools'),
        ],
                   title='Introduction'),
        pl.Section([
            lp.DimRevealListFrame([
                [pd_mono, 'has built-in tools for working with Excel'],
                [
                    pd_mono, 'can read Excel workbooks into', dfs_mono,
                    'and it can write', dfs_mono, 'back to Excel workbooks'
                ],
                'For simple uses, this may be enough. If you just need to get data from somewhere once and put it in your '
                'workbook, or you have your data in Excel and want to analyze it in Python, this is sufficient',
                [
                    "If you want to manipulate your workbook from Python, or you want to run Python code from your "
                    "workbook, look to", xlwings_mono
                ]
            ],
                                  title=f'How Far does {pd_mono} Get Us?'),
            lp.Frame([
                lp.Block([
                    df_from_excel_example,
                    pl.VSpace(-0.3),
                    pl.UnorderedList([
                        "If you don't pass a sheet name, it will take the first sheet."
                    ])
                ],
                         title='Reading Excel Files',
                         overlay=next_slide),
                pl.VFill(),
                lp.Block(
                    [
                        df_to_excel_example,
                        pl.VSpace(-0.3),
                        pl.UnorderedList(
                            [[
                                'We are passing', index_false_mono,
                                'because usually the 0, 1, 2 ... index is not useful'
                            ],
                             [
                                 "If you had set your index to something useful, then don't include",
                                 index_false_mono
                             ]])
                    ],
                    title='Writing to Excel Files',
                    overlay=next_slide),
                pl.VFill(),
                lp.AlertBlock([[
                    'When', pd_mono,
                    'writes to a workbook, it replaces the file. Do not write over an existing '
                    'workbook that you want to keep!'
                ]],
                              title='Careful When Writing!',
                              overlay=next_slide),
            ],
                     title=f'Reading and Writing to Excel Files with {pd_mono}'
                     ),
            lp.Frame([
                InClassExampleBlock([
                    pl.UnorderedList([
                        'Download the contents of the "Read Write Excel Pandas" folder in Examples',
                        'Ensure that you put the Excel file and notebook in the same folder for it to work',
                        'Follow along with the notebook'
                    ])
                ],
                                    title=
                                    f'Read and Write to Excel using {pd_mono}')
            ],
                     title='Showcasing Reading and Writing to Excel Files'),
            pd_read_write_exercise.presentation_frames(),
        ],
                   title=f'To and From Excel with {pd_mono}',
                   short_title=pd_mono),
        pl.Section([
            lp.TwoColumnGraphicDimRevealFrame([[
                'The easiest way to use Python from in Excel, or Excel from in Python, is',
                xlwings_mono
            ], "In Windows, it's based off the Microsoft COM API, which is some common tools they give for creating "
                                               "plugins.",
                                               "It's still in active development, but overall it works pretty well and is far beyond where we were "
                                               "a few years ago"],
                                              graphics=[
                                                  images_path(
                                                      'xlwings-logo.png')
                                              ],
                                              title=
                                              f'Introducing {xlwings_mono}'),
            lp.DimRevealListFrame(
                [['There are two main ways to use', xlwings_mono],
                 [
                     'You can',
                     pl.
                     Bold('manipulate Excel from Python,'),
                     'which gives you the full power of Excel from',
                     "within Python. In this class we'll focus on reading and writing values, but you can do anything",
                     "that you would normally be able to in Excel, but by executing Python code."
                 ],
                 [
                     'Or you can',
                     pl.Bold('run Python from Excel'),
                     'using one of two approaches:',
                     pl.Underline('Python as a VBA replacement'), 'and',
                     pl.Underline('user-defined functions (UDFs)')
                 ],
                 'We will focus on manipulating Excel from Python in this class. I encourage you to explore '
                 'the other two approaches on your own.'],
                title=f'What are the Main Ways to use {xlwings_mono}?'),
            lp.TwoColumnGraphicDimRevealFrame([
                pl.TextSize(-1),
                [
                    xlwings_mono,
                    'allows us to write Python values into Excel and fetch Excel values into Python'
                ],
                'There is also a complete VBA API, meaning you can do everything that you could do with VBA '
                'from within Python, which means you have the full capabilities of Excel within Python',
                'There are also convenient features to work with entire tables at once rather than '
                'a single value'
            ],
                                              graphics=[
                                                  images_path(
                                                      'python-excel-logo.png')
                                              ],
                                              title=
                                              'Using Python to Drive Excel Models'
                                              ),
            lp.Frame([
                lp.Block([read_from_excel_example],
                         title='Read Values from Excel'),
                lp.Block([write_to_excel_example],
                         title='Write Values to Excel')
            ],
                     title='Write and Read Values to and from Excel'),
            InClassExampleFrame([
                'Download the contents of the "xlwings" folder in Examples',
                'Ensure that you put the Excel file and notebook in the same folder for it to work',
                'Follow along with the notebook'
            ],
                                title=f'How to Use {xlwings_mono}',
                                block_title=f'Trying out {xlwings_mono}'),
            xlwings_exercise.presentation_frames(),
        ],
                   title=
                   f'Introducing Full Python-Excel Connection with {xlwings_mono}',
                   short_title=f'{xlwings_mono}'),
        pl.PresentationAppendix([
            lecture.pyexlatex_resources_frame,
            pd_read_write_exercise.appendix_frames(),
            xlwings_exercise.appendix_frames(),
        ])
    ]
예제 #12
0
def get_lab_exercise() -> LabExercise:
    pd_mono = pl.Monospace('pandas')
    dfs_mono = pl.Monospace('DataFrames')
    next_slide = lp.Overlay([lp.UntilEnd(lp.NextWithIncrement())])
    df_to_excel_example = pl.Python(
        "df.to_excel('data.xlsx', sheet_name='My Data', index=False)")
    df_from_excel_example = pl.Python(
        "df = pd.read_excel('data.xlsx', sheet_name='My Data')")
    index_false_mono = pl.Monospace('index=False')
    addin_install_mono = pl.Monospace('xlwings addin install')
    addin_install_success = pl.Monospace(
        'Successfuly installed the xlwings add-in! Please restart Excel.')
    random_seed_py = pl.Monospace('random_seed.py')
    random_seed_excel = pl.Monospace('random_seed.xlsm')
    quickstart_mono = pl.Monospace('xlwings quickstart')
    quickstart_project_mono = pl.Monospace(
        'xlwings quickstart my_project_name')
    cd_mono = pl.Monospace('cd')
    xw_func_decorator = pl.Monospace('@xw.func')
    random_choice_mono = pl.Monospace('random_choice')
    random_choices_mono = pl.Monospace('random_choices')
    random_choice_py = pl.Monospace('random.choices')
    xlwings_mono = pl.Monospace('xlwings')

    bullet_contents = [
        [[
            'If you have not already created your', xlwings_mono,
            'project, go back two slides', 'and follow those steps.'
        ],
         [
             'Edit the .py file to add a function', random_choice_mono,
             'which will call', random_choice_py
         ],
         'The function should accept the items to choose from, and the probabilities.',
         'Write out a few possible items to choose from in your workbook. Put probabilities next '
         'to them.',
         [
             'Call your', random_choice_mono,
             'function on these inputs, and see it pick a random item'
         ]],
        [[
            'Work off the existing', xlwings_mono,
            'project from the Level 1 exercise'
        ],
         [
             'Now add an additional function', random_choices_mono,
             'which will accept the items to',
             'choose from, the probabilities, and the number of random choices to generate.'
         ],
         [
             'The function should return multiple random choices. In Excel, you should see multiple cells of output,'
             'with each cell containing a random choice.'
         ],
         [
             'If you return a list from the Python function, it should create this behavior.'
         ]]
    ]

    return LabExercise(bullet_contents,
                       'Write a Simple UDF',
                       f"Getting your Feet Wet with {xlwings_mono}",
                       label='lab:intro-udf')
예제 #13
0
def get_content():
    random.seed(1000)

    lecture = get_visualization_lecture()
    intro_pandas_lab = get_intro_to_pandas_lab_lecture().to_pyexlatex()
    styling_pandas_lab = get_pandas_styling_lab_lecture().to_pyexlatex()
    graphing_lab = get_intro_python_visualization_lab_lecture().to_pyexlatex()
    appendix_frames = [
        lecture.pyexlatex_resources_frame,
        intro_pandas_lab.appendix_frames(),
        styling_pandas_lab.appendix_frames(),
        graphing_lab.appendix_frames()
    ]

    ret_model = RetirementModel()
    ret_df = ret_model.get_formatted_df(num_years=12)
    ret_table = lt.Tabular.from_df(ret_df, extra_header=pl.Bold('Retirement Info'))
    plt_mono = pl.Monospace('matplotlib')
    df_mono = pl.Monospace('DataFrame')
    df_basic_example = pl.Python(
"""
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df['Sales'] = [1052, 212, 346]
>>> df['Category'] = ['Aprons', 'Apples', 'Bowties']
df
"""
    )
    plot_example_code = pl.Python(
"""
>>> %matplotlib inline
>>> ret_df.plot.line(x='Time', y='Salaries')
"""
    )

    return [
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        "So far we've had one main output from our model, number of years",
                        "Salaries and wealth over time have also been outputs, but we haven't had a good way of understanding "
                        "that output. It's a bunch of numbers.",
                        "This is where visualization comes in. We have some complex result, and want to make it easily "
                        "interpretable."
                    ],
                    title='Why Visualize?'
                ),
                lp.Frame(
                    [
                        pl.Center(ret_table)
                    ],
                    title='What we Have so Far'
                ),
                lp.GraphicFrame(
                    images_path('excel-insert-chart.png'),
                    title='Visualization in Excel'
                ),
                lp.GraphicFrame(
                    lg.ModifiedPicture(
                        images_path('python-visualization-landscape.jpg'),
                        [
                            lg.Path('draw', [(0.52, 0.52), (0.85, 0.67)], options=['red'], draw_type='rectangle',
                                    overlay=lp.Overlay([2]))
                        ]
                    ),
                    title='An Overwhelming Number of Options in Python'
                ),
                lp.DimRevealListFrame(
                    [
                        ["Ultimately, we will be creating graphs using", plt_mono, "but we won't use it directly."],
                        ["Instead, we will use", pd_mono],
                        [pd_mono, "is actually creating its graphs using", plt_mono,
                         "for us, but it is simpler to use."]
                    ],
                    title='Explaining Python Visualization in This Class'
                ),
                InClassExampleFrame(
                    [
                        'I will now go back to the "Dynamic Salary Retirement Model.xlsx" Excel model to '
                        'add visualization',
                        'I have also uploaded the completed workbook from this exercise '
                        'as "Dynamic Salary Retirement Model Visualized.xlsx"',
                        'Follow along as I go through the example.',
                    ],
                    title='Visualization in Excel',
                    block_title='Adding Graphs to the Dynamic Salary Retirement Excel Model'
                ),

            ],
            title='Visualization Introduction',
            short_title='Intro'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        [pd_mono, "does", pl.Bold('a lot'), 'more than just graphing. We will use it throughout the '
                                                            'rest of the class.'],
                        "Previously we've worked with lists, numbers, strings, and even our custom types (our model dataclasses)",
                        [pd_mono, "provides the", df_mono, "as a new type that we can use."],
                        f'Before we can get to graphing, we must learn how to use the {df_mono}.'

                    ],
                    title='Some Setup Before we can Visualize in Python'
                ),
                lp.Frame(
                    [
                        ['A', df_mono, 'is essentially a table. It has rows and columns, just like in Excel.'],
                        pl.VFill(),
                        lp.Block(
                            [
                                pl.UnorderedList([
                                    'Add or remove columns or rows',
                                    'Group by and aggregate',
                                    'Load in and output data from/to Excel and many other formats',
                                    'Merge and join data sets',
                                    'Reshape and pivot data',
                                    'Time-series functionality',
                                    'Slice and query your data',
                                    'Handle duplicates and missing data'
                                ])
                            ],
                            title=f'Some Features of the {df_mono}'
                        )

                    ],
                    title=f'What is a {df_mono}?'
                ),
                lp.Frame(
                    [
                        df_basic_example,
                        pl.Graphic(images_path('df-basic-example.png'), width=0.3)

                    ],
                    title=f'A Basic {df_mono} Example'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through the notebook in '
                        '"Intro to Pandas and Table Visualization.ipynb"',
                        'Follow along as I go through the example.',
                        'We will complete everything up until DataFrame Styling'
                    ],
                    title='Introduction to Pandas',
                    block_title='Creating and Using Pandas DataFrames'
                ),
                intro_pandas_lab.presentation_frames(),
                lp.DimRevealListFrame(
                    [
                        ['It is possible to add styling to our displayed tabular data by styling the', df_mono],
                        'The styling is very flexible and essentially allows you to do anything',
                        'Out of the box, it is easy to change colors, size, and positioning of text, add a caption, do '
                        'conditional formatting, and draw a bar graph over the cells.'
                    ],
                    title='Styling Pandas DataFrames'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through the next section in '
                        '"Intro to Pandas and Table Visualization.ipynb"',
                        'Follow along as I go through the example.',
                        'This time we are covering the remainder of the notebook starting from "DataFrame Styling"'
                    ],
                    title='Introduction to Pandas',
                    block_title='Creating and Using Pandas DataFrames'
                ),
                styling_pandas_lab.presentation_frames(),
            ],
            title='Tables with Pandas DataFrames',
            short_title='Pandas'
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        lp.Block(
                            [
                                plot_example_code,
                                pl.Graphic(images_path('python-salaries-line-graph.pdf'), width=0.5)
                            ],
                            title=f'Line Graphs using {pd_mono}'
                        )
                    ],
                    title='A Minimal Plotting Example'
                ),
                lp.MultiGraphicFrame(
                    [
                        images_path('excel-salaries-line-graph.png'),
                        images_path('python-salaries-line-graph.pdf'),
                    ],
                    vertical=False,
                    title='Basic Graph Types: Line Graphs'
                ),
                lp.MultiGraphicFrame(
                    [
                        images_path('excel-salaries-bar-graph.png'),
                        images_path('python-salaries-bar-graph.pdf'),
                    ],
                    vertical=False,
                    title='Basic Graph Types: Bar Graphs'
                ),
                lp.MultiGraphicFrame(
                    [
                        images_path('excel-salaries-box-whisker-plot.png'),
                        images_path('python-salaries-box-graph.pdf'),
                    ],
                    vertical=False,
                    title='Basic Graph Types: Box and Whisker Plots'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through '
                        '"Intro to Graphics.ipynb"',
                        'Follow along as I go through the entire example notebook.',
                    ],
                    title='Introduction to Graphing',
                    block_title='Graphing Using Pandas'
                ),
                graphing_lab.presentation_frames(),
            ],
            title='Graphing using Pandas',
            short_title='Graphs'
        ),
        pl.PresentationAppendix(appendix_frames)
    ]
예제 #14
0
from lectures.visualization.main import get_visualization_lecture
from plbuild.paths import images_path
from pltemplates.exercises.lab_exercise import LabExercise
from pltemplates.frames.in_class_example import InClassExampleFrame
from pltemplates.frames.model_flowchart import (
    ModelFlowchartFrame,
    real_world_style,
    model_style,
    in_out_style
)
from pltemplates.blocks import LabBlock
from pltemplates.frames.tvm.project_1_lab import get_project_1_lab_frame
from models.retirement import RetirementModel
from schedule.main import LECTURE_6_NAME

pd_mono = pl.Monospace('pandas')

AUTHORS = ['Nick DeRobertis']
SHORT_TITLE = 'Visualization'
SUBTITLE = f'An Introduction to Visualization and {pd_mono}'

SHORT_AUTHOR = 'DeRobertis'
INSTITUTIONS = [
    ['University of Florida', 'Department of Finance, Insurance, and Real Estate'],
]
SHORT_INSTITUTION = 'UF'

DOCUMENT_CLASS = lp.Presentation
OUTPUT_LOCATION = plbuild.paths.SLIDES_BUILD_PATH
HANDOUTS_OUTPUT_LOCATION = plbuild.paths.HANDOUTS_BUILD_PATH
TITLE = LECTURE_6_NAME
예제 #15
0
def get_content():
    random.seed(1000)
    next_until_end_ov = lp.Overlay([lp.UntilEnd(lp.NextWithIncrement())])
    next_slide = lp.Overlay([lp.NextWithIncrement()])
    numpy_mono = pl.Monospace('numpy')
    lecture = get_dynamic_salary_python_lecture()


    return [
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        'We have seen how structure and organization can help the readability and maintainability of '
                        'an Excel model. The same concept exists for our Python models.',
                        ['We already learned that we should use functions to organize logic and a',
                         pl.Monospace('dataclass'),
                         'for the model inputs'],
                        "Typically you'll have functions for each step, those may be wrapped up into other functions which "
                        "perform larger steps, and ultimately you'll have one function which does everything by calling the"
                        "other functions.",
                        "Those are good ideas with any Python model, but working in Jupyter allows us some additional "
                        "organization and presentation of the model"
                    ],
                    title='How to Structure a Python Financial Model'
                ),
                lp.DimRevealListFrame(
                    [
                        'In Jupyter, we can have code, nicely formatted text, equations, sections, hyperlinks, '
                        'and graphics, all in one document',
                        ['For all you can do with these nicely formatted "Markdown" cells, see',
                         Hyperlink('https://www.markdownguide.org/basic-syntax/', 'here'), 'and',
                         Hyperlink('https://www.markdownguide.org/extended-syntax/', 'here.')],
                        'We can think of sections in Jupyter as analagous to Excel sheets/tabs. One section for each '
                        'logical part of your model. Then you can have smaller headings for subsections of the model.',
                        'Break your code up into small sections dealing with each step, with nicely formatted text '
                        'explaining it. Add comments where anything is unclear in the code.',
                    ],
                    title='Using Jupyter for Structure of a Model'
                ),
                lp.GraphicFrame(
                    [
                        get_model_structure_graphic()
                    ],
                    title='Structuring a Python Model'
                ),
                lp.DimRevealListFrame(
                    [
                        'When I develop in Jupyter, I have lots of cells going everywhere testing things out',
                        ['When I finish a project in Jupyter, I remove these testing cells and make sure it runs and '
                         'logically flows from end to end', pl.Monospace('(restart kernel and run all cells)')],
                        "Run your model with different inputs, and make sure the outputs change in the expected fashion. This "
                        "is a good way to check your work.",
                        'There may be outputs in each section, but the final output should be at the end of the notebook',
                    ],
                    title='Workflow and Final Output'
                ),
            ],
            title='Structuring a Model in Python and Jupyter',
            short_title='Model Structure'
        ),
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        'The first project is aimed at approaching a new time value of money and cash flow model',
                        'It covers the same concepts as the retirement model, but in a capital budgeting setting',
                        'We need to introduce some economic equations to handle this model. You should have covered these in microeconomics.'
                    ],
                    title='Introducing Project 1'
                ),
                lp.GraphicFrame(
                    images_path('supply-demand-graph.png'),
                    title='A Quick Review of Supply and Demand'
                ),
                lp.Frame(
                    [
                        lp.Block(
                            [
                                "There are a couple of basic economic equations we haven't talked about that "
                                "we'll need for this:",
                                pl.Equation(str_eq='R = PQ', inline=False),
                                pl.Equation(str_eq='Q = min(D, S)', inline=False),
                                pl.UnorderedList([
                                    f'{pl.Equation(str_eq="R")}: Revenue',
                                    f'{pl.Equation(str_eq="Q")}: Quantity Purchased',
                                    f'{pl.Equation(str_eq="D")}: Quantity Demanded',
                                    f'{pl.Equation(str_eq="S")}: Quantity Supplied',
                                ])
                            ],
                            title='New Required Equations'
                        )
                    ],
                    title='Equations for Project 1'
                ),
                lp.Frame(
                    [
                        pl.UnorderedList([
                            lp.DimAndRevealListItems([
                                'We need to cover one more Python concept and one gotcha before you can complete the first project.',
                                "On the next slide I'll introduce error handling, and show an example of how it's useful"
                            ],
                                vertical_fill=True)
                        ]),
                        lp.AlertBlock(
                            [
                                pl.UnorderedList([
                                    f'The NPV function in {numpy_mono} works slightly differently than the NPV function in Excel.',
                                    f'Excel treats the first cash flow as period 1, while {numpy_mono} treats the first cash flow as period 0.',
                                    'If taking NPV where the first cash flow is period 1, pass directly to Excel, and for Python, pass 0 as the first cash flow, then the rest.',
                                    'If taking NPV where the first cash flow is period 0, pass from period 1 to end to Excel and add period 0 separately, pass directly to Python.'
                                ])
                            ],
                            title='NPV Gotcha',
                            overlay=next_slide
                        )
                    ],
                    title='A Couple More Things on the Python Side'
                ),
            ],
            title='Project 1 Additional Material',
            short_title='Project 1'
        ),
        pl.Section(
            [
                get_retirement_model_overview_frame(),
                lp.Frame(
                    [
                        lp.Block(
                            salary_block_content,
                            title='Salary with Promotions and Cost of Living Raises'
                        )
                    ],
                    title='Revisiting the Model Salary Equation'
                ),
                lp.Frame(
                    [
                        pl.UnorderedList([
                            'For wealths, we need to add the investment return and then the savings in each year',
                        ], overlay=next_until_end_ov),
                        pl.VFill(),
                        lp.Block(
                            [
                                lp.adjust_to_full_size_and_center(
                                    pl.Equation(str_eq=r'W_t = W_{t-1}  (1 + r_i) + S_t  v')),
                                pl.UnorderedList([
                                    f'{pl.Equation(str_eq="S_t")}:  Salary at year {pl.Equation(str_eq="t")}',
                                    f'{pl.Equation(str_eq="W_t")}:  Wealth at year {pl.Equation(str_eq="t")}',
                                    f'{pl.Equation(str_eq="r_i")}:  Investment return',
                                    f'{pl.Equation(str_eq="t")}:  Number of years',
                                    f'{pl.Equation(str_eq="v")}:  Savings rate',
                                ])
                            ],
                            title='Calculating Wealth',
                            overlay=next_until_end_ov,
                        )
                    ],
                    title='Building the Wealth Model'
                ),
                InClassExampleFrame(
                    [
                        'I will now show the process I use to create a full model.',
                        'I will be recreating the model "Dynamic Salary Retirement Model.ipynb"',
                        'Go ahead and download that to follow along as you will also extend it in a lab exercise',
                    ],
                    title='Creating a Full Model in Python',
                    block_title='Dynamic Salary Retirement Model in Python',
                ),
                get_extend_dynamic_retirement_python_lab_lecture().to_pyexlatex().presentation_frames(),
                LabExercise(
                    [
                        [
                            "Usually I would try to have smaller labs but it didn't fit the format of this lecture. "
                            "Most will not be able to complete this during class.",
                            "For this lab, attempt the practice problem "
                            '"P1 Python Retirement Savings Rate Problem.pdf"',
                            'This is similar to how the projects will be assigned, so it is good preparation',
                            "I would encourage you to try it from scratch. If you are totally stuck, try working off "
                            "of the retirement model I completed today to have a lot of the structure already. If you "
                            "still are having trouble with that, check the solution and see me in office hours.",
                            'Note: this is not an official lab exercise you need to submit, it is practice only, but '
                            'I would highly encourage you to complete it.'
                        ]
                    ],
                    block_title='Practice Building A Model',
                    frame_title='Extending the Simple Retirement Model in a Different Way',
                    label='lab:retire-model'
                ),
            ],
            title='Building the Dynamic Salary Retirement Model',
            short_title='Build the Model'
        ),
        pl.PresentationAppendix(
            [
                lecture.pyexlatex_resources_frame,
                get_extend_dynamic_retirement_python_lab_lecture().to_pyexlatex().appendix_frames(),
            ]
        )
    ]
예제 #16
0
def get_content():
    align_c = lt.ColumnAlignment('c')
    align_l = lt.ColumnAlignment('l')
    align = lt.ColumnsAlignment([align_l, align_c])

    n_phones = 100000
    price_scrap = 50000
    price_phone = 500
    cogs_phone = 250
    price_machine_adv = 1000000
    n_life = 10
    n_machines = 5
    d_1 = 100000
    g_d = 0.2
    max_year = 20
    interest = 0.05

    pmm = PhoneManufacturingModel(n_phones, price_scrap, price_phone, n_life,
                                  n_machines, d_1, g_d, max_year, interest)

    scipy_mono = pl.Monospace('scipy')
    scipy_minimize_link = 'https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html#' \
                          'scipy-optimize-minimize-scalar'
    scipy_minimize_mono = pl.Monospace('scipy.optimize.minimize_scalar')

    possible_elasicity_str = ', '.join([
        f'($E = {ec[0]}$, $d_c = {ec[1]}$)' for ec in ELASTICITY_CONSTANT_CASES
    ])
    possible_elasicity_str = ', '.join([
        f'($E = {ec[0]}$, $d_c = {ec[1]}$)' for ec in ELASTICITY_CONSTANT_CASES
    ])
    possible_elasicity_str = '[' + possible_elasicity_str + ']'

    return [
        pl.Section([
            pl.SubSection([
                'You work for a new startup that is trying to manufacture phones. You are tasked with building '
                'a model which will help determine how many machines to invest in and how much to spend on '
                'marketing. Each machine produces $n_{output}$ phones per year. Each phone sells '
                r'for \$$p_{phone}$ and costs \$$c_{phone}$ in variable costs to produce. '
                'After $n_{life}$ years, the machine can no longer produce output, but may be scrapped for '
                r'\$$p_{scrap}$. The machine will not be replaced, so you may end up with zero total output '
                r'before your model time period ends. '
                'Equity investment is limited, so in each year you can spend $c_{machine}$ to either buy a machine or buy '
                'advertisements. In the first year you must buy a machine. Any other machine purchases must '
                'be made one after another (advertising can only begin after machine buying is done). '
                'Demand for your phones starts at '
                '$d_1$. Each time you advertise, demand increases by $g_d$%. The prevailing market interest '
                'rate is $r$.'
            ],
                          title='The Problem'),
            pl.SubSection([
                pl.UnorderedList([
                    'You may limit your model to 20 years and a maximum of 5 machines if it is helpful.',
                    'For simplicity, assume that $c_{machine}$ is paid in every year, '
                    'even after all machines have shut down.',
                    'Ensure that you can change the inputs and the outputs change as expected.',
                    'For simplicity, assume that fractional phones can be sold, you do not '
                    'need to round the quantity transacted.'
                ])
            ],
                          title='Notes'),
            pl.SubSection([
                pl.SubSubSection([
                    pl.UnorderedList([
                        '$n_{output}$: Number of phones per machine per year',
                        '$n_{machines}$: Number of machines purchased',
                        '$n_{life}$: Number of years for which the machine produces phones',
                        '$p_{phone}$: Price per phone',
                        '$p_{scrap}$: Scrap value of machine',
                        '$c_{machine}$: Price per machine or advertising year',
                        '$c_{phone}$: Variable cost per phone',
                        '$d_1$: Quantity of phones demanded in the first year',
                        '$g_d$: Percentage growth in demand for each advertisement',
                        '$r$: Interest rate earned on investments'
                    ])
                ],
                                 title='Inputs'),
                pl.SubSubSection([
                    pl.UnorderedList([
                        'Cash flows in each year, up to 20 years',
                        'PV of cash flows, years 1 - 20',
                    ])
                ],
                                 title='Outputs')
            ],
                          title='The Model'),
            pl.SubSection([
                "It is unrealistic to assume that price and demand are unrelated. To extend the model, "
                "we can introduce a relationship between price and demand, given by the following equation: ",
                pl.Equation(str_eq=r'd_1 = d_c - Ep_{phone}', inline=False),
                pl.UnorderedList([
                    f'{pl.Equation(str_eq="E")}: Price elasticity of demand',
                    f'{pl.Equation(str_eq="d_c")}: Demand constant'
                ]),
                [
                    f"For elasticities and constants {possible_elasicity_str} "
                    f"({len(ELASTICITY_CONSTANT_CASES)} total cases), and taking the other "
                    "model inputs in the ",
                    pl.NameRef('check-work'),
                    ' section, determine the optimal price for each '
                    'elasticity, that is the price which maximizes the NPV.'
                ],
                pl.SubSubSection([
                    pl.UnorderedList([
                        '$d_1$ is no longer an input, but an output.',
                        'This bonus requires optimization, which we have not yet covered in class.',
                        'In Excel, you can use Solver.',
                        [
                            f'In Python, the {scipy_mono} package provides optimization tools. You will '
                            f'probably want to use:',
                        ],
                        pl.UnorderedList([
                            Hyperlink(scipy_minimize_link,
                                      scipy_minimize_mono),
                            "You will need to write a function which accepts price and returns NPV, "
                            "with other model inputs fixed.",
                            pl.UnorderedList([[
                                'Depending on how you set this up,',
                                Hyperlink(
                                    'https://www.learnpython.org/en/Partial_functions',
                                    'functools.partial'),
                                'may be helpful for this.'
                            ]]),
                            "It will actually need to return negative NPV, as the optimizer only minimizes, "
                            "but we want maximum NPV.",
                            [
                                'No answers to check your work are given for this bonus. The',
                                pl.NameRef('check-work'),
                                'section only applies to without the bonus.'
                            ]
                        ])
                    ])
                ],
                                 title='Notes')
            ],
                          title='Bonus Problem'),
        ],
                   title='Overview'),
        pl.Section([
            'You must start from "Project 1 Template.xlsx". Ensure that you reference all inputs from '
            'the Inputs/Outputs tab. Also ensure that all outputs are referenced back to the Inputs/Outputs tab. '
            'Do not change any locations of the inputs or outputs. '
            'The final submission is your Excel workbook.'
        ],
                   title='Excel Exercise'),
        pl.Section([[
            'You must start from "Project 1 Template.ipynb". '
            'I should be able to run all the '
            'cells and get the output of your model at the bottom. ',
            [
                'You should not change the name of the',
                pl.Monospace('ModelInputs'), 'class or the',
                pl.Monospace('model_data'), 'variable.'
            ], 'You need to define',
            pl.Monospace('cash_flows'), 'as your output cash flows (numbers, '
            'not formatted), and ',
            pl.Monospace('npv'),
            'as your NPV (number, not formatted). When you '
            'show your final outputs in the notebook, then they should be formatted.'
        ]],
                   title='Python Exercise'),
        pl.Section([
            Center(
                lt.Tabular([
                    lt.MultiColumnLabel('Grading Breakdown', span=2),
                    lt.TopRule(),
                    lt.ValuesTable.from_list_of_lists(
                        [['Category', 'Percentage']]),
                    lt.TableLineSegment(0, 1),
                    lt.ValuesTable.from_list_of_lists([[
                        'Model Accuracy', '60%'
                    ], ['Model Readability', '20%'], [
                        'Model Formatting', '10%'
                    ], ['Following the Template', '10%'], ['Bonus', '5%']]),
                    lt.MidRule(),
                    lt.ValuesTable.from_list_of_lists(
                        [['Total Possible', '105%']]),
                    lt.BottomRule()
                ],
                           align=align))
        ],
                   title='Grading'),
        pl.Section(
            [
                'If you pass the following inputs (to the basic model, not bonus model): ',
                pl.UnorderedList([
                    f'{pl.Equation(str_eq="n_{output}")}: {n_phones:,.0f}',
                    f'{pl.Equation(str_eq="p_{scrap}")}: \${price_scrap:,.0f}',
                    f'{pl.Equation(str_eq="p_{phone}")}: \${price_phone:,.0f}',
                    f'{pl.Equation(str_eq="c_{machine}")}: \${price_machine_adv:,.0f}',
                    f'{pl.Equation(str_eq="c_{phone}")}: \${cogs_phone:,.0f}',
                    f'{pl.Equation(str_eq="n_{life}")}: {n_life}',
                    f'{pl.Equation(str_eq="n_{machines}")}: {n_machines}',
                    f'{pl.Equation(str_eq="d_1")}: {d_1:,.0f}',
                    f'{pl.Equation(str_eq="g_d")}: {g_d:.0%}',
                    f'{pl.Equation(str_eq="r")}: {interest:.0%}',
                ]),
                'You should get the following result:',
                # TODO [#10]: replace project 1 result using notebook executor
                """
                Cash Flows:

Year 1: \$24,000,000

Year 2: \$24,000,000

Year 3: \$24,000,000

Year 4: \$24,000,000

Year 5: \$24,000,000

Year 6: \$29,000,000

Year 7: \$35,000,000

Year 8: \$42,200,000

Year 9: \$50,840,000

Year 10: \$61,208,000

Year 11: \$73,699,600

Year 12: \$74,050,000

Year 13: \$49,050,000

Year 14: \$24,050,000

Year 15: \$-950,000

Year 16: \$-1,000,000

Year 17: \$-1,000,000

Year 18: \$-1,000,000

Year 19: \$-1,000,000

Year 20: \$-1,000,000



NPV: \$369,276,542
                """
            ],
            title='Check your Work',
            label='check-work')
    ]
def get_content():
    random.seed(1000)

    lecture = get_sensitivity_analysis_lecture()
    sensitivity_excel_lab = get_sensitivity_analysis_excel_lab_lecture().to_pyexlatex()
    dictionaries_lab = get_dictionaries_lab_lecture().to_pyexlatex()
    list_comp_lab = get_list_comprehensions_lab_lecture().to_pyexlatex()
    sensitivity_python_lab = get_sensitivity_analysis_python_lab_lecture().to_pyexlatex()
    appendix_frames = [
        lecture.pyexlatex_resources_frame,
        sensitivity_excel_lab.appendix_frames(),
        dictionaries_lab.appendix_frames(),
        list_comp_lab.appendix_frames(),
        sensitivity_python_lab.appendix_frames(),
    ]

    pd_mono = pl.Monospace('pandas')
    series_mono = pl.Monospace('Series')
    df_mono = pl.Monospace('DataFrame')
    apply_mono = pl.Monospace('apply')
    import_mono = pl.Monospace('import')
    for_mono = pl.Monospace('for')
    dict_mono = pl.Monospace('dict')
    np_mono = pl.Monospace('numpy')
    pd_mono = pl.Monospace('pandas')
    import_something_mono = pl.Monospace('import something')
    something_file = pl.Monospace('something.py')
    numpy_file = pl.Monospace('numpy.py')
    def_mono = pl.Monospace('def')
    class_mono = pl.Monospace('class')
    sensitivity_file_mono = pl.Monospace('sensitivity.py')
    sensitivity_import = pl.Monospace('import sensitivity')
    sensitivity_func_mono = pl.Monospace('sensitivity.sensitivity_hex_plots?')
    pip_install_mypackage = pl.Monospace('pip install mypackage')
    jupyter_install_mypackage = pl.Monospace('!pip install mypackage')
    itertools_product = pl.Monospace('itertools.product')
    sensitivity = pl.Monospace('sensitivity')

    series_ex_1 = pl.Python(
"""
>>> df = pd.DataFrame()
>>> df['Numbers'] = [1, 2, 3]
>>> df['Categories'] = ['apple', 'apple', 'orange']
>>> df
"""
    )
    series_ex_2 = pl.Python(
"""
>>> df['Categories']
0     apple
1     apple
2    orange
Name: Categories, dtype: object
>>> type(df['Categories'])
pandas.core.series.Series
"""
    )
    series_ex_3 = pl.Python(
"""
>>> cats = df['Categories']
>>> cats
0     apple
1     apple
2    orange
Name: Categories, dtype: object
>>> cats[2]
'orange'
>>> cats.index = ['a', 'b', 'c']
>>> cats
a     apple
b     apple
c    orange
Name: Categories, dtype: object
>>> cats['b']
'apple'
"""
    )

    list_comprehension_ex_1 = pl.Python(
"""
>>> out_values = []
>>> for i in range(5):
>>>     out_values.append(i + 10)
>>> out_values
[10, 11, 12, 13, 14]
"""
    )

    list_comprehension_ex_2 = pl.Python(
"""
>>> out_values = [i + 10 for i in range(5)]
>>> out_values
[10, 11, 12, 13, 14]
"""
    )
    dict_example = pl.Python(
"""
>>> coffee_levels_emotions = {
>>>     'high': 'happy',
>>>     'pretty high': 'happy',
>>>     'medium': 'neutral',
>>>     'low': 'sad',
>>>     'empty': 'desparate'
>>> }
>>> coffee_levels_emotions['pretty high']
'happy'
>>> for coffee_level, emotion in coffee_levels_emotions.items():
>>>     print(f"I'm {emotion} when my coffee is {coffee_level}")
"""
    )

    dict_printout = """
I'm happy when my coffee is high
I'm happy when my coffee is pretty high
I'm neutral when my coffee is medium
I'm sad when my coffee is low
I'm desparate when my coffee is empty
    """

    dict_printouts_mono = [pl.Monospace(item) for item in dict_printout.split('\n') if item]
    dict_printout_output = []
    for po in dict_printouts_mono:
        dict_printout_output.append(po)
        dict_printout_output.append('')

    dict_example_2 = pl.Python(
"""
>>> coffee_levels_emotions.update({'overflowing': 'burned'})
>>> coffee_levels_emotions['negative'] = 'confused'
>>> high_value = coffee_levels_emotions.pop('high')
>>> coffee_levels_emotions
{'pretty high': 'happy',
 'medium': 'neutral',
 'low': 'sad',
 'empty': 'desparate',
 'overflowing': 'burned',
 'negative': 'confused'}
>>> high_value
'happy'
"""
    )

    plain_sensitivity_example = pl.Python(
"""
inp1_values = [1, 2]
inp2_values = [4, 5]
results = []
for inp1 in inp1_values:
    for inp2 in inp2_values:
        result = model(inp1, inp2,)
        results.append(
            (inp1, inp2, result)
        )
pd.DataFrame(results, columns=['inp1', 'inp2',  'Result'])
"""
    )

    easy_sensitivity_example = pl.Python(
"""
from sensitivity import SensitivityAnalyzer

sensitivity_values = {
    'inp1': [1, 2],
    'inp2': [4, 5],
}
sa = SensitivityAnalyzer(sensitivity_values, model)
sa.df
"""
    )

    return [
        pl.Section(
            [
                lp.DimRevealListFrame(
                    [
                        'So far, I have given you some inputs to use and you have been getting one or more outputs from '
                        'those inputs',
                        'We have not considered how those inputs may change, and how that affects the outputs',
                        'This is where building a model vs. doing a calculation really starts to pay off'
                    ],
                    title='Moving from a Static Model'
                ),
                lp.GraphicFrame(
                    explore_parameters_graphic(),
                    title='How to Explore Inputs and their Affect on Outputs'
                ),
                lp.DimRevealListFrame(
                    [
                        pl.TextSize(-1),
                        'In this lecture, we will be discussing sensitivity analysis as an approach to exploring the '
                        'parameter space.',
                        'After we cover probabilistic modeling, we will revisit exploring the parameter space with other '
                        'methods: scenario analysis and Monte Carlo Simulation.',
                        'In sensitivity analysis, a fixed set of values for the parameters are chosen, while in Monte Carlo '
                        'Simulation, each parameter is assigned a distribution.',
                        'In scenario analysis, several realistic cases of the inputs are chosen which represent '
                        'possible real-world situations',
                        'All three methods may be used together to fully understand a model.'
                    ],
                    title='Methods of Parameter Exploration'
                ),
            ],
            title='Introduction to Parameter Exploration',
            short_title='Explore Parameters'
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        'For the model given by:',
                        pl.Equation(str_eq='y = f(X)', inline=False),
                        pl.Equation(str_eq='X = [x_1, x_2, ..., x_n]', inline=False),
                        pl.UnorderedList([
                            [pl.Equation(str_eq='y:'), 'Model output'],
                            [pl.Equation(str_eq='X:'), 'Model input matrix'],
                            [pl.Equation(str_eq='x_i:'), 'Value of $i$th $x$ variable']

                        ]),
                        'Follow the following steps:',
                        pl.OrderedList([
                            ['Choose a set of values for each', pl.Equation(str_eq='x_i')],
                            ['Take the cartesian product of these values as',
                             pl.Equation(str_eq='[X_1, X_2, ..., X_m]')],
                            ['For each', pl.Equation(str_eq='X_i'), 'calculate', pl.Equation(str_eq='y_i = f(X_i)')],
                            ['Store the values of', pl.Equation(str_eq='X_i'), 'mapped to', pl.Equation(str_eq='y_i')],
                            ['Visualize', pl.Equation(str_eq='y_i'), 'versus', pl.Equation(str_eq='X_i')]
                        ])
                    ],
                    title='Sensitivity Analysis, Formally'
                ),
                get_sensitivity_analysis_example_frames(),
            ],
            title='Sensitivity Analysis Theory',
            short_title='SA Theory'
        ),
        pl.Section(
            [
                lp.GraphicFrame(
                    images_path('excel-data-table.png'),
                    title='Sensitivity Analysis in Excel'
                ),
                lp.DimRevealListFrame(
                    [
                        'There are two main ways to visualize sensitivity analysis results in Excel: graphing and '
                        'conditional formatting.',
                        'Graphing is usually appropriate for one-way data tables',
                        'Conditional formatting is usually appropriate for two-way data tables'
                    ],
                    title='Visualizing Sensitivity Analysis in Excel'
                ),
                lp.GraphicFrame(
                    images_path('excel-conditional-formatting.png'),
                    title='Conditional Formatting in Excel'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through adding sensitivity analysis to the Dynamic Salary Retirement Model '
                        'in Excel',
                        'The completed exercise on the course site, '
                        '"Dynamic Salary Retirement Model Sensitivity.xlsx"'
                    ],
                    title='Sensitivity Analysis in Excel',
                    block_title='Adding Sensitivity Analysis to the Dynamic Retirement Excel Model'
                ),
                sensitivity_excel_lab.presentation_frames(),
            ],
            title='Sensitivity Analysis in Excel with Data Tables',
            short_title='SA Excel'
        ),
        pl.Section(
            [
                lp.Frame(
                    [
                        "We'll cover a couple more Python patterns and a new data type before jumping into sensitivity analysis",
                        pl.UnorderedList([
                            'Dictionaries',
                            'List comprehensions',
                            ['Python', import_mono, 'system and custom code']
                        ])
                    ],
                    title=f'Going Deeper into Python Code Structure'
                ),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        ['A dictionary, or', dict_mono,
                         'for short, is another basic Python data type like lists, numbers, and strings.'],
                        'Like a list, it is a collection: it holds other objects.',
                        ['Unlike a list, a', dict_mono,
                         'is composed of key-value pairs. It holds relationships between objects.']
                    ],
                    graphics=[
                        lg.ModifiedPicture(
                            images_path('dictionary-book.jpg'),
                            draw_items=[
                                lg.Path('draw', [(0.5, 0.5)], draw_type='circle',
                                        options=['radius=0.4', 'red', 'line width=5mm']),
                                lg.Path('draw', [(0.2, 0.75), (0.8, 0.2)], draw_type='--',
                                        options=['red', 'line width=5mm'])
                            ]
                        )
                    ],
                    title='What is a Dictionary?'
                ),
                lp.Frame(
                    [
                        lp.Block(
                            [
                                pl.TextSize(-2),
                                dict_example,
                                *dict_printout_output
                            ],
                            title='Basic Dictionary Example'
                        )
                    ],
                    title='How to Use Dictionaries'
                ),
                lp.Frame(
                    [
                        lp.Block(
                            [
                                dict_example_2,
                            ],
                            title='Add and Delete Items from Dictionaries'
                        )
                    ],
                    title='How to Modify Dictionaries'
                ),
                InClassExampleFrame(
                    [
                        'I will now start going through the example notebook called '
                        '"Python Dicts, List comprehensions, and Imports.ipynb"',
                        'I will go through the Dictionaries section for now'
                    ],
                    title='More About Dictionaries in Python',
                    block_title='Using Dictionaries'
                ),
                dictionaries_lab.presentation_frames(),
                lp.Frame(
                    [
                        pl.TextSize(-1),
                        lp.Block(
                            [
                                list_comprehension_ex_1
                            ],
                            title=f'The Original Way'
                        ),
                        lp.Block(
                            [
                                list_comprehension_ex_2
                            ],
                            title=f'With List Comprehension'
                        ),
                        lp.Block(
                            ['You', pl.Bold('never'),
                             'need to use list comprehension, it is just for convenience. The original', for_mono,
                             'loop syntax will always work fine.'],
                            title='Notice'
                        )
                    ],
                    title=f"An Easier way to Build Simple Lists from Loops"
                ),
                InClassExampleFrame(
                    [
                        'I will continue going through the example notebook '
                        '"Python Dicts, List comprehensions, and Imports.ipynb"',
                        'I will go through the List Comprehensions section for now'
                    ],
                    title='Easier Loops in Python',
                    block_title='Using List Comprehensions'
                ),
                list_comp_lab.presentation_frames(),
                lp.DimRevealListFrame(
                    [
                        ['In the past we have used', import_mono, 'to load packages such as', np_mono, 'and', pd_mono],
                        ['These packages are just Python files. We can also write our own Python files and',
                         import_mono,
                         'them the same way'],
                        [f'When you {import_something_mono}, Python first searches the current directory for a file',
                         something_file, "and if it doesn't find it, it searches your installed packages"],
                        ['In fact if you added a', numpy_file, 'in the current directory and tried to', import_mono,
                         np_mono,
                         'it would', import_mono, 'the contents of that file rather than the', np_mono, 'package.']
                    ],
                    title=f'Understanding Python {import_mono}s'
                ),
                lp.DimRevealListFrame(
                    [
                        ['You can write your own functions and classes, then put them in a Python file and',
                         import_mono,
                         'them into your notebook.'],
                        ['When you', import_mono,
                         'a file, it executes the contents of that file. So you generally want just '
                         'function and class definitions, and not really anything outside of', def_mono, 'or',
                         class_mono,
                         'statements.'],
                        'Using Python files is a more maintainable structure for building complex models and apps versus '
                        'Jupyter notebooks only.'
                    ],
                    title='Importing Custom Code'
                ),
                lp.DimRevealListFrame(
                    [
                        'Sometimes you will need a package which does not already come installed with Anaconda',
                        ['The general way to do this is with', pip_install_mypackage, 'replacing mypackage with',
                         'the package you want to install'],
                        ['You would run this in Anaconda Prompt, or in Jupyter you can run it but you need to put an '
                        'exclaimation mark before it to say you want to run it in a terminal. So in Jupyter it would be',
                        jupyter_install_mypackage]
                    ],
                    title='Installing Packages'
                ),
                InClassExampleFrame(
                    [
                        'I will continue going through the example notebook '
                        '"Python Dicts, List comprehensions, and Imports.ipynb"',
                        'I will go through the Imports and Installing Packages section for now'
                    ],
                    title='Installing Packages in Python',
                    block_title='How to Install Packages'
                ),

            ],
            title='Python List Comprehensions, Installing Packages, and More on Dictionaries',
            short_title='Extra Python Basics'
        ),
        pl.Section(
            [
                lp.GraphicFrame(
                    images_path('python-sensitivity-hex-bins.pdf'),
                    title='Sensitivity Analysis in Python - Hex-Bin'
                ),
                lp.GraphicFrame(
                    images_path('sensitivity-analysis-styled-df.png'),
                    title='Sensitivity Analysis in Python - Styled DataFrame'
                ),
                lp.DimRevealListFrame(
                    [
                        'Generally, to do sensitivity analysis in Python without any special tools, you would just '
                        'create one nested for loop for each input, and finally within all the loops, run your model '
                        'with the inputs from the loops',
                        'This will work fine, but you will have many nested loops which can become hard to read. Also '
                        'it is a fair bit of setup involved.',
                        ['You can avoid the nested loops with', itertools_product, 'but then this becomes more '
                         'difficult to use and read']

                    ],
                    title='How to Do Sensitivity Analysis in Python (The Hard Way)'
                ),
                lp.Frame(
                    [
                        pl.TextSize(-3),
                        pl.UnorderedList([
                            'Say you have a function which runs your model, called model, which takes inputs of '
                            'inp1 and inp2'
                        ]),
                        lp.Block(
                            [
                                plain_sensitivity_example,
                                pl.Graphic(images_path('plain-sensitivity-result.png'), width=0.2),
                            ],
                            title='Sensitivity Analysis in Python with No Libraries'
                        )
                    ],
                    title='Sensitivity Analysis Example (Hard Way)'
                ),
                lp.DimRevealListFrame(
                    [
                        "When I first created this course, I thought there should be a good sensitivity analysis "
                        "tool in Python and I couldn't find it",
                        "The beauty of Python is if you want a tool that doesn't exist, you can create it, and "
                        "share it with others so that nobody else has to deal with the problem.",
                        ['So I created', sensitivity, 'a package for sensitivity analysis in Python, '
                                                      'which makes it very easy']

                    ],
                    title='How to Do Sensitivity Analysis in Python (The Easy Way)'
                ),
                lp.Frame(
                    [
                        pl.TextSize(-3),
                        pl.UnorderedList([
                            'Say you have a function which runs your model, called model, which takes inputs of '
                            'inp1 and inp2'
                        ]),
                        lp.Block(
                            [
                                easy_sensitivity_example,
                                pl.Graphic(images_path('plain-sensitivity-result.png'), width=0.2),
                            ],
                            title=f'Sensitivity Analysis in Python with {sensitivity}'
                        )
                    ],
                    title='Sensitivity Analysis Example (Easy Way)'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through Sensitivity Analysis example Jupyter notebook',
                        'This notebook shows both the standard approach and using the sensitivity package',
                    ],
                    title='Intro to Sensitivity Analysis in Python',
                    block_title='An Overview of the Manual and Automated Approaches'
                ),
                InClassExampleFrame(
                    [
                        'I will now go through adding sensitivity analysis to the Dynamic Salary Retirement Model '
                        'in Python',
                        'The completed exercise available on the course site is called '
                        '"Dynamic Salary Retirement Model Sensitivity.ipynb"'
                    ],
                    title='Applying Sensitivity Analysis in Python',
                    block_title='Adding Sensitivity Analysis to the Dynamic Retirement Python Model'
                ),
                sensitivity_python_lab.presentation_frames(),
            ],
            title='Sensitivity Analysis in Python',
            short_title='SA Python'
        ),
        pl.PresentationAppendix(appendix_frames),
    ]
예제 #18
0
def get_content():
    random.seed(1000)
    ev_bet = (999999 / 1000000) * 1 + (1 / 1000000) * (-750001)
    xlwings_mono = pl.Monospace('xlwings')
    pd_mono = pl.Monospace('pandas')
    quickstart_mono = pl.Monospace('quickstart')

    read_from_excel_example = pl.Python("""
my_value = xw.Range("G11").value  # single value
# all values in cell range
my_value = xw.Range("G11:F13").value  
# expands cell range down and left getting all values
my_values = xw.Range("G11").expand().value  
    """)

    write_to_excel_example = pl.Python("""
xw.Range("G11").value = 10
xw.Range("G11").value = [10, 11]  # horizontal
xw.Range("G11").value = [[10], [11]]  # vertical
xw.Range("G11").value = [[10, 11], [12, 13]]  # table
    """)

    ball_options = ['fill', 'circle', 'inner sep=8pt']

    blue_ball_options = ball_options + ['blue']

    red_ball_options = ball_options + ['red']

    def rand_pos():
        return random.randrange(-150, 150) / 100

    blue_nodes = [
        lg.Node(None, (rand_pos(), rand_pos()), options=blue_ball_options)
        for _ in range(10)
    ]
    red_nodes = [
        lg.Node(None, (rand_pos(), rand_pos()), options=red_ball_options)
        for _ in range(10)
    ]

    red_blue_ball_graphic = lg.TikZPicture([
        lg.Rectangle(5, 5, shape_options=['blue', 'thick']), *blue_nodes,
        *red_nodes
    ])

    lecture = get_monte_carlo_lecture()
    intro_mc_python_lab = get_intro_monte_carlo_lab_lecture().to_pyexlatex()
    mc_python_lab = get_python_retirement_monte_carlo_lab_lecture(
    ).to_pyexlatex()
    mc_excel_lab = get_excel_retirement_monte_carlo_lab_lecture().to_pyexlatex(
    )

    return [
        pl.Section([
            lp.
            TwoColumnGraphicDimRevealFrame([
                [
                    pl.Bold('Monte Carlo Simulation'),
                    'is a technique which allows understanding the probability '
                    'of acheiving certain outputs from a model.'
                ],
                'This gives the modeler a greater understanding of the likelihood of different outputs, rather '
                'than relying on a single number',
            ],
                                           graphics=[
                                               images_path(
                                                   'random-numbers.jpg')
                                           ],
                                           title=
                                           'What is Monte Carlo Simulation?'),
            lp.DimRevealListFrame([
                r'Imagine you have a one-time opportunity to place a bet for \$1. ',
                r'If you win the bet, you will receive \$2. If you lose the bet, you will lose \$750,000. '
                r'You cannot avoid the payment by declaring bankruptcy.',
                r'The odds of winning the bet are 999,999/1,000,000. In 1/1,000,000 you lose the \$750,000.',
                fr'The expected profit from the bet is \${ev_bet:.2f}. Should you take it? Depends on your '
                fr'risk tolerance.',
                'Therefore not only the expected outcome matters, but also what other outcomes may occur and '
                'their probabilities.'
            ],
                                  title='Why Use Monte Carlo Simulation?'),
            lp.GraphicFrame(explore_parameters_graphic(),
                            title='Monte Carlo Simulation in One Picture'),
            lp.DimRevealListFrame([
                'Monte Carlo simulation is carried out similarly to external scenario analysis.',
                'The main difference is that we manually picked specific cases for the inputs with scenario '
                'analysis.',
                'In Monte Carlo simulation, we assign distributions to the inputs, and input values are drawn '
                'from the distributions for each run of the model',
                'Finally, we can fit a probability distribution to the outputs to be able to talk about the '
                'chance of a certain outcome occurring'
            ],
                                  title=
                                  'Basic Process for Monte Carlo Simulation')
        ],
                   title='Introduction'),
        pl.Section(
            [
                lp.DimRevealListFrame([
                    'Monte Carlo simulation can be applied to any model',
                    'It is generally easier to run them in Python than in Excel.',
                    "With pure Excel, you're either going to VBA or hacking something with data tables",
                    'In Python, just loop for N iterations, each time drawing inputs, running the model, and collecting '
                    'outputs.',
                    [
                        'We will start with a pure Python model, then move to using',
                        xlwings_mono, 'to add Monte Carlo '
                        'simulations to our Excel models.'
                    ],
                ],
                                      title=
                                      'Running Monte Carlo Simulations - Python or Excel?'
                                      ),
                lp.Frame([
                    lp.Block([
                        r'You have \$1,000 now and need to pay \$1,050 in one year. You have available to you '
                        r'two assets: a risk free asset that returns 3%, and a stock that returns 10% with a '
                        r'20% standard deviation. How much should you invest in the two assets to maximize '
                        r'your probability of having at least \$1,050 in one year?'
                    ],
                             title='An Investment Problem'),
                    pl.VFill(),
                    pl.UnorderedList([
                        lp.DimAndRevealListItems([
                            'We must first construct the basic model which gets the portfolio value for given '
                            'returns',
                            'Then draw values of the stock return from a normal distribution, and run the model '
                            'many times and visualize the outputs. ',
                            'Then repeat this process with each weight to determine the best weight.'
                        ])
                    ])
                ],
                         title='An Example Application'),
                InClassExampleFrame([
                    'Go to the course site and download the Jupyter notebook "MC Investment Returns.ipynb" from '
                    'Monte Carlo Examples',
                    'I will go through this example notebook to solve the problem from the prior slide.'
                ],
                                    title='Simluating Portfolio Values',
                                    block_title=
                                    'Example for Simulating Portfolio Values'),
                pl.TextSize(-2),
                intro_mc_python_lab.presentation_frames(),
                pl.TextSize(0),
            ],
            title='Running a First Monte Carlo Simulation',
            short_title='Run MC',
        ),
        pl.Section(
            [
                lp.Frame([
                    pl.TextSize(-2), 'For the model given by:',
                    pl.Equation(str_eq='y = f(X)', inline=False),
                    pl.Equation(str_eq='X = [x_1, x_2, ..., x_n]',
                                inline=False),
                    pl.UnorderedList([[
                        pl.Equation(str_eq='y:'), 'Model output'
                    ], [pl.Equation(str_eq='X:'), 'Model input matrix'],
                                      [
                                          pl.Equation(str_eq='x_i:'),
                                          'Value of $i$th $x$ variable'
                                      ]]),
                    'To run $N$ Monte Carlo simulations, follow the following steps:',
                    pl.OrderedList(
                        [[
                            'Assign a probability distribution for each',
                            pl.Equation(str_eq='x_i')
                        ],
                         [
                             'For each',
                             pl.Equation(str_eq='x_i'),
                             'randomly pick a value from its probability distribution. Store them as',
                             pl.Equation(str_eq='X_j')
                         ],
                         [
                             'Repeat the previous step $N$ times, yielding',
                             pl.Equation(str_eq='[X_1, X_2, ..., X_N]')
                         ],
                         [
                             'For each',
                             pl.Equation(str_eq='X_j'), 'calculate',
                             pl.Equation(str_eq='y_j = f(X_j)')
                         ],
                         [
                             'Store the values of',
                             pl.Equation(str_eq='X_j'), 'mapped to',
                             pl.Equation(str_eq='y_j')
                         ],
                         [
                             'Visualize and analyze',
                             pl.Equation(str_eq='y_j'), 'versus',
                             pl.Equation(str_eq='X_j')
                         ]])
                ],
                         title='Monte Carlo Simulation Process'),
                lp.DimRevealListFrame([
                    'There are a multitude of outputs we can get from a Monte Carlo simulation. We saw a few '
                    'already in the example.',
                    [
                        pl.Bold('Outcome probability distributions'),
                        'are the main output. We saw this with two '
                        'approaches in the example, a',
                        pl.Underline('histogram'), 'and a',
                        pl.Underline('probability table.')
                    ],
                    [
                        'We also examined the',
                        pl.Bold('probability of a certain outcome'),
                        'in whether we reached '
                        'the desired cash.'
                    ],
                    [
                        'The last main output is examining the',
                        pl.Bold('relationship between inputs and outputs.'),
                        'for which common approaches include',
                        pl.Underline('scatter plots'), 'and',
                        pl.Underline('regressions.')
                    ]
                ],
                                      title=
                                      'Outputs from Monte Carlo Simulation'),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        pl.TextSize(-3),
                        'The outcome probability distribution represents the chance of receiving different '
                        'outcomes from your model.',
                        'There are two main ways to visualize a probability distribution: a plot and a table.',
                        [
                            'The plot, usually a',
                            pl.Underline('histogram'), 'or',
                            pl.Underline('KDE'),
                            'gives a high-level overview of the probabilities and can uncover any non-normal '
                            'features of the distribution.'
                        ],
                        [
                            'The probability table represents the chance of receiving the given value or '
                            'lower.'
                        ],
                        'The Value at Risk (VaR) is a common measure calculated in the industry, and it represents '
                        'the probability of losing at least a certain amount. This would be a subset of this analysis '
                        'and so this analysis can be used to calculate VaR',
                    ],
                    graphics=[
                        images_path('outcome-probability-distribution.png'),
                        lt.Tabular([
                            pl.MultiColumnLabel('Probability Table', span=2),
                            lt.TopRule(),
                            lt.ValuesTable.from_list_of_lists(
                                [['Probability', 'Value']]),
                            lt.TableLineSegment(0, 1),
                            lt.ValuesTable.from_list_of_lists(
                                [['25%', '1020'], ['50%', '1039'],
                                 ['75%', '1053']]),
                            lt.BottomRule()
                        ],
                                   align='c|c')
                    ],
                    title='Outcome Probability Distributions',
                    graphics_on_right=False,
                ),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        'Imagine a box which contains red and blue balls. You do not know in advance how many there '
                        'are of each color.',
                        'You want to estimate the probability of getting a blue ball when pulling a ball from the box.',
                        'To evaluate this, you grab a ball, write down its color, and put it back, 1,000 times.',
                        'You pull a blue ball in 350 out of the 1,000 trials. What is the probability of getting blue?'
                    ],
                    graphics=[red_blue_ball_graphic
                              ],
                    title='Probability of a Certain Outcome - A Simple Example'
                ),
                lp.DimRevealListFrame([
                    'We followed the same logic when estimating the probability of receiving our desired cash '
                    'in the investment example.',
                    pl.Equation(
                        str_eq=
                        fr'p = \frac{{{pl.Text("Count of positive outcomes")}}}{{{pl.Text("Count of trials")}}}'
                    ),
                    [
                        'For the balls example, this is simply',
                        pl.Equation(str_eq=r'p = \frac{350}{1000} = 0.35'),
                    ],
                    [
                        'In the investment example, we used', pd_mono,
                        'to check for each trial, whether it was a '
                        'positive outcome (made it a 1) or not (made it a 0). Then the sum is the count of '
                        'positive outcomes and so the mean is the probability.'
                    ],
                ],
                                      title=
                                      'Probability of a Certain Outcome, Formally'
                                      ),
                lp.DimRevealListFrame([
                    'Monte Carlo simulation can also provide a more comprehensive look at the relationship between '
                    'inputs and outputs.',
                    'While sensitivity analysis can be used to estimate the relationship between an input and '
                    'output, it is usually done with other inputs at their base case',
                    'The values of inputs may affect how other inputs affect the output. E.g. for the retirement '
                    'model, an increase in interest rate increases wealth more if the initial salary was higher.',
                    'As all the inputs change each time, you can get a more realistic view of the relationship, e.g. '
                    'some trials with a higher interest rate will have high salary and some will have low salary.'
                ],
                                      title=
                                      'Why Monte Carlo Simulations Help Understand Inputs vs. Outputs'
                                      ),
                lp.TwoColumnGraphicDimRevealFrame(
                    [
                        pl.TextSize(-1),
                        'A scatter plot is a simple way to visualize the relationship between two variables',
                        'If there is a relationship, you will see some defined pattern in the points. This may be '
                        'somewhat of an upward or downward line (linear relationship) or some other shape such '
                        'as a U (non-linear relationship).',
                        'If there is no relationship, then there will just be a random cloud of points (lower '
                        'plot) or a horizontal line.'
                    ],
                    graphics=[
                        images_path('scatter-plot-line.png'),
                        images_path('scatter-plot-no-relationship.png')
                    ],
                    graphics_on_right=False,
                    title=
                    'Visualizing the Relationship between Inputs and Outputs'),
                lp.TwoColumnGraphicDimRevealFrame([
                    pl.TextSize(-2),
                    'The scatter plots help give a broad understanding of the relationship but do not answer the '
                    'question, how much will my output change if my input changes? E.g. if I earn 10,000 more '
                    'for a starting salary, how much sooner can I retire?',
                    'Simply increasing the input in your model and checking the output is not enough, because it '
                    'does not take into account how all the other variables may be changing.',
                    'Multivariate regression is a general tool which is good at answering these kinds of questions, '
                    'while taking into account all the changing inputs.'
                ],
                                                  graphics=[
                                                      images_path(
                                                          'excel-multivariate-reg.png'
                                                      )
                                                  ],
                                                  title=
                                                  'Numerically Analyzing the Relationships'
                                                  ),
                lp.DimRevealListFrame([
                    pl.TextSize(-1),
                    'The coefficient in a multivariate regression represents how much the outcome variable '
                    'changes with a one unit change in the input variable.',
                    'E.g. a coefficient of -.0002 on starting salary in explaining years to retirement would mean '
                    r'that a \$1 increase in starting salary is associated with a decrease in years to retirement by .0002 years, or '
                    r'a \$10,000 increase in starting salary is associated with a decrease in years to retirement by 2 years.',
                    'All interpretations are "all else constant", meaning that it does not consider relationships '
                    'between the inputs. E.g. if starting salary is higher because of a good economy, and interest '
                    'rates are also higher due to the good economy, the starting salary coefficient is not taking '
                    'into account the increase in interest rates.',
                    'Be careful about units. If you use decimals for percentages, you will need to multiply or '
                    'divide by 100 to get the effect in percentages.'
                ],
                                      title='How to use Multivariate Regression'
                                      ),
                InClassExampleFrame(
                    [
                        'I will now go through adding a Monte Carlo simulation to the Dynamic Salary Retirement '
                        'Model in Python',
                        'The completed example is on the course site in '
                        'Monte Carlo Examples',
                    ],
                    title='Adding Monte Carlo Simulation to a Formal Model',
                    block_title='Dynamic Salary Retirement with Monte Carlo'),
                mc_python_lab.presentation_frames(),
            ],
            title='A More Formal Treatment of Monte Carlo Simulation',
            short_title='Formal MC',
        ),
        pl.Section([
            lp.DimRevealListFrame([
                'In pure Excel, it is much more difficult to run a Monte Carlo Simulation',
                'Without going to VBA, typically the only way is to use a data table',
                'A data table can be used in situations where you only want to have one or two inputs '
                'varying at once. Just generate the random inputs and use them as the axes of the data table',
                'If you want to vary more than two inputs, VBA or Python would be required',
                'There are also add-ons that accomplish this but they are usually not free'
            ],
                                  title=
                                  "How is it Different Running MC in Excel?"),
            lp.DimRevealListFrame([
                'The process for Monte Carlo Simulation which works for any number of variables is '
                'very similar to what we were doing in Python.',
                'We are still just changing the inputs, running the model, and storing the outputs from each run',
                [
                    'Using', xlwings_mono,
                    'from Python code we can change and retrieve the values of cells'
                ],
                'This allows us to change inputs, run the model, and store outputs, just as in Python, but running our Excel model.',
                'We can either analyze the outputs in Python or output them back to Excel for analysis'
            ],
                                  title=
                                  'Monte Carlo in Excel with More than Two Variables'
                                  ),
            InClassExampleFrame([
                'Go to the course site and download the "Dynamic Salary Retirement Model.xlsx" and '
                '"Excel Monte Carlo.ipynb" from the Monte Carlo Examples',
                'Open up the Jupyter notebook and follow along with me',
                'The completed Excel model is also there in case you lose track. Visualizations '
                'were added after running the Jupyter notebook on the original Excel model.',
            ],
                                title='Monte Carlo Excel Retirement Model',
                                block_title=
                                f'Using {xlwings_mono} to Run Monte Carlo Simulations'
                                ),
            mc_excel_lab.presentation_frames(),
        ],
                   title='Monte Carlo Simulation in Excel',
                   short_title='Excel MC'),
        pl.PresentationAppendix([
            lecture.pyexlatex_resources_frame,
            intro_mc_python_lab.appendix_frames(),
            mc_python_lab.appendix_frames(),
            mc_excel_lab.appendix_frames(),
        ])
    ]