Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / ALTO_bitmask.R
Created December 21, 2025 13:02
gather & spread of ALTO (adaptive linearized tensor operation)
#ref:
#Jan Laukemann et al. (2025) Accelerating Sparse Tensor Decomposition Using Adaptive Linearized Representation
#https://arxiv.org/abs/2403.06348
ALTO_indexing <- function(object, data = environment(object), ...) {
mf <- model.frame(object, data, ...)
t <- if (missing(data)) terms(object) else terms(object, data = data)
labs <- attr(t, "term.labels")
mf <- lapply(labs, function(x) {
@abikoushi
abikoushi / overparametrize.R
Created December 21, 2025 10:09
冗長性のあるワンホットエンコーディングによるパラメータ化の係数をフルランクの計画行列の係数に変換する
library(moltenNMF)
library(Matrix)
df = expand.grid(letters[1:2], LETTERS[1:2])
X = sparse_onehot(~., data = df)
A = matrix(
c(0.5, 0.5, 0.5, 0.5, 0, 1, 0, 0, 0, 0, 0, 1),
nrow = 4,
ncol = 3
@abikoushi
abikoushi / ALTO_indexing.R
Last active December 20, 2025 11:31
ALTO (adaptive linearized tensor operation) indexing
#ref:
#Jan Laukemann et al. (2025) Accelerating Sparse Tensor Decomposition Using Adaptive Linearized Representation
#https://arxiv.org/abs/2403.06348
library(dplyr)
ALTO_indexing <- function(object, data = environment(object), ...) {
mf <- model.frame(object, data, ...)
t <- if (missing(data)) terms(object) else terms(object, data = data)
labs <- attr(t, "term.labels")
@abikoushi
abikoushi / get_MERS_table.R
Last active December 19, 2025 01:51
get MERS-2025 data using rvest
library(ggplot2)
library(dplyr)
library(tidyr)
library(rvest)
library(readr)
url = "https://ja.wikipedia.org/wiki/2015年韓国におけるMERSの流行"
taball <- read_html(url) |>
html_table()
@abikoushi
abikoushi / overparametrize.R
Created December 18, 2025 01:11
フルランクの計画行列の係数を冗長性のあるワンホットエンコーディングによるパラメータ化の係数に変換する
library(moltenNMF)
library(Matrix)
df = expand.grid(letters[1:2], LETTERS[1:2])
X = sparse_onehot(~. , data = df)
A=matrix(c(0.5,0.5,0.5,0.5,
0,1,0,0,
0,0,0,1), 4,3)
@abikoushi
abikoushi / CSCNMF.jl
Created December 17, 2025 06:03
VB-NMF for CSC sparse matrix format
module VI
using LogExpFunctions
using SparseArrays
using SpecialFunctions
using Distributions
# A = sparse([1, 1, 2, 3], [1, 3, 2, 3], [0, 1, 2, 0])
# A.m # Number of rows
# A.n # Number of columns
@abikoushi
abikoushi / MontyHall_problem.R
Created November 24, 2025 08:46
モンティ・ホール問題のシミュレーション
library(dplyr)
library(gt)
doors = LETTERS[1:3]
policy1 = 0L
policy2 = 0L
iter = 100000
res_raw = matrix(NA_character_, iter, 4)
set.seed(1124)
system.time({
@abikoushi
abikoushi / draw_utilityfun.R
Created November 23, 2025 00:15
draw utility (or expectation) function
Ut <- function(x,alpha,beta,lambda){
ifelse(x>0, x^alpha, -lambda*((-x)^beta))
}
params <- list(
list(alpha=1,beta=1,lambda=1),
list(alpha=1,beta=1,lambda=2),
list(alpha=1/2,beta=1/2,lambda=2),
list(alpha=2,beta=2,lambda=1)
)
@abikoushi
abikoushi / heatmap.R
Created November 20, 2025 06:37
Try ComplexHeatmap
#BiocManager::install("ComplexHeatmap")
library(tidyr)
library(dplyr)
library(tibble)
library(ComplexHeatmap)
df0 <- rownames_to_column(mtcars, var="car") %>%
separate(car, into=c("name","model"), extra="merge")
head(df0)
@abikoushi
abikoushi / crossing_border.R
Created November 16, 2025 04:36
2点を結ぶとき線分が多角形の境界をまたぐかの判定
#ref https://qiita.com/zu_rin/items/e04fdec4e3dec6072104
Judge <- function(a, b, c, d) {
s = (a$x - b$x) * (c$y - a$y) - (a$y - b$y) * (c$x - a$x);
t = (a$x - b$x) * (d$y - a$y) - (a$y - b$y) * (d$x - a$x);
if (s * t > 0){
return(FALSE)
}
s = (c$x - d$x) * (a$y - c$y) - (c$y - d$y) * (a$x - c$x);
t = (c$x - d$x) * (b$y - c$y) - (c$y - d$y) * (b$x - c$x);
if (s * t > 0){