Skip to content

Commit

Permalink
eps added
Browse files Browse the repository at this point in the history
  • Loading branch information
emrergin committed Jan 9, 2024
1 parent 1a49cbe commit 7ae01b4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/pages/demo1/eps.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.epsContainer {
display: grid;
grid-template-columns: 1fr 1fr;
}

.forecastsContainer {
grid-column-start: 1;
grid-row-start: 1;
grid-row-end: 3;
}
.guessInput {
display: block;
}

.epsButtons {
margin-top: 1.5rem;
display: flex;
gap: 1rem;
flex-wrap: wrap;
width: 300px;
}

.epsButton {
padding: 1rem 2rem;
}
86 changes: 86 additions & 0 deletions src/pages/demo1/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { gaussianRandom } from "@/utilities/functions";
import { useEffect, useRef, useState } from "react";
import styles from "./eps.module.css";
import styles1 from "@/styles/Home.module.css";

export default function Home() {
const [actualEps, setActualEps] = useState(0);
const [forecasts, setForecasts] = useState([...new Array(10)].map(() => 0));
const guessRef = useRef<HTMLInputElement>(null);
const [qsrReward, setQsrReward] = useState<number | null>(null);
const [bsrReward, setBsrReward] = useState<number | null>(null);
const [errorThreshold, setErrorThreshold] = useState<number | null>(null);

useEffect(() => {
renew();
}, []);

function renew() {
const actual = gaussianRandom(60, 20);
const forecastCal = [...new Array(10)].map(
() => gaussianRandom(0, Math.sqrt(8)) + actual,
);
setActualEps(actual);
setForecasts(forecastCal);
setQsrReward(null);
setBsrReward(null);
setErrorThreshold(Math.random() * 6);

if (guessRef.current) {
guessRef.current.value = "";
}
}

function guess() {
if (guessRef.current?.value && errorThreshold) {
setBsrReward(
(Number(guessRef.current?.value) - actualEps) ** 2 < errorThreshold
? 80
: 0,
);
setQsrReward(
90 - 25 * (actualEps - Number(guessRef.current?.value)) ** 2,
);
}
}
return (
<main className={styles1.main} style={{ userSelect: "none" }}>
<div className={styles.epsContainer}>
<div className={styles.forecastsContainer}>
<h3>Tahminler</h3>
{forecasts.map((forecast, index) => (
<p key={index}>{forecast}</p>
))}
<h3>Ortalama Tahmin </h3>
{forecasts.reduce((acc, curr) => acc + curr, 0) / forecasts.length}
</div>
<div className={styles.rewardContainer}>
<h3>Gerçek Hisse Kazancı (T)</h3>
{qsrReward ? actualEps : "..."}
<h3> QSR kazanç</h3>
{qsrReward ?? "..."}
<h3> BSR kazanç</h3>
{bsrReward ?? "..."}
<h3> BSR için hata eşiği (K)</h3>
{bsrReward !== null ? errorThreshold : "..."}
</div>
<div className={styles.epsButtons}>
<button className={styles.epsButton} onClick={() => renew()}>
Yenile
</button>
<button className={styles.epsButton} onClick={() => guess()}>
Tahmin
</button>
<label htmlFor="eps-guess">Tahmin (M)</label>
<input
type="text"
name="eps-guess"
id="eps-guess"
className={styles.guessInput}
ref={guessRef}
/>
</div>
</div>
</main>
);
}
8 changes: 8 additions & 0 deletions src/utilities/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ export function getDateText() {
}) + ":00"
);
}

export function gaussianRandom(mean = 0, stdev = 1) {
const u = 1 - Math.random(); // Converting [0,1) to (0,1]
const v = Math.random();
const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
// Transform to the desired mean and standard deviation:
return z * stdev + mean;
}

0 comments on commit 7ae01b4

Please sign in to comment.