Skip to content

Instantly share code, notes, and snippets.

View EconometricsBySimulation's full-sized avatar

Francis Smart EconometricsBySimulation

View GitHub Profile
@EconometricsBySimulation
EconometricsBySimulation / gist:d0af8273f15b6ccb85a4
Last active September 12, 2023 04:23
Rapidly sample from arbitrary pdf
I recently found myself in need of a function to sample randomly from an arbitrarily defined probability density function. An excellent post by Quantitations shows how to accomplish this using some of Rs fairly sophisticated functional approximation tools such as integrate and uniroot. The only problem with this excellent post was that the machine cost was enormous with samples of 1000 draws taking 10 seconds on my machine and repeated samples of 100,000+ draws (which I was after) clearly being unworkable.
Thus I decided to take my own crack at it. First let us review the basics of drawing random variables from non-uniform distributions. The standard method I think most algorithms use works as follows:
Assumptions
1. You can draw pseudo-random uniform variable u
2. You can integrate the pdf to construct a cdf
$$p = F(x) = \int_{-\infty}^\infty f(x) dx$$
3. You can invert the cdf in order to solve for p
$$G(F(x))=F^{-1}(F(x))=F^{-1}(p)=x$$
@EconometricsBySimulation
EconometricsBySimulation / lmOut
Last active November 22, 2022 09:12
A simple command to grab coefficients, t-stats, p-values, f-stats, etc from a regression and export them as an easy to use spreadsheet.
lmOut <- function(res, file="test.csv", ndigit=3, writecsv=T) {
# If summary has not been run on the model then run summary
if (length(grep("summary", class(res)))==0) res <- summary(res)
co <- res$coefficients
nvar <- nrow(co)
ncol <- ncol(co)
f <- res$fstatistic
formatter <- function(x) format(round(x,ndigit),nsmall=ndigit)
print("Functions F1")
def F1():
print("F1 Run")
f = lambda : print('hello world')
print('Hello World')
txt2 month
<<NO TEXT FOUND FOR SPECIFIED DATE>> 12
apoo od f n@N2enuo ttc aEon0t 0THOTesv1t tioil0,s ct'en O1t"to i cdCert.piawob Cd epEoRu gE h oIl i t coerdee VtR sou'eoet n crnrho C .ik/e lkreigypMn Rf s troCokeecT 0es nCoc4 oDn O oerlisd lo'ontom/ei i9Ir,2hgu,bt n/r kr20ta wtontl iriiC e Paots eu 0e12y1esenp tcaat )n eniTseon 4tE2l oCotoaN0 encuRi n eu e nrsroidNcinwp -t iECr9( gep2. k oiRub01gT'n1i Irmn ecT T.0'pbCu" 0w pt"Fso.f rdneeTs UW.nnl/s2tt uneTk't itri,ig nO.eth tere0eci 0 a pl( ' "rtvlbOntuftfr tn Onyn t1n10thtt i d eoe a rHc CtdorltCniwmfbao9 rdC vtecf pl nTOahf10ornet wueotoT o .s 12
<<NO TEXT FOUND FOR SPECIFIED DATE>> 12

emailto: psteiner@umd.edu student: Francis Smart fsmart@gmail.com EDMS769G

# Text Network graph
#           Student Ability _______________________
#            /           |                         \_____
#          /             |  Public/Private Transport     \
#         ↓              ↓     ↓                          ↘
# Estimating Weekly consumption from periodic purchasing data
library(dplyr)
library(data.table)
library(reshape)
library(tidyr)
library(ggplot2)
# Day Purchase
rm(list=ls())
library(data.table)
library(dplyr)
library(ggplot2)
library(scales)
setwd('Z:/Data/FEC')
dsum <- function(...) dplyr::summarize(...)
rm(list=ls())
library(data.table)
library(dplyr)
library(ggplot2)
setwd('Z:/Data/FEC')
dsum <- function(...) dplyr::summarize(...)
to.data.table <- function(x) {(class(x) <- class(data.table())) ;x}