Skip to content

Instantly share code, notes, and snippets.

@mages
mages / gist:1544009
Created December 31, 2011 13:30
Search and replace string across files with R
## The following example demonstrates
## how a serach and replace string task
## can be peformed with R across several files
## Create two text files with content
filenames <- c( tempfile(), tempfile() )
for( f in filenames ){
cat("We wish you a Merry Christmas!\n\nBest regards\n", file=f)
}
@mages
mages / Read_Arduino_Sensor_Data.py
Last active May 24, 2022 01:05
Reading temperature from Arduino
# Based on code by Paul McWhorter:
# http://www.toptechboy.com/tutorial/python-with-arduino-lesson-11-plotting-and-graphing-live-data-from-arduino-with-matplotlib/
import serial
import re
import csv
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
library(mnormt)
mycols <- topo.colors(100,0.5)
xhat <- c(0.2, -0.2)
Sigma <- matrix(c(0.4, 0.3,
0.3, 0.45), ncol=2)
x1 <- seq(-2, 4,length=151)
x2 <- seq(-4, 2,length=151)
f <- function(x1,x2, mean=xhat, varcov=Sigma)
dmnorm(cbind(x1,x2), mean,varcov)
z <- outer(x1,x2, f)

Insurance Data Science Conference 2018

Abstracts

Modelling longevity risk for retirement and pension planning

Gareth Peters, Heriot-Watt University, Edinburgh

In this talk we build on a sequence of papers recently developed to enhance the modelling of life expectancy based on mortality data. Forecasting life expectancy and mortality are two important aspects for the study of demography that influence pension planning, retirement decisions and government policy.

We demonstrate how to develop regression models incorporating stochastic factors such as graduation temporal effects, period effects, cohort effects, stochastic volatility and long memory to enhance the forecasting and estimation of life tables. In addition, we show the mispricing that occurs in standard annuities, pure endowments and Gauranteed Annuity Oprtions (GAOs)

@mages
mages / Chapter_15.R
Created January 11, 2014 09:33
R code from Computational Actuarial Science with R, Chapter 15
### R code from Computational Actuarial Science with R, Chapter 15
### Author: Markus Gesmann
options(prompt = "R> ", digits = 4, show.signif.stars = TRUE)
options(continue=" ")
library(ChainLadder)
library(lattice)
library(AER)
library(fitdistrplus)
lattice.options(default.theme = standard.theme(color = FALSE))
@mages
mages / Gamma_Exponential_Stan.R
Last active September 24, 2021 17:35
Gamma Exponential with Stan
library(rstan)
stanmodelcode <- "
data {
int<lower=0> N;
int<lower=0> y[N];
}
parameters {
real<lower=0.00001> Theta;
}
model {
@mages
mages / snip.R
Created January 11, 2018 21:41
Mac: Copy R data frame to clipboard
snip <- function(input) {
pb <- pipe("pbcopy", "w")
write.table(input, sep="\t", file=pb, row.names = FALSE)
close(pb)
}
# Markus Gesmann
library(arm) # for 'display' function only
icecream <- data.frame(
# http://www.statcrunch.com/5.0/viewreport.php?reportid=34965&groupid=1848
temp=c(11.9, 14.2, 15.2, 16.4, 17.2, 18.1,
18.5, 19.4, 22.1, 22.6, 23.4, 25.1),
units=c(185L, 215L, 332L, 325L, 408L, 421L,
406L, 412L, 522L, 445L, 544L, 614L)
)
basicPlot <- function(...){
@mages
mages / add.alpha.R
Last active December 20, 2018 14:31
How to change the alpha value of colours in R
## Add an alpha value to a colour
add.alpha <- function(col, alpha=1){
if(missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb)/255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=alpha))
}