Skip to content

Instantly share code, notes, and snippets.

library(MASS)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
waic <- function(loglik){
te <- -mean(log(colMeans(exp(log_lik))))
fv <- mean(colMeans(log_lik^2) - colMeans(log_lik)^2)
waic <- te + fv
return(waic)
@unaoya
unaoya / gauss.py
Last active December 19, 2017 07:14
def issol(x,y,z,p,k):
return (pow(x,k,p) + pow(y,k,p)) % p == pow(z,k,p)
def count(p,k):
c = 0
for x in range(1,p):
for y in range(1,p):
for z in range(1,p):
if issol(x,y,z,p,k):
c += 1
import numpy as np
import tensorflow as tf
import edward as ed
from edward.models import Bernoulli, Beta, PointMass
ed.set_seed(42)
# DATA
n = 4
@unaoya
unaoya / satotate.py
Last active December 25, 2017 12:20
import numpy as np
import math
import matplotlib.pyplot as plt
def primes(n):
result = set(range(2,n))
for i in range(2,math.floor(math.sqrt(n))+1):
for j in range(2,n//i+1):
result.discard(i*j)
return list(result)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
library(ggplot2)
library(tidyr)
n <- 10^6
N <- 1000
p <- 0.3
t <- 2
result <- rep(0, n)
Mmean <- rep(0, n)
@unaoya
unaoya / base.stan
Last active December 23, 2018 14:16
検索量を用いた状態空間モデルによる売上予測
data {
int N; //学習期間の長さ
int N_pred; //予測期間の長さ
vector[N] Y; //販売台数データ
}
parameters {
vector[N] alpha; //状態のトレンド成分
vector[N] season; //状態の季節成分
real<lower=0> s_Y; //観測誤差の分散
@unaoya
unaoya / compare_mean_median.R
Last active December 25, 2018 09:49
標本平均と標本中央値の比較
library(dplyr)
library(ggplot2)
compare <- function(N,n,p,dist){
rng <- get(dist)
sample.med <- c()
sample.mea <- c()
for(i in 1:N){
sample <- rng(n)
sample.med[i] <- sort(sample)[p]
@unaoya
unaoya / beta.R
Created December 29, 2018 12:42
順序統計量とBeta分布
library(dplyr)
library(ggplot2)
set.seed(123)
runif(10)
runif(10)
ord_sampling <- function(N,n,p){
x <- c()
for(i in 1:N){
@unaoya
unaoya / NormalSample.py
Created January 16, 2019 00:15
標準正規分布に従う標本の平均と分散の分布
import numpy as np
import matplotlib.pyplot as plt
mu = 0
sigma = 1
n = 5
sample = np.random.normal(size=n, loc=mu, scale=sigma)
print(np.mean(sample))
print(np.var(sample))