Skip to content

Instantly share code, notes, and snippets.

def expected_utility_logarithmic(simulations, a=1):
utilities = a * np.log(simulations)
return np.mean(utilities)
# Evaluate the expected utility of the simulations
expected_utility = expected_utility_logarithmic(simulations)
print(f'Expected Utility: {expected_utility}')
def utility_logarithmic(wealth, a=1):
return a * np.log(wealth)
wealth = np.linspace(1, 100, 100) # Wealth values from 1 to 100
utility = utility_logarithmic(wealth)
plt.plot(wealth, utility)
plt.xlabel('Wealth')
plt.ylabel('Utility')
plt.title('Logarithmic Utility Function')
import numpy as np
import matplotlib.pyplot as plt
def simulate_game():
tosses = 0
while np.random.choice(['H', 'T']) == 'T':
tosses += 1
return 2**(tosses + 1)
# Simulate the game 10,000 times to approximate the expected payoff
def gaussian_prior(mean: float, std: float) -> Callable[[int], float]:
"""
Create a Gaussian prior distribution centered around the mean.
Parameters:
- mean (float): The mean of the Gaussian distribution
- std (float): The standard deviation of the Gaussian distribution
Returns:
- Callable[[int], float]: A function representing the Gaussian prior
from scipy import stats
import numpy as np
def remove_outliers(data: List[float], threshold: float = 1.5) -> List[float]:
"""
Remove outliers from the data using the Z-score method.
Parameters:
- data (List[float]): The observed data
- threshold (float): The Z-score threshold for outlier detection
from typing import Callable, List, Tuple
import numpy as np
from scipy.stats import norm
class SegmentChangePointDetector:
def __init__(self, prior: Callable[[int], float]) -> None:
"""
Initialize the SegmentChangePointDetector model.
Parameters:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
def build_advanced_cnn(input_shape):
"""
Build an advanced CNN model with dropout layers.
Parameters:
input_shape (tuple): Shape of input images.
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
def preprocess_data(X_raw):
"""
Normalize the raw data for LSTM processing.
Parameters:
import numpy as np
import tensorflow as tf
def preprocess_data(X_raw):
"""
Preprocess the raw data for model training.
Parameters:
X_raw (array): The raw feature set.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
def build_cnn(input_shape):
"""
Build a Convolutional Neural Network (CNN) for medical imaging.
Parameters:
input_shape (tuple): The shape of input images.