Big O notation : limited input

Question:

As an exercise, I am trying to set Monte Carlo Simulation on a chosen ticker symbol.

from numpy.random import randint
from datetime import date
from datetime import timedelta
import pandas as pd
import yfinance as yf
from math import log

# ticker symbol
ticker_input = "AAPL"  # change

# start day + endday for Yahoo Finance API, 5 years of data
start_date = date.today()
end_date = start_date - timedelta(days=1826)

# retrieve data from Yahoo Finance
data = yf.download(ticker_input, end_date,start_date)
yf_data = data.reset_index()

# dataframe : define columns
df = pd.DataFrame(columns=['date', "ln_change", 'open_price', 'random_num'])

open_price = []
date_historical = []

for column in yf_data:
    open_price = yf_data["Open"].values
    date_historical = yf_data["Date"].values  

# list order: descending
open_price[:] = open_price[::-1]
date_historical[:] = date_historical[::-1]

# Populate data into dataframe
for i in range(0, len(open_price)-1):
    # date
    day = date_historical[i]
    
    # ln_change
    lnc = log(open_price[i]/open_price[i+1], 2)

    # random number
    rnd = randint(1, 1258)

    # op = (open_price[i]) open price
    df.loc[i] = [day, open_price[i], lnc, rnd]

I was wondering how to calculate Big O if you have e.g. nested loops or exponential complexity but have a limited input like one in my example, maximum input size is 1259 instances of float number. Input size is not going to change.

How do you calculate code complexity in that scenario?

Asked By: Jovica

||

Answers:

It is a matter of points of view. Both ways of seeing it are technically correct. The question is: What information do you wish to convey to the reader?

Consider the following code:

quadraticAlgorithm(n) {
    for (i <- 1...n)
        for (j <- 1...n)
            doSomethingConstant();
}
quadraticAlgorithm(1000);

The function is clearly O(n2). And yet the program will always run in the same, constant time, because it just contains one function call with n=1000. It is still perfectly valid to refer to the function as O(n2). And we can refer to the program as O(1).

But sometimes the boundaries are not that clear. Then it is up to you to choose if you wish to see it as an algorithm with a time complexity as some function of n, or as a piece of constant code that runs in O(1). The importance is to make it clear to the reader how you define things.

Answered By: Berthur
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.