Skip to content

Instantly share code, notes, and snippets.

View irudnyts's full-sized avatar
🏠
Working from home

Iegor Rudnytskyi irudnyts

🏠
Working from home
View GitHub Profile
life_table <- read.table(file = "path_to_file/.../life_table.txt", header = T,
skip = 2, stringsAsFactors = F)
q_x <- life_table[life_table$Year == 2011, "qx"] # q_x[x + 1] reffers to age x
p_x <- 1 - q_x
rm(life_table)
library(copula)
library(stats4)
set.seed(1)
# simulate sample from copula
u <- rCopula(n = 1000, copula = claytonCopula(param = 2, dim = 2))
# simulate marginals
x <- cbind(qnorm(u[, 1], mean = 10, sd = 10), qnorm(u[, 2], mean = 5, sd = 5))
# WARNING: searches for risk aversion coefficient root assuming to be in range
# 0.01 and 1000
markowitz_portfolio <- function(means, sds, corr,
fixed = c("mean", "variance", "lambda"),
fixed_level) {
fixed <- match.arg(fixed)
n <- length(means)
one <- rep(1, n)
@irudnyts
irudnyts / steakplot.R
Created February 14, 2017 17:35
Steakplot from ggplot2
# library("ggplot2")
# library(reshape2)
adjust_data <- function(vector, name, na_list) {
vector <- clean_data(vector = vector, na_list = na_list)
frequencies <- numeric(5)
for(i in seq_along(vector)) {
frequencies[vector[i]] <- frequencies[vector[i]] + 1
@irudnyts
irudnyts / excel2latex.R
Created February 14, 2017 17:37
Help to convert excel tables to latex and latex tables to excel spreadsheet
library(readxl)
library(xtable)
excel2latex <- function(path, sheet = 1, col_names = TRUE, col_types = NULL,
na = "", skip = 0) {
data <- readxl::read_excel(path = path, sheet = sheet,
col_names = col_names, col_types = col_types,
na = na, skip = skip)
browser()
output <- print(xtable(data, digits = 5), include.rownames = FALSE)
@irudnyts
irudnyts / country_continent.R
Created February 14, 2017 17:41
Useful correspondence between contries and continents
country_continent <- c(
AD= "Europe",
AE = "Asia",
AF = "Asia",
AG = "North America",
AI = "North America",
AL = "Europe",
AM = "Asia",
AN = "North America",
AO = "Africa",
@irudnyts
irudnyts / generate_lp.R
Created February 22, 2017 10:42
An example of generating a linear problem in lp format for GLPK or CPLEX
string <- "Maximize
obj: x1 + 2 x2 + 3 x3 + x4
Subject To
c1: - x1 + x2 + x3 + 10 x4 <= 20
c2: x1 - 3 x2 + x3 <= 30
c3: x2 - 3.5 x4 = 0
Bounds
0 <= x1 <= 40
2 <= x4 <= 3
General
@irudnyts
irudnyts / ils.R
Created June 26, 2017 15:24
The code for the blog post about ILS.
library("rvest")
library("stringr")
base_url <- "http://www.artemis.bm"
url <- "http://www.artemis.bm/deal_directory/"
links <- paste0(base_url,
read_html(url) %>%
html_nodes(".table-style01 a") %>%
@irudnyts
irudnyts / students.R
Last active September 5, 2017 09:24
The code for the blog post about students' data.
library("ggplot2")
log_alm <- read.csv(file = "/Users/irudnyts/Documents/projects/data/alm_logs.csv",
stringsAsFactors = FALSE)
exercises <- as.Date(c("2016-10-18", "2016-11-02", "2016-11-16", "2016-12-07"))
before_exercises <- exercises - 7
midterm <- as.Date("2016-11-23")
colnames(log_alm) <- tolower(colnames(log_alm))
library("ggplot2")
library("gridExtra")
set.seed(1)
x <- runif(min = 0, max = 100, n = 20)
y <- x + rnorm(20, sd = 20)
fit <- lm(y ~ x)