Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Shape of g in Asset Pricing Code #126

Merged
merged 1 commit into from
Sep 29, 2023
Merged

Update Shape of g in Asset Pricing Code #126

merged 1 commit into from
Sep 29, 2023

Conversation

HumphreyYang
Copy link
Collaborator

This PR resolves #125.

@netlify
Copy link

netlify bot commented Sep 28, 2023

Deploy Preview for incomparable-parfait-2417f8 ready!

Name Link
🔨 Latest commit 4f8f451
🔍 Latest deploy log https://app.netlify.com/sites/incomparable-parfait-2417f8/deploys/6514cc3da922750007d16820
😎 Deploy Preview https://deploy-preview-126--incomparable-parfait-2417f8.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions
Copy link

@github-actions github-actions bot temporarily deployed to pull request September 28, 2023 00:55 Inactive
@HumphreyYang
Copy link
Collaborator Author

HumphreyYang commented Sep 28, 2023

Hi @jstac,

The reshaping of g should not affect the final output due to broadcasting (as the first three dimensions can be broadcasted). To ensure my guess is correct and the output is identical, I had a quick experiment here:

def A(g, sv_model, shapes):
    # Set up
    P, hc_grid, Q, hd_grid, R, z_grid, β, γ, bar_σ, μ_c, μ_d = sv_model
    I, J, K = shapes
    # Reshape and broadcast over (i, j, k, i', j', k')
    hc = np.reshape(hc_grid,     (I, 1, 1, 1,  1,  1))
    hd = np.reshape(hd_grid,     (1, J, 1, 1,  1,  1))
    z = np.reshape(z_grid,       (1, 1, K, 1,  1,  1))
    P = np.reshape(P,            (I, 1, 1, I,  1,  1))
    Q = np.reshape(Q,            (1, J, 1, 1,  J,  1))
    R = np.reshape(R,            (1, 1, K, 1,  1,  K))
    a = μ_d - γ * μ_c
    b = bar_σ**2 * (jnp.exp(2 * hd) + γ**2 * jnp.exp(2 * hc)) / 2
    κ = jnp.exp(a + (1 - γ) * z + b)
    A = β * κ * P * Q * R
    Ag = jnp.sum(A * g, axis=(3, 4, 5))
    return Ag


def A_modified(g, sv_model, shapes):
    # Set up
    P, hc_grid, Q, hd_grid, R, z_grid, β, γ, bar_σ, μ_c, μ_d = sv_model
    I, J, K = shapes
    # Reshape and broadcast over (i, j, k, i', j', k')
    hc = np.reshape(hc_grid,     (I, 1, 1, 1,  1,  1))
    hd = np.reshape(hd_grid,     (1, J, 1, 1,  1,  1))
    z = np.reshape(z_grid,       (1, 1, K, 1,  1,  1))
    P = np.reshape(P,            (I, 1, 1, I,  1,  1))
    Q = np.reshape(Q,            (1, J, 1, 1,  J,  1))
    R = np.reshape(R,            (1, 1, K, 1,  1,  K))
    g = np.reshape(g,            (1, 1, 1, I,  J,  K))
    a = μ_d - γ * μ_c
    b = bar_σ**2 * (jnp.exp(2 * hd) + γ**2 * jnp.exp(2 * hc)) / 2
    κ = jnp.exp(a + (1 - γ) * z + b)
    A = β * κ * P * Q * R
    Ag = jnp.sum(A * g, axis=(3, 4, 5))
    return Ag


np.allclose(A(np.ones(shapes), sv_model, shapes), A_modified(np.ones(shapes), sv_model, shapes))
True

Please let me know if there is anything else I can further improve.

Many thanks in advance.

@jstac
Copy link
Contributor

jstac commented Sep 29, 2023

Thanks @HumphreyYang , nice work, and thanks also @shlff, @sgcbayli024 and @pilarbarros. Merging.

@jstac jstac merged commit 1924e23 into main Sep 29, 2023
5 checks passed
@jstac jstac deleted the fix-asset-pricing branch September 29, 2023 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in asset pricing code
2 participants