Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / Gam_Dir_Po.R
Created January 3, 2026 08:28
reparameterized Poisson NMF
## reparameterized Poisson NMF
NMF_re <- function(X,
M=2,
a_W=1,
a_H=1,
alpha_b=1, beta_b=1,
tol=1e-3, maxit=100){
D <- nrow(X)
N <- ncol(X)
b_W <- rgamma(1, 1, 1)
@abikoushi
abikoushi / Gam_Dir_Po.R
Created January 1, 2026 00:23
Gamma-Dirichlet-Poisson-NMF (reparametrization)
NMF <- function(X, M=2,a_W=1,b_W=1,a_H=1,b_H=1,tol=1e-2,maxit=10000,
seed=1234){
set.seed(seed)
D <-nrow(X)
N <-ncol(X)
W<-matrix(rgamma(D*M,a_W,b_W),D,M)
H<-matrix(rgamma(M*N,a_H,b_H),M,N)
logW0 <-log(W)
logH0 <-log(H)
@abikoushi
abikoushi / CanonicalFormNormal.R
Created December 29, 2025 10:18
Canonical form parametrized normal distribution
rcfnorm <- function(n, shape=0, inv_scale=1){
rnorm(n, shape/inv_scale, 1/sqrt(inv_scale))
}
dcfnorm <- function(n, shape=0, inv_scale=1, ...){
dnorm(n, shape/inv_scale, 1/sqrt(inv_scale), ...)
}
pcfnorm <- function(n, shape=0, inv_scale=1, ...){
pnorm(n, shape/inv_scale, 1/sqrt(inv_scale), ...)
@abikoushi
abikoushi / readmtxfiles_asALTO.R
Last active December 26, 2025 00:51
read MM file as ALTO format
size_mtx <- function(file_path){
con <- file(file_path, open = "r") #Open for reading in text mode
size <- scan(con, what=integer(), comment.char = "%", nlines = 1, skip = 1, quiet = TRUE)
close(con)
names(size) <- c("row","column","nonzero")
return(size)
}
take_last <- function(x){
x[length(x)]
@abikoushi
abikoushi / spread.tex
Created December 24, 2025 04:45
A TikZ picture of ALTO spread
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{matrix,positioning,quotes}
\begin{document}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
@abikoushi
abikoushi / HairEye2.tex
Created December 24, 2025 03:45
A TikZ picture of HairEyeColor data as ALTO format
%ref:
%https://tex.stackexchange.com/questions/516073/how-to-draw-3d-matrix-using-tikz
%https://tikz.net/tikz-table/
%https://tikz.net/conv2d/
%
\documentclass[tikz,border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usetikzlibrary{matrix,positioning}
\begin{document}
@abikoushi
abikoushi / HairEye.tex
Created December 22, 2025 11:02
A TikZ picture of HairEyeColor data as ALTO format
%ref:
%https://tex.stackexchange.com/questions/516073/how-to-draw-3d-matrix-using-tikz
%https://tikz.net/tikz-table/
%https://tikz.net/conv2d/
%
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\tikzset{
@abikoushi
abikoushi / HairEye.tex
Created December 22, 2025 04:51
A TikZ picture of HairEyeColor data
%ref:
%https://tex.stackexchange.com/questions/516073/how-to-draw-3d-matrix-using-tikz
%https://tikz.net/tikz-table/
%
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\tikzset{
table/.style={
@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