Wednesday, November 27, 2024

Iiizunamaru-sama and Tengu's Barley-Rice (Actual natural monument) - Touhou Project

 


 

Posted on my pixiv page: https://www.pixiv.net/en/artworks/124668895 and NicoNico-Douga: https://www.nicovideo.jp/watch/sm44366753

 
27th November is the day of Megumu Iizunamaru: Iiizunamaru-sama and Tengu's Barley-Rice (Actual natural monument)
Arch-Tengu. Megumu iizunamaru and Tengu's Barley-Rice (Actual natural monument)  








Tuesday, November 26, 2024

Python, matplotlib.animation: Test showing changing values of Index Vs Stock

I am testing "matplotlib.animation" using the stock market index (NASDAQ in this case example) and the stock market price (Amazon in this case example)

# Parameter Adjustment

# Set Ticker Symbol
# e.g. Ticker="^GSPC" for S&P 500, Ticker="NDAQ" for NASDAQ, Ticker="TOPX" for TOPIX
Ticker_index="NDAQ" # Ticker sympol for the index
# Ticker="AMZN" for Amazon, Ticker="NTDOY" for NINTENDO
Ticker_stock="AMZN" # Ticker symbok for a stock

# Set the number of years to download data
No_Years_Data=1;

# Set the numbers of days for the future forecast
No_Days_FutureForecast=250*1


# For mathematics
import numpy as np
import math
import statistics

# For plotting
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation
import itertools # for joining lists inside a list
from itertools import count

# Setting Plot Size
plt.rcParams["figure.figsize"] = (10,7)

# Calling stock values from Yahoo finance API
# Ref. https://www.kaggle.com/code/alessandrozanette/s-p500-analysis-using-yfinance-data
import yfinance as yf
%config InlineBackend.figure_format='retina'
import warnings
warnings.filterwarnings("ignore")

# Now, let's retrieve the data of the past x years using yfinance
Years="".join([str(No_Years_Data),'y']) # Set the number of years ! Adjust
Index = yf.Ticker(Ticker_index).history(period=Years)  # Ticker Symbokl ! Adjust !
Stock = yf.Ticker(Ticker_stock).history(period=Years)  # Ticker Symbokl ! Adjust !
# Let's take a look at the data
display(Index.tail())
display(Stock.tail())

# Change to list
# https://stackoverflow.com/questions/39597553/from-datetimeindex-to-list-of-times
Dates=Stock.index.tolist()
Dates=[Dates[t].strftime("%Y-%m-%d") for t in range(len(Dates))]
Values_Index=Index["Close"].tolist()
Values_Stock=Stock["Close"].tolist()

# Taking a difference of natural log of these values
y_1=np.diff(np.log(Index["Close"])).tolist()
y_2=np.diff(np.log(Stock["Close"])).tolist()



# Inline animations in Jupyter
# https://stackoverflow.com/questions/43445103/inline-animations-in-jupyter
epsilon=[] # storing errors
Window=30; # business days in 1.5 months
for b in range(math.floor(len(y_1)/Window)-1):
    st=0+Window*b; ed=st+Window; print(st,ed)

    plt.rcParams["animation.html"] = "jshtml"
    plt.rcParams['figure.dpi'] = 150

    fig, ax = plt.subplots()
    x_value = []
    y1_value = []
    y2_value = []
    epsilon_value=[]

    count_ = count();
    def animate(a):
        counts=next(count_)
        x_value=Dates[st+counts:ed+counts].copy()
        y1_value=y_1[st+counts:ed+counts].copy()
        y2_value=y_2[st+counts:ed+counts].copy()
        #epsilon_value=[abs((y1_value[w]-y2_value[w])/y2_value[w]) for w in range(len(y1_value))] # Relative
        epsilon_value=[abs((y1_value[w]-y2_value[w])) for w in range(len(y1_value))] # Absolute
        ax.cla()
        ax.plot(x_value,y1_value, label=Ticker_index, color='slategrey')
        ax.plot(x_value,y2_value, label=Ticker_stock, color='darkseagreen')
        plt.legend(loc="upper right")
        plt.xticks(rotation=90)
        plt.title(f'From {Dates[st]} to {Dates[ed+Window]}')
        ax.set_xlim(0,Window)
        epsilon.append(epsilon_value)
    Animation=animation.FuncAnimation(fig, animate, frames=Window, interval = 500)
    display(Animation)

# Separating figures
plt.show(block=False)

# Analysing errors
# joininig lists inside a list
epsilon=list(itertools.chain.from_iterable(epsilon))

print(statistics.mean(epsilon))
plt.figure(); plt.hist(epsilon); plt.show(block=False)



 

Saturday, November 23, 2024

Monday, November 11, 2024

Normal Distribution Random Normal T-test Type I Error







 

This test is based on the standard probability density function (PDF) following a Gaussian distribution.

The PDF is configured with a mean of 0 and a standard deviation of 1 to generate the random values.

A series of these randomly generated values is then tested using a T-test to determine whether there is sufficient evidence to reject the null hypothesis (mean = 0).

In most cases, with a 95% confidence level (the standard probability level used in economics and finance), there is typically insufficient evidence to reject the null hypothesis, i.e., the mean is 0.

However, with a significantly high number of trials (e.g., 1000), under the same confidence level, there are instances where there is sufficient evidence to reject the null hypothesis, i.e., the mean is not 0.

# For statistics
import random
import numpy as np
from scipy import stats
from scipy.stats import norm
# For plotting
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
# For table output
import pandas as pd

# Setting Plot Size
plt.rcParams["figure.figsize"] = (10,7)

# Reference list
LinkList=["List of colours: https://matplotlib.org/stable/gallery/color/named_colors.html",
          "Random Normal Distribution (Gausian PDF): https://numpy.org/doc/stable/reference/random/generated/numpy.random.normal.html",
          "Normal Distribution Functions: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.t.html",
          "T-test: ",
          "   - One sample: https://builtin.com/data-science/t-test-python",
          "   - Independent: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html",
          "   - Confidence Interval:",
          "      https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data",
          "      https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.t.html",
          "Plot Filling https://matplotlib.org/stable/gallery/lines_bars_and_markers/fill_between_demo.html"]
for k in range(len(LinkList)):
    print(LinkList[k])

# Setting parameters
n=1000; # Number of elements
mu=0 # mean of the distribution
sigma=1 # Standard deviation
# Random normally distributed elements with n data points
x=np.random.normal(mu, sigma, n) # A variable of elements

# Gausian Probability Density Function (PDF)
def Gausian_PDF(bins,mu,sigma):
    g=1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins-mu)**2/(2*sigma**2)) # PDF formula
    return g

# Plotting function
def Disn_Hist(x,Confidence_Level):
    # Histogram and Probability-Density setting
    No_Bins=100 #Setting number of bins i.e. how many slices of bars
    Probability_Density, bins, ignored = plt.hist(x, No_Bins, density=True, color = "lightgreen",  label='Probability Density')
    # Mean and Standard Deviation of the sample data x
    x_bar=np.mean(x); s=np.std(x)
    # Normal distribution of the sample
    g_x=Gausian_PDF(bins,x_bar,s) # Calling PDF
    plt.plot(bins,g_x,linewidth=2, color='Green', label='Sample Distribution') # Plotting line graph of PDF
    plt.axvline(x=x_bar, label=f'x_bar is {round(x_bar,3)}', color='Green') # Sample mean
    # Normal distribution of the null hypothesis
    mu=0; sigma=1
    g_0=Gausian_PDF(bins,mu,sigma) # Calling PDF
    plt.plot(bins,g_0,linewidth=2, color='Grey', label='Null Hypothesis', linestyle='dashed') # Plotting line graph of PDF
    plt.axvline(x=x_bar, label=f'mu is {round(mu,3)}', color='Grey') # Sample mean
    # Confidence Interval: qnorm
    # Graoh Output
    plt.legend(loc="upper left")
    plt.title("Histogram & Mean Comparison")
    plt.show()

Confidence_Level=95
Disn_Hist(x,Confidence_Level)

# T-test testing the null-hypothesis
def Ttest(x, mu, Confidence_Level, Print):
    x_bar=np.mean(x)
    t,p=stats.ttest_1samp(x, mu, alternative='two-sided')
    z=stats.t.ppf((1+Confidence_Level/100)/2.,n-1) # Z-value
    CI=[x_bar-stats.sem(x)*z, x_bar+stats.sem(x)*z] # Confidence Interval
    #Plotting a Gausian PDF
    points=np.linspace(-3,3,100)
    SEM=1 #stats.sem(x)
    g_0=Gausian_PDF(points,mu,SEM)
    plt.plot(points,g_0,linewidth=2, color='Grey', label='Sample Distribution') # Plotting line graph of PDF
    plt.axvline(x=0, color='Grey') # Null-Hypothesis
    plt.title('t-test')
    plt.xlabel('t-value')
    # T-test critical point
    SigLevel=1-Confidence_Level/100
    # Lower Fill
    LowerT_Pnts=np.linspace(min(points),stats.norm.ppf(SigLevel/2))
    LowerT_Dsts=Gausian_PDF(LowerT_Pnts,0,1)
    #LowerT=[(bins[k],LowerT_Dsts[k]) for k in range(len(bins))]
    plt.fill_between(LowerT_Pnts,LowerT_Dsts, color='darkseagreen')
    # Upper Fill
    UpperT_Pnts=np.linspace(stats.norm.ppf(1-SigLevel/2),max(points))
    UpperT_Dsts=Gausian_PDF(UpperT_Pnts,0,1)
    #UpperT=[(bins[k],UpperT_Dsts[k]) for k in range(len(bins))]
    plt.fill_between(UpperT_Pnts,UpperT_Dsts, color='darkseagreen')
   
    plt.axvline(x=t, label=f't is {round(t,3)}', color='Magenta') # Null-Hypothesis
    h=['H0: mu = x_bar ∴ No sufficient evidence to reject the null hypothesis (H0).',
       'H1: mu ≠ x_bar ∴ Sufficient evidence to reject the null hypothesis (H0).']
    if Print==1:
        print(f'Confidence Interval: {CI}')
        print(f'T-value: {t},  p-value: {p}')
        print(h[int(np.floor(1-p+SigLevel))])
    return t,p

print('\n')
print('T-test testing the null-hypothesis')
Confidence_Level=95 #%
ShowingTstatDetail=1
x=np.random.normal(mu, sigma, n) # A variable of elements
plt.figure(); Ttest(x, 0, Confidence_Level, ShowingTstatDetail)
 
# Separating figures
plt.show(block=False)


# T-testing null-Hypothesis mu=0 t trials
t=10 # Number of trials
for k in range(t):
    print('\n')
    print('T-test testing the null-hypothesis')
    Confidence_Level=95 #%
    ShowingTstatDetail=1
    x=np.random.normal(mu, sigma, n) # A variable of elements
    plt.figure(); Ttest(x, 0, Confidence_Level, ShowingTstatDetail)
    plt.show(block=False)
   


# Why there is sometimes no significant evidence proving it is zero.
def Type_I_Error_test(Confidence_Level,Number_of_datapoints, Number_of_trial,Print):
    X_bar=[];T=[];P=[];SigLevel=1-Confidence_Level/100
    for l in range(Number_of_trial):
        mu=0; sigma=1; n=Number_of_datapoints
        x=np.random.normal(mu, sigma, n)
        t,p=stats.ttest_1samp(x, mu)
        if p<SigLevel:
            #plt.figure(); Disn_Hist(x,Confidence_Level)
            plt.figure(); Ttest(x, 0, Confidence_Level, Print)
            x_bar=np.mean(x)
            X_bar.append(x_bar)
            T.append(t)
            P.append(p)
    return X_bar,T,P

print('\n')
print('Why there is sometimes no significant evidence proving it is zero.')
Confidence_Level=95 #%
Number_of_datapoints=1000
Number_of_trial=100
ShowingTstatDetail=0
X_bar,T,P=Type_I_Error_test(Confidence_Level, Number_of_datapoints, Number_of_trial, ShowingTstatDetail)
print(f"There are {len(P)} times out of {Number_of_trial} times of the trial not be able to reject the null hypothesis.")
print(f"The percentage of no sufficient evidence to reject the null hypothesis is {round(len(P)/Number_of_trial,2)}%")
d = {'x_bar':X_bar,'t':T, 'p':P}
df = pd.DataFrame(data=d)
display(df)


I've edited and published Yukkuri Kaisetsu movie with my original stand picture of Statistical test based on Python programming.


Thursday, November 07, 2024

Manifesto Fictosexualism!

 

I would like to declare for the manifesto insisting on the rise and the full right of Fictosexualism! 

This is one of the glorious cause of supporting Progressive Libertarianism /  Classical Liberalism!

I follow the ethics of my admirable philosopher Jeremy Bentham saying "The greatest sum of pleasures with the lowest sum of pains is the principle of moral and legislation." Nobody harms and an individual gains the happiness from being fictosexual!

I sincerely despise the staunch Christians, feminists, and the staunch structuralists, persecuting my personality trait!