Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchspa authored May 17, 2024
1 parent 110f91a commit 4a778bf
Show file tree
Hide file tree
Showing 2 changed files with 510 additions and 0 deletions.
227 changes: 227 additions & 0 deletions EnvDesignChoices.ipynb

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions NonstationaryAdvEnvironment.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "aaa6f053",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np \n",
"import matplotlib.pyplot as plt\n",
"import scipy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a916fbdb",
"metadata": {},
"outputs": [],
"source": [
"class NonstationaryAdvEnvironment:\n",
" def __init__(self, T, n_users, n_competitors, auctions_per_day):\n",
" np.random.seed(1)\n",
" \n",
" # Pricing Env\n",
" self.t = 0 \n",
" self.prices = np.linspace(0,1,100) # price discretization\n",
" user_effect = np.random.normal(0,0.2,size = (n_users, T)) # user specific effect for every day\n",
" \n",
" prob_func = lambda p,t: scipy.special.expit(5*p*t/T) # design choice\n",
" self.prob_history = np.vectorize(prob_func)(self.prices[:, np.newaxis], range(1,T+1))\n",
" self.current_prob = np.zeros(n_users)\n",
" \n",
" \n",
" # Auction Env\n",
" self.auc_t = 0 \n",
" self.n_auctions = auctions_per_day*T\n",
" \n",
" sin = np.sin(np.linspace(0,10, self.n_auctions)) # design choice\n",
" self.bids_history = np.tile(sin, (n_competitors,1)) + np.random.normal(0,1,size=(n_competitors,self.n_auctions))\n",
" self.bids_history = np.maximum(0,self.bids_history)\n",
" \n",
" self.current_reward = 0\n",
" self.cumulative_reward = 0\n",
" \n",
" def get_prob(self, a_t):\n",
" self.current_prob = self.prob_history[a_t, self.t] + + np.random.normal(0, 0.2, self.current_prob.shape)\n",
" self.current_prob = np.maximum(0, np.minimum(1, self.current_prob))\n",
" self.t += 1\n",
" return self.current_prob\n",
" \n",
" def get_bid(self):\n",
" bid = self.bids_history[:,self.auc_t]\n",
" self.auc_t += 1\n",
" return bid\n",
" \n",
" def next_round(self, slots_won, a_t):\n",
" prob = self.current_prob \n",
" self.current_reward = slots_won * np.dot(prob, np.ones(prob.shape) * self.prices[a_t])\n",
" self.cumulative_reward += self.current_reward\n",
" pass"
]
},
{
"cell_type": "markdown",
"id": "387939ea",
"metadata": {},
"source": [
"### Usage Example"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ae8dd0bc",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Day 1 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.51833233 0.52264634].\n",
"\n",
"Auction 1 started. Competitors Bids: [0. 0. 0.].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.5257468005458048.\n",
"\n",
"Auction 2 started. Competitors Bids: [1.64707486 0. 1.75521931].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.5257468005458048.\n",
"\n",
"Cumulative Reward at the end of day 0: 1.0514936010916096.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 2 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.66095776 0.70565581].\n",
"\n",
"Auction 3 started. Competitors Bids: [1.77032034 0.12157132 1.38165944].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.6902088750736839.\n",
"\n",
"Auction 4 started. Competitors Bids: [1.50246112 2.69242138 0.70187395].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.6902088750736839.\n",
"\n",
"Cumulative Reward at the end of day 1: 2.4319113512389774.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 3 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.72048712 0.7046289 ].\n",
"\n",
"Auction 5 started. Competitors Bids: [1.76139629 0.91134809 1.34905848].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.7197555655272247.\n",
"\n",
"Auction 6 started. Competitors Bids: [0. 0. 0.4126175].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.7197555655272247.\n",
"\n",
"Cumulative Reward at the end of day 2: 3.871422482293427.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 4 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.59890746 0.80855267].\n",
"\n",
"Auction 7 started. Competitors Bids: [0. 0.17461412 1.11532803].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.7108384511274305.\n",
"\n",
"Auction 8 started. Competitors Bids: [0. 1.58387554 1.00343722].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.7108384511274305.\n",
"\n",
"Cumulative Reward at the end of day 3: 5.293099384548288.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 5 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.80384213 1. ].\n",
"\n",
"Auction 9 started. Competitors Bids: [0. 0. 1.30888738].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.9110313773498577.\n",
"\n",
"Auction 10 started. Competitors Bids: [0. 0. 0.].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.9110313773498577.\n",
"\n",
"Cumulative Reward at the end of day 4: 7.115162139248003.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 6 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [1. 0.85685469].\n",
"\n",
"Auction 11 started. Competitors Bids: [0. 0. 0.].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.9378053990601611.\n",
"\n",
"Auction 12 started. Competitors Bids: [0. 0. 0.].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.9378053990601611.\n",
"\n",
"Cumulative Reward at the end of day 5: 8.990772937368327.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 7 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.77911153 0.72642243].\n",
"\n",
"Auction 13 started. Competitors Bids: [0. 0. 0.19263546].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.7603706875865166.\n",
"\n",
"Auction 14 started. Competitors Bids: [0. 0.18092809 1.40643974].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.7603706875865166.\n",
"\n",
"Cumulative Reward at the end of day 6: 10.511514312541358.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 8 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.96760077 0.89836991].\n",
"\n",
"Auction 15 started. Competitors Bids: [0.21316733 0.67551923 1.20004841].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.9424094334035618.\n",
"\n",
"Auction 16 started. Competitors Bids: [0.98650502 1.58579281 0. ].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.9424094334035618.\n",
"\n",
"Cumulative Reward at the end of day 7: 12.396333179348481.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 9 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.83782226 0.91531237].\n",
"\n",
"Auction 17 started. Competitors Bids: [0. 1.68246136 0.53727393].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.8854215286964495.\n",
"\n",
"Auction 18 started. Competitors Bids: [0.6938956 1.39058198 1.28745455].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.8854215286964495.\n",
"\n",
"Cumulative Reward at the end of day 8: 14.16717623674138.\n",
"\n",
"------------------------------------------------------------------------\n",
"Day 10 started. Price = 0.5.\n",
"Current Purchase Probability for every user: [0.80189309 1. ].\n",
"\n",
"Auction 19 started. Competitors Bids: [1.61091542 0.23670057 0.18120798].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.910047017618852.\n",
"\n",
"Auction 20 started. Competitors Bids: [0.19802305 0.34112005 0.21799007].\n",
"Number of AdvSlots won: 1.\n",
"Round Reward: 0.910047017618852.\n",
"\n",
"Cumulative Reward at the end of day 9: 15.987270271979085.\n",
"\n",
"------------------------------------------------------------------------\n"
]
}
],
"source": [
"auctions_per_day = 2\n",
"T = 10\n",
"n_users = 2\n",
"n_competitors = 3\n",
"\n",
"env = NonstationaryAdvEnvironment(T, n_users, n_competitors, auctions_per_day)\n",
"\n",
"i = 0\n",
"while i < T:\n",
" # Assess price (ex: p = 0.5 fixed)\n",
" arm = 50\n",
" print(f'Day {i+1} started. Price = {0.5}.')\n",
" prob = env.get_prob(arm)\n",
" print(f'Current Purchase Probability for every user: {prob}.\\n')\n",
" \n",
" for j in range(i*auctions_per_day, (i+1)*auctions_per_day):\n",
" bid = env.get_bid()\n",
" print(f'Auction {j+1} started. Competitors Bids: {bid}.')\n",
" \n",
" # Assess auction (ex: 1 slot won)\n",
" slot_won = 1\n",
" print(f'Number of AdvSlots won: {slot_won}.')\n",
" \n",
" env.next_round(slot_won, arm)\n",
" print(f'Round Reward: {env.current_reward}.\\n')\n",
" \n",
" print(f'Cumulative Reward at the end of day {i}: {env.cumulative_reward}.\\n')\n",
" print('------------------------------------------------------------------------')\n",
" \n",
" i += 1"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 4a778bf

Please sign in to comment.