Created
August 28, 2024 01:22
-
-
Save laksjdjf/3b917c34df8e870dc50904be7ed679da to your computer and use it in GitHub Desktop.
This file contains 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 numpy as np | |
import matplotlib.pyplot as plt | |
def sigmoid(x): | |
return 1 / (1 + np.exp(-x)) | |
def inverse_sigmoid(y): | |
return np.log(y / (1 - y)) | |
# 逆シグモイド関数の微分 | |
def inverse_sigmoid_derivative(y): | |
return 1 / (y * (1 - y)) | |
def shift_func(x, shift): | |
return (x*shift) / (1 + (shift - 1) * x) | |
def inverse_shift(y, shift): | |
return y / (shift - y * (shift - 1)) | |
def inverse_shift_derivative(y, shift): | |
return shift / (shift - y * (shift - 1))**2 | |
def gaussian(x): | |
return (1 / (np.sqrt(2 * np.pi))) * np.exp(-0.5 * x ** 2) | |
# 確率密度関数 | |
def transformed_pdf(x, shift): | |
y = inverse_sigmoid(inverse_shift(x, shift)) | |
dydx = inverse_sigmoid_derivative(inverse_shift(x, shift)) * inverse_shift_derivative(x, shift) | |
return gaussian(y) * dydx | |
shift = 0.5 | |
# 確率分布 | |
y_values = np.linspace(0.01, 0.99, 1000) # y = 0, 1 に近づくと逆シグモイドが発散するので端を避ける | |
pdf_values = transformed_pdf(y_values, shift) | |
# サンプリング | |
sample_size = 10000 | |
normal_samples = np.random.normal(0, 1.0, sample_size) | |
transformed_samples = shift_func(sigmoid(normal_samples), shift) | |
plt.figure(figsize=(12, 6)) | |
plt.hist(transformed_samples, bins=50, density=True, alpha=0.6, color='g', label="Sampled Histogram") | |
plt.plot(y_values, pdf_values, label="Transformed PDF (Sigmoid applied)") | |
plt.title(f"shift={shift}") | |
plt.xlabel("y") | |
plt.ylabel("Density") | |
plt.legend() | |
plt.grid(True) | |
plt.show() |
Author
laksjdjf
commented
Aug 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment