This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from random import randint | |
| class Agent: | |
| """Agent that can share their public key with another agent to compute a shared secret, without sharing that secret.""" | |
| def __init__(self, name: str, prime: int = 2**17 - 1, base: int = 2**5 - 1) -> None: | |
| self.name = name | |
| self.prime = prime | |
| self.base = base |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import polars as pl | |
| import numpy as np | |
| from polarsgrid import expand_grid | |
| from scipy.stats import norm, t, uniform, ttest_ind | |
| from tqdm import tqdm | |
| import plotnine as p9 | |
| grid = expand_grid( | |
| # data generating process parameters | |
| sample_size=[10, 20, 40, 80, 160, 320, 640], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Packages for this script | |
| library(tidyverse) | |
| library(lavaan) | |
| library(dagitty) | |
| library(glue) | |
| library(rstanarm) | |
| ## SIMULATION ## | |
| # here is a true data-generating process in lavaan (SEM) syntax |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| virtual_grid <- function(...) { | |
| pars <- list(...) | |
| lens <- vapply(pars, length, 1L) | |
| return(structure(list(pars = pars, lens = lens), class = c("vgrid", "tbl_lazy"))) | |
| } | |
| dim.vgrid <- function(x) { | |
| as.integer(c(prod(x$lens), length(x$lens))) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # panel data factor model simulation | |
| # See hollingsworth & wing (2022) tactics for design.. | |
| # paragraph 2.3, assumption 2 | |
| n_timepoints <- 100 | |
| n_unobserved <- 1 | |
| n_units <- 200 | |
| n_covar <- 5 | |
| ru <- 0.6 | |
| mean_ly <- 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Comparing lintsampler to basic uniform importance sampling | |
| from scipy.stats import norm, uniform | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from lintsampler import LintSampler | |
| NSAMPLES = 1000000 | |
| # GMM example | |
| def gmm_pdf(x): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Simple probabilistic simulation script | |
| # Causal graph: | |
| # NetIncome -> + CulturalActivities | |
| # NetIncome -> + SportsActivities | |
| # NetIncome -> - Debts | |
| # NetIncome -> + SocialComparison | |
| # SportsActivities -> + Health | |
| # SportsActivities -> + Partnership | |
| # CulturalActivities -> + SocialComparison |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using StatsBase: sample, mean, cor | |
| using LinearAlgebra: norm | |
| using Plots, Random | |
| """ | |
| permutefun!(x::Vector, y::Vector, rule::Function, score::Real; tol::Number = 1e-3, max_iter::Int = 10_000, max_search::Number = 100, verbose::Bool = true) | |
| Permute y values to approximate a functional constraint (rule) between x and y. | |
| # Arguments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Turing | |
| using LogExpFunctions: logsumexp | |
| using DataFrames | |
| # The rank ordered logit model in Turing | |
| # The rank-ordered logit likelihood | |
| function rank_ordered_logit(ordered_skills::Vector{<:Real}) | |
| ll = 0.0 | |
| for m in 1:(length(ordered_skills) - 1) | |
| ll += ordered_skills[m] - logsumexp(ordered_skills[m:end]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Ideal point model in stan / R | |
| # example taken & simplified from https://medewitt.github.io/resources/stan_ideal_point.html | |
| library(tidyverse) | |
| library(cmdstanr) | |
| # simulate data: 100 legislators, 150 votes | |
| set.seed(1834) | |
| N_legislators <- 50 | |
| N_bills <- 150 |
NewerOlder