#Inconsistent behaviour of a formula

1 messages · Page 1 of 1 (latest)

soft lagoon
#

Hello; can anybody help me? I have to compute a exponential moving average of the S&P500 but when I do it, I get results that are not consistent with the ones that should occur. The by-hand computed value that the fourth row of the ewma_1 column of the dataframe should be the one that is inside the println statement; 0.15140859291637426 but it gives me the wrong value even if the code suggests it's the same, not only that but the output is inconsistent among different executions of the program. I have not included any random elements to any of the other cells of the notebook or in this one; could this be a vscode problem or am I doing something wrong?

ashen nymph
#

I'm beginning to run the code

#

but I'm needing to format the code before it can work

#

Basically whatever the export is doing it's removing all new lines and escaped \

#

The first cell doesn't format the data for me cos it just says I've already formatted it

#

In other words it probably went out of bounds

soft lagoon
#

Yep

#

That is what is supposed to happen

ashen nymph
#

Ok, the 2nd code block doesnt run cos it assigns to constant_vol which doesn't yet exist

#

but it prob would work changing .= to =

soft lagoon
#

Huh

#

weird, it works in my pc

#

In what line does the error occur

#

maybe try to preallocate constant_vol

#

or just delete the plotting stuff because the problem is the ewma

ashen nymph
#

Ok I'm starting to guess that it's a list comprehension thing

#

lazy execution and all maybe

#

I rewrote it as a for loop and it seems stable

#
function ewma_094(df)
    nrows = nrow(df)
    df.ewma_1 = Array{Float64, 1}(undef, nrows)
    
    df.ewma_1[1:99] .= 0.0
    df.ewma_1[100] = sqrt( 252 * sum( 0.94^(i-1) * df.Change[(100-i)]^2 for i in 1:99 ) * (1-0.94))
    for ridx in 101:nrows
        df.ewma_1[ridx] = sqrt( (0.94*(df.Change[ridx]^2) + 0.06*((df.ewma_1[ridx - 1])^2)) * sqrt(252) )
    end
    return df
end

ewma_094(us_500)```
#

The only real change is actually at the for loop part

#

I get ```julia
52-element Vector{Float64}:
0.0
0.1452236480399996
0.15262717563008693
0.15140859291637426
0.14951808593099433
0.15862618073364068
0.15846421040896336
0.1555520368563569
0.15206112404377298
0.16526156207840798
0.16139158199426953
0.15816260661792444
0.15533178693338331

#

our number 0.15140859291637426 is there

#

Hmm I take it back it's not the list comprehension, but I suspect it's the DataFrames library where assigning to an array utilises multithreading

#

Then maybe it computes stuff out of order when it has to strictly go in sequence

soft lagoon
#

kay

soft lagoon
#

Okay

#

It seems to work now

#

I loaded the data into an array before sending it into the df

#

Ye it works now

#

Thanks man

ashen nymph
#

I wonder if anyone has reported on this problem before

soft lagoon
#

I dunno

#

@ashen nymph want me to quote you on the bugfix?

ashen nymph
#

Sure, thanks