Skip to content

Instantly share code, notes, and snippets.

View wilsonfreitas's full-sized avatar

Wilson Freitas wilsonfreitas

View GitHub Profile
@wilsonfreitas
wilsonfreitas / computing-ewma.R
Last active February 13, 2021 14:42
Computing EWMA in R using two different approaches: loop and functional. Clearly functional approach is more efficient.
ewma.func <- function(rets, lambda) {
sig.p <- 0
sig.s <- vapply(rets, function(r) sig.p <<- sig.p*lambda + (r^2)*(1 - lambda), 0)
return(sqrt(sig.s))
}
ewma.loop <- function(rets, lambda) {
n <- length(rets)+1
sig.s <- rep(0, n)
for (i in 2:n) {
@wilsonfreitas
wilsonfreitas / PETR4.daily.raw.csv
Last active December 21, 2015 08:48
Convergence of EWMA volatility estimator
Date Open High Low Close Volume Adj Close
2013-01-02 19.99 20.21 19.69 19.69 30182600 19.69
2012-12-28 19.50 19.63 19.30 19.52 28368600 19.52
2012-12-27 20.06 20.17 19.35 19.41 29298500 19.41
2012-12-26 20.54 20.68 20.00 20.06 24075000 20.06
2012-12-21 20.66 20.74 20.39 20.69 29390200 20.69
2012-12-20 20.87 21.17 20.63 21.05 25639600 21.05
2012-12-19 20.40 20.99 20.40 20.93 48430400 20.93
2012-12-18 19.86 20.26 19.85 20.17 24685000 20.17
2012-12-17 20.08 20.14 19.80 19.92 28421900 19.92
@wilsonfreitas
wilsonfreitas / README.md
Last active December 21, 2015 11:09
Simple implementation for business days calculations in R.

This is a simple implemetation in R to compute business day between 2 dates. This is quite useful in financial calculations, mainly for pricing interest rate financial instruments.

data(holidaysANDIMA)
holidaysANDIMA <- holidaysANDIMA[holidaysANDIMA >= '2013-01-01' & holidaysANDIMA <= '2026-01-01']
cal <- calendar(holidaysANDIMA)
cal$bizdays('2013-07-12', '2014-07-12') # returns 251
@wilsonfreitas
wilsonfreitas / ts.R
Created August 22, 2013 11:01
Flat-forward interpolation in R
rate <- function(r, t) list(r=r, t=t)
comp.factor <- function(f, t) list(f=f, t=t)
implied.rate <- function(f, t=f$t, dib=360) rate(f$f^(dib/t)-1, t)
compound <- function(r, t=r$t, dib=360) comp.factor((1+r$r)^(t/dib), t)
compound.div <- function(d, u) comp.factor(u$f/d$f, u$t-d$t)
@wilsonfreitas
wilsonfreitas / gist:6411317
Created September 2, 2013 10:09
Use of regular expression in VBA
Dim reMean As Object
Dim matches As Object
Dim match As Object
Set reMean = CreateObject("VBScript.RegExp")
With reMean
.Global = False
.IgnoreCase = False
End With
@wilsonfreitas
wilsonfreitas / gist:6420196
Created September 3, 2013 06:10
Plotting the payoff of a structured note thru Monte Carlo Simulation in R
r <- 0.07
q <- 0.02
sig <- 0.40
S_0 <- 142.12
K <- S_0
H <- S_0*0.75
CMC <- 0.14
CAP <- 0.3
dt <- 1/252

irc - interest rate curve

  • create interest rate curve
  • append rates to an interest rate curve
  • InterestRate
    • rate
    • Compounding
    • Frequency
  • DayCount
# coding:utf-8
import locale
from datetime import datetime
import re
from BeautifulSoup import BeautifulSoup
# Função que transforma 6 formatos de data em ISO
# input: list, string
# output: list
def dateISOer(content):
# coding:utf-8
import re
import datetime
import locale
'''
20130909
09/18/13
18/09/2013
library(stringr)
sqlitefy.handler <- function(obj) {
function (text) {
ret <- FALSE
if (str_detect(text, obj$re)) {
ret <- TRUE
attr(ret, 'text') <- obj$transform(text, obj$re)
}
ret